javax.lang.model.element.AnnotationMirror Java Examples
The following examples show how to use
javax.lang.model.element.AnnotationMirror.
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: AnnotationUtil.java From netbeans with Apache License 2.0 | 6 votes |
/** * return AnnotationMirror for first found annotation from annotationFqns * @param element * @param info * @param annotationFqns * @return */ public static AnnotationMirror getAnnotationMirror(Element element, CompilationInfo info , String... annotationFqns) { Set<TypeElement> set = new HashSet<TypeElement>(); Elements els = info.getElements(); for( String annotation : annotationFqns){ TypeElement annotationElement = els.getTypeElement( annotation); if ( annotationElement != null ){ set.add( annotationElement ); } } List<? extends AnnotationMirror> annotations = els.getAllAnnotationMirrors( element ); for (AnnotationMirror annotationMirror : annotations) { Element declaredAnnotation = info.getTypes().asElement( annotationMirror.getAnnotationType()); if ( set.contains( declaredAnnotation ) ){ return annotationMirror; } } return null; }
Example #2
Source File: ElementJavadoc.java From netbeans with Apache License 2.0 | 6 votes |
private void appendAnnotation(StringBuilder sb, AnnotationMirror annotationDesc, boolean topLevel) { DeclaredType annotationType = annotationDesc.getAnnotationType(); if (annotationType != null && (!topLevel || isDocumented(annotationType))) { appendType(sb, annotationType, false, false, true); Map<? extends ExecutableElement, ? extends AnnotationValue> values = annotationDesc.getElementValues(); if (!values.isEmpty()) { sb.append('('); //NOI18N for (Iterator<? extends Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> it = values.entrySet().iterator(); it.hasNext();) { Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> value = it.next(); createLink(sb, value.getKey(), value.getKey().getSimpleName()); sb.append('='); //NOI18N appendAnnotationValue(sb, value.getValue()); if (it.hasNext()) sb.append(","); //NOI18N } sb.append(')'); //NOI18N } if (topLevel) sb.append("<br>"); //NOI18N } }
Example #3
Source File: NamedStereotype.java From netbeans with Apache License 2.0 | 6 votes |
@Override public boolean refresh( TypeElement type ) { List<? extends AnnotationMirror> allAnnotationMirrors = getHelper().getCompilationController().getElements(). getAllAnnotationMirrors(type); Map<String, ? extends AnnotationMirror> annotationsByType = getHelper().getAnnotationsByType( allAnnotationMirrors ); boolean isStereotype = annotationsByType.get( StereotypeChecker.STEREOTYPE) != null ; if ( !isStereotype ){ return false; } boolean hasNamed = NamedStereotypeObjectProvider.hasNamed(type, getHelper()); return hasNamed; }
Example #4
Source File: DecoratorInterceptorLogic.java From netbeans with Apache License 2.0 | 6 votes |
@Override public InterceptorsResultImpl getInterceptors( Element element ) { Collection<InterceptorObject> interceptors = getModel(). getInterceptorsManager().getObjects(); Set<TypeElement> result = new HashSet<TypeElement>(); Collection<AnnotationMirror> elementBindings = getInterceptorBindings(element); Set<String> elementBindingsFqns = getAnnotationFqns(elementBindings); for (InterceptorObject interceptor : interceptors) { TypeElement typeElement = interceptor.getTypeElement(); if ( hasInterceptorBindings( typeElement , elementBindingsFqns )){ result.add(typeElement); } } filterBindingsByMembers(elementBindings, result, TypeElement.class); return getInterceptorsResult( element , result ); }
Example #5
Source File: BindingsPanel.java From netbeans with Apache License 2.0 | 6 votes |
protected void setStereotypes( WebBeansModel model, Element element ) throws CdiException { if (getResult() != null) { List<AnnotationMirror> stereotypes = getResult().getAllStereotypes( element); if (stereotypes.isEmpty()) { getStereotypesComponent().setText(""); return; } StringBuilder text = new StringBuilder(); boolean isFqn = showFqns(); for (AnnotationMirror stereotype : stereotypes) { appendAnnotationMirror(stereotype, text, isFqn); } getStereotypesComponent().setText(text.substring(0, text.length() - 2)); } }
Example #6
Source File: Utils.java From netbeans with Apache License 2.0 | 6 votes |
private static void populateParam(CompilationController controller, VariableElement paramEl, ParamModel paramModel) { paramModel.setParamType(paramEl.asType().toString()); TypeElement paramAnotationEl = controller.getElements().getTypeElement("javax.jws.WebParam"); //NOI18N List<? extends AnnotationMirror> paramAnnotations = paramEl.getAnnotationMirrors(); for (AnnotationMirror anMirror : paramAnnotations) { if (controller.getTypes().isSameType(paramAnotationEl.asType(), anMirror.getAnnotationType())) { Map<? extends ExecutableElement, ? extends AnnotationValue> expressions = anMirror.getElementValues(); for(Entry<? extends ExecutableElement, ? extends AnnotationValue> entry: expressions.entrySet()) { ExecutableElement ex = entry.getKey(); AnnotationValue value = entry.getValue(); if (ex.getSimpleName().contentEquals("name")) { //NOI18N paramModel.name = (String)value.getValue(); } else if (ex.getSimpleName().contentEquals("partName")) { //NOI18N paramModel.setPartName((String)value.getValue()); } else if (ex.getSimpleName().contentEquals("targetNamespace")) { //NOI18N paramModel.setTargetNamespace((String)value.getValue()); } else if (ex.getSimpleName().contentEquals("mode")) { //NOI18N paramModel.setMode(Mode.valueOf(value.getValue().toString())); } } } } }
Example #7
Source File: ManagedAttributeValueTypeValidator.java From qpid-broker-j with Apache License 2.0 | 6 votes |
private boolean isAbstract(final TypeElement annotationElement, final Element typeElement) { for (AnnotationMirror annotation : typeElement.getAnnotationMirrors()) { if (annotation.getAnnotationType().asElement().equals(annotationElement)) { Map<? extends ExecutableElement, ? extends AnnotationValue> annotationValues = processingEnv.getElementUtils().getElementValuesWithDefaults(annotation); for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> element : annotationValues.entrySet()) { if ("isAbstract".contentEquals(element.getKey().getSimpleName())) { return element.getValue().getValue().equals(Boolean.TRUE); } } break; } } return false; }
Example #8
Source File: ProcessingUtils.java From annotation-processing-example with Apache License 2.0 | 6 votes |
public static Set<TypeElement> getTypeElementsToProcess(Set<? extends Element> elements, Set<? extends Element> supportedAnnotations) { Set<TypeElement> typeElements = new HashSet<>(); for (Element element : elements) { if (element instanceof TypeElement) { boolean found = false; for (Element subElement : element.getEnclosedElements()) { for (AnnotationMirror mirror : subElement.getAnnotationMirrors()) { for (Element annotation : supportedAnnotations) { if (mirror.getAnnotationType().asElement().equals(annotation)) { typeElements.add((TypeElement) element); found = true; break; } } if (found) break; } if (found) break; } } } return typeElements; }
Example #9
Source File: JSFConfigUtilities.java From netbeans with Apache License 2.0 | 6 votes |
private static boolean containsAnnotatedJsfResource(CompilationController parameter) { if (jsfResourcesElementHandles == null) { loadJsfResourcesElementsHandles(parameter); } ClassIndex classIndex = parameter.getClasspathInfo().getClassIndex(); for (ElementHandle jsfResourceElementHandle : jsfResourcesElementHandles) { Set<ElementHandle<TypeElement>> elements = classIndex.getElements( jsfResourceElementHandle, EnumSet.of(ClassIndex.SearchKind.TYPE_REFERENCES), EnumSet.of(ClassIndex.SearchScope.SOURCE)); for (ElementHandle<TypeElement> handle : elements) { TypeElement element = handle.resolve(parameter); if (element != null) { List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors(); for (AnnotationMirror annotationMirror : annotationMirrors) { if (ElementHandle.create(annotationMirror.getAnnotationType().asElement()) .equals(jsfResourceElementHandle)) { return true; } } } } } return false; }
Example #10
Source File: GeneratedFieldAndMethod.java From FastAdapter with MIT License | 6 votes |
public FieldSpec buildField() { FieldSpec.Builder fieldSpec = FieldSpec.builder(TypeName.get(mAnnotatedField.type), mAnnotatedField.name); if (mAnnotatedField.annotationMirrors != null) { for (AnnotationMirror annotationMirror : mAnnotatedField.annotationMirrors) { TypeElement annotationElement = (TypeElement) annotationMirror.getAnnotationType().asElement(); if (annotationElement.getQualifiedName().contentEquals(Override.class.getName())) { // Don't copy @Override if present, since we will be adding our own @Override in the // implementation. continue; } fieldSpec.addAnnotation(AnnotationSpec.get(annotationMirror)); } } return fieldSpec.build(); }
Example #11
Source File: AbstractTestGenerator.java From netbeans with Apache License 2.0 | 6 votes |
private static boolean isClassEjb31Bean(WorkingCopy wc, TypeElement srcClass) { ClassPath cp = wc.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE); if (cp == null || cp.findResource("javax/ejb/embeddable/EJBContainer.class") == null) { // if EJBContainer class is not available on classpath then it is not EJB 3.1 return false; } List<? extends AnnotationMirror> annotations = wc.getElements().getAllAnnotationMirrors(srcClass); for (AnnotationMirror am : annotations) { String annotation = ((TypeElement)am.getAnnotationType().asElement()).getQualifiedName().toString(); if (annotation.equals("javax.ejb.Singleton") || // NOI18N annotation.equals("javax.ejb.Stateless") || // NOI18N annotation.equals("javax.ejb.Stateful")) { // NOI18N // class is an EJB return true; } } return false; }
Example #12
Source File: UseDatabaseGeneratorTest.java From netbeans with Apache License 2.0 | 6 votes |
private static void checkDatasourceField(TypeElement typeElement, String name) { List<VariableElement> elements = ElementFilter.fieldsIn(typeElement.getEnclosedElements()); VariableElement variableElement = elements.get(0); assertTrue(variableElement.getSimpleName().contentEquals(name)); // field name DeclaredType declaredType = (DeclaredType) variableElement.asType(); TypeElement returnTypeElement = (TypeElement) declaredType.asElement(); assertTrue(returnTypeElement.getQualifiedName().contentEquals("javax.sql.DataSource")); // field type AnnotationMirror annotationMirror = variableElement.getAnnotationMirrors().get(0); DeclaredType annotationDeclaredType = annotationMirror.getAnnotationType(); TypeElement annotationTypeElement = (TypeElement) annotationDeclaredType.asElement(); assertTrue(annotationTypeElement.getQualifiedName().contentEquals("javax.annotation.Resource")); // annotation type Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry = annotationMirror.getElementValues().entrySet().iterator().next(); String attributeName = entry.getKey().getSimpleName().toString(); String attributeValue = (String) entry.getValue().getValue(); assertEquals("name", attributeName); // attributes assertEquals(name, attributeValue); }
Example #13
Source File: AnnotationScannerTest.java From netbeans with Apache License 2.0 | 6 votes |
public void testScanAnnotationsOnFields() throws Exception { final AnnotationModelHelper helper = AnnotationModelHelper.create(createClasspathInfoForScanningAnnotations()); final Set<String> elements = new HashSet<String>(); helper.runJavaSourceTask(new Callable<Void>() { public Void call() throws InterruptedException { helper.getAnnotationScanner().findAnnotations( "javax.annotation.Resource", EnumSet.of(ElementKind.FIELD), new AnnotationHandler() { public void handleAnnotation(TypeElement typeElement, Element element, AnnotationMirror annotationMirror) { assertEquals("foo.MyClass", typeElement.getQualifiedName().toString()); elements.add(element.getSimpleName().toString()); } }); return null; } }); assertEquals(1, elements.size()); assertTrue(elements.contains("myDS")); }
Example #14
Source File: WSInjectiontargetQueryImplementation.java From netbeans with Apache License 2.0 | 5 votes |
public boolean isInjectionTarget(CompilationController controller, TypeElement typeElement) { if (controller == null || typeElement==null) { throw new NullPointerException("Passed null to WSInjectionTargetQueryImplementation.isInjectionTarget(CompilationController, TypeElement)"); // NOI18N } FileObject fo = controller.getFileObject(); Project project = FileOwnerQuery.getOwner(fo); if (ProjectUtil.isJavaEE5orHigher(project) && !isTomcatTargetServer(project) && !(ElementKind.INTERFACE==typeElement.getKind())) { List<? extends AnnotationMirror> annotations = typeElement.getAnnotationMirrors(); boolean found = false; for (AnnotationMirror m : annotations) { Name qualifiedName = ((TypeElement)m.getAnnotationType().asElement()).getQualifiedName(); if (qualifiedName.contentEquals("javax.jws.WebService")) { //NOI18N found = true; break; } if (qualifiedName.contentEquals("javax.jws.WebServiceProvider")) { //NOI18N found = true; break; } } if (found) return true; } return false; }
Example #15
Source File: AnnotationValidator.java From picocli with Apache License 2.0 | 5 votes |
private AnnotationMirror getPicocliAnnotationMirror(Element element) { AnnotationMirror annotationMirror = null; for (AnnotationMirror mirror : element.getAnnotationMirrors()) { if (mirror.getAnnotationType().toString().startsWith("picocli")) { annotationMirror = mirror; } } return annotationMirror; }
Example #16
Source File: IndexedStereotypesProvider.java From java-technology-stack with MIT License | 5 votes |
private boolean isAnnotatedWithIndexed(Element type) { for (AnnotationMirror annotation : type.getAnnotationMirrors()) { if (isIndexedAnnotation(annotation)) { return true; } } return false; }
Example #17
Source File: IndexedStereotypesProvider.java From java-technology-stack with MIT License | 5 votes |
private Element collectStereotypes(Set<Element> seen, Set<String> stereotypes, Element element, AnnotationMirror annotation) { if (isIndexedAnnotation(annotation)) { stereotypes.add(this.typeHelper.getType(element)); } return getCandidateAnnotationElement(seen, annotation); }
Example #18
Source File: JavacElements.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Returns the best approximation for the tree node and compilation unit * corresponding to the given element, annotation and value. * If the element is null, null is returned. * If the annotation is null or cannot be found, the tree node and * compilation unit for the element is returned. * If the annotation value is null or cannot be found, the tree node and * compilation unit for the annotation is returned. */ public Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel( Element e, AnnotationMirror a, AnnotationValue v) { if (e == null) return null; Pair<JCTree, JCCompilationUnit> elemTreeTop = getTreeAndTopLevel(e); if (elemTreeTop == null) return null; if (a == null) return elemTreeTop; JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst); if (annoTree == null) return elemTreeTop; if (v == null) return new Pair<>(annoTree, elemTreeTop.snd); JCTree valueTree = matchAttributeToTree( cast(Attribute.class, v), cast(Attribute.class, a), annoTree); if (valueTree == null) return new Pair<>(annoTree, elemTreeTop.snd); return new Pair<>(valueTree, elemTreeTop.snd); }
Example #19
Source File: AnnotationExtractor.java From litho with Apache License 2.0 | 5 votes |
/** * We consider an annotation to be valid for extraction if it's not an internal annotation (i.e. * is in the <code>com.facebook.litho</code> package and is not a source-only annotation. * * <p>We also do not consider the kotlin.Metadata annotation to be valid as it represents the * metadata of the Spec class and not of the class that we are generating. * * @return Whether or not to extract the given annotation. */ private static boolean isValidAnnotation(AnnotationMirror annotation) { final Retention retention = annotation.getAnnotationType().asElement().getAnnotation(Retention.class); if (retention != null && retention.value() == RetentionPolicy.SOURCE) { return false; } String annotationName = annotation.getAnnotationType().toString(); return !annotationName.startsWith("com.facebook.") && !annotationName.equals("kotlin.Metadata"); }
Example #20
Source File: LookupProviderAnnotationProcessor.java From netbeans with Apache License 2.0 | 5 votes |
private List<TypeMirror> findServiceAnnotation(Element e) throws LayerGenerationException { for (AnnotationMirror ann : e.getAnnotationMirrors()) { if (!ProjectServiceProvider.class.getName().equals(ann.getAnnotationType().toString())) { continue; } for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> attr : ann.getElementValues().entrySet()) { if (!attr.getKey().getSimpleName().contentEquals("service")) { continue; } List<TypeMirror> r = new ArrayList<TypeMirror>(); for (Object item : (List<?>) attr.getValue().getValue()) { TypeMirror type = (TypeMirror) ((AnnotationValue) item).getValue(); Types typeUtils = processingEnv.getTypeUtils(); for (TypeMirror otherType : r) { for (boolean swap : new boolean[] {false, true}) { TypeMirror t1 = swap ? type : otherType; TypeMirror t2 = swap ? otherType : type; if (typeUtils.isSubtype(t1, t2)) { processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "registering under both " + typeUtils.asElement(t2).getSimpleName() + " and its subtype " + typeUtils.asElement(t1).getSimpleName() + " will not work if LookupMerger<" + typeUtils.asElement(t2).getSimpleName() + "> is used (#205151)", e, ann, attr.getValue()); } } } r.add(type); } return r; } throw new LayerGenerationException("No service attr found", e); } throw new LayerGenerationException("No @ProjectServiceProvider found", e); }
Example #21
Source File: NullabilityUtil.java From NullAway with MIT License | 5 votes |
private static Stream<? extends AnnotationMirror> getTypeUseAnnotations(Symbol symbol) { Stream<Attribute.TypeCompound> rawTypeAttributes = symbol.getRawTypeAttributes().stream(); if (symbol instanceof Symbol.MethodSymbol) { // for methods, we want the type-use annotations on the return type return rawTypeAttributes.filter((t) -> t.position.type.equals(TargetType.METHOD_RETURN)); } return rawTypeAttributes; }
Example #22
Source File: InitializerSpec.java From spring-init with Apache License 2.0 | 5 votes |
private void addRegistrarInvokers(MethodSpec.Builder builder) { addImportInvokers(builder, configurationType); List<? extends AnnotationMirror> annotationMirrors = configurationType.getAnnotationMirrors(); for (AnnotationMirror am : annotationMirrors) { // Looking up something like @EnableBar TypeElement element = (TypeElement) am.getAnnotationType().asElement(); addImportInvokers(builder, element); } }
Example #23
Source File: ModelUtils.java From netbeans with Apache License 2.0 | 5 votes |
public static Collection<String> extractAnnotationNames(Element elem) { Collection<String> annotationsOnElement = new LinkedList<String>(); for (AnnotationMirror ann : elem.getAnnotationMirrors()) { TypeMirror annType = ann.getAnnotationType(); Element typeElem = ((DeclaredType) annType).asElement(); String typeName = ((TypeElement) typeElem).getQualifiedName().toString(); annotationsOnElement.add(typeName); } return annotationsOnElement; }
Example #24
Source File: StereotypedObject.java From netbeans with Apache License 2.0 | 5 votes |
@Override public boolean refresh( TypeElement type ) { List<? extends AnnotationMirror> allAnnotationMirrors = getHelper().getCompilationController().getElements(). getAllAnnotationMirrors(type); Map<String, ? extends AnnotationMirror> annotationsByType = getHelper().getAnnotationsByType( allAnnotationMirrors ); return annotationsByType.get( myStereotype) != null ; }
Example #25
Source File: Processor.java From Java-9-Programming-By-Example with MIT License | 5 votes |
private void processClass(final Element element) throws ScriptException, FileNotFoundException { for (final AnnotationMirror annotationMirror : element .getAnnotationMirrors()) { processAnnotation(annotationMirror); } }
Example #26
Source File: ConfiguredObjectFactoryGenerator.java From qpid-broker-j with Apache License 2.0 | 5 votes |
private String getParamName(final AnnotationMirror paramAnnotation) { String paramName = null; for(ExecutableElement paramElement : paramAnnotation.getElementValues().keySet()) { if(paramElement.getSimpleName().toString().equals("name")) { AnnotationValue value = paramAnnotation.getElementValues().get(paramElement); paramName = value.getValue().toString(); break; } } return paramName; }
Example #27
Source File: RelationshipMappingRename.java From netbeans with Apache License 2.0 | 5 votes |
RelationshipAnnotationRenameRefactoringElement(FileObject fo, VariableElement var, AnnotationTree at, AnnotationMirror annotation, String attrValue, int start, int end) { this.fo = fo; this.annotation = annotation; this.attrValue = attrValue; if((end-start)>attrValue.length()){//handle quotes start++; end--; } loc = new int[]{start, end}; this.at = at; this.var = var; }
Example #28
Source File: ScopedBeanAnalyzer.java From netbeans with Apache License 2.0 | 5 votes |
private void checkInterceptorDecorator( TypeElement scopeElement, Element element, WebBeansModel model, Result result ) { if ( scopeElement.getQualifiedName().contentEquals(AnnotationUtil.DEPENDENT)){ return; } AnnotationMirror annotationMirror = AnnotationUtil.getAnnotationMirror( element, model.getCompilationController(), AnnotationUtil.INTERCEPTOR, AnnotationUtil.DECORATOR); if ( annotationMirror!= null ){ result.addNotification( Severity.WARNING, element, model, NbBundle.getMessage(ScopedBeanAnalyzer.class, "WARN_ScopedDecoratorInterceptor" )); // NOI18N } }
Example #29
Source File: AbstractServiceProviderProcessor.java From netbeans with Apache License 2.0 | 5 votes |
/** * @param annotation an annotation instance (null permitted) * @param name the name of an attribute of that annotation * @return the corresponding value if found */ private AnnotationValue findAnnotationValue(AnnotationMirror annotation, String name) { if (annotation != null) { for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> entry : annotation.getElementValues().entrySet()) { if (entry.getKey().getSimpleName().contentEquals(name)) { return entry.getValue(); } } } return null; }
Example #30
Source File: WorkArounds.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public boolean isDeprecated0(Element e) { if (!utils.getDeprecatedTrees(e).isEmpty()) { return true; } JavacTypes jctypes = ((DocEnvImpl)configuration.docEnv).toolEnv.typeutils; TypeMirror deprecatedType = utils.getDeprecatedType(); for (AnnotationMirror anno : e.getAnnotationMirrors()) { if (jctypes.isSameType(anno.getAnnotationType().asElement().asType(), deprecatedType)) return true; } return false; }