My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians


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.


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;

public @interface DummyStereotype {

A dummy BeanA:

// BeanA
package some.beans;

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

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;

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;

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 {
      if (target != null) {
          System.out.println("target=" + target.value()[0]);
      } else {
  } else {
    System.out.println("No injection point found !!!");

Output for default Annotated#getAnnotation():

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

Output for default Beans#getAnnotation():

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


Niciun comentariu :

Trimiteți un comentariu


Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors