Results 1 to 6 of 6
- 01-22-2011, 10:48 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
calling super method via reflection
Hi @all,
please imagine the following scenario:
In my team we are using MDA, so we are generating classes like the above one. In our environment, we can use arbitrary strings to specify the super class of the generated class. This also means that there is no meta information in order to determine its methods. Therefore, "void aMethod()" must be generated statically.Java Code:class myGeneratedClass extends *a_super_type_we_have_no_information_about* { void aMethod() { *call super.aMethod() IF AVAILABLE * } }
We cannot call "super.aMethod()" there, because it is possible that the super class does not implement this method. So we have to check and invoke during runtime (calling the super method, if available, is a requirement we have). We thought that reflection is able to do that. However, we have not found a possibility yet. Do you know, if this is possible?
Thanks in advance!
- 01-22-2011, 11:05 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Read the API documentation for the Class class; especially the methods getSuperClass() and getDeclaredMethod(s)( ... ) may be of interest to you.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-22-2011, 11:09 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
Hello,
I have implemented an algorithm which uses getDeclaredMethod(x) for the super class (or better say ANY super class... it searches recursively until method is found). I also find the method. However, if I invoke the found method (with object this), I did NOT call the super method. It invoked the method of the current class again. So it results in an endless loop :(
Maybe, I did something wrong, but it doesn't seem that getDeclaredMethod(x) is made for calling super type methods. Are you sure that this should work?
Thanks.
- 01-22-2011, 11:15 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
Well, let's simply attach the code, which does not seem to work as expected.
Java Code:public void callSuperAMethod(Class<?> clazz) { try { Method m = clazz.getDeclaredMethod("aMethod"); try { m.invoke(this); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } catch (NoSuchMethodException e) { if (clazz == Object.class) return; callSuperAMethod(clazz.getSuperclass()); } } public void aMethod() { callSuperAMethod(this.getClass().getSuperclass()); }
- 01-22-2011, 11:45 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Darn, you're right; the invoke method does a dynamic method lookup and spoils the game, i.e. it indeed finds the method defined in the sub class. I don't see anything in the Class class documentation (or the Method class) that can prevent it. Unless you have an object that is an instance of the super class (but not of the sub class ...)
I don't know the answer, sorry about that. I wonder how a super.method( ... ) is invoked through reflection ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-22-2011, 12:00 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
reflection invoke method
By pprl in forum New To JavaReplies: 12Last Post: 11-16-2010, 05:43 PM -
Calling to missing super(); ??
By alacn in forum New To JavaReplies: 1Last Post: 08-16-2010, 11:30 AM -
calling variable using super super..
By Stephen Douglas in forum New To JavaReplies: 7Last Post: 08-16-2010, 06:12 AM -
Class Reflection: Finding super class names
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:12 PM -
Getting method names using Reflection
By Java Tip in forum Java TipReplies: 0Last Post: 01-24-2008, 03:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks