Skip to content

Instantly share code, notes, and snippets.

@aparnachaudhary
Last active August 29, 2015 14:02
Show Gist options
  • Save aparnachaudhary/fcf4060f0e4a99a3bed5 to your computer and use it in GitHub Desktop.
Save aparnachaudhary/fcf4060f0e4a99a3bed5 to your computer and use it in GitHub Desktop.
jqassistant CDI Plugin

Concepts

Bean

Managed Bean

  • Concrete POJO

    • No-arg constructor

    • Constructor with @Inject

  • Objects returned by Producers

  • EE Spec

    • Session Bean

    • DataSource, PersistentContext

Injection Points

  • Field Injection

MATCH
     (a:Type)-[:DECLARES]->(member:Field:Cdi:InjectionPoint)
RETURN
     DISTINCT a.fqn AS FieldInjectionTarget
  • Constructor Injection

MATCH
     (a:Type)-[:DECLARES]->(member:Method:Cdi:InjectionPoint)
WHERE member.name = "<init>"
RETURN
     a.fqn AS ConstructorInjectionTarget
  • Parameter Injection

    • Setter Methods

MATCH
     (a:Type)-[:DECLARES]->(member:Method:Cdi:InjectionPoint)
RETURN
     a.fqn AS InjectionTarget, member.name AS SetterInjectionPoint
  • Initializer Method

  • Observer, Producer, Disposer Methods

Injection Targets

POJOs, Session Beans, Servlets

Bean Scopes

ApplicationScoped, RequestScoped, SessionScoped, ConversationScoped, Dependent

Alternative Bean

Specialized Bean

Stereotype

Qualifiers

Producer Methods

A producer method may be declared by annotating a method with the @javax.enterprise.inject.Produces annotation.

Specialized Producer Methods

Producer Fields

Disposer Methods

Disposer Fields

Decorators

Interceptors

Container Lifecycle Events

CDI Event Producer

MATCH
     (a:Type)-[:DECLARES]->(member:Field:Cdi:InjectionPoint)-[:OF_TYPE]->(injectType:Type)
WHERE
     injectType.fqn = "javax.enterprise.event.Event"
RETURN
     a.fqn AS CdiEventProducer

CDI Event Consumer

MATCH
     (a:Type)-[:DECLARES]->(member:Method)-[:HAS]->(p:Parameter)-[:ANNOTATED_BY]->()-[:OF_TYPE]->(injectType:Type)
WHERE injectType.fqn = "javax.enterprise.event.Observes"
RETURN
      a.fqn AS CdiEventConsumer

CDI Singleton

Singleton scoped classes

MATCH
	(t:Type)-[:ANNOTATED_BY]->()-[:OF_TYPE]->(scopeType:Type)
WHERE
	scopeType.fqn="javax.inject.Singleton"
RETURN
	t.fqn AS cdiSingleton

Methods producing singletons

MATCH
	(t:Type)-[:DECLARES]->(m:Method),
	(m)-[:ANNOTATED_BY]->()-[:OF_TYPE]->(scopeType:Type)
WHERE
	scopeType.fqn="javax.inject.Singleton"
RETURN
	m.signature, t.fqn AS cdiSingletonProducer

Constraints

Avoid Bean Names in injection points

@Inject @Named ( "ordersBean" )
private OrdersBean orderBean ;
MATCH
  (t:Type)-[:DECLARES]->(member:Cdi:InjectionPoint),
  (member)-[:ANNOTATED_BY]->()-[:OF_TYPE]->(a:Type)
WHERE
  a.fqn = "javax.inject.Named"
RETURN
  distinct t.fqn AS invalidBean

Avoid setter injection

@WebServlet(urlPatterns = "/itemServlet")
public class ItemServlet extends HttpServlet {

    private NumberGenerator numberGenerator;
    private ItemEJB itemEJB;

    @Inject
    public void setNumberGenerator(@ThirteenDigits NumberGenerator numberGenerator) {
        this.numberGenerator = numberGenerator;
    }

    @Inject
    public void setItemEJB(ItemEJB itemEJB) {
        this.itemEJB = itemEJB;
    }
    ...
}

Avoid Qualifier Hell

Use Enums on qualifiers if multiple managed bean implementations are available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment