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

Twitter

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

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors