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 SomeClass {
// Notice no access modifier here so it's package-default
SomeClass(Log log, File in, File out) {
some stuff ...
}
// public constructor
public SomeClass() {}
// 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 testSomeClass() {
Constructor<SomeClass> constructor = SomeClass.class.getDeclaredConstructor(Log.class, File.class, File.class);
constructor.setAccessible(true);
// Default access level
SomeClass builder = constructor.newInstance(log, contentDirectory, outputDirectory);
//Here is where the problem occurs
builder.compiler(schemaFile);
}
}
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
EDIT:
Here is my stack trace, if someone can make sense of this...
java.lang.NullPointerException
at com.intuit.ctg.tla.compilers.SomeClass.compile(SomeClass.java:95)
at com.intuit.ctg.tla.compilers.qa.testSomeClass.testSomeClass(testCompilerLinker.java:80)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
I've followed this stack trace to this line of code:
// This piece of code recursively searches in directory treesDirectory and
// lists all file with xml extentions
Collection<File> flowchartFiles = FileUtils.listFiles(treesDirectory, mapExtension, true);
somehow treesDirectory (type File) is getting a NULL pointer even though I pass this via the constructor, here as the 2nd argument:
public static File treesDirectory = new File("src/test/resources/input_graphs/");
public static File outputDirectory = new File("src/test/resources/output");
public static Log log;
SomeClass builder = constructor.newInstance(log, treesDirectory, outputDirectory);
something funny is happening in the reflected constructor... Anybody have any ideas? I've been on this for 2 days now...