Loading tools/processors/property_cache/src/java/android/processor/property_cache/CacheConfig.java +8 −1 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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(); Loading @@ -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() { Loading @@ -76,6 +79,10 @@ public class CacheConfig { return mClassName; } public String getGeneratedClassName() { return mGeneratedClassName; } public String getQualifiedName() { return mQualifiedName; } Loading tools/processors/property_cache/src/java/android/processor/property_cache/CachedPropertyProcessor.java +38 −21 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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 Loading @@ -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"); Loading @@ -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); Loading @@ -99,3 +115,4 @@ public class CachedPropertyProcessor extends AbstractProcessor { writer.close(); } } } tools/processors/property_cache/src/java/android/processor/property_cache/IpcDataCacheComposer.java +3 −7 Original line number Diff line number Diff line Loading @@ -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"; Loading @@ -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( Loading Loading @@ -96,7 +92,7 @@ public class IpcDataCacheComposer { */ public String generateInvalidatePropertyCall() { String invalidateName = "invalidate" + mCacheConfig.getPropertyName(); return mCacheConfig.getClassName() + "Cache." + invalidateName + "();"; return mCacheConfig.getGeneratedClassName() + "." + invalidateName + "();"; } /** Loading tools/processors/property_cache/test/java/android/processor/property_cache/CachedPropertyProcessorTest.java +2 −2 Original line number Diff line number Diff line Loading @@ -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); } } tools/processors/property_cache/test/resources/Custom.java +9 −9 Original line number Diff line number Diff line Loading @@ -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(); } /** Loading @@ -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); } /** Loading @@ -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); } /** Loading @@ -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); } /** Loading @@ -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()); } /** Loading @@ -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()); } Loading @@ -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 Loading
tools/processors/property_cache/src/java/android/processor/property_cache/CacheConfig.java +8 −1 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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(); Loading @@ -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() { Loading @@ -76,6 +79,10 @@ public class CacheConfig { return mClassName; } public String getGeneratedClassName() { return mGeneratedClassName; } public String getQualifiedName() { return mQualifiedName; } Loading
tools/processors/property_cache/src/java/android/processor/property_cache/CachedPropertyProcessor.java +38 −21 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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 Loading @@ -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"); Loading @@ -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); Loading @@ -99,3 +115,4 @@ public class CachedPropertyProcessor extends AbstractProcessor { writer.close(); } } }
tools/processors/property_cache/src/java/android/processor/property_cache/IpcDataCacheComposer.java +3 −7 Original line number Diff line number Diff line Loading @@ -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"; Loading @@ -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( Loading Loading @@ -96,7 +92,7 @@ public class IpcDataCacheComposer { */ public String generateInvalidatePropertyCall() { String invalidateName = "invalidate" + mCacheConfig.getPropertyName(); return mCacheConfig.getClassName() + "Cache." + invalidateName + "();"; return mCacheConfig.getGeneratedClassName() + "." + invalidateName + "();"; } /** Loading
tools/processors/property_cache/test/java/android/processor/property_cache/CachedPropertyProcessorTest.java +2 −2 Original line number Diff line number Diff line Loading @@ -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); } }
tools/processors/property_cache/test/resources/Custom.java +9 −9 Original line number Diff line number Diff line Loading @@ -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(); } /** Loading @@ -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); } /** Loading @@ -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); } /** Loading @@ -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); } /** Loading @@ -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()); } /** Loading @@ -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()); } Loading @@ -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