2

I can't figure out, how to use getDeclaredMethods() (reflection) if the user chooses the file from GUI.

inFile is File type, and I get it when the user chooses the file from GUI.

public static void read_file_methods () {  

   Class in_class= inFile.getClass();     
   Method[] methods = in_class.getDeclaredMethods();

....
}

I can't get the input file's class, just object's, but how do I get the object from input? I can't use MyProgram m = new MyProgram(); ... and don't know how to use .newInstance() to be working.

"I'd like to get the declared methods from the input file, and then list them in my GUI. The user can choose a txt or java file, when he/she did, the program will get the methods from this (if there are any) and list them on a jList. Works if I know the Object's name, but not if it comes later."

Pjrat111
  • 213
  • 1
  • 3
  • 9
AllainLG
  • 108
  • 1
  • 1
  • 8
  • What do you want to achieve? You display a file chooser, the user chooses a file. Which type of file? What do you want to do next with the file? What does the file contain? – JB Nizet Nov 03 '12 at 17:29
  • I'd like to get the declared methods from the input file, and then list them in my GUI. The user can choose a txt or java file, when he/she did, the program will get the methods from this (if there are any) and list them on a jList. Works if I know the Object's name, but not if it comes later. – AllainLG Nov 03 '12 at 18:45
  • So you would like to open a Java source file, and parse it to discover which methods are declared in the class(es) it contains? You won't be able to do that easily. Are you aware of the difference between a Java source file and a complied Java class file? – JB Nizet Nov 04 '12 at 12:05

1 Answers1

0

Is this what you're asking?

    URLClassLoader loader = URLClassLoader.newInstance(new URL[]{fileName.toURI().toURL()}); //This code creates a new URLClassLoader based on the file that you define

    Class<?> clazz = loader.loadClass("ClassName");  //This loads the class with the given name

    for(Method m: clazz.getDeclaredMethods()) {  //This code will print the names of every method in the class

    System.out.println(m.getName());
    }

From here you can add all of the method names to a JList or whatever UI element you are using to display the methods.

Pjrat111
  • 213
  • 1
  • 3
  • 9
  • Yes, this is, but it's not working. I don't know what to write in "ClassName". I've tried what I could, but nothing helped, so I get this exception: ClassName. Also by fileName you mean the File type inFile I wrote before? (I get this from JFileChooser.getSelectedFile() – AllainLG Nov 03 '12 at 21:40
  • Yes the fileName would be your inFile. Class name would be the name of the class you are trying to load the methods from. If the class is named "Main" then you would put main there. If the application is packaged, then you would need to enter the entire path, like "org.main.Main" – Pjrat111 Nov 03 '12 at 22:43
  • But the class I'm trying to load the methods from is unknown. It comes from the file, the users choose. So I don't know nor the name, nor the package, this is my main problem. – AllainLG Nov 03 '12 at 23:11
  • In that situation I would say unzip the jar and iterate through the files to check the methods in each one. Check if the files extension is ".class" Reference this http://stackoverflow.com/a/1429275/1229874 – Pjrat111 Nov 03 '12 at 23:31
  • I tried everything I could, but still nothing, can't figure out how to get the class name. Couldn't figure out how to use the codes from that topic to be working. – AllainLG Nov 04 '12 at 15:08
  • There's really no good solution to load all the classes from a jar file if the jar file is packaged. – Pjrat111 Nov 04 '12 at 18:46
  • And how can I go round this? I don't need the input's package, maybe removing it somehow? Or if I tell the user, that only jar without package can be choosen, then how? – AllainLG Nov 04 '12 at 18:51
  • There's no way to remove the packaging without completely messing up the program you are reflecting on. You're going to have to read the file path of files to determine the package (not sure if that would work well at all). Otherwise, if the program isn't package, then you will have to iterate through all the files in the jar and see if they end in .class. If so, then they're valid class files and you can reflect to finds all of their methods. – Pjrat111 Nov 05 '12 at 22:01
  • I'd like to get the inpt file to an ArrayList, the package name is irrelevant to me. What if I create a temporary file, replace the package name with my program's package, then, when I need it again I put it back? I only need the input's methods and fields. The user can select one file only, I get this to an ArrayList, get a temporary file and change the package name. Would it work for sure? The user chooses txts or java files, so there will be no .class files I can examine. – AllainLG Nov 05 '12 at 22:30
  • What about javax.tools.JavaCompiler? Is it useful to me? – AllainLG Nov 07 '12 at 17:28