Search in sources :

Example 66 with DeclaredType

use of javax.lang.model.type.DeclaredType in project androidannotations by androidannotations.

the class APTCodeModelHelper method getMethods.

/**
	 * Gets all of the methods of the class and includes the methods of any
	 * implemented interfaces.
	 *
	 * @param typeElement
	 * @return full list of methods.
	 */
public List<ExecutableElement> getMethods(TypeElement typeElement) {
    List<? extends Element> enclosedElements = typeElement.getEnclosedElements();
    List<ExecutableElement> methods = new ArrayList<>(ElementFilter.methodsIn(enclosedElements));
    // through the validator.
    for (TypeMirror iface : typeElement.getInterfaces()) {
        DeclaredType dt = (DeclaredType) iface;
        methods.addAll(ElementFilter.methodsIn(dt.asElement().getEnclosedElements()));
    }
    return methods;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) DeclaredType(javax.lang.model.type.DeclaredType)

Example 67 with DeclaredType

use of javax.lang.model.type.DeclaredType 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)

Example 68 with DeclaredType

use of javax.lang.model.type.DeclaredType in project androidannotations by androidannotations.

the class APTCodeModelHelper method getActualTypes.

private Map<String, TypeMirror> getActualTypes(Types typeUtils, DeclaredType baseClass, TypeMirror annotatedClass) {
    List<TypeMirror> superTypes = new ArrayList<>();
    superTypes.add(annotatedClass);
    while (!superTypes.isEmpty()) {
        TypeMirror x = superTypes.remove(0);
        if (typeUtils.isSameType(typeUtils.erasure(x), typeUtils.erasure(baseClass))) {
            DeclaredType type = (DeclaredType) x;
            Map<String, TypeMirror> actualTypes = new HashMap<>();
            for (int i = 0; i < type.getTypeArguments().size(); i++) {
                TypeMirror actualArg = type.getTypeArguments().get(i);
                TypeMirror formalArg = baseClass.getTypeArguments().get(i);
                if (!typeUtils.isSameType(actualArg, formalArg)) {
                    actualTypes.put(formalArg.toString(), actualArg);
                }
            }
            return actualTypes;
        }
        superTypes.addAll(typeUtils.directSupertypes(x));
    }
    return Collections.emptyMap();
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) DeclaredType(javax.lang.model.type.DeclaredType)

Example 69 with DeclaredType

use of javax.lang.model.type.DeclaredType in project androidannotations by androidannotations.

the class APTCodeModelHelper method narrowGeneratedClass.

public AbstractJClass narrowGeneratedClass(AbstractJClass generatedClass, TypeMirror fromTypeArguments) {
    DeclaredType type = (DeclaredType) fromTypeArguments;
    for (TypeMirror param : type.getTypeArguments()) {
        AbstractJClass paramClass = typeMirrorToJClass(param);
        generatedClass = generatedClass.narrow(paramClass);
    }
    return generatedClass;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) AbstractJClass(com.helger.jcodemodel.AbstractJClass) DeclaredType(javax.lang.model.type.DeclaredType)

Example 70 with DeclaredType

use of javax.lang.model.type.DeclaredType in project androidannotations by androidannotations.

the class AnnotationHelper method extractAnnotationClassArrayParameter.

public List<DeclaredType> extractAnnotationClassArrayParameter(Element element, String annotationName, String methodName) {
    AnnotationMirror annotationMirror = findAnnotationMirror(element, annotationName);
    Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror.getElementValues();
    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementValues.entrySet()) {
        /*
			 * "methodName" is unset when the default value is used
			 */
        if (methodName.equals(entry.getKey().getSimpleName().toString())) {
            AnnotationValue annotationValue = entry.getValue();
            @SuppressWarnings("unchecked") List<AnnotationValue> annotationClassArray = (List<AnnotationValue>) annotationValue.getValue();
            List<DeclaredType> result = new ArrayList<>(annotationClassArray.size());
            for (AnnotationValue annotationClassValue : annotationClassArray) {
                result.add((DeclaredType) annotationClassValue.getValue());
            }
            return result;
        }
    }
    return null;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ArrayList(java.util.ArrayList) AnnotationValue(javax.lang.model.element.AnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

DeclaredType (javax.lang.model.type.DeclaredType)138 TypeElement (javax.lang.model.element.TypeElement)82 TypeMirror (javax.lang.model.type.TypeMirror)75 ExecutableElement (javax.lang.model.element.ExecutableElement)38 Element (javax.lang.model.element.Element)28 VariableElement (javax.lang.model.element.VariableElement)26 AnnotationMirror (javax.lang.model.element.AnnotationMirror)19 Test (org.junit.Test)19 ArrayType (javax.lang.model.type.ArrayType)15 ArrayList (java.util.ArrayList)14 List (java.util.List)10 Map (java.util.Map)9 AbstractJClass (com.helger.jcodemodel.AbstractJClass)8 HashSet (java.util.HashSet)8 AnnotationValue (javax.lang.model.element.AnnotationValue)8 HashMap (java.util.HashMap)6 TypeParameterElement (javax.lang.model.element.TypeParameterElement)6 Types (javax.lang.model.util.Types)6 Name (javax.lang.model.element.Name)5 PackageElement (javax.lang.model.element.PackageElement)5