vendredi 12 février 2016

Invoke a singleton method with reflection in Java

Having an issue invoking my singleton with reflection in Java. Here's the code:

The class to be invoked:

public class Config {

private volatile static Config instance = null;

private Config() {...} // truncated for brevity

public static Config get() throws IllegalStateException {
    if(instance == null && ParseConfig.getRunStatus()) {
        instance = ParseConfig.config;
    } else if(instance == null && !ParseConfig.getRunStatus()) {
        throw new IllegalStateException("...");
    }

    return instance;
}

and here's the class that's invoking the Config singleton

...
private static final Config CONFIG = Config.get();

Method givePartialCredit = null;
Boolean state = false;

try {
    givePartialCredit = Config.class.getMethod("get" + c + "GivePartialCredit", String.class);
} catch(NoSuchMethodException e) {
    ReportGenerator.write(CONFIG.getAdminReportingErrorLogName(), e.getMessage());
    ReportGenerator.writeStacktrace(CONFIG.getAdminReportingErrorLogName(), "", e.getStackTrace());
}

if(givePartialCredit != null) {
    for(String file : files) {
        try {
            file = file.substring(0, file.lastIndexOf('.') - 1);
            givePartialCredit.invoke(CONFIG, file);
        } catch(InvocationTargetException e) {
            ReportGenerator.write(CONFIG.getAdminReportingErrorLogName(), "Caused by " + e.getCause().toString());
            ReportGenerator.write(CONFIG.getAdminReportingErrorLogName(), e.getMessage());
            ReportGenerator.writeStacktrace(CONFIG.getAdminReportingErrorLogName(), "", e.getStackTrace());
        } catch(IllegalAccessException e) {
            ReportGenerator.write(CONFIG.getAdminReportingErrorLogName(), e.getMessage());
            ReportGenerator.writeStacktrace(CONFIG.getAdminReportingErrorLogName(), "", e.getStackTrace());
        }
    }
}
...

I did some poking around SO and some forums and I found these:

The last Coderanch forum thread seems to answer my question:

get the getInstance() method and invoke it with null (because it's a static method) as the first argument.

But I don't what they mean by this... I'm not trying to invoke get(), I need to somehow chain the method so I get Config.get().getArgsGivePartialCredit(). I played around with various configurations:

 givePartialCredit.invoke(CONFIG, file);
 givePartialCredit.invoke(Config.get(), file);
 givePartialCredit.invoke(null, file);

but I end up with a NullPointerException which leads me to believe, as an example, I'm calling getArgsGivePartialCredit() on null which is not what I want.

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at javaGrader.pointManager.PointManager.createMapWithRMInstances(PointManager.java:181)
    at javaGrader.visitor.Visitor.createNewDeductionTable(Visitor.java:102)
    at javaGrader.visitor.Visitor.visitFile(Visitor.java:63)
    at javaGrader.visitor.Visitor.visitFile(Visitor.java:23)
    at java.nio.file.Files.walkFileTree(Files.java:2670)
    at java.nio.file.Files.walkFileTree(Files.java:2742)
    at javaGrader.visitor.FileVisitor.visitFiles(FileVisitor.java:45)
    at javaGrader.visitor.FileVisitor.getFiles(FileVisitor.java:35)
    at javaGrader.visitor.FileVisitor.getFiles(FileVisitor.java:25)
    at javaGrader.JavaGrader.main(JavaGrader.java:66)
Caused by: java.lang.NullPointerException
    at javaGrader.config.Config.getArgsGivePartialCredit(Config.java:359)
    ... 14 more

Thank you!





Aucun commentaire:

Enregistrer un commentaire