Search in sources :

Example 66 with TypeParameterElement

use of javax.lang.model.element.TypeParameterElement in project androidannotations by androidannotations.

the class APTCodeModelHelper method overrideAnnotatedMethod.

public JMethod overrideAnnotatedMethod(ExecutableElement executableElement, GeneratedClassHolder holder) {
    TypeMirror annotatedClass = holder.getAnnotatedElement().asType();
    DeclaredType baseClass = (DeclaredType) executableElement.getEnclosingElement().asType();
    Types typeUtils = environment.getProcessingEnvironment().getTypeUtils();
    Map<String, TypeMirror> actualTypes = getActualTypes(typeUtils, baseClass, annotatedClass);
    Map<String, List<AbstractJClass>> methodTypes = new LinkedHashMap<>();
    for (TypeParameterElement typeParameter : executableElement.getTypeParameters()) {
        List<? extends TypeMirror> bounds = typeParameter.getBounds();
        List<AbstractJClass> addedBounds = typeBoundsToJClass(bounds, actualTypes);
        methodTypes.put(typeParameter.toString(), addedBounds);
    }
    actualTypes.keySet().removeAll(methodTypes.keySet());
    JMethod existingMethod = findAlreadyGeneratedMethod(executableElement, holder);
    if (existingMethod != null) {
        return existingMethod;
    }
    String methodName = executableElement.getSimpleName().toString();
    AbstractJClass returnType = typeMirrorToJClass(executableElement.getReturnType(), actualTypes);
    int modifier = elementVisibilityModifierToJMod(executableElement);
    JMethod method = holder.getGeneratedClass().method(modifier, returnType, methodName);
    copyNonAAAnnotations(method, executableElement.getAnnotationMirrors());
    if (!hasAnnotation(method, Override.class)) {
        method.annotate(Override.class);
    }
    for (Map.Entry<String, List<AbstractJClass>> typeDeclaration : methodTypes.entrySet()) {
        List<AbstractJClass> bounds = typeDeclaration.getValue();
        addTypeBounds(method, bounds, typeDeclaration.getKey());
    }
    int i = 0;
    for (VariableElement parameter : executableElement.getParameters()) {
        boolean varParam = i == executableElement.getParameters().size() - 1 && executableElement.isVarArgs();
        addParamToMethod(method, parameter, JMod.FINAL, actualTypes, varParam);
        i++;
    }
    for (TypeMirror superThrownType : executableElement.getThrownTypes()) {
        AbstractJClass thrownType = typeMirrorToJClass(superThrownType, actualTypes);
        method._throws(thrownType);
    }
    callSuperMethod(method, holder, method.body());
    return method;
}
Also used : Types(javax.lang.model.util.Types) AbstractJClass(com.helger.jcodemodel.AbstractJClass) VariableElement(javax.lang.model.element.VariableElement) LinkedHashMap(java.util.LinkedHashMap) TypeParameterElement(javax.lang.model.element.TypeParameterElement) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) ArrayList(java.util.ArrayList) JMethod(com.helger.jcodemodel.JMethod) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

TypeParameterElement (javax.lang.model.element.TypeParameterElement)66 TypeElement (javax.lang.model.element.TypeElement)30 TypeMirror (javax.lang.model.type.TypeMirror)26 Test (org.junit.Test)18 ExecutableElement (javax.lang.model.element.ExecutableElement)13 TypeVariable (javax.lang.model.type.TypeVariable)13 Element (javax.lang.model.element.Element)10 VariableElement (javax.lang.model.element.VariableElement)9 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)7 List (java.util.List)7 DeclaredType (javax.lang.model.type.DeclaredType)7 MethodSpec (com.squareup.javapoet.MethodSpec)6 Map (java.util.Map)6 FieldSpec (com.squareup.javapoet.FieldSpec)5 HashSet (java.util.HashSet)4 Modifier (javax.lang.model.element.Modifier)4 ArrayType (javax.lang.model.type.ArrayType)4 Types (javax.lang.model.util.Types)4 MoreElements (com.google.auto.common.MoreElements)3