Click here to Skip to main content
11,642,402 members (61,688 online)
Rate this: bad
good
Please Sign up or sign in to vote.
Hi,

I can't figure out, how to use getDeclaredMethods() (reflection) if the user chooses the file from GUI.
Something like this:
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 5-Nov-12 13:02pm
Edited 6-Nov-12 1:37am
Nelek72.1K
v2
Comments
Sergey Alexandrovich Kryukov at 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 at 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. at 6-Nov-12 1:44am
   
declared methods from the input file

what>'s that supposed to be? Are you working via a network?
AllainLG at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
v2
Comments
AllainLG at 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. at 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 at 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 at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100