Java Code Examples for java.lang.annotation.ElementType

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project hibernate-validator, under directory /engine/src/main/java/org/hibernate/validator/internal/engine/.

Source file: ValidatorImpl.java

  17 
vote

private boolean isCascadeRequired(ValidationContext<?,?> validationContext,ValueContext<?,?> valueContext,Member member){
  final ElementType type=member instanceof Field ? ElementType.FIELD : ElementType.METHOD;
  boolean isReachable;
  boolean isCascadable;
  PathImpl path=valueContext.getPropertyPath();
  Path pathToObject=path.getPathWithoutLeafNode();
  if (ElementType.TYPE.equals(type)) {
    isReachable=true;
  }
 else {
    try {
      isReachable=validationContext.getTraversableResolver().isReachable(valueContext.getCurrentBean(),path.getLeafNode(),validationContext.getRootBeanClass(),pathToObject,type);
    }
 catch (    RuntimeException e) {
      throw log.getErrorDuringCallOfTraversableResolverIsReachableException(e);
    }
  }
  if (ElementType.TYPE.equals(type)) {
    isCascadable=true;
  }
 else {
    try {
      isCascadable=validationContext.getTraversableResolver().isCascadable(valueContext.getCurrentBean(),path.getLeafNode(),validationContext.getRootBeanClass(),pathToObject,type);
    }
 catch (    RuntimeException e) {
      throw log.getErrorDuringCallOfTraversableResolverIsCascadableException(e);
    }
  }
  return isReachable && isCascadable;
}
 

Example 2

From project jsfunit, under directory /jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/arquillian/container/.

Source file: JSFUnitSessionFactory.java

  17 
vote

public static JSFSession findJSFSession(AnnotatedElement injectionPoint) throws IOException {
  JSFUnitSessionFactory factory=new JSFUnitSessionFactory();
  ElementType injectionType=factory.getElementType(injectionPoint);
  HttpSession httpSession=WebConversationFactory.getSessionFromThreadLocal();
  JSFSession jsfSession=null;
  if (injectionType == ElementType.FIELD) {
    jsfSession=(JSFSession)httpSession.getAttribute(JSFSESSION_FIELD_INJECTED);
  }
  if (injectionType == ElementType.METHOD) {
    jsfSession=(JSFSession)httpSession.getAttribute(JSFSESSION_METHOD_INJECTED);
  }
  if (jsfSession != null) {
    return jsfSession;
  }
  jsfSession=factory.createJSFSession(injectionPoint,injectionType,httpSession);
  return jsfSession;
}
 

Example 3

From project beanvalidation-tck, under directory /tests/src/main/java/org/hibernate/beanvalidation/tck/tests/metadata/.

Source file: ElementDescriptorTest.java

  15 
vote

@Test @SpecAssertion(section="5.2",id="e") public void testDeclaredOn(){
  Validator validator=TestUtil.getValidatorUnderTest();
  BeanDescriptor beanDescriptor=validator.getConstraintsForClass(SubClass.class);
  assertNotNull(beanDescriptor);
  Set<ConstraintDescriptor<?>> descriptors=beanDescriptor.getConstraintsForProperty("myField").findConstraints().lookingAt(Scope.HIERARCHY).declaredOn(ElementType.TYPE).getConstraintDescriptors();
  assertTrue(descriptors.size() == 0);
  descriptors=beanDescriptor.getConstraintsForProperty("myField").findConstraints().lookingAt(Scope.HIERARCHY).declaredOn(ElementType.METHOD).getConstraintDescriptors();
  assertTrue(descriptors.size() == 0);
  descriptors=beanDescriptor.getConstraintsForProperty("myField").findConstraints().lookingAt(Scope.HIERARCHY).declaredOn(ElementType.FIELD).getConstraintDescriptors();
  assertTrue(descriptors.size() == 2);
}
 

Example 4

From project beanvalidation-tck, under directory /tests/src/main/java/org/hibernate/beanvalidation/tck/tests/traversableresolver/.

Source file: SnifferTraversableResolver.java

  15 
vote

public SnifferTraversableResolver(Suit suit){
  expectedReachCalls.add(new Call(suit,"size",Suit.class,"",ElementType.FIELD));
  expectedReachCalls.add(new Call(suit,"trousers",Suit.class,"",ElementType.FIELD));
  expectedCascadeCalls.add(new Call(suit,"trousers",Suit.class,"",ElementType.FIELD));
  expectedReachCalls.add(new Call(suit.getTrousers(),"length",Suit.class,"trousers",ElementType.FIELD));
  expectedReachCalls.add(new Call(suit,"jacket",Suit.class,"",ElementType.METHOD));
  expectedCascadeCalls.add(new Call(suit,"jacket",Suit.class,"",ElementType.METHOD));
  expectedReachCalls.add(new Call(suit.getJacket(),"width",Suit.class,"jacket",ElementType.METHOD));
}
 

Example 5

From project cas, under directory /cas-server-core/src/main/java/org/jasig/cas/util/.

Source file: CustomBeanValidationPostProcessor.java

  15 
vote

public CustomBeanValidationPostProcessor(){
  final Configuration configuration=Validation.byDefaultProvider().configure();
  configuration.traversableResolver(new TraversableResolver(){
    public boolean isReachable(    final Object o,    final Path.Node node,    final Class<?> aClass,    final Path path,    final ElementType elementType){
      return true;
    }
    public boolean isCascadable(    final Object o,    final Path.Node node,    final Class<?> aClass,    final Path path,    final ElementType elementType){
      return true;
    }
  }
);
  final Validator validator=configuration.buildValidatorFactory().getValidator();
  setValidator(validator);
}
 

Example 6

From project cdk, under directory /generator/src/main/java/org/richfaces/cdk/apt/.

Source file: JavaSourceProcessor.java

  15 
vote

private boolean isAppropriateTarget(Element element,Target target){
  boolean match=false;
  ElementKind kind=element.getKind();
  if (null != target) {
    for (    ElementType targetType : target.value()) {
switch (targetType) {
case TYPE:
        match|=ElementKind.CLASS.equals(kind) || ElementKind.INTERFACE.equals(kind) || ElementKind.ENUM.equals(kind);
      break;
case PACKAGE:
    match|=ElementKind.PACKAGE.equals(kind);
  break;
case METHOD:
match|=ElementKind.METHOD.equals(kind);
break;
case FIELD:
match|=ElementKind.FIELD.equals(kind);
break;
default :
break;
}
}
}
 else {
match=ElementKind.CLASS.equals(kind) || ElementKind.INTERFACE.equals(kind) || ElementKind.ENUM.equals(kind)|| ElementKind.PACKAGE.equals(kind)|| ElementKind.METHOD.equals(kind)|| ElementKind.FIELD.equals(kind);
}
return match;
}
 

Example 7

From project Collections, under directory /src/test/java/vanilla/java/collections/hand/.

Source file: HandTypesElement.java

  15 
vote

@Override public void writeExternal(ObjectOutput out) throws IOException {
  BooleanFieldModel.write(out,getBoolean());
  Boolean2FieldModel.write(out,getBoolean2());
  ByteFieldModel.write(out,getByte());
  Byte2FieldModel.write(out,getByte2());
  CharFieldModel.write(out,getChar());
  DoubleFieldModel.write(out,getDouble());
  Enum8FieldModel.write(out,ElementType.class,getElementType());
  Enumerated16FieldModel.write(out,String.class,getString());
  FloatFieldModel.write(out,getFloat());
  IntFieldModel.write(out,getInt());
  LongFieldModel.write(out,getLong());
  ShortFieldModel.write(out,getShort());
  ObjectFieldModel.write(out,ObjectTypes.A.class,getA());
}
 

Example 8

From project Collections, under directory /src/test/java/vanilla/java/collections/hand/.

Source file: HandTypesElement.java

  15 
vote

@Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  setBoolean(BooleanFieldModel.read(in));
  setBoolean2(Boolean2FieldModel.read(in));
  setByte(ByteFieldModel.read(in));
  setByte2(Byte2FieldModel.read(in));
  setChar(CharFieldModel.read(in));
  setDouble(DoubleFieldModel.read(in));
  setElementType(Enum8FieldModel.read(in,ElementType.class));
  setString(Enumerated16FieldModel.read(in,String.class));
  setFloat(FloatFieldModel.read(in));
  setInt(IntFieldModel.read(in));
  setLong(LongFieldModel.read(in));
  setShort(ShortFieldModel.read(in));
  setA(ObjectFieldModel.read(in,ObjectTypes.A.class));
}
 

Example 9

From project geronimo-xbean, under directory /xbean-finder/src/main/java/org/apache/xbean/finder/.

Source file: AnnotationFinder.java

  15 
vote

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 10

From project geronimo-xbean, under directory /xbean-finder/src/main/java/org/apache/xbean/finder/.

Source file: MetaAnnotatedElement.java

  15 
vote

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 11

From project hibernate-validator, under directory /annotation-processor/src/main/java/org/hibernate/validator/ap/checks/.

Source file: TargetCheck.java

  15 
vote

private boolean containsAtLeastOneSupportedElementType(Target target){
  ElementType[] elementTypes=target.value();
  for (  ElementType oneElementType : elementTypes) {
    if (supportedTypes.contains(oneElementType)) {
      return true;
    }
  }
  return false;
}
 

Example 12

From project jandex, under directory /src/test/java/org/jboss/jandex/test/.

Source file: BasicTestCase.java

  15 
vote

private void verifyDummy(Index index){
  AnnotationInstance instance=index.getAnnotations(DotName.createSimple(TestAnnotation.class.getName())).get(0);
  assertEquals("Test",instance.value("name").asString());
  assertTrue(Arrays.equals(new int[]{1,2,3,4,5},instance.value("ints").asIntArray()));
  assertEquals(Void.class.getName(),instance.value("klass").asClass().name().toString());
  assertTrue(1.34f == instance.value("nested").asNested().value().asFloat());
  assertTrue(3.14f == instance.value("nestedArray").asNestedArray()[0].value().asFloat());
  assertTrue(2.27f == instance.value("nestedArray").asNestedArray()[1].value().asFloat());
  assertEquals(ElementType.TYPE.name(),instance.value("enums").asEnumArray()[0]);
  assertEquals(ElementType.PACKAGE.name(),instance.value("enums").asEnumArray()[1]);
  assertEquals(10,instance.value("longValue").asLong());
  assertEquals(DummyClass.class.getName(),instance.target().toString());
  List<ClassInfo> implementors=index.getKnownDirectImplementors(DotName.createSimple(Serializable.class.getName()));
  assertEquals(1,implementors.size());
  assertEquals(implementors.get(0).name(),DotName.createSimple(DummyClass.class.getName()));
  implementors=index.getKnownDirectImplementors(DotName.createSimple(InputStream.class.getName()));
  assertEquals(0,implementors.size());
}
 

Example 13

From project jsfunit, under directory /jboss-jsfunit-arquillian/src/main/java/org/jboss/jsfunit/arquillian/container/.

Source file: JSFUnitSessionFactory.java

  15 
vote

private JSFSession createJSFSession(AnnotatedElement injectionPoint,ElementType injectionType,HttpSession httpSession) throws IOException {
  JSFSession jsfSession=new JSFSession(createWebClientSpec(injectionPoint,injectionType));
  if (injectionType == ElementType.FIELD) {
    httpSession.setAttribute(JSFSESSION_FIELD_INJECTED,jsfSession);
  }
  if (injectionType == ElementType.METHOD) {
    httpSession.setAttribute(JSFSESSION_METHOD_INJECTED,jsfSession);
  }
  return jsfSession;
}
 

Example 14

From project metatypes, under directory /metatype-impl/src/main/java/org/metatype/.

Source file: MetaAnnotatedObject.java

  15 
vote

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 15

From project openwebbeans, under directory /webbeans-impl/src/main/java/org/apache/webbeans/intercept/.

Source file: InterceptorUtil.java

  15 
vote

/** 
 * @param clazz AUTSCH! we should use the AnnotatedType for all that stuff!
 */
public <T>void checkLifecycleConditions(Class<T> clazz,Annotation[] annots,String errorMessage){
  Asserts.nullCheckForClass(clazz);
  if (isLifecycleMethodInterceptor(clazz) && !isBusinessMethodInterceptor(clazz)) {
    Annotation[] anns=webBeansContext.getAnnotationManager().getInterceptorBindingMetaAnnotations(annots);
    for (    Annotation annotation : anns) {
      Target target=annotation.annotationType().getAnnotation(Target.class);
      ElementType[] elementTypes=target.value();
      if (!(elementTypes.length == 1 && elementTypes[0].equals(ElementType.TYPE))) {
        throw new WebBeansConfigurationException(errorMessage);
      }
    }
  }
}
 

Example 16

From project openwebbeans, under directory /webbeans-impl/src/main/java/org/apache/webbeans/intercept/.

Source file: InterceptorUtil.java

  15 
vote

public <T>void checkLifecycleConditions(AnnotatedType<T> annotatedType,Annotation[] annots,String errorMessage){
  if (isLifecycleMethodInterceptor(annotatedType) && !isBusinessMethodInterceptor(annotatedType)) {
    Annotation[] anns=webBeansContext.getAnnotationManager().getInterceptorBindingMetaAnnotations(annots);
    for (    Annotation annotation : anns) {
      Target target=annotation.annotationType().getAnnotation(Target.class);
      ElementType[] elementTypes=target.value();
      if (!(elementTypes.length == 1 && elementTypes[0].equals(ElementType.TYPE))) {
        throw new WebBeansConfigurationException(errorMessage);
      }
    }
  }
}
 

Example 17

From project plexus-containers, under directory /plexus-container-default/src/test/java/org/codehaus/plexus/component/configurator/.

Source file: AbstractComponentConfiguratorTest.java

  15 
vote

public void testComponentConfigurationWhereFieldIsEnum() throws Exception {
  String xml="<configuration>" + "  <simpleEnum>TYPE</simpleEnum>" + "  <nestedEnum>ONE</nestedEnum>"+ "</configuration>";
  PlexusConfiguration configuration=PlexusTools.buildConfiguration("<Test>",new StringReader(xml));
  ComponentWithEnumFields component=new ComponentWithEnumFields();
  ComponentDescriptor descriptor=new ComponentDescriptor();
  descriptor.setRole("role");
  descriptor.setImplementation(component.getClass().getName());
  descriptor.setConfiguration(configuration);
  ClassWorld classWorld=new ClassWorld();
  ClassRealm realm=classWorld.newRealm("test",getClass().getClassLoader());
  configureComponent(component,descriptor,realm);
  assertEquals(ElementType.TYPE,component.getSimpleEnum());
  assertEquals(ComponentWithEnumFields.NestedEnum.ONE,component.getNestedEnum());
}
 

Example 18

From project sisu, under directory /legacy/containers/sisu-inject-plexus/src/test/java/org/codehaus/plexus/component/configurator/.

Source file: AbstractComponentConfiguratorTest.java

  15 
vote

public void testComponentConfigurationWhereFieldIsEnum() throws Exception {
  final String xml="<configuration>" + "  <simpleEnum>TYPE</simpleEnum>" + "  <nestedEnum>ONE</nestedEnum>"+ "</configuration>";
  final PlexusConfiguration configuration=PlexusTools.buildConfiguration("<Test>",new StringReader(xml));
  final ComponentWithEnumFields component=new ComponentWithEnumFields();
  configureComponent(component,configuration);
  assertEquals(ElementType.TYPE,component.getSimpleEnum());
  assertEquals(ComponentWithEnumFields.NestedEnum.ONE,component.getNestedEnum());
}
 

Example 19

From project spring-retry, under directory /src/main/java/org/springframework/classify/util/.

Source file: MethodInvokerUtils.java

  15 
vote

/** 
 * Create  {@link MethodInvoker} for the method with the provided annotationon the provided object. Annotations that cannot be applied to methods (i.e. that aren't annotated with an element type of METHOD) will cause an exception to be thrown.
 * @param annotationType to be searched for
 * @param target to be invoked
 * @return MethodInvoker for the provided annotation, null if none is found.
 */
public static MethodInvoker getMethodInvokerByAnnotation(final Class<? extends Annotation> annotationType,final Object target){
  Assert.notNull(target,"Target must not be null");
  Assert.notNull(annotationType,"AnnotationType must not be null");
  Assert.isTrue(ObjectUtils.containsElement(annotationType.getAnnotation(Target.class).value(),ElementType.METHOD),"Annotation [" + annotationType + "] is not a Method-level annotation.");
  final Class<?> targetClass=(target instanceof Advised) ? ((Advised)target).getTargetSource().getTargetClass() : target.getClass();
  if (targetClass == null) {
    return null;
  }
  final AtomicReference<Method> annotatedMethod=new AtomicReference<Method>();
  ReflectionUtils.doWithMethods(targetClass,new ReflectionUtils.MethodCallback(){
    public void doWith(    Method method) throws IllegalArgumentException, IllegalAccessException {
      Annotation annotation=AnnotationUtils.findAnnotation(method,annotationType);
      if (annotation != null) {
        Assert.isNull(annotatedMethod.get(),"found more than one method on target class [" + targetClass.getSimpleName() + "] with the annotation type ["+ annotationType.getSimpleName()+ "].");
        annotatedMethod.set(method);
      }
    }
  }
);
  Method method=annotatedMethod.get();
  if (method == null) {
    return null;
  }
 else {
    return new SimpleMethodInvoker(target,annotatedMethod.get());
  }
}