Java reflection not modifying my @Named bean variables
I know shouldn't use reflection, but this is a temporary solution until ...
I have 1:
@Named("PoiBean")
@SessionScoped
public class PoiBean implements ActionContext, Serializable {
private String name = "www";
@EJB
private NavigationServiceRemote nav;
@PostConstruct
private void testReflection() {
try {
nav.TestMe(this);
} catch (NoSuchMethodException ex) {
Logger.getLogger(PoiBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(PoiBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(PoiBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(PoiBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void prepareListAllForm(开发者_运维百科) {
this.setName("test me");
}
}
I have 2:
@Stateless(mappedName="NavigationService")
public class NavigationServiceBean implements NavigationServiceRemote, NavigationContext {
@Override
public void TestMe(ActionContext ctx) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method method = ctx.getClass().getMethod("prepareListAllForm", new Class[] {});
method.invoke(ctx, new Object[] {});
}
Explains: when PoiBean starts, EJB nav is injected, after that in @PostConstruct I call test method TestMe.
When I debug, before Test me name=www, inside PoiBean::prepareListAllForm (called by reflection), name variable is modified = "test me", and after return the name returns to www.
Is like reflection calls prepareListAllForm on a copy of PoiBean ...
What I am trying to achieve now is to modify that variable using prepareListAllForm function, called using reflection from an @EJB.
Is NavigationServiceRemote annotated @Remote? Remote invocation of an EJB interface will marshall/unmarshall the arguments and return values, so the TestMe method will receive a copy of PoiBean. You will need to use a local EJB if you want to modify the instance.
精彩评论