Java Code Examples for javax.lang.model.element.ElementKind#ANNOTATION_TYPE

The following examples show how to use javax.lang.model.element.ElementKind#ANNOTATION_TYPE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RoundEnvImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * From the set of root elements and their enclosed elements, return the subset that are annotated
 * with {@code a}.  If {@code a} is annotated with the {@link java.lang.annotation.Inherited} 
 * annotation, include those elements that inherit the annotation from their superclasses.
 * Note that {@link java.lang.annotation.Inherited} only applies to classes (i.e. TypeElements).
 */
@Override
public Set<? extends Element> getElementsAnnotatedWith(TypeElement a)
{
	if (a.getKind() != ElementKind.ANNOTATION_TYPE) {
		throw new IllegalArgumentException("Argument must represent an annotation type"); //$NON-NLS-1$
	}
	Binding annoBinding = ((TypeElementImpl)a)._binding;
	if (0 != (annoBinding.getAnnotationTagBits() & TagBits.AnnotationInherited)) {
		Set<Element> annotatedElements = new HashSet<Element>(_annoToUnit.getValues(a));
		// For all other root elements that are TypeElements, and for their recursively enclosed
		// types, add each element if it has a superclass are annotated with 'a'
		ReferenceBinding annoTypeBinding = (ReferenceBinding) annoBinding;
		for (TypeElement element : ElementFilter.typesIn(getRootElements())) {
			ReferenceBinding typeBinding = (ReferenceBinding)((TypeElementImpl)element)._binding;
			addAnnotatedElements(annoTypeBinding, typeBinding, annotatedElements);
		}
		return Collections.unmodifiableSet(annotatedElements);
	}
	return Collections.unmodifiableSet(_annoToUnit.getValues(a));
}
 
Example 2
Source File: Mirrors.java    From immutables with Apache License 2.0 6 votes vote down vote up
static AttributeTypeKind from(TypeMirror type) {
  if (type.getKind() == TypeKind.DECLARED) {
    TypeElement typeElement = toElement(type);
    if (typeElement.getKind() == ElementKind.ENUM) {
      return ENUM;
    }
    if (typeElement.getKind() == ElementKind.ANNOTATION_TYPE) {
      return ANNOTATION;
    }
    Name qualifiedName = typeElement.getQualifiedName();
    if (qualifiedName.contentEquals(Class.class.getName())) {
      return TYPE;
    }
    if (qualifiedName.contentEquals(String.class.getName())) {
      return STRING;
    }
  } else if (type.getKind().isPrimitive()) {
    return PRIMITIVE;
  }
  throw new AssertionError();
}
 
Example 3
Source File: AnnotationTypeAttributeRemoved.java    From revapi with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Difference transform(@Nullable JavaModelElement oldElement, @Nullable JavaModelElement newElement,
    @Nonnull Difference difference) {

    if (oldElement == null) {
        throw new IllegalStateException("Annotation type attribute detection called with one of the elements null."
                + " That should never be the case.");
    }

    ExecutableElement method = (ExecutableElement) oldElement.getDeclaringElement();

    if (method.getEnclosingElement().getKind() == ElementKind.ANNOTATION_TYPE) {
        return Code.METHOD_ATTRIBUTE_REMOVED_FROM_ANNOTATION_TYPE.createDifference(locale,
                new LinkedHashMap<>(difference.attachments));
    }

    return difference;
}
 
Example 4
Source File: TypeDefElementVisitor.java    From sundrio with Apache License 2.0 6 votes vote down vote up
public TypeDefBuilder visit(Element e) {
    if (e instanceof TypeElement) {
        return new TypeDefBuilder(ElementTo.TYPEDEF.apply((TypeElement) e));
    }
    String name = e.getSimpleName().toString();
    builder.withName(name);
    if (e.getKind() == ElementKind.INTERFACE) {
        builder.withKind(Kind.INTERFACE);
    } else if (e.getKind() == ElementKind.ENUM) {
        builder.withKind(Kind.ENUM);
    }else if (e.getKind() == ElementKind.ANNOTATION_TYPE) {
        builder.withKind(Kind.ANNOTATION);
    } else  {
        builder.withKind(Kind.CLASS);
    }

    if (e.getEnclosingElement() instanceof PackageElement) {
        String packageName = e.getEnclosingElement().toString();
        builder.withPackageName(packageName);
    }
    return builder;
}
 
Example 5
Source File: DocumentUtil.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@NonNull
static ElementKind decodeKind (char kind) {
    switch (kind) {
        case EK_CLASS:
        case EK_LOCAL_CLASS:
            return ElementKind.CLASS;
        case EK_INTERFACE:
        case EK_LOCAL_INTERFACE:
            return ElementKind.INTERFACE;
        case EK_ENUM:
        case EK_LOCAL_ENUM:
            return ElementKind.ENUM;
        case EK_ANNOTATION:
        case EK_LOCAL_ANNOTATION:
            return ElementKind.ANNOTATION_TYPE;
        case EK_MODULE:
            return ElementKind.MODULE;
        case EK_RECORD:
        case EK_LOCAL_RECORD:
            return ElementKind.valueOf("RECORD");
        default:
            throw new IllegalArgumentException ();
    }
}
 
Example 6
Source File: AnnotationAsSuperInterface.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@TriggerTreeKind({Tree.Kind.ANNOTATION_TYPE, Tree.Kind.CLASS, Tree.Kind.ENUM, Tree.Kind.INTERFACE})
public static Iterable<ErrorDescription> run(HintContext ctx) {
    Element e = ctx.getInfo().getTrees().getElement(ctx.getPath());

    if ( e == null || !(e instanceof TypeElement) ) {
        return null;
    }
    
    List<ErrorDescription> eds = new ArrayList<ErrorDescription>();
    
    for (Tree i : ((ClassTree) ctx.getPath().getLeaf()).getImplementsClause()) {
        Element ie = ctx.getInfo().getTrees().getElement(new TreePath(ctx.getPath(), i));

        if (ie != null && ie.getKind() == ElementKind.ANNOTATION_TYPE) {
            eds.add(ErrorDescriptionFactory.forTree(ctx, i, NbBundle.getMessage(AnnotationAsSuperInterface.class,
                                "HNT_AnnotationAsSuperInterface",  // NOI18N
                                ie.getSimpleName().toString())));
        }
    }

    return eds;
}
 
Example 7
Source File: ValueTypeComposer.java    From immutables with Apache License 2.0 5 votes vote down vote up
static boolean checkAbstractValueType(Element element, Collection<String> violations) {
  boolean ofSupportedKind = false
      || element.getKind() == ElementKind.INTERFACE
      || element.getKind() == ElementKind.ANNOTATION_TYPE
      || element.getKind() == ElementKind.CLASS;

  boolean staticOrTopLevel = false
      || element.getEnclosingElement().getKind() == ElementKind.PACKAGE
      || element.getModifiers().contains(Modifier.STATIC);

  boolean nonFinal = !element.getModifiers().contains(Modifier.FINAL);

  boolean publicOrPackageVisible =
      !element.getModifiers().contains(Modifier.PRIVATE)
          && !element.getModifiers().contains(Modifier.PROTECTED);

  if (!ofSupportedKind) {
    violations.add("must be class or interface or annotation type");
  }

  if (!nonFinal) {
    violations.add("must be non-final");
  }

  if (!publicOrPackageVisible) {
    violations.add("should be public or package-visible");
  }

  if (!staticOrTopLevel) {
    violations.add("should be top-level or static inner class");
  }

  return violations.isEmpty();
}
 
Example 8
Source File: CompilationUnit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private ClassNode createClassNode(String name, TypeElement typeElement) {
    ElementKind kind = typeElement.getKind();
    if (kind == ElementKind.ANNOTATION_TYPE) {
        return createAnnotationType(name, typeElement);
    } else if (kind == ElementKind.INTERFACE) {
        return createInterfaceKind(name, typeElement);
    } else {
        return createClassType(name, typeElement);
    }
}
 
Example 9
Source File: InterceptorBindingMembersAnalyzer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void checkMembers( TypeElement element, CdiAnalysisResult result , 
        String localizedWarning ) 
{
    List<ExecutableElement> methods = ElementFilter.methodsIn(
            element.getEnclosedElements());
    for (ExecutableElement executableElement : methods) {
        TypeMirror returnType = executableElement.getReturnType();
        boolean warning = false;
        if ( returnType.getKind() == TypeKind.ARRAY ){
            warning = true;
        }
        else if ( returnType.getKind() == TypeKind.DECLARED){
            Element returnElement = result.getInfo().getTypes().asElement( 
                    returnType );
            warning = returnElement.getKind() == ElementKind.ANNOTATION_TYPE;
        }
        if ( !warning ){
            continue;
        }
        if (AnnotationUtil.hasAnnotation(executableElement, 
                AnnotationUtil.NON_BINDING,  result.getInfo()) )
        {
            continue;
        }
        result.addNotification(Severity.WARNING, element, localizedWarning); 
    }
}
 
Example 10
Source File: CreateElement.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static ElementKind getClassType(Set<ElementKind> types) {
    if (types.contains(ElementKind.CLASS))
        return ElementKind.CLASS;
    if (types.contains(ElementKind.ANNOTATION_TYPE))
        return ElementKind.ANNOTATION_TYPE;
    if (types.contains(ElementKind.INTERFACE))
        return ElementKind.INTERFACE;
    if (types.contains(ElementKind.ENUM))
        return ElementKind.ENUM;

    return null;
}
 
Example 11
Source File: TreeUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static boolean isInAnnotationType(CompilationInfo info, TreePath path) {
    Element e = info.getTrees().getElement(path);
    if (e != null) {
        e = e.getEnclosingElement();
        return e != null && e.getKind() == ElementKind.ANNOTATION_TYPE;
    }
    return false;
}
 
Example 12
Source File: NamedStereotypeObjectProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public List<NamedStereotype> createObjects( TypeElement type ) {
    if (type.getKind() == ElementKind.ANNOTATION_TYPE &&
            getHelper().hasAnnotation(type.getAnnotationMirrors(), 
            getAnnotation())) 
    {
        if ( hasNamed(type, getHelper())){
            return Collections.singletonList(createTypeElement(type));
        }
    }
    return Collections.emptyList();
}
 
Example 13
Source File: ImportHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Set<ImportCandidate> findJavaImportCandidates(FileObject fo, String packageName, String missingClass) {
    final Set<ImportCandidate> candidates = new HashSet<>();
    final ClasspathInfo pathInfo = createClasspathInfo(fo);

    Set<ElementHandle<TypeElement>> typeNames = pathInfo.getClassIndex().getDeclaredTypes(
            missingClass, NameKind.SIMPLE_NAME, EnumSet.allOf(ClassIndex.SearchScope.class));

    for (ElementHandle<TypeElement> typeName : typeNames) {
        ElementKind kind = typeName.getKind();

        // Skip classes within the same package
        String pkgName = GroovyUtils.stripClassName(typeName.getQualifiedName());
        if (packageName == null && pkgName == null) {
            // Probably both in default package
            continue;
        }

        if (packageName != null && packageName.equals(pkgName)) {
            continue;
        }

        if (kind == ElementKind.CLASS || kind == ElementKind.INTERFACE || kind == ElementKind.ANNOTATION_TYPE) {
            candidates.add(createImportCandidate(missingClass, typeName.getQualifiedName(), kind));
        }
    }
    return candidates;
}
 
Example 14
Source File: DocumentUtil.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@NonNull
public static Convertor<Document,ElementHandle<TypeElement>> typeElementConvertor() {
    return new ElementHandleConvertor<> (
            ElementKind.CLASS,
            ElementKind.ENUM,
            ElementKind.INTERFACE,
            ElementKind.ANNOTATION_TYPE);
}
 
Example 15
Source File: Symbol.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
    long flags = flags();
    if ((flags & ANNOTATION) != 0)
        return ElementKind.ANNOTATION_TYPE;
    else if ((flags & INTERFACE) != 0)
        return ElementKind.INTERFACE;
    else if ((flags & ENUM) != 0)
        return ElementKind.ENUM;
    else
        return ElementKind.CLASS;
}
 
Example 16
Source File: TopClassFinder.java    From netbeans with Apache License 2.0 4 votes vote down vote up
static boolean isTestable(TypeElement typeDeclElement) {
    ElementKind elemKind = typeDeclElement.getKind();
    return (elemKind != ElementKind.ANNOTATION_TYPE)
           && (elemKind.isClass()|| elemKind.isInterface());
}
 
Example 17
Source File: TopClassFinder.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public boolean passes(TypeElement topClass,
                      CompilationInfo compInfo) {
    ElementKind elemKind = topClass.getKind();
    return (elemKind != ElementKind.ANNOTATION_TYPE)
           && (elemKind.isClass()|| elemKind.isInterface());
}
 
Example 18
Source File: JavaEnvironment.java    From j2cl with Apache License 2.0 4 votes vote down vote up
private static boolean isInterface(TypeElement typeElement) {
  return typeElement.getKind() == ElementKind.INTERFACE
      || typeElement.getKind() == ElementKind.ANNOTATION_TYPE;
}
 
Example 19
Source File: ValueType.java    From immutables with Apache License 2.0 4 votes vote down vote up
public boolean isAnnotationType() {
  return element.getKind() == ElementKind.ANNOTATION_TYPE;
}
 
Example 20
Source File: ValueType.java    From immutables with Apache License 2.0 4 votes vote down vote up
public boolean isImplementing() {
  return element.getKind() == ElementKind.INTERFACE
      || element.getKind() == ElementKind.ANNOTATION_TYPE;
}