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;
}
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);
}
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);
}
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);
}
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;
}
Aggregations