1/* 2 * Copyright (C) 2015 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 */ 16package android.databinding.tool.reflection.annotation; 17 18import android.databinding.Bindable; 19import android.databinding.tool.reflection.ModelClass; 20import android.databinding.tool.reflection.ModelMethod; 21import android.databinding.tool.reflection.SdkUtil; 22import android.databinding.tool.reflection.TypeUtil; 23 24import java.util.List; 25 26import javax.lang.model.element.ExecutableElement; 27import javax.lang.model.element.Modifier; 28import javax.lang.model.type.DeclaredType; 29import javax.lang.model.type.ExecutableType; 30import javax.lang.model.type.TypeKind; 31import javax.lang.model.type.TypeMirror; 32import javax.lang.model.util.Types; 33 34class AnnotationMethod extends ModelMethod { 35 final ExecutableType mMethod; 36 final DeclaredType mDeclaringType; 37 final ExecutableElement mExecutableElement; 38 int mApiLevel = -1; // calculated on demand 39 40 public AnnotationMethod(DeclaredType declaringType, ExecutableElement executableElement) { 41 mDeclaringType = declaringType; 42 mExecutableElement = executableElement; 43 Types typeUtils = AnnotationAnalyzer.get().getTypeUtils(); 44 mMethod = (ExecutableType) typeUtils.asMemberOf(declaringType, executableElement); 45 } 46 47 @Override 48 public ModelClass getDeclaringClass() { 49 return new AnnotationClass(mDeclaringType); 50 } 51 52 @Override 53 public ModelClass[] getParameterTypes() { 54 List<? extends TypeMirror> parameters = mMethod.getParameterTypes(); 55 ModelClass[] parameterTypes = new ModelClass[parameters.size()]; 56 for (int i = 0; i < parameters.size(); i++) { 57 parameterTypes[i] = new AnnotationClass(parameters.get(i)); 58 } 59 return parameterTypes; 60 } 61 62 @Override 63 public String getName() { 64 return mExecutableElement.getSimpleName().toString(); 65 } 66 67 @Override 68 public ModelClass getReturnType(List<ModelClass> args) { 69 TypeMirror returnType = mMethod.getReturnType(); 70 // TODO: support argument-supplied types 71 // for example: public T[] toArray(T[] arr) 72 return new AnnotationClass(returnType); 73 } 74 75 @Override 76 public boolean isVoid() { 77 return mMethod.getReturnType().getKind() == TypeKind.VOID; 78 } 79 80 @Override 81 public boolean isPublic() { 82 return mExecutableElement.getModifiers().contains(Modifier.PUBLIC); 83 } 84 85 @Override 86 public boolean isStatic() { 87 return mExecutableElement.getModifiers().contains(Modifier.STATIC); 88 } 89 90 @Override 91 public boolean isAbstract() { 92 return mExecutableElement.getModifiers().contains(Modifier.ABSTRACT); 93 } 94 95 @Override 96 public boolean isBindable() { 97 return mExecutableElement.getAnnotation(Bindable.class) != null; 98 } 99 100 @Override 101 public int getMinApi() { 102 if (mApiLevel == -1) { 103 mApiLevel = SdkUtil.getMinApi(this); 104 } 105 return mApiLevel; 106 } 107 108 @Override 109 public String getJniDescription() { 110 return TypeUtil.getInstance().getDescription(this); 111 } 112 113 @Override 114 public boolean isVarArgs() { 115 return mExecutableElement.isVarArgs(); 116 } 117 118 @Override 119 public String toString() { 120 return "AnnotationMethod{" + 121 "mMethod=" + mMethod + 122 ", mDeclaringType=" + mDeclaringType + 123 ", mExecutableElement=" + mExecutableElement + 124 ", mApiLevel=" + mApiLevel + 125 '}'; 126 } 127} 128