如何在不知道输入文件的类的情况下获取它(java)
如果用户从GUI选择文件,我不知道如何使用getDeclaredMethods()(反射).
像这样的东西:
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();
但我不知道"ClassName",我稍后再知道.
我想从输入文件中获取声明的方法,然后在我的GUI中列出它们.用户可以选择一个txt或java文件,当他/她这样做时,程序将从中获取方法(如果有的话)并将它们列出在jList上.
如果我知道对象的名称,则可以使用,但是以后出现时,它不可用.
从下面的评论中复制的其他信息
所以我的程序将像这样工作:启动后,用户选择一个文件(txt或java),当这种情况发生时,程序将文件的文本放入Arraylist中,并列出文件的方法和JList上的字段并显示它们.
但是inputfile.getClass()无效,因为它不是对象,如果我不知道类名,我也不知道如何获取输入文件的类在程序启动之前.
谢谢您的帮助.
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.
确定.这基本上是更多的文本格式.您需要根据模式来解析文本.
http://docs.oracle.com/javase/tutorial/java/data/comparestrings.html [^ ]
我找到了一个链接,该链接描述了如何执行此操作的技术:
http://www.rgagnon.com/javadetails/java-0420.html [ ^ ]
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[^]
I found a link that describes a technique on how to do that:
http://www.rgagnon.com/javadetails/java-0420.html[^]