Merge "Fix SignatureBuilder." am: d1219acf84 am: a8aa26d1ed
am: cb84d0811c

Change-Id: I2c9be5a7db203e1fef778fd44bac9b5da189f248
diff --git a/tools/processors/unsupportedappusage/Android.bp b/tools/processors/unsupportedappusage/Android.bp
index 0e33fdd..1e96234 100644
--- a/tools/processors/unsupportedappusage/Android.bp
+++ b/tools/processors/unsupportedappusage/Android.bp
@@ -1,11 +1,6 @@
 
-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 @@
             "--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,
 }
diff --git a/tools/processors/unsupportedappusage/src/android/processor/unsupportedappusage/SignatureBuilder.java b/tools/processors/unsupportedappusage/src/android/processor/unsupportedappusage/SignatureBuilder.java
index 5a5703e..65fc733 100644
--- a/tools/processors/unsupportedappusage/src/android/processor/unsupportedappusage/SignatureBuilder.java
+++ b/tools/processors/unsupportedappusage/src/android/processor/unsupportedappusage/SignatureBuilder.java
@@ -101,14 +101,20 @@
     private String getClassSignature(TypeElement clazz) {
         StringBuilder sb = new StringBuilder("L");
         for (Element enclosing : getEnclosingElements(clazz)) {
-            if (enclosing.getKind() == PACKAGE) {
-                sb.append(((PackageElement) enclosing)
-                        .getQualifiedName()
-                        .toString()
-                        .replace('.', '/'));
-                sb.append('/');
-            } else {
-                sb.append(enclosing.getSimpleName()).append('$');
+            switch (enclosing.getKind()) {
+                case MODULE:
+                    // ignore this.
+                    break;
+                case PACKAGE:
+                    sb.append(((PackageElement) enclosing)
+                            .getQualifiedName()
+                            .toString()
+                            .replace('.', '/'));
+                    sb.append('/');
+                    break;
+                default:
+                    sb.append(enclosing.getSimpleName()).append('$');
+                    break;
             }
 
         }
diff --git a/tools/processors/unsupportedappusage/test/Android.bp b/tools/processors/unsupportedappusage/test/Android.bp
new file mode 100644
index 0000000..49ea3d4
--- /dev/null
+++ b/tools/processors/unsupportedappusage/test/Android.bp
@@ -0,0 +1,28 @@
+// 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",
+    ],
+}
diff --git a/tools/processors/unsupportedappusage/test/src/android/processor/unsupportedappusage/CsvReader.java b/tools/processors/unsupportedappusage/test/src/android/processor/unsupportedappusage/CsvReader.java
new file mode 100644
index 0000000..23db99e
--- /dev/null
+++ b/tools/processors/unsupportedappusage/test/src/android/processor/unsupportedappusage/CsvReader.java
@@ -0,0 +1,61 @@
+/*
+ * 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;
+    }
+}
diff --git a/tools/processors/unsupportedappusage/test/src/android/processor/unsupportedappusage/UnsupportedAppUsageProcessorTest.java b/tools/processors/unsupportedappusage/test/src/android/processor/unsupportedappusage/UnsupportedAppUsageProcessorTest.java
new file mode 100644
index 0000000..012e88f
--- /dev/null
+++ b/tools/processors/unsupportedappusage/test/src/android/processor/unsupportedappusage/UnsupportedAppUsageProcessorTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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");
+    }
+
+
+}