mardi 19 janvier 2016

Class instantiation inside EJB

Does the following code violate the EJB 3 specifiation? If so, what can I do about it to retain the desired functionality?

The reason why I'm asking is that IntelliJ 14 produces these warnings:

  • Usage of "java.lang.reflect.Constructor" is not allowed in EJB
  • Usage of "java.lang.reflect.InvocationTargetException" is not allowed in EJB

The class(es) I want to instantiate is no EJB, just a POJO, and the EJB serves as a repository for these POJO's (they are encapsulating business logic).

@Stateless
public class MyBean {
    public SomeInterface createSomeClass(final Class<? extends AbstractSomeClass> someClass, final MyArgument argument) {
        try {
            final Constructor constructor = someClass.getDeclaredConstructor(argument.getClass());
            constructor.setAccessible(true);
            return (SomeInterface) constructor.newInstance(state);
        } catch (InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException e) {
            // TODO fix exception handling
            throw new RuntimeException(e);
        }
    }
}

Thank you for your help.

Regards,

Simon





Aucun commentaire:

Enregistrer un commentaire