Results 1 to 6 of 6
  1. #1
    oida is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default calling super method via reflection

    Hi @all,

    please imagine the following scenario:

    Java Code:
     class myGeneratedClass extends *a_super_type_we_have_no_information_about* {
        void aMethod() {
            *call super.aMethod() IF AVAILABLE *
        }
    }
    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.

    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!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Read the API documentation for the Class class; especially the methods getSuperClass() and getDeclaredMethod(s)( ... ) may be of interest to you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    oida is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    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.

  4. #4
    oida is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    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());
    }

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by oida View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    oida is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Yes, I hoped that there is there is still a trick I don't know (offered by the JavaAPI, for example). Something like invokeSuperMethod() (which is offered by Groovy Reflection, for example).

Similar Threads

  1. reflection invoke method
    By pprl in forum New To Java
    Replies: 12
    Last Post: 11-16-2010, 05:43 PM
  2. Calling to missing super(); ??
    By alacn in forum New To Java
    Replies: 1
    Last Post: 08-16-2010, 11:30 AM
  3. calling variable using super super..
    By Stephen Douglas in forum New To Java
    Replies: 7
    Last Post: 08-16-2010, 06:12 AM
  4. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:12 PM
  5. Getting method names using Reflection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-24-2008, 03:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •