Java Code Examples for javax.lang.model.element.TypeElement#getAnnotationMirrors()

The following examples show how to use javax.lang.model.element.TypeElement#getAnnotationMirrors() . 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: ElementUtils.java    From spring-init with Apache License 2.0 6 votes vote down vote up
public List<TypeElement> getTypesFromAnnotation(TypeElement type, String annotation,
		String attribute) {
	Set<TypeElement> list = new LinkedHashSet<>();
	for (AnnotationMirror mirror : type.getAnnotationMirrors()) {
		if (((TypeElement) mirror.getAnnotationType().asElement()).getQualifiedName()
				.toString().equals(annotation)) {
			list.addAll(getTypesFromAnnotation(mirror, attribute));
		}
		Set<AnnotationMirror> metas = getAnnotations(
				mirror.getAnnotationType().asElement(), annotation);
		for (AnnotationMirror meta : metas) {
			list.addAll(getTypesFromAnnotation(meta, attribute));
		}
	}
	return new ArrayList<>(list);
}
 
Example 2
Source File: JaxWsAddOperation.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean isJaxWsImplementationClass(TypeElement classEl, CompilationController controller) {
    TypeElement wsElement = controller.getElements().getTypeElement("javax.jws.WebService"); //NOI18N
    if (wsElement != null) {
        List<? extends AnnotationMirror> annotations = classEl.getAnnotationMirrors();
        for (AnnotationMirror anMirror : annotations) {
            if (controller.getTypes().isSameType(wsElement.asType(), anMirror.getAnnotationType())) {
                Map<? extends ExecutableElement, ? extends AnnotationValue> expressions = anMirror.getElementValues();
                for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : expressions.entrySet()) {
                    if (entry.getKey().getSimpleName().contentEquals("wsdlLocation")) { //NOI18N
                        return false;
                    }
                }
                return true;
            }
        }
    }
    return false;
}
 
Example 3
Source File: AbstractTestGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether is given method declared by no-interface Bean or by interface annotated by
 * {@code @javax.ejb.Remote} or {@code @javax.ejb.Local}
 *
 * @param srcClass class for which are generated test cases
 * @param srcMethod method of interest
 * @return {@code true} if the bean is no-interface or method is declared by
 * respectively annotated interface, {@code false} otherwise
 */
private static boolean isMethodInContainerLookup(TypeElement srcClass, ExecutableElement srcMethod) {
    // check for no-interface LocalBean
    List<? extends AnnotationMirror> annotations = srcClass.getAnnotationMirrors();
    for (AnnotationMirror annotationMirror : annotations) {
        String annotation = ((TypeElement)annotationMirror.getAnnotationType().asElement()).getQualifiedName().toString();
        if (annotation.equals("javax.ejb.LocalBean"))   // NOI18N
            return true;
    }
    // check if the class has empty implements clause or given method is declared by @Remote, @Local interface
    List<? extends TypeMirror> interfaces = srcClass.getInterfaces();
    if (interfaces.isEmpty()
            || areAllowedInterfacesForLocalBean(interfaces)
            || getEjbInterfaceDeclaringMethod(srcMethod, interfaces) != null) {
        return true;
    }
    return false;
}
 
Example 4
Source File: JRepeatableCodeGenerator.java    From jackdaw with Apache License 2.0 6 votes vote down vote up
@Override
protected final void generateBody(
    final CodeGeneratorContext context, final TypeSpec.Builder builder
) throws Exception {
    final TypeElement typeElement = context.getTypeElement();
    final List<? extends AnnotationMirror> annotations = typeElement.getAnnotationMirrors();
    final String repeatableClassName = JRepeatable.class.getCanonicalName();

    for (final AnnotationMirror annotation : annotations) {
        final String annotationName = ModelUtils.getName(annotation);
        if (!annotationName.equals(repeatableClassName)
            && !SourceCodeUtils.hasAnnotation(builder, annotation))
        {
            builder.addAnnotation(createAnnotation(annotation));
        }
    }

    builder.addMethod(
        MethodSpec.methodBuilder("value")
            .returns(TypeUtils.getArrayTypeName(typeElement))
            .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
            .build()
    );
}
 
Example 5
Source File: JavaxFacesBeanIsGonnaBeDeprecated.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@TriggerTreeKind(Tree.Kind.CLASS)
public static Collection<ErrorDescription> run(HintContext hintContext) {
    List<ErrorDescription> problems = new ArrayList<>();
    final JsfHintsContext ctx = JsfHintsUtils.getOrCacheContext(hintContext);

    if (ctx.getJsfVersion() == null || !ctx.getJsfVersion().isAtLeast(JSFVersion.JSF_2_2)) {
        return problems;
    }

    CompilationInfo info = hintContext.getInfo();
    for (TypeElement typeElement : info.getTopLevelElements()) {
        for (AnnotationMirror annotationMirror : typeElement.getAnnotationMirrors()) {
            if (annotationMirror.getAnnotationType().toString().startsWith(JAVAX_FACES_BEAN)) {
                // it's javax.faces.bean annotation
                Tree tree = info.getTrees().getTree(typeElement, annotationMirror);
                List<Fix> fixes = getFixesForType(info, typeElement, annotationMirror);
                problems.add(JsfHintsUtils.createProblem(
                        tree,
                        info,
                        Bundle.JavaxFacesBeanIsGonnaBeDeprecated_display_name(),
                        Severity.HINT,
                        fixes));
            }
        }
    }
    return problems;
}
 
Example 6
Source File: AddWsOperationHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private String getEndpointInterface(TypeElement classEl, 
        CompilationController controller)  
{
    TypeElement wsElement = controller.getElements().
        getTypeElement("javax.jws.WebService");                 //NOI18N
    if ( wsElement == null ){
        isIncomplete = true;
        return null;
    }
    List<? extends AnnotationMirror> annotations = classEl.getAnnotationMirrors();
    for (AnnotationMirror anMirror : annotations) {
        if (controller.getTypes().isSameType(wsElement.asType(),
                anMirror.getAnnotationType()))
        {
            Map<? extends ExecutableElement, ? extends AnnotationValue> expressions = 
                anMirror.getElementValues();
            for (Map.Entry<? extends ExecutableElement, 
                    ? extends AnnotationValue> entry : expressions.entrySet())
            {
                if (entry.getKey().getSimpleName()
                        .contentEquals("endpointInterface")) // NOI18N
                {
                    String value = (String) expressions.get(entry.getKey())
                            .getValue();
                    if (value != null) {
                        TypeElement seiEl = controller.getElements()
                                .getTypeElement(value);
                        if (seiEl != null) {
                            return seiEl.getQualifiedName().toString();
                        }
                    }
                }
            }
        }
    }
    return null;
}
 
Example 7
Source File: GwtCompatibility.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
GwtCompatibility(TypeElement type) {
  Optional<AnnotationMirror> gwtCompatibleAnnotation = Optional.absent();
  List<? extends AnnotationMirror> annotations = type.getAnnotationMirrors();
  for (AnnotationMirror annotation : annotations) {
    Name name = annotation.getAnnotationType().asElement().getSimpleName();
    if (name.contentEquals("GwtCompatible")) {
      gwtCompatibleAnnotation = Optional.of(annotation);
    }
  }
  this.gwtCompatibleAnnotation = gwtCompatibleAnnotation;
}
 
Example 8
Source File: JaxWsNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean resolveServiceUrl(CompilationController controller, 
        TypeElement targetElement, String[] serviceName, String[] name) 
            throws IOException 
{
    boolean foundWsAnnotation = false;
    List<? extends AnnotationMirror> annotations = targetElement.getAnnotationMirrors();
    for (AnnotationMirror anMirror : annotations) {
        boolean isWebMethodAnnotation = JaxWsUtils.hasFqn(anMirror, 
                "javax.jws.WebService");   // NOI18N
        if (isWebMethodAnnotation) {
            foundWsAnnotation = true;
            Map<? extends ExecutableElement, ? extends AnnotationValue> 
                expressions = anMirror.getElementValues();
            for (Map.Entry<? extends ExecutableElement, 
                    ? extends AnnotationValue> entry : expressions.entrySet()) 
            {
                if (entry.getKey().getSimpleName().contentEquals("serviceName")) { //NOI18N
                    serviceName[0] = (String) expressions.get(entry.getKey()).
                        getValue();
                    if (serviceName[0] != null) {
                        serviceName[0] = URLEncoder.encode(serviceName[0], 
                                "UTF-8"); //NOI18N
                    }
                } else if (entry.getKey().getSimpleName().
                        contentEquals("name"))  //NOI18N
                {
                    name[0] = (String) expressions.get(entry.getKey()).getValue();
                    if (name[0] != null) {
                        name[0] = URLEncoder.encode(name[0], "UTF-8"); //NOI18N
                    }
                }
                if (serviceName[0] != null && name[0] != null) {
                    break;
                }
            }
            break;
        } // end if
    } // end for
    return foundWsAnnotation;
}
 
Example 9
Source File: WSInjectiontargetQueryImplementation.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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 10
Source File: TypeUtil.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static String getAnnotationValueName(CompilationController controller, TypeElement classElement, TypeElement annotationElement) {
    List<? extends AnnotationMirror> annotations = classElement.getAnnotationMirrors();
    for (AnnotationMirror anMirror : annotations) {
        if (controller.getTypes().isSameType(annotationElement.asType(), anMirror.getAnnotationType())) {
            Map<? extends ExecutableElement, ? extends AnnotationValue> expressions = anMirror.getElementValues();
            for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry: expressions.entrySet()) {
                if (entry.getKey().getSimpleName().contentEquals("name")) { //NOI18N
                    return (String) expressions.get(entry.getKey()).getValue();
                }
            }
        }
    }
    return null;
}
 
Example 11
Source File: PersistentObjectManagerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean refresh(TypeElement typeElement) {
    AnnotationParser parser = AnnotationParser.create(getHelper());
    parser.expectString("name", AnnotationParser.defaultValue(typeElement.getSimpleName()));
    List<? extends AnnotationMirror> annotations = typeElement.getAnnotationMirrors();
    if (annotations.size() == 0) {
        return false;
    }
    name = parser.parse(annotations.get(0)).get("name", String.class);
    return true;
}
 
Example 12
Source File: DependenciesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
Collection<AnnotationMirror> getTriggersCompleteAnnotation(TypeElement te) {
    for (AnnotationMirror am : te.getAnnotationMirrors()) {
        if (triggersCompleteAnnotation.equals(am.getAnnotationType().asElement())) {
            return Collections.singletonList(am);
        }
        if (triggersCompleteRepeatAnnotation.equals(am.getAnnotationType().asElement())) {
            return (Collection<AnnotationMirror>) findAttribute(am, "value").getValue();
        }
    }
    return Collections.emptyList();
}
 
Example 13
Source File: GwtCompatibility.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
GwtCompatibility(TypeElement type) {
  Optional<AnnotationMirror> gwtCompatibleAnnotation = Optional.absent();
  List<? extends AnnotationMirror> annotations = type.getAnnotationMirrors();
  for (AnnotationMirror annotation : annotations) {
    Name name = annotation.getAnnotationType().asElement().getSimpleName();
    if (name.contentEquals("GwtCompatible")) {
      gwtCompatibleAnnotation = Optional.of(annotation);
    }
  }
  this.gwtCompatibleAnnotation = gwtCompatibleAnnotation;
}
 
Example 14
Source File: AbstractTestGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether is interface annotated as {@code @javax.ejb.Remote} or {@code @javax.ejb.Local}
 *
 * @param trgInterface interface which should be annotated
 * @return {@code true} if the interface is annotated, {@code false} otherwise
 */
private static boolean isLocalOrRemoteInterface(TypeElement trgInterface) {
    List<? extends AnnotationMirror> annotations = trgInterface.getAnnotationMirrors();
    for (AnnotationMirror am : annotations) {
        String annotation = ((TypeElement)am.getAnnotationType().asElement()).getQualifiedName().toString();
        if (annotation.equals("javax.ejb.Local") ||  // NOI18N
            annotation.equals("javax.ejb.Remote")) { // NOI18N
            // interface is @Local or @Remote
            return true;
        }
    }
    return false;
}
 
Example 15
Source File: GwtCompatibility.java    From auto with Apache License 2.0 5 votes vote down vote up
GwtCompatibility(TypeElement type) {
  Optional<AnnotationMirror> gwtCompatibleAnnotation = Optional.empty();
  List<? extends AnnotationMirror> annotations = type.getAnnotationMirrors();
  for (AnnotationMirror annotation : annotations) {
    Name name = annotation.getAnnotationType().asElement().getSimpleName();
    if (name.contentEquals("GwtCompatible")) {
      gwtCompatibleAnnotation = Optional.of(annotation);
    }
  }
  this.gwtCompatibleAnnotation = gwtCompatibleAnnotation;
}
 
Example 16
Source File: AnnotationExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
public static ImmutableList<AnnotationSpec> extractValidAnnotations(TypeElement element) {
  final List<AnnotationSpec> annotations = new ArrayList<>();
  for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
    if (isValidAnnotation(annotationMirror)) {
      annotations.add(AnnotationSpec.get(annotationMirror));
    }
  }

  return ImmutableList.copyOf(annotations);
}
 
Example 17
Source File: BeansCompletor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isAlternative(TypeElement te) {
    List<? extends AnnotationMirror> annotationMirrors = te.getAnnotationMirrors();
    for (AnnotationMirror annotation : annotationMirrors) {
        if (annotation.getAnnotationType().asElement() instanceof TypeElement) {
            String typeName = ((TypeElement) annotation.getAnnotationType().asElement()).getQualifiedName().toString();
            if (AnnotationUtil.ALTERNATVE.equals(typeName)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 18
Source File: FlowScopedBeanWithoutCdi.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@TriggerTreeKind(Tree.Kind.CLASS)
public static Collection<ErrorDescription> run(HintContext hintContext) {
    List<ErrorDescription> problems = new ArrayList<>();
    final JsfHintsContext ctx = JsfHintsUtils.getOrCacheContext(hintContext);

    Project project = ctx.getProject();
    if (project == null) {
        return problems;
    }

    CompilationInfo info = hintContext.getInfo();
    for (TypeElement typeElement : info.getTopLevelElements()) {
        for (AnnotationMirror annotationMirror : typeElement.getAnnotationMirrors()) {
            if (FLOW_SCOPED.equals(annotationMirror.getAnnotationType().toString())) {
                // it's FlowScoped bean -> check the CDI
                CdiUtil cdiUtil = project.getLookup().lookup(CdiUtil.class);
                if (cdiUtil == null || !cdiUtil.isCdiEnabled()) {
                    Tree tree = info.getTrees().getTree(typeElement, annotationMirror);
                    problems.add(JsfHintsUtils.createProblem(
                            tree,
                            info,
                            Bundle.FlowScopedBeanWithoutCdi_display_name(),
                            Severity.WARNING,
                            Arrays.<Fix>asList(new FixCdiAvailability(project))));
                }
            }
        }
    }
    return problems;
}
 
Example 19
Source File: ConfiguredObjectRegistrationGenerator.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
private String getCategory(final TypeElement e)
{
    Elements elementUtils = processingEnv.getElementUtils();
    TypeElement annotationElement = elementUtils.getTypeElement(MANAGED_OBJECT_CANONICAL_NAME);
    String category = null;
    List<? extends AnnotationMirror> annotationMirrors = e.getAnnotationMirrors();
    if(annotationMirrors != null)
    {
        for (AnnotationMirror a : annotationMirrors)
        {
            if (a.getAnnotationType().asElement().equals(annotationElement))
            {
                category = e.getSimpleName().toString().toLowerCase();

                for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : a.getElementValues()
                        .entrySet())
                {
                    if (entry.getKey().getSimpleName().toString().equals("category"))
                    {
                        if (!Boolean.TRUE.equals(entry.getValue().getValue()))
                        {
                            category = null;
                        }

                        break;
                    }
                }
                break;
            }
        }
    }

    if (category == null)
    {
        for (TypeMirror interfaceMirror : e.getInterfaces())
        {
            category = getCategory((TypeElement) processingEnv.getTypeUtils().asElement(interfaceMirror));
            if (category != null)
            {
                break;
            }
        }
    }

    if (category == null && e.getSuperclass() != null)
    {
        TypeElement parent = (TypeElement) processingEnv.getTypeUtils().asElement(e.getSuperclass());
        if(parent != null)
        {
            category = getCategory(parent);
        }
    }

    return category;

}
 
Example 20
Source File: ConfigDocItemScanner.java    From quarkus with Apache License 2.0 4 votes vote down vote up
/**
 * Record a configuration root class. It will later be visited to find configuration items.
 */
public void addConfigRoot(final PackageElement pkg, TypeElement clazz) {
    if (Constants.SKIP_DOCS_GENERATION) {
        return;
    }

    if (pkg.toString().startsWith(IO_QUARKUS_TEST_EXTENSION_PACKAGE)) {
        return;
    }

    ConfigPhase configPhase = ConfigPhase.BUILD_TIME;

    for (AnnotationMirror annotationMirror : clazz.getAnnotationMirrors()) {
        String annotationName = annotationMirror.getAnnotationType().toString();
        if (annotationName.equals(Constants.ANNOTATION_CONFIG_ROOT)) {
            final Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror
                    .getElementValues();
            String name = Constants.EMPTY;
            for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementValues.entrySet()) {
                final String key = entry.getKey().toString();
                final String value = entry.getValue().getValue().toString();
                if ("name()".equals(key)) {
                    name = Constants.QUARKUS + Constants.DOT + value;
                } else if ("phase()".equals(key)) {
                    configPhase = ConfigPhase.valueOf(value);
                }
            }

            if (name.isEmpty()) {
                name = deriveConfigRootName(clazz.getSimpleName().toString(), configPhase);
            } else if (name.endsWith(Constants.DOT + Constants.PARENT)) {
                // take into account the root case which would contain characters that can't be used to create the final file
                name = name.replace(Constants.DOT + Constants.PARENT, "");
            }

            final Matcher pkgMatcher = Constants.PKG_PATTERN.matcher(pkg.toString());
            final String fileName;
            if (pkgMatcher.find()) {
                fileName = DocGeneratorUtil.computeExtensionDocFileName(clazz.toString());
            } else {
                fileName = name.replace(Constants.DOT, Constants.DASH.charAt(0)) + Constants.ADOC_EXTENSION;
            }

            ConfigRootInfo configRootInfo = new ConfigRootInfo(name, clazz, configPhase, fileName);
            configRoots.add(configRootInfo);
            break;
        }
    }
}