From b948bcd44de5afc72a498a8c367a7a15fd4c580b Mon Sep 17 00:00:00 2001 From: Ashley Rose Date: Wed, 20 Mar 2019 15:18:25 -0400 Subject: [PATCH] Remove unused @InspectableNodeName Change-Id: Ib5a2688e7b25c4c8ba43dc4aeb80e17a12427f9c Fix: 128997046 Test: atest --host view-inspector-annotation-processor-test --- api/current.txt | 1 - api/test-current.txt | 4 - .../view/inspector/InspectableNodeName.java | 52 ------------ .../view/inspector/InspectionCompanion.java | 22 ------ .../view/inspector/InspectableClassModel.java | 10 --- .../InspectableNodeNameProcessor.java | 79 ------------------- .../InspectablePropertyProcessor.java | 7 +- .../InspectionCompanionGenerator.java | 27 ------- .../view/inspector/ModelProcessor.java | 34 -------- .../PlatformInspectableProcessor.java | 44 ++++------- .../InspectionCompanionGeneratorTest.java | 7 -- .../NodeName.java.txt | 37 --------- 12 files changed, 17 insertions(+), 307 deletions(-) delete mode 100644 core/java/android/view/inspector/InspectableNodeName.java delete mode 100644 tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectableNodeNameProcessor.java delete mode 100644 tools/processors/view_inspector/src/java/android/processor/view/inspector/ModelProcessor.java delete mode 100644 tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NodeName.java.txt diff --git a/api/current.txt b/api/current.txt index d48b561746f62..1f0fa8b01480c 100644 --- a/api/current.txt +++ b/api/current.txt @@ -53501,7 +53501,6 @@ package android.view.inputmethod { package android.view.inspector { public interface InspectionCompanion { - method @Nullable public default String getNodeName(); method public void mapProperties(@NonNull android.view.inspector.PropertyMapper); method public void readProperties(@NonNull T, @NonNull android.view.inspector.PropertyReader); } diff --git a/api/test-current.txt b/api/test-current.txt index c2ee2c3d46848..8ebbdcd3018d2 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -3166,10 +3166,6 @@ package android.view.inputmethod { package android.view.inspector { - @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE) @java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE}) public @interface InspectableNodeName { - method public abstract String value(); - } - @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE) @java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.FIELD}) public @interface InspectableProperty { method public abstract int attributeId() default android.content.res.Resources.ID_NULL; method public abstract android.view.inspector.InspectableProperty.EnumEntry[] enumMapping() default {}; diff --git a/core/java/android/view/inspector/InspectableNodeName.java b/core/java/android/view/inspector/InspectableNodeName.java deleted file mode 100644 index 7b9a507ee45d0..0000000000000 --- a/core/java/android/view/inspector/InspectableNodeName.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2018 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.view.inspector; - -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.RetentionPolicy.SOURCE; - -import android.annotation.TestApi; - -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -/** - * Marks the node name to display to a developer in the inspection tree. - * - * This annotation is optional to marking a class as inspectable. If it is omitted, the node name - * will be inferred using the semantics of {@link Class#getSimpleName()}. The fully qualified class - * name is always available in the tree, this is for display purposes only. If a class is inflated - * from XML and the tag it inflates from does not match its simple name, this annotation should be - * used to inform the inspector to display the XML tag name in the inspection tree view. - * - * This annotation does not inherit. If a class extends an annotated parent class, but does not - * annotate itself, its node name will be inferred from its Java name. - * - * @see InspectionCompanion#getNodeName() - * @hide - */ -@Target({TYPE}) -@Retention(SOURCE) -@TestApi -public @interface InspectableNodeName { - /** - * The display name for nodes of this type. - * - * @return The name for nodes of this type - */ - String value(); -} diff --git a/core/java/android/view/inspector/InspectionCompanion.java b/core/java/android/view/inspector/InspectionCompanion.java index 62d769b6d963a..a633a58065a09 100644 --- a/core/java/android/view/inspector/InspectionCompanion.java +++ b/core/java/android/view/inspector/InspectionCompanion.java @@ -17,7 +17,6 @@ package android.view.inspector; import android.annotation.NonNull; -import android.annotation.Nullable; /** * An interface for companion objects used to inspect views. @@ -33,11 +32,6 @@ import android.annotation.Nullable; * parent class via the parent's inspection companion, and the child companion will only read * properties added or changed since the parent was defined. * - * Only one child traversal is considered for each class. If a descendant class defines a - * different child traversal than its parent, only the bottom traversal is used. If a class does - * not define its own child traversal, but one of its ancestors does, the bottom-most ancestor's - * traversal will be used. - * * @param The type of inspectable this is the companion to */ public interface InspectionCompanion { @@ -67,22 +61,6 @@ public interface InspectionCompanion { */ void readProperties(@NonNull T inspectable, @NonNull PropertyReader propertyReader); - /** - * Get an optional name to display to developers for inspection nodes of this companion's type. - * - * The default implementation returns null, which will cause the runtime to use the class's - * simple name as defined by {@link Class#getSimpleName()} as the node name. - * - * If the type of this companion is inflated from XML, this method should be overridden to - * return the string used as the tag name for this type in XML. - * - * @return A string to use as the node name, or null to use the simple class name fallback. - */ - @Nullable - default String getNodeName() { - return null; - } - /** * Thrown by {@link #readProperties(Object, PropertyReader)} if called before * {@link #mapProperties(PropertyMapper)}. diff --git a/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectableClassModel.java b/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectableClassModel.java index 147f054b1abbf..04710e436ffb4 100644 --- a/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectableClassModel.java +++ b/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectableClassModel.java @@ -39,7 +39,6 @@ import java.util.Optional; public final class InspectableClassModel { private final @NonNull ClassName mClassName; private final @NonNull Map mPropertyMap; - private @NonNull Optional mNodeName = Optional.empty(); /** * @param className The name of the modeled class @@ -54,15 +53,6 @@ public final class InspectableClassModel { return mClassName; } - @NonNull - public Optional getNodeName() { - return mNodeName; - } - - public void setNodeName(@NonNull Optional nodeName) { - mNodeName = nodeName; - } - /** * Add a property to the model, replacing an existing property of the same name. * diff --git a/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectableNodeNameProcessor.java b/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectableNodeNameProcessor.java deleted file mode 100644 index 64a60fbc91793..0000000000000 --- a/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectableNodeNameProcessor.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 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.view.inspector; - -import androidx.annotation.NonNull; - -import java.util.Optional; - -import javax.annotation.processing.ProcessingEnvironment; -import javax.lang.model.element.AnnotationMirror; -import javax.lang.model.element.Element; - -/** - * Process {@code @InspectableNodeName} annotations. - * - * @see android.view.inspector.InspectableNodeName - */ -public final class InspectableNodeNameProcessor implements ModelProcessor { - private final @NonNull String mQualifiedName; - private final @NonNull ProcessingEnvironment mProcessingEnv; - private final @NonNull AnnotationUtils mAnnotationUtils; - - /** - * @param annotationQualifiedName The qualified name of the annotation to process - * @param processingEnv The processing environment from the parent processor - */ - public InspectableNodeNameProcessor( - @NonNull String annotationQualifiedName, - @NonNull ProcessingEnvironment processingEnv) { - mQualifiedName = annotationQualifiedName; - mProcessingEnv = processingEnv; - mAnnotationUtils = new AnnotationUtils(processingEnv); - } - - /** - * Set the node name on the model if one is supplied. - * - * If the model already has a different node name, the node name will not be updated, and - * the processor will print an error the the messager. - * - * @param element The annotated element to operate on - * @param model The model this element should be merged into - */ - @Override - public void process(@NonNull Element element, @NonNull InspectableClassModel model) { - try { - final AnnotationMirror mirror = - mAnnotationUtils.exactlyOneMirror(mQualifiedName, element); - final Optional nodeName = mAnnotationUtils - .typedValueByName("value", String.class, element, mirror); - - if (!model.getNodeName().isPresent() || model.getNodeName().equals(nodeName)) { - model.setNodeName(nodeName); - } else { - final String message = String.format( - "Node name was already set to \"%s\", refusing to change it to \"%s\".", - model.getNodeName().get(), - nodeName); - throw new ProcessingException(message, element, mirror); - } - } catch (ProcessingException processingException) { - processingException.print(mProcessingEnv.getMessager()); - } - } -} diff --git a/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectablePropertyProcessor.java b/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectablePropertyProcessor.java index 7f5f9ca8a8f84..2bd867c235c73 100644 --- a/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectablePropertyProcessor.java +++ b/tools/processors/view_inspector/src/java/android/processor/view/inspector/InspectablePropertyProcessor.java @@ -46,7 +46,7 @@ import javax.lang.model.type.TypeMirror; * * @see android.view.inspector.InspectableProperty */ -public final class InspectablePropertyProcessor implements ModelProcessor { +public final class InspectablePropertyProcessor { private final @NonNull String mQualifiedName; private final @NonNull ProcessingEnvironment mProcessingEnv; private final @NonNull AnnotationUtils mAnnotationUtils; @@ -139,7 +139,6 @@ public final class InspectablePropertyProcessor implements ModelProcessor { mAnnotationUtils = new AnnotationUtils(processingEnv); } - @Override public void process(@NonNull Element element, @NonNull InspectableClassModel model) { try { final AnnotationMirror annotation = @@ -608,7 +607,7 @@ public final class InspectablePropertyProcessor implements ModelProcessor { * Build a model of an {@code int} enumeration mapping from annotation values. * * This method only handles the one-to-one mapping of mirrors of - * {@link android.view.inspector.InspectableProperty.EnumMap} annotations into + * {@link android.view.inspector.InspectableProperty.EnumEntry} annotations into * {@link IntEnumEntry} objects. Further validation should be handled elsewhere * * @see android.view.inspector.InspectableProperty#enumMapping() @@ -656,7 +655,7 @@ public final class InspectablePropertyProcessor implements ModelProcessor { * Build a model of an {@code int} flag mapping from annotation values. * * This method only handles the one-to-one mapping of mirrors of - * {@link android.view.inspector.InspectableProperty.FlagMap} annotations into + * {@link android.view.inspector.InspectableProperty.FlagEntry} annotations into * {@link IntFlagEntry} objects. Further validation should be handled elsewhere * * @see android.view.inspector.IntFlagMapping 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 c428a4613c952..6e38c245c70e2 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 @@ -163,8 +163,6 @@ public final class InspectionCompanionGenerator { .addMethod(generateMapProperties(properties, fields)) .addMethod(generateReadProperties(properties, fields, model.getClassName())); - model.getNodeName().ifPresent(name -> builder.addMethod(generateGetNodeName(name))); - return builder.build(); } @@ -450,31 +448,6 @@ public final class InspectionCompanionGenerator { return builder.build(); } - /** - * Generate an implementation of - * {@link android.view.inspector.InspectionCompanion#getNodeName()}. - * - * Example: - *
-     *     @Override
-     *     public String getNodeName() {
-     *         return "nodeName";
-     *     }
-     * 
- * - * @param nodeName The name of this node - * @return A method definition that returns the node name - */ - @NonNull - private MethodSpec generateGetNodeName(@NonNull String nodeName) { - return MethodSpec.methodBuilder("getNodeName") - .addAnnotation(Override.class) - .addModifiers(Modifier.PUBLIC) - .returns(String.class) - .addStatement("return $S", nodeName) - .build(); - } - /** * Generate the final class name for the inspection companion from the model's class name. * diff --git a/tools/processors/view_inspector/src/java/android/processor/view/inspector/ModelProcessor.java b/tools/processors/view_inspector/src/java/android/processor/view/inspector/ModelProcessor.java deleted file mode 100644 index ab38f4c9e1b02..0000000000000 --- a/tools/processors/view_inspector/src/java/android/processor/view/inspector/ModelProcessor.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 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.view.inspector; - -import androidx.annotation.NonNull; - -import javax.lang.model.element.Element; - -/** - * An interface for annotation processors that operate on a single element and a class model. - */ -public interface ModelProcessor { - /** - * Process the supplied element, mutating the model as needed. - * - * @param element The annotated element to operate on - * @param model The model this element should be merged into - */ - void process(@NonNull Element element, @NonNull InspectableClassModel model); -} 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 d9ed1fb9f3438..80d37273b4222 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 @@ -38,25 +38,18 @@ import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; import javax.lang.model.util.ElementFilter; - /** * An annotation processor for the platform inspectable annotations. * - * It mostly delegates to {@link ModelProcessor} and {@link InspectionCompanionGenerator}. This - * modular architecture allows the core generation code to be reused for comparable annotations - * outside the platform, such as in AndroidX. + * It mostly delegates to {@link InspectablePropertyProcessor} and + * {@link InspectionCompanionGenerator}. This modular architecture allows the core generation code + * to be reused for comparable annotations outside the platform. * - * @see android.view.inspector.InspectableNodeName * @see android.view.inspector.InspectableProperty */ -@SupportedAnnotationTypes({ - PlatformInspectableProcessor.NODE_NAME_QUALIFIED_NAME, - PlatformInspectableProcessor.PROPERTY_QUALIFIED_NAME -}) +@SupportedAnnotationTypes({PlatformInspectableProcessor.ANNOTATION_QUALIFIED_NAME}) public final class PlatformInspectableProcessor extends AbstractProcessor { - static final String NODE_NAME_QUALIFIED_NAME = - "android.view.inspector.InspectableNodeName"; - static final String PROPERTY_QUALIFIED_NAME = + static final String ANNOTATION_QUALIFIED_NAME = "android.view.inspector.InspectableProperty"; @Override @@ -71,18 +64,8 @@ public final class PlatformInspectableProcessor extends AbstractProcessor { final Map modelMap = new HashMap<>(); for (TypeElement annotation : annotations) { - if (annotation.getQualifiedName().contentEquals(NODE_NAME_QUALIFIED_NAME)) { - runModelProcessor( - roundEnv.getElementsAnnotatedWith(annotation), - new InspectableNodeNameProcessor(NODE_NAME_QUALIFIED_NAME, processingEnv), - modelMap); - - } else if (annotation.getQualifiedName().contentEquals(PROPERTY_QUALIFIED_NAME)) { - runModelProcessor( - roundEnv.getElementsAnnotatedWith(annotation), - new InspectablePropertyProcessor(PROPERTY_QUALIFIED_NAME, processingEnv), - modelMap); - + if (annotation.getQualifiedName().contentEquals(ANNOTATION_QUALIFIED_NAME)) { + processProperties(roundEnv.getElementsAnnotatedWith(annotation), modelMap); } else { fail("Unexpected annotation type", annotation); } @@ -106,16 +89,17 @@ public final class PlatformInspectableProcessor extends AbstractProcessor { } /** - * Run a {@link ModelProcessor} for a set of elements + * Runs {@link PlatformInspectableProcessor} on a set of annotated elements. * - * @param elements Elements to process, should be annotated correctly - * @param processor The processor to use - * @param modelMap A map of qualified class names to models + * @param elements A set of annotated elements to process + * @param modelMap A map of qualified class names to class models to update */ - private void runModelProcessor( + private void processProperties( @NonNull Set elements, - @NonNull ModelProcessor processor, @NonNull Map modelMap) { + final InspectablePropertyProcessor processor = + new InspectablePropertyProcessor(ANNOTATION_QUALIFIED_NAME, processingEnv); + for (Element element : elements) { final Optional classElement = enclosingClassElement(element); diff --git a/tools/processors/view_inspector/test/java/android/processor/view/inspector/InspectionCompanionGeneratorTest.java b/tools/processors/view_inspector/test/java/android/processor/view/inspector/InspectionCompanionGeneratorTest.java index 3ec620a1a4bfe..c6e6018869ac1 100644 --- a/tools/processors/view_inspector/test/java/android/processor/view/inspector/InspectionCompanionGeneratorTest.java +++ b/tools/processors/view_inspector/test/java/android/processor/view/inspector/InspectionCompanionGeneratorTest.java @@ -36,7 +36,6 @@ import org.junit.Test; import java.io.IOException; import java.net.URL; import java.util.Arrays; -import java.util.Optional; /** * Tests for {@link InspectionCompanionGenerator} @@ -55,12 +54,6 @@ public class InspectionCompanionGeneratorTest { mGenerator = new InspectionCompanionGenerator(null, getClass()); } - @Test - public void testNodeName() { - mModel.setNodeName(Optional.of("NodeName")); - assertGeneratedFileEquals("NodeName"); - } - @Test public void testNestedClass() { mModel = new InspectableClassModel( 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 deleted file mode 100644 index ffa1f0be02d41..0000000000000 --- a/tools/processors/view_inspector/test/resources/android/processor/view/inspector/InspectionCompanionGeneratorTest/NodeName.java.txt +++ /dev/null @@ -1,37 +0,0 @@ -package com.android.node; - -import android.view.inspector.InspectionCompanion; -import android.view.inspector.PropertyMapper; -import android.view.inspector.PropertyReader; -import java.lang.Override; -import java.lang.String; - -/** - * Inspection companion for {@link TestNode}. - * - * Generated by {@link android.processor.view.inspector.InspectionCompanionGenerator} - * on behalf of {@link android.processor.view.inspector.InspectionCompanionGeneratorTest}. - */ -public final class TestNode$InspectionCompanion implements InspectionCompanion { - /** - * Guards against reading properties before mapping them. - */ - private boolean mPropertiesMapped = false; - - @Override - public void mapProperties(PropertyMapper propertyMapper) { - mPropertiesMapped = true; - } - - @Override - public void readProperties(TestNode node, PropertyReader propertyReader) { - if (!mPropertiesMapped) { - throw new InspectionCompanion.UninitializedPropertyMapException(); - } - } - - @Override - public String getNodeName() { - return "NodeName"; - } -}