joi, 5 februarie 2015

[OmniFaces utilities (2.0)] Get program element annotation of a certain annotation type


[OmniFaces utilities] The getAnnotation() get program element annotation of a certain annotation type. The difference with Annotated#getAnnotation(Class) is that this method will recursively search inside all Stereotype annotations.

Method:
Usage:

This is a dummy example, but you should get the idea from it. First, we have defined a dummy CDI stereotype:

// DummyStereotype
package some.beans;

import static java.lang.annotation.ElementType.FIELD;

import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
import javax.enterprise.inject.Stereotype;

@Stereotype
@Target(FIELD)
@Retention(RUNTIME)
public @interface DummyStereotype {
}

A dummy BeanA:

// BeanA
package some.beans;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class BeanA {   
}

An injection point of BeanA in BeanB with dummy stereotype:

// BeanB
package some.beans;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@RequestScoped
public class BeanB {
   
    @Inject @DummyStereotype
    BeanA a;
   
}

A dummy BeanC with a simple method dedicated to test the default Annotated#getAnnotation() and the OmniFaces, Beans#getAnnotation():

// BeanC
package some.beans;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.Set;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Named;
import org.omnifaces.util.Beans;

@Named
@RequestScoped
public class BeanC {

 public void inspectAnnotations() {

  Bean<BeanB> beanBresolved = Beans.resolve(BeanB.class);
  Set<InjectionPoint> injectionPoints = beanBresolved.getInjectionPoints();

  if (injectionPoints.size() > 0) {
      beanBresolved.getInjectionPoints().stream().forEach((candidate) -> {
      Annotated annotated = candidate.getAnnotated();
               
      System.out.println("All annotations: " + annotated.getAnnotations());

      // default Annotated#getAnnotation()
      //Retention retention = annotated.getAnnotation(Retention.class);
      //Target target = annotated.getAnnotation(Target.class);
               
      //OmniFaces Beans#getAnnotation()
      Retention retention = Beans.getAnnotation(annotated, Retention.class);
      Target target = Beans.getAnnotation(annotated, Target.class);

      if (retention != null) {
          System.out.println("retention=" + retention.value().name());
      } else {
          System.out.println("NULL");
      }
      if (target != null) {
          System.out.println("target=" + target.value()[0]);
      } else {
          System.out.println("NULL");
      }
    });
  } else {
    System.out.println("No injection point found !!!");
  }
 }
}

Output for default Annotated#getAnnotation():

All annotations: [@javax.inject.Inject(), @some.beans.DummyStereotype()]
NULL
NULL

Output for default Beans#getAnnotation():

All annotations: [@javax.inject.Inject(), @some.beans.DummyStereotype()]
retention=RUNTIME
target=FIELD

API GH

Niciun comentariu:

Trimiteți un comentariu