Search in sources :

Example 16 with PackageElement

use of javax.lang.model.element.PackageElement in project checker-framework by typetools.

the class StubParser method getAllStubAnnotations.

/**
 * Returns all annotations found in the stub file, as a value for {@link #allStubAnnotations}.
 * Note that this also modifies {@link #importedConstants} and {@link #importedTypes}.
 *
 * @see #allStubAnnotations
 */
private Map<String, AnnotationMirror> getAllStubAnnotations() {
    Map<String, AnnotationMirror> result = new HashMap<>();
    assert !stubUnit.getCompilationUnits().isEmpty();
    CompilationUnit cu = stubUnit.getCompilationUnits().get(0);
    if (cu.getImports() == null) {
        return result;
    }
    for (ImportDeclaration importDecl : cu.getImports()) {
        String imported = importDecl.getNameAsString();
        try {
            if (importDecl.isAsterisk()) {
                if (importDecl.isStatic()) {
                    // Wildcard import of members of a type (class or interface)
                    TypeElement element = getTypeElement(imported, "Imported type not found");
                    if (element != null) {
                        // Find nested annotations
                        // Find compile time constant fields, or values of an enum
                        putAllNew(result, annosInType(element));
                        importedConstants.addAll(getImportableMembers(element));
                        addEnclosingTypesToImportedTypes(element);
                    }
                } else {
                    // Wildcard import of members of a package
                    PackageElement element = findPackage(imported);
                    if (element != null) {
                        putAllNew(result, annosInPackage(element));
                        addEnclosingTypesToImportedTypes(element);
                    }
                }
            } else {
                // A single (non-wildcard) import
                final TypeElement importType = elements.getTypeElement(imported);
                if (importType == null && !importDecl.isStatic()) {
                    // Class or nested class (according to JSL), but we can't resolve
                    stubWarnNotFound("Imported type not found: " + imported);
                } else if (importType == null) {
                    // Nested Field
                    Pair<String, String> typeParts = StubUtil.partitionQualifiedName(imported);
                    String type = typeParts.first;
                    String fieldName = typeParts.second;
                    TypeElement enclType = getTypeElement(type, String.format("Enclosing type of static field %s not found", fieldName));
                    if (enclType != null) {
                        if (findFieldElement(enclType, fieldName) != null) {
                            importedConstants.add(imported);
                        }
                    }
                } else if (importType.getKind() == ElementKind.ANNOTATION_TYPE) {
                    // Single annotation or nested annotation
                    AnnotationMirror anno = AnnotationBuilder.fromName(elements, imported);
                    if (anno != null) {
                        Element annoElt = anno.getAnnotationType().asElement();
                        putNoOverride(result, annoElt.getSimpleName().toString(), anno);
                        importedTypes.put(annoElt.getSimpleName().toString(), (TypeElement) annoElt);
                    } else {
                        stubWarnNotFound("Could not load import: " + imported);
                    }
                } else {
                    // Class or nested class
                    // TODO: Is this needed?
                    importedConstants.add(imported);
                    TypeElement element = getTypeElement(imported, "Imported type not found");
                    importedTypes.put(element.getSimpleName().toString(), element);
                }
            }
        } catch (AssertionError error) {
            stubWarnNotFound("" + error);
        }
    }
    return result;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) AnnotationMirror(javax.lang.model.element.AnnotationMirror) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) PackageElement(javax.lang.model.element.PackageElement) Pair(org.checkerframework.javacutil.Pair) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair)

Example 17 with PackageElement

use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.

the class FormDefinitionsProcessor method processFormDefinition.

protected void processFormDefinition(TypeElement formElement) throws Exception {
    final Messager messager = processingEnv.getMessager();
    messager.printMessage(Diagnostic.Kind.NOTE, "Discovered FormDefintion class [" + formElement.getSimpleName() + "]");
    boolean checkInheritance = false;
    FormDefinition defintion = formElement.getAnnotation(FormDefinition.class);
    checkInheritance = defintion.allowInheritance();
    List<Map<String, String>> formElements = new ArrayList<>();
    if (checkInheritance) {
        TypeElement parent = getParent(formElement);
        formElements.addAll(extractParentFormFields(parent, defintion.policy(), defintion.i18n()));
    }
    formElements.addAll(extracFormFields(formElement, defintion.policy(), defintion.i18n()));
    FormGenerationUtils.sort(defintion.startElement(), formElements);
    messager.printMessage(Diagnostic.Kind.NOTE, "Discovered " + formElements.size() + " elements for form [" + formElement.getQualifiedName().toString() + "]");
    String modelClassName = formElement.getQualifiedName().toString();
    String builderClassName = fixClassName(formElement.getQualifiedName().toString()) + "FormBuilder";
    Map<String, Object> templateContext = new HashMap<>();
    templateContext.put("modelClass", modelClassName);
    templateContext.put("builderClassName", builderClassName);
    templateContext.put("startElement", defintion.startElement());
    templateContext.put("i18n_bundle", StringUtils.isEmpty(defintion.i18n().bundle()) ? formElement.asType().toString() : defintion.i18n().bundle());
    Column[] columns = defintion.layout().value();
    List<String> layoutColumns = new ArrayList<>();
    if (columns.length == 0) {
        layoutColumns.add(ColSpan.SPAN_12.getName());
    } else {
        for (Column column : columns) {
            layoutColumns.add(column.value().getName());
        }
    }
    templateContext.put("layout_columns", layoutColumns);
    templateContext.put("elements", formElements);
    StringBuffer builder = writeTemplate("templates/FormDefinitionSettingsBuilder.ftl", templateContext);
    Map<String, String> form = new HashMap<>();
    form.put("package", ((PackageElement) formElement.getEnclosingElement()).getQualifiedName().toString());
    form.put("modelClass", modelClassName);
    form.put("builderClass", builderClassName);
    form.put("builderCode", builder.toString());
    context.getForms().add(form);
}
Also used : Messager(javax.annotation.processing.Messager) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) Column(org.kie.workbench.common.forms.adf.definitions.annotations.layout.Column) PackageElement(javax.lang.model.element.PackageElement) FormDefinition(org.kie.workbench.common.forms.adf.definitions.annotations.FormDefinition) Map(java.util.Map) HashMap(java.util.HashMap)

Example 18 with PackageElement

use of javax.lang.model.element.PackageElement in project Rocket by mozilla-tw.

the class AppModuleProcessor method getIndexedClassNames.

@SuppressWarnings("unchecked")
private FoundIndexedClassNames getIndexedClassNames(PackageElement glideGenPackage) {
    Set<String> glideModules = new HashSet<>();
    Set<String> extensions = new HashSet<>();
    List<? extends Element> glideGeneratedElements = glideGenPackage.getEnclosedElements();
    for (Element indexer : glideGeneratedElements) {
        Index annotation = indexer.getAnnotation(Index.class);
        // that we can safely ignore.
        if (annotation != null) {
            Collections.addAll(glideModules, annotation.modules());
            Collections.addAll(extensions, annotation.extensions());
        }
    }
    processorUtil.debugLog("Found GlideModules: " + glideModules);
    return new FoundIndexedClassNames(glideModules, extensions);
}
Also used : PackageElement(javax.lang.model.element.PackageElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) HashSet(java.util.HashSet)

Example 19 with PackageElement

use of javax.lang.model.element.PackageElement in project st-js by st-js.

the class TreeWrapper method getAnnotation.

// @SuppressWarnings("unchecked")
// private <A extends Annotation> Class<A> getAnnotationFromClassLoader(Class<A> annotationType) {
// try {
// return (Class<A>) context.getBuiltProjectClassLoader().loadClass(annotationType.getName());
// } catch (ClassNotFoundException e) {
// throw new STJSRuntimeException("Cannot load the annotation type:" + annotationType
// + ". This is maybe a ST-JS bug. Please report it to our website");
// }
// }
private <A extends Annotation> A getAnnotation(Class<A> annotationType) {
    A a = element.getAnnotation(annotationType);
    if (a != null) {
        return a;
    }
    PackageElement pack = ElementUtils.enclosingPackage(element);
    return pack == null ? null : pack.getAnnotation(annotationType);
}
Also used : PackageElement(javax.lang.model.element.PackageElement)

Example 20 with PackageElement

use of javax.lang.model.element.PackageElement in project st-js by st-js.

the class ElementUtils method enclosingPackage.

/**
 * Returns the innermost package element enclosing the given element. The same effect as
 * {@link javax.lang.model.util.Elements#getPackageOf(Element)}. Returns the element itself if it is a package.
 *
 * @param elem the enclosed element of a package
 * @return the innermost package element
 */
public static PackageElement enclosingPackage(final Element elem) {
    Element result = elem;
    while (result != null && result.getKind() != ElementKind.PACKAGE) {
        /* @Nullable */
        Element encl = result.getEnclosingElement();
        result = encl;
    }
    return (PackageElement) result;
}
Also used : PackageElement(javax.lang.model.element.PackageElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement)

Aggregations

PackageElement (javax.lang.model.element.PackageElement)133 TypeElement (javax.lang.model.element.TypeElement)92 Element (javax.lang.model.element.Element)51 ExecutableElement (javax.lang.model.element.ExecutableElement)34 VariableElement (javax.lang.model.element.VariableElement)32 TypeSpec (com.squareup.javapoet.TypeSpec)20 IOException (java.io.IOException)17 HashSet (java.util.HashSet)13 TypeMirror (javax.lang.model.type.TypeMirror)13 AnnotationMirror (javax.lang.model.element.AnnotationMirror)12 JavaFileObject (javax.tools.JavaFileObject)12 ClassName (com.squareup.javapoet.ClassName)11 ArrayList (java.util.ArrayList)11 Set (java.util.Set)10 Elements (javax.lang.model.util.Elements)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)9 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)8 Map (java.util.Map)8 Messager (javax.annotation.processing.Messager)8