dimanche 27 décembre 2015

Java Reflection Method Listener

I'm using reflection to execute plugins which is fine but i want to add like a proxy so everytime the sendMessage method is called from my plugin it does something from the main application.

    case "execute":
        String url = command.getArguments()[1];
        URL u = new URL("jar", "", url + "!/");
        URLClassLoader ucl = new URLClassLoader(new URL[] { u });
        Class<?> c = ucl.loadClass("com.j.Plugin");
        Object instance = c.newInstance();
        Method m = c.getMethod("execute", String.class, String[].class);
        m.invoke(instance, Constants.USER_ID, Arrays.copyOfRange(command.getArguments(), 1, command.getArguments().length));            
        //Upon sendMessage method get message here???
        break;
    }

This method executes the plugin which is fine.

package com.j;

public class Plugin {

    public void execute(String userId, String[] args) {
        System.out.println(1);
        sendMessage("2");
    }

    public void sendMessage(String message) {
        System.out.println(message);
    }

}

But i want it so everytime sendMessage is called i get what the message is under m.invoke()





Aucun commentaire:

Enregistrer un commentaire