Hi dears , what are the different ways to make an object of a class that we only have its name (not the whole packaging )by reflection in java? I do need your help!I'll be really grateful for any help! thanks
Hey have you looked through these examples? http://java.sun.com/developer/technicalArticles/ALT/Reflection/ Might have what you're looking for =)
Thanks for your help!it was nice!but I still have problem!you know in my project there is a file in which different lines of codes is written.and I should write a code to open this file and then read it line by line,then like a compiler I should write some lines of codes to compile each line and run it! e.g if I have (Integer i = new Integer(12);) I firstly cut the line into pieces and then I should make an object of the class "Integer" but my problem is that since I cannot get the whole packaging of the class name I don't know how to make an object of that class! thanks again for everything :)
you know using forName() is only possible when I have the whole packaging!
Did you get my problem?
I see your problem, you could just try some common packages to find the class, eg just try load it from java.lang.<classname> or java.until.<classname> could you explain more about what you're trying to do?
I want to open a .java file and try to compile the lines (as I read them from the file) by reflection : e.g I have a line in the file :System.out.println("Hello"); here is what I've written for compiling this line : while ((strLine = br.readLine()) != null) { while (strLine.contains("System")) { Class sc = Class.forName("java.lang.System"); Field of = sc.getField("out"); Object ov = of.get(null); Class oc = ov.getClass(); String input = strLine.substring(strLine.indexOf('(') + 1, strLine.indexOf(')')); Method pm = oc.getMethod("println", String.class); pm.invoke(ov, input); break; } }
br in the code is the bufferReader!
If i'm understanding what your trying to do. it's quite complicated. Basically you're trying to parse java code? ( sorry for the late reply my internet is playing up =( )
Yes!it's exactly what I want to do!I have a txt file in which some java codes are written I want to read them and then interpret them with reflection(like what compilers do I think). For example let this line be witten in my file : Integer in = new Integer(34); I should read the line first then creat an obj of the class Integer and then put 34 as that obj! (No matter indeed !I don't expect u to give all your attention and valuable time to my foolish questions (esp with poor English I have :( )!)
Are you trying to interpret loops of any kind? or just simple objects?
just simple objects
I put my code here
are you doing this as a challenge or need it for something?
it's my unversity's project!I've told I think!
was my answer a right one for your question or I misunderstand your question?
Here have a look at this, i think this is what you're trying to do. it's a little hackish, but hopefully it gives you some ideas
A thousands of thanks!It is really useful indeed!u helped me so much!thanks!=)I just take a look at it but I get most of its parts!
good luck with the rest =) if you have any more troubles just ask
=) thanks fore everything ...
Sorry Nick can I ask U what the problem is with the following lines? String str = " Double d2 = Math.round(12.5)"; Class c = Class.forName("java.lang.Math"); int end = str.indexOf("("); String name = str.substring(str.indexOf(".")+1 , end ); Method m = c.getDeclaredMethod(name); m.setAccessible(true); System.out.println(m.invoke(null));
I got what is wrong with it!thanks!
Can u help me to get the parameters types of a particular method,please? method.getParameterTypes() returns : "[Ljava.lang.Class;@19821" for example which is not understandable for me!
awfully sorry!I come up with the solution at the moment!thanks =))
can I ask a question?
yeah, what's the problem?
try this Class pvec[] = method.getParameterTypes(); for (int j = 0; j < pvec.length; j++) System.out.println("param #" + j + " " + pvec[j]);
thanks but my problem is sth else : why when I remove the // my code throws exception ?can u help me to figure out what's wrong with it? thanks
you're parsing a double as the parameter when it's expecting a float you can either change the parameter to type Double Method m = c2.getDeclaredMethod(name, Double.TYPE); Object o2 = m.invoke(null,12.5); System.out.println(o2); or parse it a float (note the f on the end of the number) Object o2 = m.invoke(null,12.5f);
a thousands of thanks for your great help!but I changed the way I'm thinking to the code completely!
that's alright, good luck =)
Hi again Nick, Do u have some time so that I can ask a few questions? thanks :)
sure go for it
thanks :) suppose we have : * double d = 10.0 ; for this line ,for example ,I need to make a new instance of double class and then assign 10.0 to that object.my code throws exception when I try to make a new Instance of this class on a constructor. what do u think the problem is? here is what I've written : Class c = typeMap.get(objectName);//this line gives us the type of d in * Constructor cs = c.getConstructor(); Object o = cs.newInstance();//the exception occurs here
have you tried converting double to Double?
I can't get what you mean :(
it's case sensitive, double and Double are not the same in java, but if you come across a double you could just treat it as if it was a Double. Wow ok maybe that is a little confusing to read. So you line is double d = 10.0; Would your program be able to do it if it was Double d = 10.0; ??
no!it was just a mistake in typing!I meant "double" in my file it is double but it still doesn't work properly:(
double is a primitive type, i don't believe you can create it using reflection. i'll try it out when i get eclipse installed again. what i'm suggesting is to create a Double and store the double in it
thaanks Nick for your great help!can u take a look at my code?
yeah but i wont be able to look at it until morning sorry, i'm about to go to bed
sorry to bother u by asking so many questions! my code is now more complete!but the problem is that it doesn't do anything!I don't know why!it doesn't go through any of the methods as it seems!can u take a look and help me find the problem? again thanks for everything ...
There seems to be an error on line 105 i'm pretty sure you were meant to have beforeCons = rightTemp.substring(rightTemp.indexOf("=") + 1, rightTemp.indexOf("("));
Join our real-time social learning platform and learn together with your friends!