use of javax.lang.model.element.PackageElement in project neo4j by neo4j.
the class UserFunctionVisitor method validateName.
private Stream<CompilationMessage> validateName(ExecutableElement method) {
UserFunction function = method.getAnnotation(UserFunction.class);
String name = function.name();
if (!name.isEmpty() && isInRootNamespace(name)) {
return Stream.of(rootNamespaceError(method, name));
}
String value = function.value();
if (!value.isEmpty() && isInRootNamespace(value)) {
return Stream.of(rootNamespaceError(method, value));
}
PackageElement namespace = elements.getPackageOf(method);
if (namespace == null) {
return Stream.of(rootNamespaceError(method));
}
return Stream.empty();
}
use of javax.lang.model.element.PackageElement in project bazel by bazelbuild.
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;
}
use of javax.lang.model.element.PackageElement in project tiger by google.
the class Utils method getPackage.
public static PackageElement getPackage(TypeElement typeElement) {
Element result = typeElement.getEnclosingElement();
ElementKind elementKind = result.getKind();
while (!elementKind.equals(ElementKind.PACKAGE)) {
Preconditions.checkState(elementKind.isClass() || elementKind.isInterface(), String.format("Utils.getPackage: unexpected kind: %s for type: %s", elementKind, typeElement));
result = result.getEnclosingElement();
elementKind = result.getKind();
}
return (PackageElement) result;
}
use of javax.lang.model.element.PackageElement 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;
}
use of javax.lang.model.element.PackageElement in project auto by google.
the class BasicAnnotationProcessor method validElements.
/**
* Returns the valid annotated elements contained in all of the deferred elements. If none are
* found for a deferred element, defers it again.
*/
private ImmutableSetMultimap<Class<? extends Annotation>, Element> validElements(ImmutableMap<String, Optional<? extends Element>> deferredElements, RoundEnvironment roundEnv) {
ImmutableSetMultimap.Builder<Class<? extends Annotation>, Element> deferredElementsByAnnotationBuilder = ImmutableSetMultimap.builder();
for (Entry<String, Optional<? extends Element>> deferredTypeElementEntry : deferredElements.entrySet()) {
Optional<? extends Element> deferredElement = deferredTypeElementEntry.getValue();
if (deferredElement.isPresent()) {
findAnnotatedElements(deferredElement.get(), getSupportedAnnotationClasses(), deferredElementsByAnnotationBuilder);
} else {
deferredElementNames.add(ElementName.forTypeName(deferredTypeElementEntry.getKey()));
}
}
ImmutableSetMultimap<Class<? extends Annotation>, Element> deferredElementsByAnnotation = deferredElementsByAnnotationBuilder.build();
ImmutableSetMultimap.Builder<Class<? extends Annotation>, Element> validElements = ImmutableSetMultimap.builder();
Set<ElementName> validElementNames = new LinkedHashSet<ElementName>();
// Look at the elements we've found and the new elements from this round and validate them.
for (Class<? extends Annotation> annotationClass : getSupportedAnnotationClasses()) {
// This should just call roundEnv.getElementsAnnotatedWith(Class) directly, but there is a bug
// in some versions of eclipse that cause that method to crash.
TypeElement annotationType = elements.getTypeElement(annotationClass.getCanonicalName());
Set<? extends Element> elementsAnnotatedWith = (annotationType == null) ? ImmutableSet.<Element>of() : roundEnv.getElementsAnnotatedWith(annotationType);
for (Element annotatedElement : Sets.union(elementsAnnotatedWith, deferredElementsByAnnotation.get(annotationClass))) {
if (annotatedElement.getKind().equals(PACKAGE)) {
PackageElement annotatedPackageElement = (PackageElement) annotatedElement;
ElementName annotatedPackageName = ElementName.forPackageName(annotatedPackageElement.getQualifiedName().toString());
boolean validPackage = validElementNames.contains(annotatedPackageName) || (!deferredElementNames.contains(annotatedPackageName) && validateElement(annotatedPackageElement));
if (validPackage) {
validElements.put(annotationClass, annotatedPackageElement);
validElementNames.add(annotatedPackageName);
} else {
deferredElementNames.add(annotatedPackageName);
}
} else {
TypeElement enclosingType = getEnclosingType(annotatedElement);
ElementName enclosingTypeName = ElementName.forTypeName(enclosingType.getQualifiedName().toString());
boolean validEnclosingType = validElementNames.contains(enclosingTypeName) || (!deferredElementNames.contains(enclosingTypeName) && validateElement(enclosingType));
if (validEnclosingType) {
validElements.put(annotationClass, annotatedElement);
validElementNames.add(enclosingTypeName);
} else {
deferredElementNames.add(enclosingTypeName);
}
}
}
}
return validElements.build();
}
Aggregations