Olá a todos!
Estou fazendo alguma coisa parecida com uma simulação do que o EJB 3.0 faz, porém estou com o EJB2.1, e não posso migrar.
Resumindo, criei uma annotatio @EJB, e em um arquivo pai, verifica se tem algum atributo com esta anotação, se tiver, busco o EJB do mesmo e instancio nele, porém estou com um problema que não consigo acessar este atributo do objeto filho, e ainda posso ter mais do que um.
segue o código:
for (Field field : getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(EJB.class)) {
for (java.lang.annotation.Annotation annotation : field.getAnnotations()) {
if (annotation instanceof EJB) {
EJB ejb = (EJB) annotation;
\\aqui preciso setar o atributo;
ServiceLocator.getInstance().getRemote(ejb.JNDI(), ejb.EJBHome());
}
}
}
}
Se alguem puder dar alguma ajudar, obrigado!
Galera…
Resolvi de outra forma, e vou reportar aqui soh pra caso alguém tenha o mesmo problema, tenha alguma idéia da solução que utilizei.
Usei Reflection.
Na minha classe, será obrigado o cara implementar um setMeuAtributo.
Assim, na varredura, monto o método e invoco ele, alterando o valor, o que era o objetivo inicial.
Segue o código
for (Field field : getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(EJB.class)) {
for (java.lang.annotation.Annotation annotation : field.getAnnotations()) {
if (annotation instanceof EJB) {
EJB ejb = (EJB) annotation;
String methodEJBName = "set" + ejb.JNDI().toString().substring(ejb.JNDI().toString().indexOf("/") + 1, ejb.JNDI().toString().length());
Class partypes[] = new Class[1];
partypes[0] = field.getType();
try {
Method setEJB = getClass().getMethod(methodEJBName, partypes);
setEJB.invoke(this, field.getType().cast(ServiceLocator.getInstance().getRemote(ejb.JNDI(), ejb.EJBHome())));
} catch (NoSuchMethodException e) {
throw new SpjStrutsException("Método "+ methodEJBName +
" inexistente para classe " + this.getClass().getName() + ".");
}
}
}
}
}
vlw!