Java Code Examples for javax.lang.model.util.Elements#getAllAnnotationMirrors()

The following examples show how to use javax.lang.model.util.Elements#getAllAnnotationMirrors() . 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: AbstractMicaProcessor.java    From mica-auto with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 获取注解,支持组合注解
 *
 * @param elementUtils       elementUtils
 * @param e                  Element
 * @param annotationFullName annotationFullName
 * @return {boolean}
 */
protected AnnotationMirror getAnnotation(Elements elementUtils, Element e, String annotationFullName) {
	List<? extends AnnotationMirror> annotationList = elementUtils.getAllAnnotationMirrors(e);
	for (AnnotationMirror annotation : annotationList) {
		// 如果是对于的注解
		if (isAnnotation(annotationFullName, annotation)) {
			return annotation;
		}
		// 处理组合注解
		Element element = annotation.getAnnotationType().asElement();
		// 如果是 java 元注解,继续循环
		if (element.toString().startsWith("java.lang")) {
			continue;
		}
		// 递归处理 组合注解
		return getAnnotation(elementUtils, element, annotationFullName);
	}
	return null;
}
 
Example 2
Source File: AbstractMicaProcessor.java    From mica-auto with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 判断是相同的注解,支持组合注解
 *
 * @param elementUtils       elementUtils
 * @param e                  Element
 * @param annotationFullName annotationFullName
 * @return {boolean}
 */
protected boolean isAnnotation(Elements elementUtils, Element e, String annotationFullName) {
	List<? extends AnnotationMirror> annotationList = elementUtils.getAllAnnotationMirrors(e);
	for (AnnotationMirror annotation : annotationList) {
		// 如果是对于的注解
		if (isAnnotation(annotationFullName, annotation)) {
			return true;
		}
		// 处理组合注解
		Element element = annotation.getAnnotationType().asElement();
		// 如果是 java 元注解,继续循环
		if (element.toString().startsWith("java.lang")) {
			continue;
		}
		// 递归处理 组合注解
		if (isAnnotation(elementUtils, element, annotationFullName)) {
			return true;
		}
	}
	return false;
}
 
Example 3
Source File: AutoFactoriesProcessor.java    From magic-starter with GNU Lesser General Public License v3.0 6 votes vote down vote up
private boolean isAnnotation(Elements elementUtils, Element e, String annotationFullName) {
	List<? extends AnnotationMirror> annotationList = elementUtils.getAllAnnotationMirrors(e);
	for (AnnotationMirror annotation : annotationList) {
		// 如果是对于的注解
		if (isAnnotation(annotationFullName, annotation)) {
			return true;
		}
		// 处理组合注解
		Element element = annotation.getAnnotationType().asElement();
		// 如果是 java 元注解,继续循环
		if (element.toString().startsWith("java.lang")) {
			continue;
		}
		// 递归处理 组合注解
		if (isAnnotation(elementUtils, element, annotationFullName)) {
			return true;
		}
	}
	return false;
}
 
Example 4
Source File: AnnotationUtil.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * 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 5
Source File: TestClassInfoTask.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void run(CompilationController controller) throws Exception {
    controller.toPhase(Phase.RESOLVED);
    fileObject = controller.getFileObject();
    TypeElement typeElement = null;
    List<? extends TypeElement> topLevelElements = controller.getTopLevelElements();
    for (Iterator<? extends TypeElement> it = topLevelElements.iterator(); it.hasNext();) {
        typeElement = it.next();
        if (typeElement.getKind() == ElementKind.CLASS) {
            className = typeElement.getSimpleName().toString();
            break;
        }
    }
    Elements elements = controller.getElements();
    TypeElement testcase = elements.getTypeElement(TESTCASE);
    boolean junit3 = (testcase != null && typeElement != null) ? controller.getTypes().isSubtype(typeElement.asType(), testcase.asType()) : false;
    TreePath tp = controller.getTreeUtilities().pathFor(caretPosition);
    while (tp != null && tp.getLeaf().getKind() != Kind.METHOD) {
        tp = tp.getParentPath();
    }
    if (tp != null) {
        Element element = controller.getTrees().getElement(tp);
        if (element != null) {
            String mn = element.getSimpleName().toString();
            if (junit3) {
                methodName = mn.startsWith("test") ? mn : null; //NOI18N
            } else {
                List<? extends AnnotationMirror> allAnnotationMirrors = elements.getAllAnnotationMirrors(element);
                if (isJunit4Test(allAnnotationMirrors) || isJunit5Testable(allAnnotationMirrors)) {
                    methodName = mn;
                }
            }
        }
    }
}
 
Example 6
Source File: AnnotationUtility.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Iterate over annotations of currentElement. Accept only annotation in
 * accepted set.
 *
 * @param currentElement the current element
 * @param filter the filter
 * @param listener the listener
 */
public static void forEachAnnotations(Element currentElement, AnnotationFilter filter, AnnotationFoundListener listener) {
	final Elements elementUtils=BaseProcessor.elementUtils;
	List<? extends AnnotationMirror> annotationList = elementUtils.getAllAnnotationMirrors(currentElement);
	String annotationClassName;
	// boolean valid=true;

	for (AnnotationMirror annotation : annotationList) {
		Map<String, String> values = new HashMap<String, String>();
		annotationClassName = annotation.getAnnotationType().asElement().toString();

		if (filter != null && !filter.isAccepted(annotationClassName)) {
			continue;
		}

		values.clear();
		for (Entry<? extends ExecutableElement, ? extends AnnotationValue> annotationItem : elementUtils.getElementValuesWithDefaults(annotation).entrySet()) {
			String value = annotationItem.getValue().toString();
			if (value.startsWith("\"") && value.endsWith("\"")) {
				value = value.substring(1);
				value = value.substring(0, value.length() - 1);
			}
			values.put(annotationItem.getKey().getSimpleName().toString(), value);
		}

		if (listener != null) {
			listener.onAcceptAnnotation(currentElement, annotationClassName, values);
		}
	}				
}
 
Example 7
Source File: AnnotationUtility.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Extract from an annotation of a method the attribute value specified.
 *
 * @param elementUtils the element utils
 * @param item the item
 * @param annotationName the annotation name
 * @param attribute the attribute
 * @param listener the listener
 */
static void extractAttributeValue(Elements elementUtils, Element item, String annotationName, AnnotationAttributeType attribute, OnAttributeFoundListener listener) {
	List<? extends AnnotationMirror> annotationList = elementUtils.getAllAnnotationMirrors(item);
	for (AnnotationMirror annotation : annotationList) {			
		if (annotationName.equals(annotation.getAnnotationType().asElement().toString())) {
			// found annotation
			for (Entry<? extends ExecutableElement, ? extends AnnotationValue> annotationItem : elementUtils.getElementValuesWithDefaults(annotation).entrySet()) {
				if (attribute.isEquals(annotationItem.getKey())) {
					listener.onFound(annotationItem.getValue().toString());
					return;
				}
			}
		}
	}
}
 
Example 8
Source File: EnableBeansFilter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private boolean checkClass( TypeElement element ){
    if ( element.getKind() != ElementKind.CLASS ){
        return false;
    }
    Set<Modifier> modifiers = element.getModifiers();

    Element enclosing = element.getEnclosingElement();
    if ( !( enclosing instanceof PackageElement) ){
        /*
         * If class is inner class then it should be static.
         */
        if ( !modifiers.contains( Modifier.STATIC ) ){
            return false;
        }
    }
    Elements elements = getHelper().getCompilationController().getElements();
    Types types = getHelper().getCompilationController().getTypes();

    List<? extends AnnotationMirror> allAnnotations = elements.
        getAllAnnotationMirrors(element);

    if ( modifiers.contains( Modifier.ABSTRACT ) &&
            !getHelper().hasAnnotation(allAnnotations, DECORATOR ) )
    {
        /*
         * If class is abstract it should be Decorator.
         */
        return false;
    }
    TypeElement extensionElement = elements.getTypeElement( EXTENSION );
    if ( extensionElement!= null ){
        TypeMirror extensionType = extensionElement.asType();
        /*
         * Class doesn't implement Extension
         */
        if ( types.isAssignable( element.asType(), extensionType )){
            return false;
        }
    }
    /*
     * There should be either no parameters CTOR or CTOR is annotated with @Inject
     */
    List<ExecutableElement> constructors = ElementFilter.constructorsIn(
            element.getEnclosedElements());
    boolean foundCtor = constructors.size() ==0;
    for (ExecutableElement ctor : constructors) {
        if ( ctor.getParameters().size() == 0 ){
            foundCtor = true;
            break;
        }
        if ( getHelper().hasAnnotation(allAnnotations,
                FieldInjectionPointLogic.INJECT_ANNOTATION))
        {
            foundCtor = true;
            break;
        }
    }
    return foundCtor;
}