Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I can't figure out, how to use getDeclaredMethods() (reflection) if the user chooses the file from GUI.
Something like this:
Java
Class inclass= Class.forName("ClassName");
Method[] methods = inclass.getDeclaredMethods();

But I don't know "ClassName", I get it later.

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.

additional information copied from comment below
So my program would work like this: it starts, the user chooses a file (txt or java), when this happens the program gets the file's text into an Arraylist, and list's the file's methods and fields on a JList and displays them.
But the inputfile.getClass() won't work, since it's not an object, and I don't know how to get the input file's class, if I don't know the class name before the program starts.

Thank you for helping me.
Posted
Updated 6-Nov-12 1:37am
v2
Comments
Sergey Alexandrovich Kryukov 5-Nov-12 19:37pm    
You need to explain your scenario in sufficient detail. Why later? What do you have to hook up the class then?
--SA
pasztorpisti 5-Nov-12 19:45pm    
Your description isn't clear so there is no chance to answer it. If you want the class of a java object then every object has a getClass() method.
TorstenH. 6-Nov-12 1:44am    
declared methods from the input file

what>'s that supposed to be? Are you working via a network?
AllainLG 6-Nov-12 6:23am    
So my program would work like this: it starts, the user chooses a file (txt or java), when this happens the program gets the file's text into an Arraylist, and list's the file's methods and fields on a JList and displays them.
But the inputfile.getClass() won't work, since it's not an object, and I don't know how to get the input file's class, if I don't know the class name before the program starts.

Thank you for helping me.

1 solution

OKOK. This is basically more text format thingy. You need to parse the text depending on patterns.

http://docs.oracle.com/javase/tutorial/java/data/comparestrings.html[^]

EDIT: I found a link that describes a technique on how to do that:
http://www.rgagnon.com/javadetails/java-0420.html[^]
 
Share this answer
 
v2
Comments
AllainLG 6-Nov-12 9:32am    
Thank you, but if I copy these methods to my main class, then only change the "new MyTest().doit();" to "new MyProgram().doit();"
it will list only the methods I copied, and none of my original class, it doesn't recognize any of them.
And still don't know how to get the input file's class name.
TorstenH. 6-Nov-12 12:52pm    
Can you show us your current development state? We might be able to show you a little tweak to get it working.
I'm asking cause this sounds to me like a homework task. that would require code for us to help you along.
AllainLG 6-Nov-12 15:40pm    
You are right, but the names are not in english, it would be difficult to understand.
I thing only these codes matters:

public class MyProgram {
public static void fileReading() {
lista = new ArrayList < String > ();
try {
inp = new LineNumberReader(new BufferedReader(new InputStreamReader(new FileInputStream(inFileNev), "ISO8859-1")));
String sor;
while ((sor = inp.readLine()) != null) {
lista.add(sor);
lista.add(System.getProperty("line.separator"));
}
inp.close();
} catch...


public static void beolv_osztaly_metodusok() {
try {
Class c= Class.forName("ClassName");
Method[] metodusok = c.getDeclaredMethods();

......


The rest is irrelevant, since the problem is this: I can't get the input file's class, without this I can't get the methods from it.
It works if the ClassName is my program's name.
AllainLG 7-Nov-12 12:29pm    
What about javax.tools.JavaCompiler? Is it useful to me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900