Java File.getClass().getMethod():如何获取.class和方法
我正在使用JUnit4,并且我正在尝试设置一个测试,该测试可以用于多个相同的类(为什么它们都相同并不重要),但是我正在将多个java文件传递给测试,并从中尝试创建对象,这些对象在方法Java File.getClass().getMethod():如何获取.class和方法,java,file,junit,Java,File,Junit,我正在使用JUnit4,并且我正在尝试设置一个测试,该测试可以用于多个相同的类(为什么它们都相同并不重要),但是我正在将多个java文件传递给测试,并从中尝试创建对象,这些对象在方法eg.list.add(new Object[]{testClass.class,testClass.class.methodName()});如果您输入.class的名称和方法的名称与原样完全相同(如上面的示例所示),则效果很好,但由于我想对许多不同的类执行此操作,我需要在循环中传递它们,并且我使用以下代码list
eg.list.add(new Object[]{testClass.class,testClass.class.methodName()});
如果您输入.class的名称和方法的名称与原样完全相同(如上面的示例所示),则效果很好,但由于我想对许多不同的类执行此操作,我需要在循环中传递它们,并且我使用以下代码list.add(new Object[]{currentFile.getClass(),currentFile.getClass().getMethod(addTwoNumber,int,int)}
其中currentFile
是正在处理的当前文件,.getMethod(addTwoNumber,int,int)
addTwoNumber是接受两个int的方法的名称,例如addTwoNumber(int-one,int-two)
,但我得到了以下错误
'.class' expected
'.class' expected
unexpected type
required: value
found: class
unexpected type
required: value
found: class
这是我的全部代码
CompilerForm compilerForm = new CompilerForm();
RetrieveFiles retrieveFiles = new RetrieveFiles();
@RunWith(Parameterized.class)
public class BehaviorTest {
@Parameters
public Collection<Object[]> classesAndMethods() throws NoSuchMethodException {
List<Object[]> list = new ArrayList<>();
List<File> files = new ArrayList<>();
final File folder = new File(compilerForm.getPathOfFileFromNode());
files = retrieveFiles.listFilesForFolder(folder);
for(File currentFile: files){
list.add(new Object[]{currentFile.getClass(), currentFile.getClass().getMethod(addTwoNumbers,int, int)});
}
return list;
}
private Class clazz;
private Method method;
public BehaviorTest(Class clazz, Method method) {
this.clazz = clazz;
this.method = method;
}
compilePerform compilePerform=新的compilePerform();
RetrieveFiles RetrieveFiles=新建RetrieveFiles();
@RunWith(参数化的.class)
公共类行为测试{
@参数
公共集合类sandMethods()引发NoSuchMethodException{
列表=新的ArrayList();
列表文件=新的ArrayList();
final File folder=新文件(compilePerform.getPathOfFileFromNode());
files=retrieveFiles.listFilesForFolder(文件夹);
用于(文件当前文件:文件){
添加(新对象[]{currentFile.getClass(),currentFile.getClass().getMethod(addTwoNumber,int,int)});
}
退货清单;
}
私人课堂;
私有方法;
公共行为测试(类类别、方法){
this.clazz=clazz;
这个方法=方法;
}
有没有人看到我在这行list.add(新对象[]{currentFile.getClass(),currentFile.getClass().getMethod(addTwoNumbers,int,int)})中做错了什么;
}
?我认为您需要先使用类加载器加载文件,然后再创建它,这样您就可以在类上使用反射。下面是一篇类似的文章,其中有一个答案,其中有更多关于这方面的信息
以下是有关此的更多信息:
下面是一个使用URLClassLoader的快速示例
// Create a File object on the root of the directory containing the class file
File file = new File("c:\\myclasses\\");
try {
// Convert File to a URL
URL url = file.toURL(); // file:/c:/myclasses/
URL[] urls = new URL[]{url};
// Create a new class loader with the directory
ClassLoader cl = new URLClassLoader(urls);
// Load in the class; MyClass.class should be located in
// the directory file:/c:/myclasses/com/mycompany
Class cls = cl.loadClass("com.mycompany.MyClass");
} catch (MalformedURLException e) {
} catch (ClassNotFoundException e) {
}
示例取自:
currentFile.getClass()返回
java.io.File
类,而不是该类文件中包含的任何类。通过retrieveFiles.listFilesForFolder(文件夹);
返回的所有文件都是不重要的.java文件。getClass()方法返回调用它的对象的类。这样说永远不会得到你想要的结果。哦,对了,对不起,我看错了你的评论…你知道如何从.java
文件中获取.class
文件吗?