Search in sources :

Example 41 with DeclaredType

use of javax.lang.model.type.DeclaredType in project j2objc by google.

the class CastResolver method endVisit.

/**
   * Adds a cast check to compareTo methods. This helps Comparable types behave
   * well in sorted collections which rely on Java's runtime type checking.
   */
@Override
public void endVisit(MethodDeclaration node) {
    ExecutableElement element = node.getExecutableElement();
    if (!ElementUtil.getName(element).equals("compareTo") || node.getBody() == null) {
        return;
    }
    DeclaredType comparableType = typeUtil.findSupertype(ElementUtil.getDeclaringClass(element).asType(), "java.lang.Comparable");
    if (comparableType == null) {
        return;
    }
    List<? extends TypeMirror> typeArguments = comparableType.getTypeArguments();
    List<? extends VariableElement> parameters = element.getParameters();
    if (typeArguments.size() != 1 || parameters.size() != 1 || !typeArguments.get(0).equals(parameters.get(0).asType())) {
        return;
    }
    VariableElement param = node.getParameter(0).getVariableElement();
    FunctionInvocation castCheck = createCastCheck(typeArguments.get(0), new SimpleName(param));
    if (castCheck != null) {
        node.getBody().addStatement(0, new ExpressionStatement(castCheck));
    }
}
Also used : FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableElement(javax.lang.model.element.VariableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 42 with DeclaredType

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

the class AbstractQuerydslProcessor method handleEmbeddedType.

private void handleEmbeddedType(Element element, Set<TypeElement> elements) {
    TypeMirror type = element.asType();
    if (element.getKind() == ElementKind.METHOD) {
        type = ((ExecutableElement) element).getReturnType();
    }
    String typeName = type.toString();
    if (typeName.startsWith(Collection.class.getName()) || typeName.startsWith(List.class.getName()) || typeName.startsWith(Set.class.getName())) {
        type = ((DeclaredType) type).getTypeArguments().get(0);
    } else if (typeName.startsWith(Map.class.getName())) {
        type = ((DeclaredType) type).getTypeArguments().get(1);
    }
    TypeElement typeElement = typeExtractor.visit(type);
    if (typeElement != null && !TypeUtils.hasAnnotationOfType(typeElement, conf.getEntityAnnotations())) {
        if (!typeElement.getQualifiedName().toString().startsWith("java.")) {
            elements.add(typeElement);
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 43 with DeclaredType

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

the class TypeUtils method getAnnotationValuesAsElements.

@SuppressWarnings("unchecked")
public static Set<TypeElement> getAnnotationValuesAsElements(AnnotationMirror mirror, String method) {
    Set<TypeElement> elements = new HashSet<TypeElement>();
    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : mirror.getElementValues().entrySet()) {
        if (entry.getKey().getSimpleName().toString().equals(method)) {
            List<AnnotationValue> values = ((List) entry.getValue().getValue());
            for (AnnotationValue value : values) {
                DeclaredType type = (DeclaredType) value.getValue();
                elements.add((TypeElement) type.asElement());
            }
        }
    }
    return elements;
}
Also used : Map(java.util.Map) HashSet(java.util.HashSet) DeclaredType(javax.lang.model.type.DeclaredType)

Example 44 with DeclaredType

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

the class JPAConfiguration method getRealElementType.

private TypeMirror getRealElementType(Element element) {
    AnnotationMirror mirror = TypeUtils.getAnnotationMirrorOfType(element, ManyToOne.class);
    if (mirror == null) {
        mirror = TypeUtils.getAnnotationMirrorOfType(element, OneToOne.class);
    }
    if (mirror != null) {
        return TypeUtils.getAnnotationValueAsTypeMirror(mirror, "targetEntity");
    }
    mirror = TypeUtils.getAnnotationMirrorOfType(element, OneToMany.class);
    if (mirror == null) {
        mirror = TypeUtils.getAnnotationMirrorOfType(element, ManyToMany.class);
    }
    if (mirror != null) {
        TypeMirror typeArg = TypeUtils.getAnnotationValueAsTypeMirror(mirror, "targetEntity");
        TypeMirror erasure = types.erasure(element.asType());
        TypeElement typeElement = (TypeElement) types.asElement(erasure);
        if (typeElement != null && typeArg != null) {
            if (typeElement.getTypeParameters().size() == 1) {
                return types.getDeclaredType(typeElement, typeArg);
            } else if (typeElement.getTypeParameters().size() == 2) {
                if (element.asType() instanceof DeclaredType) {
                    TypeMirror first = ((DeclaredType) element.asType()).getTypeArguments().get(0);
                    return types.getDeclaredType(typeElement, first, typeArg);
                }
            }
        }
    }
    return null;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 45 with DeclaredType

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

the class GuiceAnnotationProcessor method addMethodOrConstructorToAnnotationDatabase.

private void addMethodOrConstructorToAnnotationDatabase(String annotationClassName, Element injectionPoint) {
    String injectionPointName = injectionPoint.getSimpleName().toString();
    for (VariableElement variable : ((ExecutableElement) injectionPoint).getParameters()) {
        String parameterTypeName = getTypeName((TypeElement) ((DeclaredType) variable.asType()).asElement());
        bindableClasses.add(parameterTypeName);
        injectionPointName += ":" + parameterTypeName;
    }
    TypeElement typeElementRequiringScanning = (TypeElement) injectionPoint.getEnclosingElement();
    String typeElementName = getTypeName(typeElementRequiringScanning);
    //System.out.printf("Type: %s, injection: %s \n",typeElementName, injectionPointName);
    if (injectionPointName.startsWith("<init>")) {
        addToInjectedConstructors(annotationClassName, typeElementName, injectionPointName);
    } else {
        addToInjectedMethods(annotationClassName, typeElementName, injectionPointName);
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) String(java.lang.String) VariableElement(javax.lang.model.element.VariableElement) 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