use of javax.lang.model.element.PackageElement in project checker-framework by typetools.
the class ElementUtils method getBinaryName.
/**
* Returns the binary name of the given type.
*
* @param te a type
* @return the binary name of the type
*/
// string manipulation
@SuppressWarnings("signature:return")
@BinaryName
public static String getBinaryName(TypeElement te) {
Element enclosing = te.getEnclosingElement();
String simpleName = te.getSimpleName().toString();
if (enclosing == null) {
// is this possible?
return simpleName;
}
if (ElementUtils.isTypeElement(enclosing)) {
return getBinaryName((TypeElement) enclosing) + "$" + simpleName;
} else if (enclosing.getKind() == ElementKind.PACKAGE) {
PackageElement pe = (PackageElement) enclosing;
if (pe.isUnnamed()) {
return simpleName;
} else {
return pe.getQualifiedName() + "." + simpleName;
}
} else {
// This case occurs for anonymous inner classes. Fall back to the flatname method.
return ((ClassSymbol) te).flatName().toString();
}
}
use of javax.lang.model.element.PackageElement in project checker-framework by typetools.
the class ReportVisitor method checkReportUse.
/**
* Check for uses of the {@link ReportUse} annotation. This method has to be called for every
* explicit or implicit use of a type, most cases are simply covered by the type validator.
*
* @param node the tree for error reporting only
* @param member the element from which to start looking
*/
private void checkReportUse(Tree node, Element member) {
Element loop = member;
while (loop != null) {
boolean report = this.atypeFactory.getDeclAnnotation(loop, ReportUse.class) != null;
if (report) {
checker.reportError(node, "usage", node, ElementUtils.getQualifiedName(loop), loop.getKind(), ElementUtils.getQualifiedName(member), member.getKind());
break;
} else {
if (loop.getKind() == ElementKind.PACKAGE) {
loop = ElementUtils.parentPackage((PackageElement) loop, elements);
continue;
}
}
// Package will always be the last iteration.
loop = loop.getEnclosingElement();
}
}
use of javax.lang.model.element.PackageElement in project checker-framework by typetools.
the class AnnotationClassLoader method loadBundledAnnotationClasses.
/**
* Loads the set of annotation classes in the qual directory of a checker shipped with the Checker
* Framework.
*/
private void loadBundledAnnotationClasses() {
// retrieve the fully qualified class names of the annotations
Set<@BinaryName String> annotationNames;
// see whether the resource URL has a protocol of jar or file
if (resourceURL != null && resourceURL.getProtocol().contentEquals("jar")) {
// if the checker class file is contained within a jar, then the resource URL for the qual
// directory will have the protocol "jar". This means the whole checker is loaded as a jar
// file.
JarURLConnection connection;
// create a connection to the jar file
try {
connection = (JarURLConnection) resourceURL.openConnection();
// disable caching / connection sharing of the low level URLConnection to the Jar file
connection.setDefaultUseCaches(false);
connection.setUseCaches(false);
// connect to the Jar file
connection.connect();
} catch (IOException e) {
throw new BugInCF("AnnotationClassLoader: cannot open a connection to the Jar file " + resourceURL.getFile());
}
// open up that jar file and extract annotation class names
try (JarFile jarFile = connection.getJarFile()) {
// get class names inside the jar file within the particular package
annotationNames = getBundledAnnotationNamesFromJar(jarFile);
} catch (IOException e) {
throw new BugInCF("AnnotationClassLoader: cannot open the Jar file " + resourceURL.getFile());
}
} else if (resourceURL != null && resourceURL.getProtocol().contentEquals("file")) {
// If the checker class file is found within the file system itself within some directory
// (usually development build directories), then process the package as a file directory in
// the file system and load the annotations contained in the qual directory.
// open up the directory
File packageDir = new File(resourceURL.getFile());
annotationNames = getAnnotationNamesFromDirectory(packageName, packageDir, packageDir);
} else {
// We do not support a resource URL with any other protocols, so create an empty set.
annotationNames = Collections.emptySet();
}
if (annotationNames.isEmpty()) {
PackageElement pkgEle = checker.getElementUtils().getPackageElement(packageName);
if (pkgEle != null) {
for (Element e : pkgEle.getEnclosedElements()) {
if (e.getKind() == ElementKind.ANNOTATION_TYPE) {
// Elements needs to be annotated.
@SuppressWarnings("signature:assignment") @BinaryName String annoBinName = checker.getElementUtils().getBinaryName((TypeElement) e).toString();
annotationNames.add(annoBinName);
}
}
}
}
supportedBundledAnnotationClasses.addAll(loadAnnotationClasses(annotationNames));
}
use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.
the class SVGShapeProcessorTest method makeTypeElement.
private TypeElement makeTypeElement(final String name) {
final Name typeElementName = mock(Name.class);
final Name packageElementName = mock(Name.class);
final TypeElement typeElement = mock(TypeElement.class);
final PackageElement packageElement = mock(PackageElement.class);
when(typeElementName.toString()).thenReturn(name);
when(packageElementName.toString()).thenReturn(this.getClass().getPackage().getName());
when(typeElement.getKind()).thenReturn(ElementKind.INTERFACE);
when(typeElement.getEnclosingElement()).thenReturn(packageElement);
when(typeElement.getSimpleName()).thenReturn(typeElementName);
when(typeElement.getAnnotation(SVGViewFactory.class)).thenReturn(svgViewFactory);
when(packageElement.getQualifiedName()).thenReturn(packageElementName);
return typeElement;
}
use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.
the class MainProcessor method processDefinitionSets.
private boolean processDefinitionSets(final Element e) {
final Messager messager = processingEnv.getMessager();
final boolean isClass = e.getKind() == ElementKind.CLASS;
if (isClass) {
TypeElement classElement = (TypeElement) e;
PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
messager.printMessage(Diagnostic.Kind.NOTE, "Discovered definition set class [" + classElement.getSimpleName() + "]");
final String packageName = packageElement.getQualifiedName().toString();
final String className = classElement.getSimpleName().toString();
processingContext.setDefinitionSet(packageName, className);
String defSetClassName = packageName + "." + className;
// Description fields.
processFieldName(classElement, defSetClassName, ANNOTATION_DESCRIPTION, processingContext.getDefSetAnnotations().getDescriptionFieldNames(), false);
// Definitions identifiers.
DefinitionSet definitionSetAnn = e.getAnnotation(DefinitionSet.class);
List<? extends TypeMirror> mirrors = null;
try {
definitionSetAnn.definitions();
} catch (MirroredTypesException mte) {
mirrors = mte.getTypeMirrors();
}
if (null == mirrors) {
throw new RuntimeException("No graph class class declared for the @DefinitionSet.");
}
Set<String> defIds = new LinkedHashSet<>();
for (TypeMirror mirror : mirrors) {
if (mirror.getKind().equals(TypeKind.DECLARED)) {
final TypeElement t = (TypeElement) ((DeclaredType) mirror).asElement();
processingContext.getDefinitionElements().add(t);
}
String fqcn = mirror.toString();
defIds.add(fqcn);
}
processingContext.getDefSetAnnotations().getDefinitionIds().put(defSetClassName, defIds);
// Builder class.
processDefinitionSetModelBuilder(e, defSetClassName, processingContext.getDefSetAnnotations().getBuilderFieldNames());
// Graph factory type.
TypeMirror mirror = null;
try {
definitionSetAnn.graphFactory();
} catch (MirroredTypeException mte) {
mirror = mte.getTypeMirror();
}
if (null == mirror) {
throw new RuntimeException("No graph factory class declared for the @DefinitionSet.");
}
String fqcn = mirror.toString();
processingContext.getDefSetAnnotations().getGraphFactoryTypes().put(defSetClassName, fqcn);
// Definition Set's qualifier.
try {
definitionSetAnn.qualifier();
} catch (MirroredTypeException mte) {
mirror = mte.getTypeMirror();
}
if (null == mirror) {
throw new RuntimeException("No qualifier class declared for the @DefinitionSet.");
}
processingContext.getDefSetAnnotations().getQualifiers().put(defSetClassName, mirror.toString());
}
return true;
}
Aggregations