Java `InvocationTargetException` class 通过反射实例化
Java `InvocationTargetException` with class instantiation by reflection
我遇到的问题是:我有一系列 classes 是通过注释收集的。它们都位于同一个文件夹中,如果它们具有特定的注释,它们将通过 Reflections
library 实例化。当这些 classes 被实例化时,有一个静态初始化器调用静态工厂,它构建了一些结构。 Java 将在尝试获取工厂创建的对象时抛出 InvocationTargetException
错误。更具体地说,当我输出 ITE
的堆栈跟踪时,它直接指向向工厂请求对象的静态初始化程序。
下面是我用来重现问题的代码。
我有一个注释:InferenceRule.java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface funRule {
String ruleName();
String ruleType();
String analyze() default "node";
}
然后我将该注释应用到包 inference.rules
:
中的一些 classes
@InferenceRule(ruleName = "assign", ruleType = "term")
public class Assign extends NodeAnalyzer {
public Assign() {super();}
public Assign(String... args) { super(args); }
public Rule gatherAllCOnstraints(InstructionNode node) {
// use the Identifier object here.
}
// rest of class here
}
NodeAnalyzer
class,上面Assign
class的super:
public abstract class NodeAnalyzer {
protected Identifier identifier;
protected NodeAnalyzer() {
// Construct things here
}
protected NodeAnalyzer(String... args) {
// Construct other things here
}
// Construct common things here
{
this.identifier = IdentifierFactory.getIdentifier();
}
// rest of class here
}
Assign
class实例化在Inference
class中,如下所述:
public class Inference {
public final String NODE_ANALYSIS = "NODE";
public static final String INFERENCE_PACKAGE = "inference.rules";
private final Map<String, NodeAnalyzer> nodeAnalyzer = new HashMap<>();
private final Map<String, EdgeAnalyzer> edgeAnalyzer = new HashMap<>();
public Inference() {
}
// other non-interesting things here
private void loadRules() {
Reflections reflection = new Reflections(INFERENCE_PACKAGE);
Set<Class<?>> annotated = reflection.getTypesAnnotatedWith(InferenceRule.class);
for(Class<?> clazz : annotated) {
try {
String name = clazz.getAnnotation(InferenceRule.class).ruleName();
String type = clazz.getAnnotation(InferenceRule.class).ruleType();
String analyze = clazz.getAnnotation(InferenceRule.class).analyze();
if (StringUtils.equalsIgnoreCase(analyze, NODE_ANALYSIS)) {
final NodeAnalyzer newInstance = (NodeAnalyzer) clazz.getConstructor(InferenceType.class).newInstance(InferenceType.valueOf(type));
this.nodeAnalyzer.put(name, newInstance);
}
// handle other cases...
} catch(InvocationTargetException ite) {
// For debugging, only
ite.printStackTrace();
logger.error(ite.getCause.getMessage());
logger.error(ite.getTargetException.getMessage());
}
}
}
}
可以看到,从Assign
和NodeAnalyzer
中的实例化路径来看,肯定是调用了IdentifierFactory
class:
public class IdentifierFactory {
private static final Identifier identifier;
static {
if (ConfigFactory.getConfig().isDebEnabled()) {
identifier = new DBIdentifier();
} else {
identifier = new NaiveIdentifier();
}
}
public static Identifier getIdentifier() {
return identifier;
}
}
NaiveIdentifier
class:
public class NaiveIdentifier {
private Set<Integer> unknowns = new HashSet<Integer>() {{
unknowns.add(0);
// add more here.
};
public NaiveIdentifier() {} // empty default constructor
}
ConfigFactory
class 遵循与 IdentifierFactory
class 类似的模式。它根据特定输入构建配置。
抛出的确切异常如下所示:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at phases.inference.Inference.loadRules(Inference.java:197)
at phases.inference.Inference.<init>(Inference.java:76)
at phases.PhaseFacade$PHASES.getPhase(PhaseFacade.java:27)
at phases.PhaseFacade.<init>(PhaseFacade.java:42)
at compilation.Compiler.runPhases(Compiler.java:126)
at compilation.Compiler.runAllOps(Compiler.java:118)
at Main.main(Main.java:45)
Caused by: java.lang.ExceptionInInitializerError
at phases.inference.rules.NodeAnalyzer.<init>(NodeAnalyzer.java:35)
at phases.inference.rules.Assign.<init>(Assign.java:22)
... 11 more
Caused by: java.lang.NullPointerException
at typesystem.identification.NaiveIdentifier.<init>(NaiveIdentifier.java:23)
at typesystem.identification.NaiveIdentifier.<init>(NaiveIdentifier.java:22)
at typesystem.identification.IdentifierFactory.<clinit>(IdentifierFactory.java:25)
... 13 more
和:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at phases.inference.Inference.loadRules(Inference.java:197)
at phases.inference.Inference.<init>(Inference.java:76)
at phases.PhaseFacade$PHASES.getPhase(PhaseFacade.java:27)
at phases.PhaseFacade.<init>(PhaseFacade.java:42)
at compilation.Compiler.runPhases(Compiler.java:126)
at compilation.Compiler.runAllOps(Compiler.java:118)
at Main.main(Main.java:45)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class typesystem.identification.IdentifierFactory
at phases.inference.rules.NodeAnalyzer.<init>(NodeAnalyzer.java:35)
at phases.inference.rules.Assign.<init>(Assign.java:18)
... 11 more
从这些,我无法充分辨别根本原因是什么。更复杂的是,我尝试 运行 使用其他输入文件,它在这些文件上工作得很好。
这个代码
public class NaiveIdentifier {
private Set<Integer> unknowns = new HashSet<Integer>() {{
unknowns.add(0);
// add more here.
}}; // ( <- added missing brace here)
public NaiveIdentifier() {} // empty default constructor
}
正在使用“双花括号初始化”反模式。通常,这种反模式用于节省源代码中的一些输入:
public class NaiveIdentifier {
private Set<Integer> unknowns = new HashSet<Integer>() {{
// yeah, we saved writing the nine characters "unknowns."
add(0);
// add more here.
}};
public NaiveIdentifier() {} // empty default constructor
}
以创建集合 class 的新子 class 为代价,并可能造成内存泄漏,因为内部 class 持有对其外部 class 实例的引用, 如 this Q&A.
中所讨论
具有讽刺意味的是,您没有省略字符 unknowns.
,因此不仅没有利用这种反模式,还造成了这个错误,因为您正在访问应该初始化的字段使用集合的构造函数中构造的集合实例。也就是说,你的代码等价于下面的代码:
public class NaiveIdentifier {
private Set<Integer> unknowns;
{
Set<Integer> temp = new HashSet<Integer>() {{
unknowns.add(0);
// add more here.
}};
unknowns = temp;
}
public NaiveIdentifier() {} // empty default constructor
}
这很清楚,为什么此代码会因 NullPointerException
.
而失败
您可以通过始终如一地使用反模式来解决此问题,即删除 unknowns.
字符以更改外部实例字段对 superclass 调用的访问(如上面的第二个代码示例), 但是,既然字符在那里,您可以轻松更改代码以使用没有反模式的干净初始化程序:
public class NaiveIdentifier {
private Set<Integer> unknowns = new HashSet<Integer>();
{
unknowns.add(0);
// add more here.
}
public NaiveIdentifier() {} // empty default constructor
}
当使用单花括号时,您并不是在创建 HashSet
的内部 class subclass,而只是定义一个将添加到 [= 的构造函数中的初始值设定项19=],以预期的程序文本顺序执行,首先是初始化程序 unknowns = new HashSet<Integer>()
,然后是 unknowns.add(…);
语句。
对于简单的初始化语句,您可以考虑替代方案
public class NaiveIdentifier {
private Set<Integer> unknowns = new HashSet<>(Arrays.asList(0, 1, 2, 3 …));
public NaiveIdentifier() {} // empty default constructor
}
我遇到的问题是:我有一系列 classes 是通过注释收集的。它们都位于同一个文件夹中,如果它们具有特定的注释,它们将通过 Reflections
library 实例化。当这些 classes 被实例化时,有一个静态初始化器调用静态工厂,它构建了一些结构。 Java 将在尝试获取工厂创建的对象时抛出 InvocationTargetException
错误。更具体地说,当我输出 ITE
的堆栈跟踪时,它直接指向向工厂请求对象的静态初始化程序。
下面是我用来重现问题的代码。
我有一个注释:InferenceRule.java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface funRule {
String ruleName();
String ruleType();
String analyze() default "node";
}
然后我将该注释应用到包 inference.rules
:
@InferenceRule(ruleName = "assign", ruleType = "term")
public class Assign extends NodeAnalyzer {
public Assign() {super();}
public Assign(String... args) { super(args); }
public Rule gatherAllCOnstraints(InstructionNode node) {
// use the Identifier object here.
}
// rest of class here
}
NodeAnalyzer
class,上面Assign
class的super:
public abstract class NodeAnalyzer {
protected Identifier identifier;
protected NodeAnalyzer() {
// Construct things here
}
protected NodeAnalyzer(String... args) {
// Construct other things here
}
// Construct common things here
{
this.identifier = IdentifierFactory.getIdentifier();
}
// rest of class here
}
Assign
class实例化在Inference
class中,如下所述:
public class Inference {
public final String NODE_ANALYSIS = "NODE";
public static final String INFERENCE_PACKAGE = "inference.rules";
private final Map<String, NodeAnalyzer> nodeAnalyzer = new HashMap<>();
private final Map<String, EdgeAnalyzer> edgeAnalyzer = new HashMap<>();
public Inference() {
}
// other non-interesting things here
private void loadRules() {
Reflections reflection = new Reflections(INFERENCE_PACKAGE);
Set<Class<?>> annotated = reflection.getTypesAnnotatedWith(InferenceRule.class);
for(Class<?> clazz : annotated) {
try {
String name = clazz.getAnnotation(InferenceRule.class).ruleName();
String type = clazz.getAnnotation(InferenceRule.class).ruleType();
String analyze = clazz.getAnnotation(InferenceRule.class).analyze();
if (StringUtils.equalsIgnoreCase(analyze, NODE_ANALYSIS)) {
final NodeAnalyzer newInstance = (NodeAnalyzer) clazz.getConstructor(InferenceType.class).newInstance(InferenceType.valueOf(type));
this.nodeAnalyzer.put(name, newInstance);
}
// handle other cases...
} catch(InvocationTargetException ite) {
// For debugging, only
ite.printStackTrace();
logger.error(ite.getCause.getMessage());
logger.error(ite.getTargetException.getMessage());
}
}
}
}
可以看到,从Assign
和NodeAnalyzer
中的实例化路径来看,肯定是调用了IdentifierFactory
class:
public class IdentifierFactory {
private static final Identifier identifier;
static {
if (ConfigFactory.getConfig().isDebEnabled()) {
identifier = new DBIdentifier();
} else {
identifier = new NaiveIdentifier();
}
}
public static Identifier getIdentifier() {
return identifier;
}
}
NaiveIdentifier
class:
public class NaiveIdentifier {
private Set<Integer> unknowns = new HashSet<Integer>() {{
unknowns.add(0);
// add more here.
};
public NaiveIdentifier() {} // empty default constructor
}
ConfigFactory
class 遵循与 IdentifierFactory
class 类似的模式。它根据特定输入构建配置。
抛出的确切异常如下所示:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at phases.inference.Inference.loadRules(Inference.java:197)
at phases.inference.Inference.<init>(Inference.java:76)
at phases.PhaseFacade$PHASES.getPhase(PhaseFacade.java:27)
at phases.PhaseFacade.<init>(PhaseFacade.java:42)
at compilation.Compiler.runPhases(Compiler.java:126)
at compilation.Compiler.runAllOps(Compiler.java:118)
at Main.main(Main.java:45)
Caused by: java.lang.ExceptionInInitializerError
at phases.inference.rules.NodeAnalyzer.<init>(NodeAnalyzer.java:35)
at phases.inference.rules.Assign.<init>(Assign.java:22)
... 11 more
Caused by: java.lang.NullPointerException
at typesystem.identification.NaiveIdentifier.<init>(NaiveIdentifier.java:23)
at typesystem.identification.NaiveIdentifier.<init>(NaiveIdentifier.java:22)
at typesystem.identification.IdentifierFactory.<clinit>(IdentifierFactory.java:25)
... 13 more
和:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at phases.inference.Inference.loadRules(Inference.java:197)
at phases.inference.Inference.<init>(Inference.java:76)
at phases.PhaseFacade$PHASES.getPhase(PhaseFacade.java:27)
at phases.PhaseFacade.<init>(PhaseFacade.java:42)
at compilation.Compiler.runPhases(Compiler.java:126)
at compilation.Compiler.runAllOps(Compiler.java:118)
at Main.main(Main.java:45)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class typesystem.identification.IdentifierFactory
at phases.inference.rules.NodeAnalyzer.<init>(NodeAnalyzer.java:35)
at phases.inference.rules.Assign.<init>(Assign.java:18)
... 11 more
从这些,我无法充分辨别根本原因是什么。更复杂的是,我尝试 运行 使用其他输入文件,它在这些文件上工作得很好。
这个代码
public class NaiveIdentifier {
private Set<Integer> unknowns = new HashSet<Integer>() {{
unknowns.add(0);
// add more here.
}}; // ( <- added missing brace here)
public NaiveIdentifier() {} // empty default constructor
}
正在使用“双花括号初始化”反模式。通常,这种反模式用于节省源代码中的一些输入:
public class NaiveIdentifier {
private Set<Integer> unknowns = new HashSet<Integer>() {{
// yeah, we saved writing the nine characters "unknowns."
add(0);
// add more here.
}};
public NaiveIdentifier() {} // empty default constructor
}
以创建集合 class 的新子 class 为代价,并可能造成内存泄漏,因为内部 class 持有对其外部 class 实例的引用, 如 this Q&A.
中所讨论具有讽刺意味的是,您没有省略字符 unknowns.
,因此不仅没有利用这种反模式,还造成了这个错误,因为您正在访问应该初始化的字段使用集合的构造函数中构造的集合实例。也就是说,你的代码等价于下面的代码:
public class NaiveIdentifier {
private Set<Integer> unknowns;
{
Set<Integer> temp = new HashSet<Integer>() {{
unknowns.add(0);
// add more here.
}};
unknowns = temp;
}
public NaiveIdentifier() {} // empty default constructor
}
这很清楚,为什么此代码会因 NullPointerException
.
您可以通过始终如一地使用反模式来解决此问题,即删除 unknowns.
字符以更改外部实例字段对 superclass 调用的访问(如上面的第二个代码示例), 但是,既然字符在那里,您可以轻松更改代码以使用没有反模式的干净初始化程序:
public class NaiveIdentifier {
private Set<Integer> unknowns = new HashSet<Integer>();
{
unknowns.add(0);
// add more here.
}
public NaiveIdentifier() {} // empty default constructor
}
当使用单花括号时,您并不是在创建 HashSet
的内部 class subclass,而只是定义一个将添加到 [= 的构造函数中的初始值设定项19=],以预期的程序文本顺序执行,首先是初始化程序 unknowns = new HashSet<Integer>()
,然后是 unknowns.add(…);
语句。
对于简单的初始化语句,您可以考虑替代方案
public class NaiveIdentifier {
private Set<Integer> unknowns = new HashSet<>(Arrays.asList(0, 1, 2, 3 …));
public NaiveIdentifier() {} // empty default constructor
}