mardi 8 mars 2016

Having difficulty instantiating new instance of java class with constructor arguments

I have the following line of code in a top level module:

String username = "testuser987-abc-zyx@gmail.com"
ModelBuilder userBuilder = utilityManager.getUtility("user").generateBuilder(username);

Inside my "user" utility class I have the following:

private class UserBuilder extends ModelBuilder {
    private final String username;

    public UserBuilder(String username) {
        this.username = username;
    }

    @Override
    public User execute() {
        return populateUserInformation(username);
    }
}

@Override
public UserBuilder generateBuilder(Object... arguments) throws ReflectiveOperationException {
    try {
        return UserBuilder.class.getConstructor(String.class).newInstance((String) arguments[0]);
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
        throw new ReflectiveOperationException(e);
    }
}

I am trying to use the generateBuilder(Object... arguments) method to return a new instance of my UserBuilder class (as a ModelBuilder), however, when I run the code I get the following exception:

nested exception is java.lang.ReflectiveOperationException: java.lang.NoSuchMethodException:   
com.login.util.UserOperationsUtility$UserBuilder.<init>(java.lang.String)] with root cause
java.lang.NoSuchMethodException: com.login.util.UserOperationsUtility$UserBuilder.<init>(java.lang.String)
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_66]
at java.lang.Class.getConstructor(Class.java:1825) ~[na:1.8.0_66]

I tried to follow the java docs for Constructor, but I'm not sure how to debug this. My knowledge of Java's reflective capabilities is pretty small.





Aucun commentaire:

Enregistrer un commentaire