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

Commit 1418b9e4 authored by Piyush Singhania's avatar Piyush Singhania Committed by Android (Google) Code Review
Browse files

Merge "Fix: Allowing the developer to override the generated class name in the...

Merge "Fix: Allowing the developer to override the generated class name in the top-level annotation." into main
parents 9702183b f46c114f
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ public class CacheConfig {
    private final String mModuleName;
    private final String mApiName;
    private final String mClassName;
    private final String mGeneratedClassName;
    private final String mQualifiedName;
    private String mPropertyName;
    private String mMethodName;
@@ -44,7 +45,8 @@ public class CacheConfig {

        mModuleName = methodAnnotation.module().isEmpty() ? classAnnotation.module()
                : methodAnnotation.module();
        mClassName = classElement.getSimpleName().toString();
        mClassName = classAnnotation.name().isEmpty() ? classElement.getSimpleName().toString()
                : classAnnotation.name();
        mQualifiedName = classElement.getQualifiedName().toString();
        mModifiers = new CacheModifiers(methodAnnotation.mods());
        mMethodName = method.getSimpleName().toString();
@@ -58,6 +60,7 @@ public class CacheConfig {
                method.getParameters().get(0).asType().toString());
        }
        mResultType = primitiveTypeToObjectEquivalent(method.getReturnType().toString());
        mGeneratedClassName = classAnnotation.name().isEmpty() ? mClassName + "Cache" : mClassName;
    }

    public CacheModifiers getModifiers() {
@@ -76,6 +79,10 @@ public class CacheConfig {
        return mClassName;
    }

    public String getGeneratedClassName() {
        return mGeneratedClassName;
    }

    public String getQualifiedName() {
        return mQualifiedName;
    }
+38 −21
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableSet;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -66,6 +67,18 @@ public class CachedPropertyProcessor extends AbstractProcessor {
        return false;
    }

    private List<ExecutableElement> getAnnotatedMethods(TypeElement classElement) {
        List<ExecutableElement> annotatedMethods = new ArrayList<>();
        final List<ExecutableElement> methods =
                ElementFilter.methodsIn(classElement.getEnclosedElements());
        for (ExecutableElement method : methods) {
            if (method.getAnnotation(CachedProperty.class) != null) {
                annotatedMethods.add(method);
            }
        }
        return annotatedMethods;
    }

    private void generateCachedClass(TypeElement classElement, Filer filer) throws IOException {
        String packageName =
                processingEnv
@@ -73,7 +86,12 @@ public class CachedPropertyProcessor extends AbstractProcessor {
                        .getPackageOf(classElement)
                        .getQualifiedName()
                        .toString();
        String className = classElement.getSimpleName().toString() + "Cache";
        final List<ExecutableElement> methods = getAnnotatedMethods(classElement);
        if (methods.size() == 0) {
            return;
        } else {
            final String className = new CacheConfig(classElement, methods.get(0))
                    .getGeneratedClassName();
            JavaFileObject jfo = filer.createSourceFile(packageName + "." + className);
            Writer writer = jfo.openWriter();
            writer.write("package " + packageName + ";\n\n");
@@ -81,16 +99,14 @@ public class CachedPropertyProcessor extends AbstractProcessor {
            writer.write("\n    /** \n    * This class is auto-generated \n    * @hide \n    **/");
            writer.write("\npublic class " + className + " {\n");

        List<ExecutableElement> methods =
                ElementFilter.methodsIn(classElement.getEnclosedElements());
            String initCache = String.format(Constants.METHOD_COMMENT,
                    " - initialise all caches for class " + className)
                    + "\npublic static void initCache() {";
            for (ExecutableElement method : methods) {
            if (method.getAnnotation(CachedProperty.class) != null) {
                mIpcDataCacheComposer.generatePropertyCache(writer, classElement, method);
                initCache += "\n    " + mIpcDataCacheComposer.generateInvalidatePropertyCall();
            }
                CacheConfig cacheConfig = new CacheConfig(classElement, method);
                mIpcDataCacheComposer.generatePropertyCache(writer, cacheConfig);
                initCache += "\n    " + mIpcDataCacheComposer
                        .generateInvalidatePropertyCall();
            }
            initCache += "\n}";
            writer.write(initCache);
@@ -99,3 +115,4 @@ public class CachedPropertyProcessor extends AbstractProcessor {
            writer.close();
        }
    }
}
+3 −7
Original line number Diff line number Diff line
@@ -19,9 +19,6 @@ package android.processor.property_cache;
import java.io.IOException;
import java.io.Writer;

import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;

public class IpcDataCacheComposer {

    private static final String PROPERTY_DEFINITION_LINE = "private %s%s %s;\n";
@@ -39,10 +36,9 @@ public class IpcDataCacheComposer {
     * @param method       method element to generate code for.
     * @throws IOException if writer throws IOException.
     */
    public void generatePropertyCache(Writer writer, TypeElement classElement,
            ExecutableElement method) throws IOException {
    public void generatePropertyCache(Writer writer, CacheConfig cacheConfig) throws IOException {

        mCacheConfig = new CacheConfig(classElement, method);
        mCacheConfig = cacheConfig;

        ParamComposer inputParam = new ParamComposer(null, null);
        ParamComposer binderParam = new ParamComposer(
@@ -96,7 +92,7 @@ public class IpcDataCacheComposer {
     */
    public String generateInvalidatePropertyCall() {
        String invalidateName = "invalidate" + mCacheConfig.getPropertyName();
        return mCacheConfig.getClassName() + "Cache." + invalidateName + "();";
        return mCacheConfig.getGeneratedClassName() + "." + invalidateName + "();";
    }

    /**
+2 −2
Original line number Diff line number Diff line
@@ -52,13 +52,13 @@ public class CachedPropertyProcessorTest {

    @Test
    public void testCustomValues() {
        JavaFileObject expectedJava = JavaFileObjects.forResource("CustomCache.java");
        JavaFileObject expectedJava = JavaFileObjects.forResource("TestCache.java");

        Compilation compilation = mCompiler.compile(JavaFileObjects.forResource("Custom.java"));
        assertThat(compilation).succeeded();
        assertThat(compilation)
                .generatedFile(StandardLocation.SOURCE_OUTPUT,
                        "android/processor/property_cache/test/CustomCache.java")
                        "android/processor/property_cache/test/TestCache.java")
                .hasSourceEquivalentTo(expectedJava);
    }
}
+9 −9
Original line number Diff line number Diff line
@@ -22,13 +22,13 @@ import com.android.internal.annotations.CachedPropertyDefaults;

import java.util.Date;

@CachedPropertyDefaults(max = 4, module = "bluetooth")
@CachedPropertyDefaults(max = 4, module = "bluetooth", name = "TestCache")
public class Custom {
    BirthdayManagerService mService = new BirthdayManagerService();
    Object mCache = new CustomCache();
    Object mCache = new TestCache();

    public Custom() {
        CustomCache.initCache();
        TestCache.initCache();
    }

    /**
@@ -39,7 +39,7 @@ public class Custom {
     */
    @CachedProperty()
    public Date getBirthday(int userId) {
        return CustomCache.getBirthday(mService::getBirthday, userId);
        return TestCache.getBirthday(mService::getBirthday, userId);
    }

    /**
@@ -50,7 +50,7 @@ public class Custom {
     */
    @CachedProperty(mods = {CacheModifier.STATIC})
    public int getDaysTillBirthday(int userId) {
        return CustomCache.getDaysTillBirthday(mService::getDaysTillBirthday, userId);
        return TestCache.getDaysTillBirthday(mService::getDaysTillBirthday, userId);
    }

    /**
@@ -61,7 +61,7 @@ public class Custom {
     */
    @CachedProperty(mods = {})
    public int getDaysSinceBirthday(int userId) {
        return ((CustomCache) mCache).getDaysSinceBirthday(mService::getDaysSinceBirthday, userId);
        return ((TestCache) mCache).getDaysSinceBirthday(mService::getDaysSinceBirthday, userId);
    }

    /**
@@ -71,7 +71,7 @@ public class Custom {
     */
    @CachedProperty(mods = {CacheModifier.STATIC}, max = 1)
    public int getDaysTillMyBirthday() {
        return CustomCache.getDaysTillMyBirthday((Void) -> mService.getDaysTillMyBirthday());
        return TestCache.getDaysTillMyBirthday((Void) -> mService.getDaysTillMyBirthday());
    }

    /**
@@ -82,7 +82,7 @@ public class Custom {
     */
    @CachedProperty(mods = {}, max = 1, api = "my_unique_key")
    public int getDaysSinceMyBirthday() {
        return ((CustomCache) mCache).getDaysSinceMyBirthday(
        return ((TestCache) mCache).getDaysSinceMyBirthday(
                (Void) -> mService.getDaysSinceMyBirthday());
    }

@@ -93,7 +93,7 @@ public class Custom {
     */
    @CachedProperty(module = "telephony")
    public String getBirthdayWishesFromUser(int userId) {
        return CustomCache.getBirthdayWishesFromUser(mService::getBirthdayWishesFromUser,
        return TestCache.getBirthdayWishesFromUser(mService::getBirthdayWishesFromUser,
                userId);
    }

Loading