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

Commit fdb5af22 authored by Ember Rose's avatar Ember Rose
Browse files

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
parent a091cc2d
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -53452,11 +53452,6 @@ package android.view.inputmethod {
package android.view.inspector {
  public class GeneratedInspectionCompanionProvider implements android.view.inspector.InspectionCompanionProvider {
    ctor public GeneratedInspectionCompanionProvider();
    method @Nullable public <T> android.view.inspector.InspectionCompanion<T> provide(@NonNull Class<T>);
  }
  public interface InspectionCompanion<T> {
    method @Nullable public default String getNodeName();
    method public void mapProperties(@NonNull android.view.inspector.PropertyMapper);
@@ -53537,6 +53532,11 @@ package android.view.inspector {
    ctor public PropertyReader.PropertyTypeMismatchException(int, @NonNull String, @NonNull String);
  }
  public class StaticInspectionCompanionProvider implements android.view.inspector.InspectionCompanionProvider {
    ctor public StaticInspectionCompanionProvider();
    method @Nullable public <T> android.view.inspector.InspectionCompanion<T> provide(@NonNull Class<T>);
  }
  public final class WindowInspector {
    method @NonNull public static java.util.List<android.view.View> getGlobalWindowViews();
  }
+10 −5
Original line number Diff line number Diff line
@@ -20,15 +20,15 @@ import android.annotation.NonNull;
import android.annotation.Nullable;

/**
 * An inspection companion provider that loads pre-generated inspection companions
 * An inspection companion provider that finds companions as inner classes or generated code.
 *
 * @see android.processor.view.inspector.PlatformInspectableProcessor
 */
public class GeneratedInspectionCompanionProvider implements InspectionCompanionProvider {
public class StaticInspectionCompanionProvider implements InspectionCompanionProvider {
    /**
     * The suffix used for the generated class
     * The suffix used for the generated classes and inner classes
     */
    private static final String COMPANION_SUFFIX = "$$InspectionCompanion";
    private static final String COMPANION_SUFFIX = "$InspectionCompanion";

    @Override
    @Nullable
@@ -39,7 +39,12 @@ public class GeneratedInspectionCompanionProvider implements InspectionCompanion
        try {
            final Class<InspectionCompanion<T>> companionClass =
                    (Class<InspectionCompanion<T>>) cls.getClassLoader().loadClass(companionName);

            if (InspectionCompanion.class.isAssignableFrom(companionClass)) {
                return companionClass.newInstance();
            } else {
                return null;
            }
        } catch (ClassNotFoundException e) {
            return null;
        } catch (IllegalAccessException e) {
+1 −1
Original line number Diff line number Diff line
@@ -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.
+27 −1
Original line number Diff line number Diff line
@@ -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,12 +128,37 @@ 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.
     *
+1 −1
Original line number Diff line number Diff line
@@ -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.
     */
Loading