Java Code Examples for java.lang.annotation.Target#value()

The following examples show how to use java.lang.annotation.Target#value() . 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: Annotations.java    From immutables with Apache License 2.0 6 votes vote down vote up
static boolean annotationMatchesTarget(Element annotationElement, ElementType elementType) {
  @Nullable Target target = annotationElement.getAnnotation(Target.class);
  if (target != null) {
    ElementType[] targetTypes = target.value();
    if (targetTypes.length == 0) {
      return false;
    }
    boolean found = false;
    for (ElementType t : targetTypes) {
      if (t == elementType) {
        found = true;
      }
    }
    if (!found) {
      return false;
    }
  }
  return true;
}
 
Example 2
Source File: JAXBUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static boolean isJAXB22() {
    Target t = XmlElement.class.getAnnotation(Target.class);
    //JAXB 2.2 allows XmlElement on params.
    for (ElementType et : t.value()) {
        if (et == ElementType.PARAMETER) {
            return true;
        }
    }
    return false;
}
 
Example 3
Source File: MetaAnnotationHelper.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
private static boolean validTarget(Class<? extends Annotation> type) {
	final Target target = type.getAnnotation(Target.class);

	if(target==null) {
		return false;
	}

	final ElementType[] targets=target.value();

	return 
		targets.length == 1 && 
		targets[0] == ElementType.ANNOTATION_TYPE;
}
 
Example 4
Source File: TestFixture.java    From jfixture with MIT License 5 votes vote down vote up
@Test
public void Annotation_can_only_be_applied_to_fields() {
    Target target = Fixture.class.getAnnotation(Target.class);
    assertEquals(1, target.value().length);
    ElementType type = target.value()[0];
    assertEquals(ElementType.FIELD, type);
}
 
Example 5
Source File: TestFromListOf.java    From jfixture with MIT License 5 votes vote down vote up
@Test
public void Annotation_can_only_be_applied_to_fields() {
    Target target = FromListOf.class.getAnnotation(Target.class);
    assertEquals(1, target.value().length);
    ElementType type = target.value()[0];
    assertEquals(ElementType.FIELD, type);
}
 
Example 6
Source File: AnnotationUtils.java    From spring-fabric-gateway with MIT License 5 votes vote down vote up
public static <A extends Annotation> AnnotatedElement getAnnotatedElement(Class<?> clazz, Class<A> annotationType) {
	if (clazz == null || annotationType == null) {
		return null;
	}
	Target target = annotationType.getAnnotation(Target.class);
	if (target == null) {
		return null;
	}
	AnnotatedElement element = null;
	ElementType[] value = target.value();
	for (ElementType elementType : value) {

		switch (elementType) {
		case FIELD:
			element = getField(clazz, annotationType);
			break;
		case METHOD:
			element = getMethod(clazz, annotationType);
			break;
		case TYPE:
			element = getType(clazz, annotationType);
			break;
		default:
			break;
		}
		if (element != null) {
			break;
		}
	}
	return element;
}
 
Example 7
Source File: ExtraIntTest.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotationTarget() {
    final Target retention = ExtraInt.class.getAnnotation(Target.class);
    final ElementType[] elementTypes = retention.value();
    assertArrayEquals(new ElementType[] { ElementType.PARAMETER }, elementTypes);
}
 
Example 8
Source File: ExtraDoubleTest.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotationTarget() {
    final Target retention = ExtraDouble.class.getAnnotation(Target.class);
    final ElementType[] elementTypes = retention.value();
    assertArrayEquals(new ElementType[] { ElementType.PARAMETER }, elementTypes);
}
 
Example 9
Source File: ExtraLongTest.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotationTarget() {
    final Target retention = ExtraLong.class.getAnnotation(Target.class);
    final ElementType[] elementTypes = retention.value();
    assertArrayEquals(new ElementType[] { ElementType.PARAMETER }, elementTypes);
}
 
Example 10
Source File: ExtraCharTest.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotationTarget() {
    final Target retention = ExtraChar.class.getAnnotation(Target.class);
    final ElementType[] elementTypes = retention.value();
    assertArrayEquals(new ElementType[] { ElementType.PARAMETER }, elementTypes);
}
 
Example 11
Source File: ExtraShortTest.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotationTarget() {
    final Target retention = ExtraShort.class.getAnnotation(Target.class);
    final ElementType[] elementTypes = retention.value();
    assertArrayEquals(new ElementType[] { ElementType.PARAMETER }, elementTypes);
}
 
Example 12
Source File: ExtraStringTest.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotationTarget() {
    final Target retention = ExtraString.class.getAnnotation(Target.class);
    final ElementType[] elementTypes = retention.value();
    assertArrayEquals(new ElementType[] { ElementType.PARAMETER }, elementTypes);
}
 
Example 13
Source File: ExtraBooleanTest.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotationTarget() {
    final Target retention = ExtraBoolean.class.getAnnotation(Target.class);
    final ElementType[] elementTypes = retention.value();
    assertArrayEquals(new ElementType[] { ElementType.PARAMETER }, elementTypes);
}
 
Example 14
Source File: ExtraTest.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotationTarget() {
    final Target retention = Extra.class.getAnnotation(Target.class);
    final ElementType[] elementTypes = retention.value();
    assertArrayEquals(new ElementType[] { ElementType.PARAMETER }, elementTypes);
}
 
Example 15
Source File: IntentDataTest.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotationTarget() {
    final Target retention = IntentData.class.getAnnotation(Target.class);
    final ElementType[] elementTypes = retention.value();
    assertArrayEquals(new ElementType[] { ElementType.PARAMETER }, elementTypes);
}
 
Example 16
Source File: ExtraFloatTest.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotationTarget() {
    final Target retention = ExtraFloat.class.getAnnotation(Target.class);
    final ElementType[] elementTypes = retention.value();
    assertArrayEquals(new ElementType[] { ElementType.PARAMETER }, elementTypes);
}
 
Example 17
Source File: ExtraByteTest.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotationTarget() {
    final Target retention = ExtraByte.class.getAnnotation(Target.class);
    final ElementType[] elementTypes = retention.value();
    assertArrayEquals(new ElementType[] { ElementType.PARAMETER }, elementTypes);
}
 
Example 18
Source File: OnActivityResultTest.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotationTarget() {
    final Target retention = OnActivityResult.class.getAnnotation(Target.class);
    final ElementType[] elementTypes = retention.value();
    assertArrayEquals(new ElementType[] { ElementType.METHOD }, elementTypes);
}
 
Example 19
Source File: SAnnoDef.java    From Latte-lang with MIT License 4 votes vote down vote up
public boolean canPresentOn(ElementType type) throws SyntaxException {
        String name = fullName();
        try {
                Class<?> cls = Class.forName(name);
                Annotation[] annotations = cls.getAnnotations();
                for (Annotation a : annotations) {
                        if (a instanceof Target) {
                                Target target = (Target) a;
                                ElementType[] types = target.value();
                                for (ElementType t : types) {
                                        if (t.equals(type)) return true;
                                }
                                return false;
                        }
                }
                return true;
        } catch (ClassNotFoundException e) {
                // annotation not compiled yet
                // check manually
                SAnno targetAnno = null;
                for (SAnno anno : annos()) {
                        if (anno.type().fullName().equals(Target.class.getName())) {
                                targetAnno = anno;
                                break;
                        }
                }
                if (null == targetAnno) return true;
                SAnnoField valueAnnoF = null;
                for (SAnnoField f : targetAnno.type().annoFields()) {
                        if (f.name().equals("value")) {
                                valueAnnoF = f;
                                break;
                        }
                }
                if (valueAnnoF == null) throw new LtBug("it should not be null");
                SArrayValue arrV = (SArrayValue) targetAnno.values().get(valueAnnoF);
                for (Value v : arrV.values()) {
                        if (v instanceof EnumValue && v.type().fullName().equals(ElementType.class.getName())) {
                                if (type.name().equals(((EnumValue) v).enumStr())) return true;
                        }
                }
                return false;
        }
}
 
Example 20
Source File: AnnotationUtils.java    From simple-robot-core with Apache License 2.0 4 votes vote down vote up
/**
 * 从某个类上获取注解对象,注解可以深度递归
 * 如果存在多个继承注解,则优先获取浅层第一个注解,如果浅层不存在,则返回第一个获取到的注解
 * 请尽可能保证仅存在一个或者一种继承注解,否则获取到的类型将不可控
 *
 * @param from           获取注解的某个类
 * @param annotationType 想要获取的注解类型
 * @param ignored        获取注解列表的时候的忽略列表
 * @return 获取到的第一个注解对象
 */
public static <T extends Annotation> T getAnnotation(AnnotatedElement from, Class<T> annotationType, Class<T>... ignored) {
    // 首先尝试获取缓存
    T cache = getCache(from, annotationType);
    if(cache != null){
        return cache;
    }

    if(isNull(from, annotationType)){
        return null;
    }


    //先尝试直接获取
    T annotation = from.getAnnotation(annotationType);

    //如果存在直接返回,否则查询
    if (annotation != null) {
        saveCache(from, annotation);
        return annotation;
    }

    // 获取target注解
    Target target = annotationType.getAnnotation(Target.class);
    // 判断这个注解能否标注在其他注解上,如果不能,则不再深入获取
    boolean annotationable = false;
    if (target != null) {
        for (ElementType elType : target.value()) {
            if (elType == ElementType.TYPE || elType == ElementType.ANNOTATION_TYPE) {
                annotationable = true;
                break;
            }
        }
    }

    Annotation[] annotations = from.getAnnotations();
    annotation = annotationable ? getAnnotationFromArrays(annotations, annotationType, ignored) : null;


    // 如果还是获取不到,看看查询的注解类型有没有对应的ByNameType
    if (annotation == null) {
        annotation = getByNameAnnotation(from, annotationType);
    }

    // 如果无法通过注解本身所指向的byName注解获取,看看有没有反向指向此类型的注解
    // 此情况下不进行深层获取
    if(annotation == null){
        annotation = getAnnotationFromByNames(annotations, annotationType);
    }

    // 如果最终不是null,计入缓存
    if(annotation != null){
        saveCache(from, annotation);
    }else{
        nullCache(from, annotationType);
    }

    return annotation;
}