diff options
author | Ashley Rose <ashleyrose@google.com> | 2019-03-08 17:23:38 -0500 |
---|---|---|
committer | Ashley Rose <ashleyrose@google.com> | 2019-03-08 18:27:51 -0500 |
commit | fdb5af22aa43056de570526799ddf28101037d55 (patch) | |
tree | 487531bfc81d0e98da77087d1d24fe008d7df24e /tools/processors | |
parent | a091cc2d8b3ac4caae4f5400174927753d59477e (diff) |
InspectionCompanions as nested classes
+ Generate inspection companions as MyClass$InspectionCompanion instead
of MyClass$$InspectionCompanion. This allows the discovery of custom
inspection companions written as nested classes.
+ Rename GeneratedInspectionCompanionProvider to
StaticInspectionCompanionProvider to more clearly articulate how it
function in the new world.
+ StaticInspectionCompanionProvider now explicitly checks if a class it
discovered implements InspectionCompanion, and returns null instead of
throwing a ClassCastException.
+ The annotation processor checks for the existence of a nested class
named InspectionCompanion, and fails the build if a class has both a
custom InspectionCompanion and @InspectableProperty annotations.
Test: atest --host view-inspector-annotation-processor-test
Bug: 126913705
Change-Id: Ic0d2100ec22420e36f9db44e56c66fe9146eeb0c
Diffstat (limited to 'tools/processors')
10 files changed, 36 insertions, 10 deletions
diff --git a/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectionCompanionGenerator.java b/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectionCompanionGenerator.java index 44d88bb4b73d..6f6c1aa485ac 100644 --- a/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectionCompanionGenerator.java +++ b/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectionCompanionGenerator.java @@ -97,7 +97,7 @@ public final class InspectionCompanionGenerator { /** * The suffix of the generated class name after the class's binary name. */ - private static final String GENERATED_CLASS_SUFFIX = "$$InspectionCompanion"; + private static final String GENERATED_CLASS_SUFFIX = "$InspectionCompanion"; /** * The null resource ID, copied to avoid a host dependency on platform code. diff --git a/tools/processors/view_inspector/src/java/android/processor/view/inspector/PlatformInspectableProcessor.java b/tools/processors/view_inspector/src/java/android/processor/view/inspector/PlatformInspectableProcessor.java index 01d94307f871..fd142c62e8e3 100644 --- a/tools/processors/view_inspector/src/java/android/processor/view/inspector/PlatformInspectableProcessor.java +++ b/tools/processors/view_inspector/src/java/android/processor/view/inspector/PlatformInspectableProcessor.java @@ -34,6 +34,7 @@ import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; +import javax.lang.model.util.ElementFilter; /** @@ -127,13 +128,38 @@ public final class PlatformInspectableProcessor extends AbstractProcessor { final InspectableClassModel model = modelMap.computeIfAbsent( classElement.get().getQualifiedName().toString(), - k -> new InspectableClassModel(ClassName.get(classElement.get()))); + k -> { + if (hasNestedInspectionCompanion(classElement.get())) { + fail( + String.format( + "Class %s already has an inspection companion.", + classElement.get().getQualifiedName().toString()), + element); + } + return new InspectableClassModel(ClassName.get(classElement.get())); + }); processor.process(element, model); } } /** + * Determine if a class has a nested class named {@code InspectionCompanion}. + * + * @param typeElement A type element representing the class to check + * @return f the class contains a class named {@code InspectionCompanion} + */ + private static boolean hasNestedInspectionCompanion(TypeElement typeElement) { + for (TypeElement nestedClass : ElementFilter.typesIn(typeElement.getEnclosedElements())) { + if (nestedClass.getSimpleName().toString().equals("InspectionCompanion")) { + return true; + } + } + + return false; + } + + /** * Get the nearest enclosing class if there is one. * * If {@param element} represents a class, it will be returned wrapped in an optional. diff --git a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/FieldProperty.java.txt b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/FieldProperty.java.txt index a44c43ec0b21..9a0fe5b76e27 100644 --- a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/FieldProperty.java.txt +++ b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/FieldProperty.java.txt @@ -12,7 +12,7 @@ import java.lang.Override; * Generated by {@link android.processor.view.inspector.InspectionCompanionGenerator} * on behalf of {@link android.processor.view.inspector.InspectionCompanionGeneratorTest}. */ -public final class TestNode$$InspectionCompanion implements InspectionCompanion<TestNode> { +public final class TestNode$InspectionCompanion implements InspectionCompanion<TestNode> { /** * Set by {@link #mapProperties(PropertyMapper)} once properties have been mapped. */ diff --git a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/IntEnum.java.txt b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/IntEnum.java.txt index 764aa8bfbd63..b491de1d63ad 100644 --- a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/IntEnum.java.txt +++ b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/IntEnum.java.txt @@ -13,7 +13,7 @@ import java.lang.Override; * Generated by {@link android.processor.view.inspector.InspectionCompanionGenerator} * on behalf of {@link android.processor.view.inspector.InspectionCompanionGeneratorTest}. */ -public final class TestNode$$InspectionCompanion implements InspectionCompanion<TestNode> { +public final class TestNode$InspectionCompanion implements InspectionCompanion<TestNode> { /** * Set by {@link #mapProperties(PropertyMapper)} once properties have been mapped. */ diff --git a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/IntFlag.java.txt b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/IntFlag.java.txt index 75f281314965..7d180580b45e 100644 --- a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/IntFlag.java.txt +++ b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/IntFlag.java.txt @@ -12,7 +12,7 @@ import java.lang.Override; * Generated by {@link android.processor.view.inspector.InspectionCompanionGenerator} * on behalf of {@link android.processor.view.inspector.InspectionCompanionGeneratorTest}. */ -public final class TestNode$$InspectionCompanion implements InspectionCompanion<TestNode> { +public final class TestNode$InspectionCompanion implements InspectionCompanion<TestNode> { /** * Set by {@link #mapProperties(PropertyMapper)} once properties have been mapped. */ diff --git a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NestedClass.java.txt b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NestedClass.java.txt index 0cac462fba51..dc27abbe6fbd 100644 --- a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NestedClass.java.txt +++ b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NestedClass.java.txt @@ -11,7 +11,7 @@ import java.lang.Override; * Generated by {@link android.processor.view.inspector.InspectionCompanionGenerator} * on behalf of {@link android.processor.view.inspector.InspectionCompanionGeneratorTest}. */ -public final class Outer$Inner$$InspectionCompanion implements InspectionCompanion<Outer.Inner> { +public final class Outer$Inner$InspectionCompanion implements InspectionCompanion<Outer.Inner> { /** * Set by {@link #mapProperties(PropertyMapper)} once properties have been mapped. */ diff --git a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NoAttributeId.java.txt b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NoAttributeId.java.txt index ce0f867d5332..738bcd3dab36 100644 --- a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NoAttributeId.java.txt +++ b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NoAttributeId.java.txt @@ -11,7 +11,7 @@ import java.lang.Override; * Generated by {@link android.processor.view.inspector.InspectionCompanionGenerator} * on behalf of {@link android.processor.view.inspector.InspectionCompanionGeneratorTest}. */ -public final class TestNode$$InspectionCompanion implements InspectionCompanion<TestNode> { +public final class TestNode$InspectionCompanion implements InspectionCompanion<TestNode> { /** * Set by {@link #mapProperties(PropertyMapper)} once properties have been mapped. */ diff --git a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NodeName.java.txt b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NodeName.java.txt index f7357fece2bf..82dd66e2597e 100644 --- a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NodeName.java.txt +++ b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NodeName.java.txt @@ -12,7 +12,7 @@ import java.lang.String; * Generated by {@link android.processor.view.inspector.InspectionCompanionGenerator} * on behalf of {@link android.processor.view.inspector.InspectionCompanionGeneratorTest}. */ -public final class TestNode$$InspectionCompanion implements InspectionCompanion<TestNode> { +public final class TestNode$InspectionCompanion implements InspectionCompanion<TestNode> { /** * Set by {@link #mapProperties(PropertyMapper)} once properties have been mapped. */ diff --git a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/SimpleProperties.java.txt b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/SimpleProperties.java.txt index 556d8ddc5d5e..08ea69679086 100644 --- a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/SimpleProperties.java.txt +++ b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/SimpleProperties.java.txt @@ -12,7 +12,7 @@ import java.lang.Override; * Generated by {@link android.processor.view.inspector.InspectionCompanionGenerator} * on behalf of {@link android.processor.view.inspector.InspectionCompanionGeneratorTest}. */ -public final class TestNode$$InspectionCompanion implements InspectionCompanion<TestNode> { +public final class TestNode$InspectionCompanion implements InspectionCompanion<TestNode> { /** * Set by {@link #mapProperties(PropertyMapper)} once properties have been mapped. */ diff --git a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/SuppliedAttributeId.java.txt b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/SuppliedAttributeId.java.txt index d72cdd533205..3bfa78ac857f 100644 --- a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/SuppliedAttributeId.java.txt +++ b/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/SuppliedAttributeId.java.txt @@ -11,7 +11,7 @@ import java.lang.Override; * Generated by {@link android.processor.view.inspector.InspectionCompanionGenerator} * on behalf of {@link android.processor.view.inspector.InspectionCompanionGeneratorTest}. */ -public final class TestNode$$InspectionCompanion implements InspectionCompanion<TestNode> { +public final class TestNode$InspectionCompanion implements InspectionCompanion<TestNode> { /** * Set by {@link #mapProperties(PropertyMapper)} once properties have been mapped. */ |