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

The following examples show how to use javax.lang.model.element.ExecutableElement#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: DaoMetaFactory.java    From doma with Apache License 2.0 6 votes vote down vote up
private void validateMethod(TypeElement interfaceElement, ExecutableElement methodElement) {
  TypeElement foundAnnotationTypeElement = null;
  for (AnnotationMirror annotation : methodElement.getAnnotationMirrors()) {
    DeclaredType declaredType = annotation.getAnnotationType();
    TypeElement typeElement = ctx.getMoreTypes().toTypeElement(declaredType);
    if (typeElement.getAnnotation(DaoMethod.class) != null) {
      if (foundAnnotationTypeElement != null) {
        throw new AptException(
            Message.DOMA4087,
            methodElement,
            new Object[] {
              foundAnnotationTypeElement.getQualifiedName(), typeElement.getQualifiedName()
            });
      }
      if (methodElement.isDefault()
          || ctx.getMoreElements().isVirtualDefaultMethod(interfaceElement, methodElement)) {
        throw new AptException(
            Message.DOMA4252, methodElement, new Object[] {typeElement.getQualifiedName()});
      }
      foundAnnotationTypeElement = typeElement;
    }
  }
}
 
Example 2
Source File: TooStrongCast.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static boolean isPolymorphicSignature(CompilationInfo info, TreePath path) {
    TypeElement polymorphicEl=  info.getElements().getTypeElement("java.lang.invoke.MethodHandle.PolymorphicSignature"); // NOI18N
    if (polymorphicEl == null) {
        // unsuitable platform
        return false;
    }
    TypeMirror polyType = polymorphicEl.asType();
    Element target = info.getTrees().getElement(path);
    if (target == null || target.getKind() != ElementKind.METHOD) {
        return false;
    }
    if (target.getEnclosingElement() == null || !target.getEnclosingElement().getKind().isClass()) {
        return false;
    }
    ExecutableElement ee = (ExecutableElement)target;
    TypeElement parent = (TypeElement)target.getEnclosingElement();
    if (!parent.getQualifiedName().toString().startsWith("java.lang.invoke.")) { // NOI18N
        return false;
    }
    for (AnnotationMirror am : ee.getAnnotationMirrors()) {
        if (info.getTypes().isSameType(polyType, am.getAnnotationType())) {
            return true;
        }
    }
    return false;
}
 
Example 3
Source File: JavaSourceHelper.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static TypeElement getXmlRepresentationClass(TypeElement typeElement, String defaultSuffix) {

        List<ExecutableElement> methods = ElementFilter.methodsIn(typeElement.getEnclosedElements());
        for (ExecutableElement method : methods) {
            List<? extends AnnotationMirror> anmirs = method.getAnnotationMirrors();

            AnnotationMirror mirrorHttpMethod = findAnnotation(anmirs, Constants.GET);
            if (mirrorHttpMethod != null) {
                TypeMirror tm = method.getReturnType();
                if (tm != null && tm.getKind() == TypeKind.DECLARED) {
                    TypeElement returnType = (TypeElement) ((DeclaredType) tm).asElement();
                    if (returnType.getSimpleName().toString().endsWith(defaultSuffix)) {
                        return returnType;
                    }
                }
            }
        }
        return null;
    }
 
Example 4
Source File: AddWsOperationHelper.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean checkMethodAnnotation( TypeElement element, 
        Element annotationElement ) 
{
    List<ExecutableElement> methods = ElementFilter.methodsIn( 
            element.getEnclosedElements() );
    if ( methods.size() == 0 ){
        return true;
    }
    for (ExecutableElement method : methods) {
        // ignore constructors
        if ( method.getKind() == ElementKind.METHOD ){
            List<? extends AnnotationMirror> annotationMirrors = 
                method.getAnnotationMirrors();
            for (AnnotationMirror annotationMirror : annotationMirrors) {
                DeclaredType annotationType = 
                    annotationMirror.getAnnotationType();
                Element annotation = annotationType.asElement();
                if ( annotationElement.equals( annotation )){
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 5
Source File: DelegateProcessor.java    From Fourinone with Apache License 2.0 6 votes vote down vote up
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for(TypeElement te:annotations){
          note("annotation:"+te.toString());
      }
      
      Set<? extends Element> elements = roundEnv.getRootElements();
      for(Element e:elements){
	List enclosedElems = e.getEnclosedElements();
	List<ExecutableElement> ees = ElementFilter.methodsIn(enclosedElems);
          for(ExecutableElement ee:ees){
		note("--ExecutableElement name is "+ee.getSimpleName());
              List<? extends AnnotationMirror> as = ee.getAnnotationMirrors();
		note("--as="+as);
              for(AnnotationMirror am:as){
			Map map= am.getElementValues();
			Set<ExecutableElement> ks = map.keySet();
                  for(ExecutableElement k:ks){
                      AnnotationValue av = (AnnotationValue)map.get(k);
                      note("----"+ee.getSimpleName()+"."+k.getSimpleName()+"="+av.getValue());
                  }
              }
          }
      }
      return false;
  }
 
Example 6
Source File: JavaSourceHelper.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static TypeElement getXmlRepresentationClass(TypeElement typeElement, String defaultSuffix) {
    List<ExecutableElement> methods = ElementFilter.methodsIn(typeElement.getEnclosedElements());
    for (ExecutableElement method : methods) {
        List<? extends AnnotationMirror> anmirs = method.getAnnotationMirrors();

        AnnotationMirror mirrorHttpMethod = findAnnotation(anmirs, RestConstants.GET);
        if (mirrorHttpMethod != null) {
            TypeMirror tm = method.getReturnType();
            if (tm != null && tm.getKind() == TypeKind.DECLARED) {
                TypeElement returnType = (TypeElement) ((DeclaredType) tm).asElement();
                if (returnType.getSimpleName().toString().endsWith(defaultSuffix)) {
                    return returnType;
                }
            }
        }
    }
    return null;
}
 
Example 7
Source File: MethodSignatureFormatter.java    From dagger2-sample with Apache License 2.0 5 votes vote down vote up
/**
 * Formats an ExecutableElement as if it were contained within the container, if the container is
 * present.
 */
public String format(ExecutableElement method, Optional<DeclaredType> container) {
  StringBuilder builder = new StringBuilder();
  TypeElement type = MoreElements.asType(method.getEnclosingElement());
  ExecutableType executableType = MoreTypes.asExecutable(method.asType());
  if (container.isPresent()) {
    executableType = MoreTypes.asExecutable(types.asMemberOf(container.get(), method));
    type = MoreElements.asType(container.get().asElement());
  }

  // TODO(cgruber): AnnotationMirror formatter.
  List<? extends AnnotationMirror> annotations = method.getAnnotationMirrors();
  if (!annotations.isEmpty()) {
    Iterator<? extends AnnotationMirror> annotationIterator = annotations.iterator();
    for (int i = 0; annotationIterator.hasNext(); i++) {
      if (i > 0) {
        builder.append(' ');
      }
      builder.append(ErrorMessages.format(annotationIterator.next()));
    }
    builder.append(' ');
  }
  builder.append(nameOfType(executableType.getReturnType()));
  builder.append(' ');
  builder.append(type.getQualifiedName());
  builder.append('.');
  builder.append(method.getSimpleName());
  builder.append('(');
  checkState(method.getParameters().size() == executableType.getParameterTypes().size());
  Iterator<? extends VariableElement> parameters = method.getParameters().iterator();
  Iterator<? extends TypeMirror> parameterTypes = executableType.getParameterTypes().iterator();
  for (int i = 0; parameters.hasNext(); i++) {
    if (i > 0) {
      builder.append(", ");
    }
    appendParameter(builder, parameters.next(), parameterTypes.next());
  }
  builder.append(')');
  return builder.toString();
}
 
Example 8
Source File: WorkingRangesMethodExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
@Nullable
private static SpecMethodModel<EventMethod, WorkingRangeDeclarationModel>
    generateWorkingRangeMethod(
        Elements elements,
        ExecutableElement executableElement,
        List<Class<? extends Annotation>> permittedInterStageInputAnnotations,
        Messager messager,
        Class<? extends Annotation> annotationType) {
  final List<MethodParamModel> methodParams =
      getMethodParams(
          executableElement,
          messager,
          getPermittedMethodParamAnnotations(permittedInterStageInputAnnotations),
          permittedInterStageInputAnnotations,
          ImmutableList.of());

  final String nameInAnnotation =
      ProcessorUtils.getAnnotationParameter(
          elements, executableElement, annotationType, "name", String.class);

  List<? extends AnnotationMirror> annotationMirrors = executableElement.getAnnotationMirrors();
  AnnotationMirror mirror = null;
  for (AnnotationMirror m : annotationMirrors) {
    if (m.getAnnotationType().toString().equals(annotationType.getCanonicalName())) {
      mirror = m;
      break;
    }
  }

  return SpecMethodModel.<EventMethod, WorkingRangeDeclarationModel>builder()
      .annotations(ImmutableList.of())
      .modifiers(ImmutableList.copyOf(new ArrayList<>(executableElement.getModifiers())))
      .name(executableElement.getSimpleName())
      .returnTypeSpec(generateTypeSpec(executableElement.getReturnType()))
      .typeVariables(ImmutableList.copyOf(getTypeVariables(executableElement)))
      .methodParams(ImmutableList.copyOf(methodParams))
      .representedObject(executableElement)
      .typeModel(new WorkingRangeDeclarationModel(nameInAnnotation, mirror))
      .build();
}
 
Example 9
Source File: JacksonSupport.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
private static GenerateAnnotation generateDefaultAnnotations(ExecutableElement getterMethod) {
  for (AnnotationMirror annotationMirror : getterMethod.getAnnotationMirrors()) {
    TypeElement annotationTypeElement =
        (TypeElement) (annotationMirror.getAnnotationType().asElement());
    QualifiedName annotationType = QualifiedName.of(annotationTypeElement);
    if (DISABLE_PROPERTY_ANNOTATIONS.contains(annotationType)) {
      return GenerateAnnotation.NONE;
    }
    if (JSON_ANY_GETTER.equals(annotationType)) {
      return GenerateAnnotation.JSON_ANY;
    }
  }
  return GenerateAnnotation.DEFAULT;
}
 
Example 10
Source File: AutoValueRedactedExtension.java    From auto-value-redacted with Apache License 2.0 5 votes vote down vote up
private static Set<String> getAnnotations(ExecutableElement element) {
  Set<String> set = new LinkedHashSet<>();

  List<? extends AnnotationMirror> annotations = element.getAnnotationMirrors();
  for (AnnotationMirror annotation : annotations) {
    set.add(annotation.getAnnotationType().asElement().getSimpleName().toString());
  }

  return Collections.unmodifiableSet(set);
}
 
Example 11
Source File: WithMethod.java    From auto-value-with with Apache License 2.0 5 votes vote down vote up
private WithMethod(ExecutableElement method, List<Property> properties,
        List<String> methodPropertyNames) {
    this.methodName = method.getSimpleName().toString();
    this.methodModifiers = method.getModifiers();
    this.methodAnnotations = method.getAnnotationMirrors();
    this.properties = properties;
    this.propertyNames = methodPropertyNames;
}
 
Example 12
Source File: AttributeAnnotationValidator.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private AnnotationMirror getAnnotationMirror(final ExecutableElement methodElement, final String annotationClassName)
{
    for (AnnotationMirror annotationMirror : methodElement.getAnnotationMirrors())
    {
        Element annotationAsElement = annotationMirror.getAnnotationType().asElement();
        if (annotationClassName.equals(getFullyQualifiedName(annotationAsElement)))
        {
            return annotationMirror;
        }
    }
    return null;
}
 
Example 13
Source File: ElementTo.java    From sundrio with Apache License 2.0 5 votes vote down vote up
public Method apply(ExecutableElement executableElement) {
    Map<AttributeKey, Object> attributes = new HashMap<>();
    if (executableElement.getDefaultValue() != null) {
       attributes.put(Attributeable.DEFAULT_VALUE, executableElement.getDefaultValue().getValue());
    }
    MethodBuilder methodBuilder = new MethodBuilder()
            .withModifiers(TypeUtils.modifiersToInt(executableElement.getModifiers()))
            .withName(executableElement.getSimpleName().toString())
            .withReturnType(MIRROR_TO_TYPEREF.apply(executableElement.getReturnType()))
            .withAttributes(attributes);



    //Populate constructor parameters
    for (VariableElement variableElement : executableElement.getParameters()) {
        methodBuilder = methodBuilder.addToArguments(PROPERTY.apply(variableElement));

        List<ClassRef> exceptionRefs = new ArrayList<ClassRef>();
        for (TypeMirror thrownType : executableElement.getThrownTypes()) {
            if (thrownType instanceof ClassRef) {
                exceptionRefs.add((ClassRef) thrownType);
            }
        }
        methodBuilder = methodBuilder.withExceptions(exceptionRefs);
    }

    List<ClassRef> annotationRefs = new ArrayList<ClassRef>();
    for (AnnotationMirror annotationMirror : executableElement.getAnnotationMirrors()) {
        methodBuilder.withAnnotations(ANNOTATION_REF.apply(annotationMirror));
    }
    return methodBuilder.build();
}
 
Example 14
Source File: SpringConfigurationValidationProcessor.java    From spring-configuration-validation-processor with Apache License 2.0 5 votes vote down vote up
private void processAutowiredConstructor(ExecutableElement constructor) {
  List<? extends AnnotationMirror> annotations = constructor.getAnnotationMirrors();
  for (AnnotationMirror annotationMirror : annotations) {

    Element annotationTypeElement = annotationMirror.getAnnotationType().asElement();
    if (annotationTypeElement.equals(this.autowiredTypeElement)) {
      printMessage(SpringConfigurationMessage.AUTOWIRED_CONSTRUCTOR, constructor, annotationMirror);
    }
  }
}
 
Example 15
Source File: AutoMatterProcessor.java    From auto-matter with Apache License 2.0 5 votes vote down vote up
private AnnotationMirror nullableAnnotation(final ExecutableElement field) {
  for (AnnotationMirror annotation : field.getAnnotationMirrors()) {
    if (annotation.getAnnotationType().asElement().getSimpleName().contentEquals("Nullable")) {
      return annotation;
    }
  }
  return null;
}
 
Example 16
Source File: AsynchronousMethodInvocation.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isAsynchronousAnnotated(ExecutableElement method) {
    boolean knownClasses = HintsUtils.isContainingKnownClasses(method);
    for (AnnotationMirror am : method.getAnnotationMirrors()) {
        if (EJBAPIAnnotations.ASYNCHRONOUS.equals(am.getAnnotationType().asElement().toString()) && knownClasses) {
            return true;
        }
    }
    return false;
}
 
Example 17
Source File: AddWsOperationHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private String findNewOperationName(TypeElement classEl, CompilationController controller, String suggestedMethodName) 
    throws IOException {
    
    TypeElement methodElement = controller.getElements().getTypeElement("javax.jws.WebMethod"); //NOI18N
    Set<String> operationNames = new HashSet<String>();
    if (methodElement != null) {
        List<ExecutableElement> methods = getMethods(controller,classEl);
        for (ExecutableElement m:methods) {
            String opName = null;
            List<? extends AnnotationMirror> annotations = m.getAnnotationMirrors();
            for (AnnotationMirror anMirror : annotations) {
                if (controller.getTypes().isSameType(methodElement.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("operationName")) { //NOI18N
                            opName = (String) expressions.get(entry.getKey()).getValue();
                            break;
                        }
                    }
                } // end if
                if (opName != null) break;
            } //enfd for
            if (opName == null) opName = m.getSimpleName().toString();
            operationNames.add(opName);
        }
    }
    return findNewOperationName(operationNames, suggestedMethodName);
}
 
Example 18
Source File: MethodVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 *  Determines if the WSDL operation is the corresponding Java method
 */
private boolean isMethodFor(ExecutableElement method, String operationName){
    if(method.getSimpleName().toString().equals(operationName)){
        return true;
    }
    
    //if method name is not the same as the operation name, look at WebMethod annotation
    List<? extends AnnotationMirror> methodAnnotations = 
            method.getAnnotationMirrors();
    for (AnnotationMirror anMirror : methodAnnotations) {
        if (JaxWsUtils.hasFqn(anMirror, "javax.jws.WebMethod")) {   // NOI18N
            Map<? extends ExecutableElement, 
                    ? extends AnnotationValue> expressions = 
                            anMirror.getElementValues();
            for(Entry<? extends ExecutableElement, 
                    ? extends AnnotationValue> entry: expressions.entrySet()) 
            {
                if (entry.getKey().getSimpleName().
                        contentEquals("operationName"))  //NOI18N
                {
                    String name = (String)expressions.get(entry.getKey()).
                        getValue();
                    if (operationName.equals(name)){
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
 
Example 19
Source File: MethodVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 *  Determines if the method has a WebMethod annotation and if it does
 *  that the exclude attribute is not set
 */
private boolean hasWebMethodAnnotation(ExecutableElement method){
    boolean isWebMethod = false;
    List<? extends AnnotationMirror> methodAnnotations = method.getAnnotationMirrors();
    for (AnnotationMirror anMirror : methodAnnotations) {
        if (JaxWsUtils.hasFqn(anMirror, "javax.jws.WebMethod")) {       // NOI18N
            //WebMethod found, set boolean to true
            isWebMethod = true;
            //Now determine if "exclude" is present and set to true
            Map<? extends ExecutableElement, 
                    ? extends AnnotationValue> expressions = anMirror.
                        getElementValues();
            for(Entry<? extends ExecutableElement, 
                    ? extends AnnotationValue> entry: expressions.entrySet()) 
            {
                if (entry.getKey().getSimpleName().contentEquals("exclude")) { //NOI18N
                    String value = (String)expressions.get(entry.getKey()).
                        getValue();
                    if ("true".equals(value)){
                        isWebMethod = false;
                        break;
                    }
                }
            }
        }
        break;
    }
    return isWebMethod;
}
 
Example 20
Source File: AddWsOperationHelper.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private String findNewOperationName(TypeElement classEl, 
        CompilationController controller, String suggestedMethodName) 
        throws IOException 
{
    
    TypeElement methodElement = controller.getElements().
        getTypeElement("javax.jws.WebMethod"); //NOI18N
    if ( methodElement == null ){
        isIncomplete = true;
    }
    Set<String> operationNames = new HashSet<String>();
    List<ExecutableElement> methods = getMethods(controller, classEl);
    for (ExecutableElement m : methods) {
        String opName = null;
        List<? extends AnnotationMirror> annotations = m
                .getAnnotationMirrors();
        for (AnnotationMirror anMirror : annotations) {
            if ( methodElement!= null && 
                    controller.getTypes().isSameType(methodElement.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("operationName")) // NOI18N
                    {
                        opName = (String) expressions.get(entry.getKey()).getValue();
                        break;
                    }
                }
            } // end if
            if (opName != null)
                break;
        } // enfd for
        if (opName == null) {
            opName = m.getSimpleName().toString();
        }
        operationNames.add(opName);
    }
    return findNewOperationName(operationNames, suggestedMethodName);
}