3

I would like to get the instance of a reflected class. Let me explain:
I create a new instance of a reflected method in 'A' program:

// Before this, class loading and stuff that are not transcendental for this situation.
Method executeApp = loadedClass.getMethod("execute", Main.class)
executeApp.invoke(loadedClassInstance, MAIN); // This "MAIN" is an instance of "Main" class

And from another program ('B' program):

public class b {
  public void execute(net.package.Main instance) {
    System.out.println(instance.getUserInput()); // Just an example, the point is to get information from the instance
  }
}

A better example of what I'm trying to do is this: http://pastebin.com/61ZR9U0C
I don't have any idea of how I'm going to make 'B' program understand what is net.package.Main
Any idea? Maybe it's not possible...

5
  • Looks like you are just asking for polymorphism. Main should be a type that has a public method getUserInput() that you then call. Commented Mar 19, 2016 at 2:23
  • Yes, but what I meant with getUserInput() is that is a method which NEEDS to be in the same instance that the 'A' program, an example would be a method which extracts the text from a JTextField Commented Mar 19, 2016 at 2:31
  • I'm not 100% sure what you're saying, but if you want to call a method using reflection, you have to have the instance and the method, plus any parameters for the method. So minimum two arguments to b.execute(). Commented Mar 19, 2016 at 2:33
  • @markspace Of course, I know it, but I want 'B' program to request data from 'A' program in the same instance. Maybe I'm not explaining myself very well... This example should be easy to understand: pastebin.com/61ZR9U0C Commented Mar 19, 2016 at 9:58
  • Does the Command pattern help? Commented Mar 19, 2016 at 10:33

1 Answer 1

2

let the parameter in B.execute be of type Object so you won't struggle with the package name since every class extends Object

package: stackoverflow

import stackoverflow.somepackage.B;
class Main{

        public String getUserInput(){
            return "I 'am user foo";
        }

}

class A{

    public void callB() throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        Class loadedClass = B.class;
        Method executeApp = loadedClass.getMethod("execute", Object.class);
        executeApp.invoke(loadedClass.newInstance(),Main.class.newInstance());
    }

}

package: stackoverflow.somepackage

class B{

    public void execute(Object object) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class mainClass = object.getClass();

        // for your purpose
        // Method method = mainClass.getMethod("setLabelText", String.class);
        // String text = "Some Text";
        // method.invoke(object, text);

        // for demonstration
        Method method = mainClass.getMethod("getUserInput");
        System.out.println(method.invoke(object));
    }

}

package: stackoverflow

public class ReflectionTest {

    @Test
    public void callB() throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        A myA = new A();
        myA.callB();
    }


}

I 'am user foo

Sign up to request clarification or add additional context in comments.

7 Comments

The problem is that 'A' and 'B' classes are in different files. Have a look at the pastebin I replied to @markspace.
so A and B are in different packages
Yes, they are. That is the problem.
I mean now in my example they are in different packages.
Good idea using imports in 'B' program, but as I said before 'A' and 'B' are in different jar files, so I can't import stackoverflow.Main. Is possible to use a reflected import?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.