• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Java Reflection

 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




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 ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think it would be bug. There must be some rational reason behind it.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sound. Thanks Jim.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic