1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.processor.property_cache; 18 19 import com.android.internal.annotations.CachedProperty; 20 import com.android.internal.annotations.CachedPropertyDefaults; 21 22 import com.google.common.base.CaseFormat; 23 24 import javax.lang.model.element.ExecutableElement; 25 import javax.lang.model.element.TypeElement; 26 27 public class CacheConfig { 28 private final CacheModifiers mModifiers; 29 private final int mMaxSize; 30 private final String mModuleName; 31 private final String mApiName; 32 private final String mClassName; 33 private final String mQualifiedName; 34 private String mPropertyName; 35 private String mMethodName; 36 private int mNumberOfParams = 0; 37 private String mInputType = Constants.JAVA_LANG_VOID; 38 private String mResultType; 39 CacheConfig(TypeElement classElement, ExecutableElement method)40 public CacheConfig(TypeElement classElement, ExecutableElement method) { 41 CachedPropertyDefaults classAnnotation = classElement.getAnnotation( 42 CachedPropertyDefaults.class); 43 CachedProperty methodAnnotation = method.getAnnotation(CachedProperty.class); 44 45 mModuleName = methodAnnotation.module().isEmpty() ? classAnnotation.module() 46 : methodAnnotation.module(); 47 mClassName = classElement.getSimpleName().toString(); 48 mQualifiedName = classElement.getQualifiedName().toString(); 49 mModifiers = new CacheModifiers(methodAnnotation.mods()); 50 mMethodName = method.getSimpleName().toString(); 51 mPropertyName = getPropertyName(mMethodName); 52 mApiName = methodAnnotation.api().isEmpty() ? getUniqueApiName(mClassName, mPropertyName) 53 : methodAnnotation.api(); 54 mMaxSize = methodAnnotation.max() == -1 ? classAnnotation.max() : methodAnnotation.max(); 55 mNumberOfParams = method.getParameters().size(); 56 if (mNumberOfParams > 0) { 57 mInputType = primitiveTypeToObjectEquivalent( 58 method.getParameters().get(0).asType().toString()); 59 } 60 mResultType = primitiveTypeToObjectEquivalent(method.getReturnType().toString()); 61 } 62 getModifiers()63 public CacheModifiers getModifiers() { 64 return mModifiers; 65 } 66 getMaxSize()67 public int getMaxSize() { 68 return mMaxSize; 69 } 70 getApiName()71 public String getApiName() { 72 return mApiName; 73 } 74 getClassName()75 public String getClassName() { 76 return mClassName; 77 } 78 getQualifiedName()79 public String getQualifiedName() { 80 return mQualifiedName; 81 } 82 getModuleName()83 public String getModuleName() { 84 return mModuleName; 85 } 86 getMethodName()87 public String getMethodName() { 88 return mMethodName; 89 } 90 getPropertyName()91 public String getPropertyName() { 92 return mPropertyName; 93 } 94 getPropertyVariable()95 public String getPropertyVariable() { 96 return (mModifiers.isStatic() ? "s" : "m") + mPropertyName; 97 } 98 getPropertyName(String methodName)99 private String getPropertyName(String methodName) { 100 if (methodName.startsWith("get")) { 101 return methodName.substring(3); 102 } else if (methodName.startsWith("is")) { 103 return methodName.substring(2); 104 } else { 105 return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, methodName); 106 } 107 } 108 getNumberOfParams()109 public int getNumberOfParams() { 110 return mNumberOfParams; 111 } 112 getInputType()113 public String getInputType() { 114 return mInputType; 115 } 116 getResultType()117 public String getResultType() { 118 return mResultType; 119 } 120 121 /** 122 * This method returns the unique api name for a given class and property name. 123 * Property name is retrieved from the method name. 124 * Both names are combined and converted to lower snake case. 125 * 126 * @param className The name of the class that contains the property. 127 * @param propertyName The name of the property. 128 * @return The registration name for the property. 129 */ getUniqueApiName(String className, String propertyName)130 private String getUniqueApiName(String className, String propertyName) { 131 return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, className + propertyName); 132 } 133 primitiveTypeToObjectEquivalent(String simpleType)134 private String primitiveTypeToObjectEquivalent(String simpleType) { 135 // checking against primitive types 136 return Constants.PRIMITIVE_TYPE_MAP.getOrDefault(simpleType, simpleType); 137 } 138 } 139