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...

jlxip
  • 125
  • 1
  • 12
  • Looks like you are just asking for polymorphism. `Main` should be a type that has a public method `getUserInput()` that you then call. – markspace Mar 19 '16 at 02: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 – jlxip Mar 19 '16 at 02: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()`. – markspace Mar 19 '16 at 02: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: http://pastebin.com/61ZR9U0C – jlxip Mar 19 '16 at 09:58
  • Does the [Command pattern](https://en.wikipedia.org/wiki/Command_pattern) help? – Bohemian Mar 19 '16 at 10:33

1 Answers1

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

jam
  • 1,253
  • 1
  • 12
  • 26
  • The problem is that 'A' and 'B' classes are in different files. Have a look at the pastebin I replied to @markspace. – jlxip Mar 19 '16 at 10:01
  • so A and B are in different packages – jam Mar 19 '16 at 10:11
  • Yes, they are. That is the problem. – jlxip Mar 19 '16 at 10:12
  • I mean now in my example they are in different packages. – jam Mar 19 '16 at 10:14
  • 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? – jlxip Mar 19 '16 at 10:19
  • so you have to load the classes from the jar file like explained [here](http://stackoverflow.com/questions/460364/how-to-use-classes-from-jar-files) @GabrieleV and import it like in my example – jam Mar 19 '16 at 10:27
  • Buff, I think this topic is very compilcated to explain... 'B' program doesn't know where is it calling from. In my case, 'A' is a program and 'B' is an 'A' extension. So what I want is to call 'B' program from 'A' program, and let 'B' program change data of 'A' program (such as a JLabel text). – jlxip Mar 19 '16 at 10:39