[英]NoSuchMethodException when using reflection
Interface...界面...
package com.filter.tool
public interface Tool extends Resource {
...
List findTools(int[] sizes, int counts);
}
Code...代码...
package com.filter.tool.impl;
class Tool extends Resource implements com.filter.tool{ //default class (not visible)
Tool(Log log, Key key, Def def, Init init, Collection items) {
super(log, key, def, init, null, null, 0, 0, 0, 0, 0, null, null, 0, false, items);
}
...
public List findTools(int[] sizes, int counts) {
....
}
...
}
In toolTest在工具测试中
package com.jtest.tool;
import com.filter.tool
public class ToolTest {
@Test
public void test(){
int[] sizes = {1,2};
int counts = 1;
try {
Class<?> cls = Tool.class;
Constructor<?> constructor = cls.getDeclaredConstructor();
constructor.setAccessible(true);
Tool tool = (Tool) constructor.newInstance();
List pref = tool.findTools(sizes, counts);
System.out.println(pref);
} catch (Exception e) {
e.printStackTrace();
}
}
When I run the test I got ..当我运行测试时,我得到了..
java.lang.NoSuchMethodException: com.filter.tool.<init>()
at java.lang.Class.getConstructor0(Class.java:2715)
at java.lang.Class.getDeclaredConstructor(Class.java:1987)
at com.att.lpp.prov.jtest.resman.impl.PoolTest.test(PoolTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
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:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
But if I change the package name from the toolTest it would warn me and tried to make me change the visibility of the code.但是如果我从 toolTest 更改包名称,它会警告我并试图让我更改代码的可见性。 I dont want to change the code.
我不想更改代码。 Am I missing something?
我错过了什么吗?
First: it seems, due to import in your test class, it tries to look for a constructor for an Interface instance, because com.filter.tool.Tool is the Tool interface, not a class.第一:看起来,由于在您的测试类中导入,它试图寻找接口实例的构造函数,因为 com.filter.tool.Tool 是 Tool 接口,而不是类。 You have to rename your class or interface, to make it clearer or to do something with packages...
你必须重命名你的类或接口,使其更清晰或对包做一些事情......
And the second: you have a constructor with arguments in your Tool class, that means, that you don't have a default one yet.第二个:你的 Tool 类中有一个带参数的构造函数,这意味着你还没有默认的构造函数。 But your test uses reflection to get a default constructor without arguments.
但是您的测试使用反射来获取没有参数的默认构造函数。 You have to modify your test method logic, to make it looking for the constuctor with arguments, for that you have to call getDeclaredConstructor with arguments classes array like:
您必须修改您的测试方法逻辑,使其寻找带参数的构造函数,为此您必须使用参数类数组调用 getDeclaredConstructor,例如:
Class[] cArg = new Class[5];
cArg[0] = Log.class;
cArg[1] = Key.class;
...
Constructor<?> constructor = cls.getDeclaredConstructor(cArg);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.