我有一些从硬盘驱动器读取的工厂课程。
这些工厂没有公共构造函数,它们有 2 个私有构造函数:一个用于反射的构造函数,一个用于创建的构造函数。
当只有一个公共构造函数时,这段代码可以工作,但它会抛出一个奇怪的异常:
public static void setupCalculators()
{
String calculatorsBasePackage = "feature.calculators";
ArrayList<String> res;
try
{
res = ReflectionHelper.getClassNamesFromPackage(calculatorsBasePackage);
for (String file : res)
{
file = file.replace("/", "");
Class<?> klass = Class.forName(calculatorsBasePackage + "." + file);
//The exception happens on the following line:
Constructor<?> c = klass.getDeclaredConstructor((Class[])null);
c.setAccessible(true);
ICalculatorFactory calculatorFactory =
(ICalculatorFactory) c.newInstance((Object[])null);
addCalculator(file.replace("_Calculator", ""), calculatorFactory);
}
}
catch (IOException | ClassNotFoundException | SecurityException |
IllegalArgumentException | InstantiationException |
IllegalAccessException | InvocationTargetException |
NoSuchMethodException e)
{
e.printStackTrace();
}
}
这是要检查的类:
private POS_F_Calculator()
{
}
private POS_F_Calculator()
{
}
private POS_F_Calculator(StructureRepresentation representation)
{
this.representation = representation;
}
@Override
public ICalculator newCalculator(IRepresentation representation)
{
return new POS_F_Calculator((StructureRepresentation) representation);
}
这是抛出的异常:
java.lang.NoSuchMethodException: feature.calculators.POS_F_Calculator$1.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at feature.CalculatorHandler.setupCalculators(CalculatorHandler.java:37)
at driver.Driver.main(Driver.java:36)
我试图弄清楚我做错了什么。这个例外是什么意思?