Search in sources :

Example 76 with TypeElement

use of javax.lang.model.element.TypeElement in project j2objc by google.

the class ElementReferenceMapper method endVisit.

//TODO(malvania): Add the field type class to reference classes.
//Currently, jdt only supports well known types. Soon, we can get type mirror from field 
//and resolve the type by its name using a resolve method in the parser environment.
@Override
public void endVisit(VariableDeclarationFragment fragment) {
    //TODO(malvania): Add field to elementReferenceMap when field detection is enabled and the
    //  ElementUtil.getBinaryName() method doesn't break when called on a static block's
    //  ExecutableElement.
    //String fieldID = stitchFieldIdentifier(fragment);
    //elementReferenceMap.putIfAbsent(fieldID, new FieldReferenceNode(fragment));
    Element element = fragment.getVariableElement().getEnclosingElement();
    if (element instanceof TypeElement) {
        TypeElement type = (TypeElement) element;
        if (ElementUtil.isPublic(fragment.getVariableElement())) {
            ClassReferenceNode node = (ClassReferenceNode) elementReferenceMap.get(stitchClassIdentifier(type));
            if (node == null) {
                node = new ClassReferenceNode(type);
            }
            node.containsPublicField = true;
            elementReferenceMap.putIfAbsent(stitchClassIdentifier(type), node);
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement)

Example 77 with TypeElement

use of javax.lang.model.element.TypeElement in project realm-java by realm.

the class RealmProcessor method processAnnotations.

// Create all proxy classes
private boolean processAnnotations(RoundEnvironment roundEnv) {
    for (Element classElement : roundEnv.getElementsAnnotatedWith(RealmClass.class)) {
        // The class must either extend RealmObject or implement RealmModel
        if (!Utils.isImplementingMarkerInterface(classElement)) {
            Utils.error("A RealmClass annotated object must implement RealmModel or derive from RealmObject.", classElement);
            return false;
        }
        // Check the annotation was applied to a Class
        if (!classElement.getKind().equals(ElementKind.CLASS)) {
            Utils.error("The RealmClass annotation can only be applied to classes.", classElement);
            return false;
        }
        ClassMetaData metadata = new ClassMetaData(processingEnv, (TypeElement) classElement);
        if (!metadata.isModelClass()) {
            continue;
        }
        Utils.note("Processing class " + metadata.getSimpleClassName());
        if (!metadata.generate()) {
            return false;
        }
        classesToValidate.add(metadata);
        backlinksToValidate.addAll(metadata.getBacklinkFields());
        RealmProxyInterfaceGenerator interfaceGenerator = new RealmProxyInterfaceGenerator(processingEnv, metadata);
        try {
            interfaceGenerator.generate();
        } catch (IOException e) {
            Utils.error(e.getMessage(), classElement);
        }
        RealmProxyClassGenerator sourceCodeGenerator = new RealmProxyClassGenerator(processingEnv, metadata);
        try {
            sourceCodeGenerator.generate();
        } catch (IOException e) {
            Utils.error(e.getMessage(), classElement);
        } catch (UnsupportedOperationException e) {
            Utils.error(e.getMessage(), classElement);
        }
    }
    return true;
}
Also used : Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) IOException(java.io.IOException)

Example 78 with TypeElement

use of javax.lang.model.element.TypeElement in project realm-java by realm.

the class ClassMetaData method checkListTypes.

private boolean checkListTypes() {
    for (VariableElement field : fields) {
        if (Utils.isRealmList(field) || Utils.isRealmResults(field)) {
            // Check for missing generic (default back to Object)
            if (Utils.getGenericTypeQualifiedName(field) == null) {
                Utils.error("No generic type supplied for field", field);
                return false;
            }
            // Check that the referenced type is a concrete class and not an interface
            TypeMirror fieldType = field.asType();
            List<? extends TypeMirror> typeArguments = ((DeclaredType) fieldType).getTypeArguments();
            String genericCanonicalType = typeArguments.get(0).toString();
            TypeElement typeElement = elements.getTypeElement(genericCanonicalType);
            if (typeElement.getSuperclass().getKind() == TypeKind.NONE) {
                Utils.error("Only concrete Realm classes are allowed in RealmLists. " + "Neither interfaces nor abstract classes are allowed.", field);
                return false;
            }
        }
    }
    return true;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 79 with TypeElement

use of javax.lang.model.element.TypeElement in project camel by apache.

the class AnnotationProcessorHelper method findTypeElement.

public static TypeElement findTypeElement(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, String className) {
    if (isNullOrEmpty(className) || "java.lang.Object".equals(className)) {
        return null;
    }
    Set<? extends Element> rootElements = roundEnv.getRootElements();
    for (Element rootElement : rootElements) {
        if (rootElement instanceof TypeElement) {
            TypeElement typeElement = (TypeElement) rootElement;
            String aRootName = canonicalClassName(typeElement.getQualifiedName().toString());
            if (className.equals(aRootName)) {
                return typeElement;
            }
        }
    }
    // fallback using package name
    Elements elementUtils = processingEnv.getElementUtils();
    int idx = className.lastIndexOf('.');
    if (idx > 0) {
        String packageName = className.substring(0, idx);
        PackageElement pe = elementUtils.getPackageElement(packageName);
        if (pe != null) {
            List<? extends Element> enclosedElements = getEnclosedElements(pe);
            for (Element rootElement : enclosedElements) {
                if (rootElement instanceof TypeElement) {
                    TypeElement typeElement = (TypeElement) rootElement;
                    String aRootName = canonicalClassName(typeElement.getQualifiedName().toString());
                    if (className.equals(aRootName)) {
                        return typeElement;
                    }
                }
            }
        }
    }
    return null;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) Elements(javax.lang.model.util.Elements)

Example 80 with TypeElement

use of javax.lang.model.element.TypeElement in project camel by apache.

the class AnnotationProcessorHelper method hasSuperClass.

public static boolean hasSuperClass(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement classElement, String superClassName) {
    String aRootName = canonicalClassName(classElement.getQualifiedName().toString());
    // do not check the classes from JDK itself
    if (isNullOrEmpty(aRootName) || aRootName.startsWith("java.") || aRootName.startsWith("javax.")) {
        return false;
    }
    String aSuperClassName = canonicalClassName(classElement.getSuperclass().toString());
    if (superClassName.equals(aSuperClassName)) {
        return true;
    }
    TypeElement aSuperClass = findTypeElement(processingEnv, roundEnv, aSuperClassName);
    if (aSuperClass != null) {
        return hasSuperClass(processingEnv, roundEnv, aSuperClass, superClassName);
    } else {
        return false;
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement)

Aggregations

TypeElement (javax.lang.model.element.TypeElement)1562 ExecutableElement (javax.lang.model.element.ExecutableElement)517 Element (javax.lang.model.element.Element)502 TypeMirror (javax.lang.model.type.TypeMirror)420 VariableElement (javax.lang.model.element.VariableElement)333 DeclaredType (javax.lang.model.type.DeclaredType)201 ArrayList (java.util.ArrayList)177 PackageElement (javax.lang.model.element.PackageElement)159 AnnotationMirror (javax.lang.model.element.AnnotationMirror)151 Test (org.junit.Test)136 HashMap (java.util.HashMap)118 HashSet (java.util.HashSet)109 Elements (javax.lang.model.util.Elements)101 Map (java.util.Map)98 IOException (java.io.IOException)97 List (java.util.List)97 ClassName (com.squareup.javapoet.ClassName)88 Test (org.junit.jupiter.api.Test)80 MethodSpec (com.squareup.javapoet.MethodSpec)72 TypeSpec (com.squareup.javapoet.TypeSpec)63