java.lang.reflect.Constructor parameter names
Good morning everybody
I'm using reflection to get instances of my classes.
I have to read the paramter names out of the Constructor.
say I have a class Test.java and it has constructor
public Test(int column,int line){
this.column = column;
this.line = line;
}
There is a method within Constructor to return the parameter types, but I need the parameter names. In this case this would be {column, index}.
Do you have any idea?
Do you need more information?
Thank you very much!
Stefan,
Sorry mate, I don't think the argument names are accessible via reflections... I guess the only way you'll get the names is by parsing the original source code... which might be doable in an open source product, but is definitely butt ugly.
Why do you need the names anyway? why not just generate names like "int1" and "int2" ? Or are you attempting to infer the attribute name from the parameter name in order to regenerate the original constructor code?
It's an interesting little problem.
Keith.
Hello Keith,
I try to help a friend of mine, she needs a small program.
It will be a small GUI based program.
I want to make the program configurable (extendable) from outside using XML.
I'm not expierenced in XML, but I found a way do access and parse it via javax.xml.parsers.
Want I want to end up with that I give my friends a list of tags which represent the parameters in a class.
Thing is, the parameters in the XML file can change position, but in my code they dont. And since my Parameters are for example all "int" I cant do it via the Parameter type and ParsingExceptions.
Can you understand what the problem is. At the moment I got to say to my friend "Never change the order of the parameters". I dont want to do that.
Greetings,
Stefan
Stefan,
I've just had another look at the reflections API doco and sun's tutorial at http://java.sun.com/docs/books/tutorial/reflect/member/methodType.html
I really think the only way you'll get the parameter names is by parsing the source code... which in your situation is doable, if ugly.
You can extract the method definitions from your source code using a regex something like ^[ \t]*(public |protected |private |static).*\([^;]*$
You just need to package your source code in with the compiled classes in the released jar file... which is an increasingly common practice in open source products.
Message was edited by: corlettk - typo's again.