Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a8aa26d1 authored by Mathew Inwood's avatar Mathew Inwood Committed by android-build-merger
Browse files

Merge "Fix SignatureBuilder."

am: d1219acf

Change-Id: I5cba47131a355a5dfb8ec0f653d47fb9a13bf161
parents 8eda9661 d1219acf
Loading
Loading
Loading
Loading
+14 −7
Original line number Diff line number Diff line

java_plugin {
    name: "unsupportedappusage-annotation-processor",
    processor_class: "android.processor.unsupportedappusage.UnsupportedAppUsageProcessor",

    java_resources: [
        "META-INF/**/*",
    ],
java_library_host {
    name: "unsupportedappusage-annotation-processor-lib",
    srcs: [
        "src/**/*.java",
    ],
@@ -22,6 +17,18 @@ java_plugin {
            "--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
        ],
    },
}

java_plugin {
    name: "unsupportedappusage-annotation-processor",
    processor_class: "android.processor.unsupportedappusage.UnsupportedAppUsageProcessor",

    java_resources: [
        "META-INF/**/*",
    ],
    static_libs: [
        "unsupportedappusage-annotation-processor-lib"
    ],

    use_tools_jar: true,
}
+14 −8
Original line number Diff line number Diff line
@@ -101,14 +101,20 @@ public class SignatureBuilder {
    private String getClassSignature(TypeElement clazz) {
        StringBuilder sb = new StringBuilder("L");
        for (Element enclosing : getEnclosingElements(clazz)) {
            if (enclosing.getKind() == PACKAGE) {
            switch (enclosing.getKind()) {
                case MODULE:
                    // ignore this.
                    break;
                case PACKAGE:
                    sb.append(((PackageElement) enclosing)
                            .getQualifiedName()
                            .toString()
                            .replace('.', '/'));
                    sb.append('/');
            } else {
                    break;
                default:
                    sb.append(enclosing.getSimpleName()).append('$');
                    break;
            }

        }
+28 −0
Original line number Diff line number Diff line
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

java_test_host {
    name: "unsupportedappusage-processor-test",

    srcs: ["src/**/*.java"],

    static_libs: [
        "libjavac",
        "unsupportedappusage-annotation-processor-lib",
        "truth-host-prebuilt",
        "mockito-host",
        "junit-host",
        "objenesis",
    ],
}
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.processor.unsupportedappusage;

import com.google.common.base.Splitter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CsvReader {

    private final Splitter mSplitter;
    private final List<String> mColumns;
    private final List<Map<String, String>> mContents;

    public CsvReader(InputStream in) throws IOException {
        mSplitter = Splitter.on(",");
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        mColumns = mSplitter.splitToList(br.readLine());
        mContents = new ArrayList<>();
        String line = br.readLine();
        while (line != null) {
            List<String> contents = mSplitter.splitToList(line);
            Map<String, String> contentMap = new HashMap<>();
            for (int i = 0; i < Math.min(contents.size(), mColumns.size()); ++i) {
                contentMap.put(mColumns.get(i), contents.get(i));
            }
            mContents.add(contentMap);
            line = br.readLine();
        }
        br.close();
    }

    public List<String> getColumns() {
        return mColumns;
    }

    public List<Map<String, String>> getContents() {
        return mContents;
    }
}
+96 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.processor.unsupportedappusage;

import static com.google.common.truth.Truth.assertThat;

import com.android.javac.Javac;

import com.google.common.base.Joiner;

import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

public class UnsupportedAppUsageProcessorTest {

    private Javac mJavac;

    @Before
    public void setup() throws IOException {
        mJavac = new Javac();
        mJavac.addSource("dalvik.annotation.compat.UnsupportedAppUsage", Joiner.on('\n').join(
                "package dalvik.annotation.compat;",
                "public @interface UnsupportedAppUsage {",
                "    String expectedSignature() default \"\";\n",
                "    String someProperty() default \"\";",
                "}"));
    }

    private CsvReader compileAndReadCsv() throws IOException {
        mJavac.compileWithAnnotationProcessor(new UnsupportedAppUsageProcessor());
        return new CsvReader(
                mJavac.getOutputFile("unsupportedappusage/unsupportedappusage_index.csv"));
    }

    @Test
    public void testSignatureFormat() throws Exception {
        mJavac.addSource("a.b.Class", Joiner.on('\n').join(
                "package a.b;",
                "import dalvik.annotation.compat.UnsupportedAppUsage;",
                "public class Class {",
                "  @UnsupportedAppUsage",
                "  public void method() {}",
                "}"));
        assertThat(compileAndReadCsv().getContents().get(0)).containsEntry(
                "signature", "La/b/Class;->method()V"
        );
    }

    @Test
    public void testSourcePosition() throws Exception {
        mJavac.addSource("a.b.Class", Joiner.on('\n').join(
                "package a.b;", // 1
                "import dalvik.annotation.compat.UnsupportedAppUsage;", // 2
                "public class Class {", // 3
                "  @UnsupportedAppUsage", // 4
                "  public void method() {}", // 5
                "}"));
        Map<String, String> row = compileAndReadCsv().getContents().get(0);
        assertThat(row).containsEntry("startline", "4");
        assertThat(row).containsEntry("startcol", "3");
        assertThat(row).containsEntry("endline", "4");
        assertThat(row).containsEntry("endcol", "23");
    }

    @Test
    public void testAnnotationProperties() throws Exception {
        mJavac.addSource("a.b.Class", Joiner.on('\n').join(
                "package a.b;", // 1
                "import dalvik.annotation.compat.UnsupportedAppUsage;", // 2
                "public class Class {", // 3
                "  @UnsupportedAppUsage(someProperty=\"value\")", // 4
                "  public void method() {}", // 5
                "}"));
        assertThat(compileAndReadCsv().getContents().get(0)).containsEntry(
                "properties", "someProperty=%22value%22");
    }


}