0

I am using JUnit4 and I am trying to set up a test that can be used for multiple classes that are identical(not important why they all are), but I am passing in multiple java files to the test and from that i am trying to create objects that have both the .class and the name of a method in the method eg. list.add(new Object[]{testClass.class, testClass.class.methodName()}); It works fine if you enter the name of the .class and the name of the method exactly as is(as in the example above) but as I want to do this for a number of different classes I need to pass them in in a loop and i am using the following code list.add(new Object[]{currentFile.getClass(), currentFile.getClass().getMethod(addTwoNumbers,int, int)} where currentFile is the current file being processed and .getMethod(addTwoNumbers,int, int) addTwoNumbers is the name of the method which takes two ints eg. addTwoNumbers(int one, int two) but I am getting the following error

'.class' expected

'.class' expected

unexpected type
required: value
found:    class

unexpected type
required: value
found:    class

Here is my full code

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;
    }

Does anyone see what I am doing wrong with this line list.add(new Object[]{currentFile.getClass(), currentFile.getClass().getMethod(addTwoNumbers,int, int)}); }?

newSpringer
  • 1,018
  • 10
  • 28
  • 44
  • 1
    currentFile.getClass() returns the `java.io.File` class, not whatever class is contained in that classfile. – Alex Aug 02 '12 at 15:48
  • all the files that are returned by `retrieveFiles.listFilesForFolder(folder);` are all .java files – newSpringer Aug 02 '12 at 15:50
  • That doesn't matter. The getClass() method returns the class of the *object* that you're calling it on. In this case, currentFile is an instance of java.io.File, so that's what is returned by getClass(). Calling it this way will never get the results you're trying to get. – Alex Aug 02 '12 at 15:54
  • oh right, sorry i read your comment wrong... do you know how to get the `.class` file from a `.java` file so? – newSpringer Aug 02 '12 at 15:55

1 Answers1

1

I believe that you need to load the file first using a ClassLoader and then create it so you can use reflection on the class. Here's a similar post with an answer that has more info on this. How to load an arbitrary java .class file from the filesystem and reflect on it?

Here's some more info on this:

A Look At The Java Class Loader

Dynamic Class Loading and Reloading in Java

And here's a quick example using 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) {
}

the example was taken from:

Loading a Class That Is Not on the Classpath

Community
  • 1
  • 1
Samuel Lopez
  • 2,360
  • 6
  • 29
  • 39