I've asked a similar question but I prematurely accepted an answer. Here it is again.
Context: I'm trying to instantiate the constructor of a class which I imported as a Maven dependency via it's coordinates.
Problem: The problem I have is that the particular constructor of this class, is invisible to me because it has no access modifier associated with it so it is default, meaning I can't access it from outside.
Example: The class I'm trying to use is here:
public class DecisionTableBuilder {
// Notice no access modifier here so it's package-default
DecisionTableBuilder(Log log, File in, File out) {
some stuff ...
}
// public constructor
public DecisionTableBuilder() {}
// Method 1
public void compiler(File schema) {
some stuff ...
}
// Method 2
public void linker(File attribute) {
some stuff ...
}
}
Here is my toplevel in a separate project:
public class TopLevel {
public void testDecisionTableBuilder() {
Constructor<DecisionTableBuilder> constructor = DecisionTableBuilder.class.getDeclaredConstructor(Log.class, File.class, File.class);
constructor.setAccessible(true);
// Default access level
DecisionTableBuilder builder = constructor.newInstance(log, contentDirectory, outputDirectory);
}
}
Comment: This does not seem to work, with the above implementation in my toplevel, constructor and the methods in the class are now all visible to me and I am able to call them but I get an exception thrown at me from ReflectiveCallable abstract method which when invoked, throws the exception from the reflected method, rather than wrapping it in an InvocationTargetException. It seems there is something wrong with the way the class DecisionTableBuilder was instantiated. Hence my question here.
Am I doing this right? Is there another way to reflect a default access modified constructor/method? When I explicitly reflect a constructor, do I also need to reflect the classes methods too?
Any help would be must appreciated. Thanks everyone in advance
Aucun commentaire:
Enregistrer un commentaire