aspose file tools*
The moose likes Java in General and the fly likes Regarding Java Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding Java Reflection" Watch "Regarding Java Reflection" New topic
Author

Regarding Java Reflection

Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874




Output:


My question is, if we can access some method normally then why not through reflection. Is there anything in specification that will answer my WHY?

Is there any hidden problem in forcing all methods, which are accessible normally, to become accessible through reflection as well?

[Edited]
Modified the post to make things render properly.
[ May 24, 2007: Message edited by: Adeel Ansari ]
Amit Biswas
Ranch Hand

Joined: Jun 01, 2006
Posts: 52
Hi Adeel,
I am using Java 1.4 and I am not able to re-create the problem you are facing.
Moreover, there are some differences in the Reflection API signatures. Please find the same below and investigate whether you are still facing exceptions:

//Super-class in package one
package one;

public class ParentTest {

public String returnParam(String param){
return param;
}
}


//sub-class in package one
package one;

public class ChildTest extends ParentTest {

}

//class in package two
package two;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import one.ChildTest;

public class TestReflection {

/**
* @param args
*/
public static void main(String[] args) {

ChildTest test = new ChildTest();
System.out.println(test.returnParam("Hi"));

try
{
TestReflection tr = new TestReflection();
Class c = Class.forName("one.ChildTest");
Constructor cstr = c.getConstructor(new Class[]{});
ChildTest ct = (ChildTest)cstr.newInstance(new Object[]{});

tr.invokePublicMethods(c , ct);
tr.invokePublicMethodsAnyway(c, ct);

}
catch(Exception e){e.printStackTrace();}
}

public void invokePublicMethods(Class clazz, ChildTest childTest){
try {
Method method = clazz.getMethod( "returnParam", new Class[]{String.class});
Object obj= method.invoke( childTest, new Object[]{"Hello1!"});
System.out.println(obj);
}
catch (Exception ex) {
ex.printStackTrace();
}

}

public void invokePublicMethodsAnyway(Class clazz, ChildTest childTest){
try {
Method method = clazz.getMethod( "returnParam", new Class[]{String.class});
method.setAccessible(true);

Object obj= method.invoke( childTest, new Object[]{"Hello2!"});
System.out.println(obj);
}
catch (Exception ex) {
ex.printStackTrace();
}
}

}


Output
Hi
Hello1!
Hello2!
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Nope you wouldn't get it after modifying your parent class as public.
Please note my parent class Test is not public.
Thanks.
Amit Biswas
Ranch Hand

Joined: Jun 01, 2006
Posts: 52
I got you. This seems to be some kind of bug and can be seen in the sun bug list. The workaround is what you have done in the second method i.e, method.setAccessible(true);
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
I don't think it would be bug. There must be some rational reason behind it.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, it seems that the code works fine under JDK 6, but not JDK 5. So I'd say yes, it's a bug, and it's been fixed, but only very recently.


"I'm not back." - Bill Harding, Twister
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Sound. Thanks Jim.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Jim, thanks for your attempt. I tried it with Java 6 as well, but doesn't work. Have you tried it yourself? just curious.

I found a few matching threads in the Java 6 bugs list. Among those,
this thread seems that this bug is not fixed, yet. I believe, you might, mistakenly, considered this one.

Thanks.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Adeel, yes I tested it using JDK 1.6.0_01 on Windows 2000 Pro, using the exact code that you provided in your first post. It prints

Hello!
Hello!
Hello!

But if I switch to JDK 1.5.0_11, I get the error you describe. Are you using a different JDK? Perhaps there is some subtle difference between my code and yours. Maybe there is a partial fix in place, but it doesn't work in all circumstances. Maybe it was fixed accidentally in JDK 1.6.0_01 in response to some other bug report, and they neglected to update the one you found. I don't know. I would agree with the earlier suggestions that if you're seeing this behavior, it's a bug. But I don't know why it seems to work for me and not you, unless it was fixed in _01 and you're using _00.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
I am using jdk1.6.0_01 on SUSE Linux 10.0 and IDEA. Its working now. I have deleted the class files before recompiling with Java 6, that worked. Thanks for the help and sorry for disturbing you, the problem was on my end.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regarding Java Reflection