Merge "Remove unused @InspectableNodeName"
This commit is contained in:
committed by
Android (Google) Code Review
commit
7177077bb5
@@ -53507,7 +53507,6 @@ package android.view.inputmethod {
|
||||
package android.view.inspector {
|
||||
|
||||
public interface InspectionCompanion<T> {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -3186,10 +3186,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 {};
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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 <T> The type of inspectable this is the companion to
|
||||
*/
|
||||
public interface InspectionCompanion<T> {
|
||||
@@ -67,22 +61,6 @@ public interface InspectionCompanion<T> {
|
||||
*/
|
||||
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)}.
|
||||
|
||||
@@ -39,7 +39,6 @@ import java.util.Optional;
|
||||
public final class InspectableClassModel {
|
||||
private final @NonNull ClassName mClassName;
|
||||
private final @NonNull Map<String, Property> mPropertyMap;
|
||||
private @NonNull Optional<String> mNodeName = Optional.empty();
|
||||
|
||||
/**
|
||||
* @param className The name of the modeled class
|
||||
@@ -54,15 +53,6 @@ public final class InspectableClassModel {
|
||||
return mClassName;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Optional<String> getNodeName() {
|
||||
return mNodeName;
|
||||
}
|
||||
|
||||
public void setNodeName(@NonNull Optional<String> nodeName) {
|
||||
mNodeName = nodeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a property to the model, replacing an existing property of the same name.
|
||||
*
|
||||
|
||||
@@ -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<String> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
* <pre>
|
||||
* @Override
|
||||
* public String getNodeName() {
|
||||
* return "nodeName";
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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<String, InspectableClassModel> 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<? extends Element> elements,
|
||||
@NonNull ModelProcessor processor,
|
||||
@NonNull Map<String, InspectableClassModel> modelMap) {
|
||||
final InspectablePropertyProcessor processor =
|
||||
new InspectablePropertyProcessor(ANNOTATION_QUALIFIED_NAME, processingEnv);
|
||||
|
||||
for (Element element : elements) {
|
||||
final Optional<TypeElement> classElement = enclosingClassElement(element);
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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<TestNode> {
|
||||
/**
|
||||
* 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";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user