lundi 6 juin 2016

Java Reflection Mac OSX

I am using Java Reflection to get some classes and methods running at run time. For that I get the com.apple.eat.Application class and call its method addApplicationListener so that it gets triggered on open file event. Below is the code snippet for the same :

Object application 
    = Class.forName("com.apple.eawt.Application")
        .getMethod("getApplication",(Class[]) null)
        .invoke(null, (Object[]) null);

Object al 
    = Proxy.newProxyInstance(
        Class.forName("com.apple.eawt.ApplicationListener").getClassLoader(), 
        new Class[] { Class.forName("com.apple.eawt.ApplicationListener") }, 
        new Adapter()
    );

application.getClass().getMethod(
    "addApplicationListener", 
    new Class[] {Class.forName("com.apple.eawt.ApplicationListener")}
).invoke(application, new Object[] {al});

Using this when we double click a file , the application listener gets triggered and the class adapter which implements the InvocationHandler gets called. This class contains a method invoke which accepts the proxy, method and args.

My signal reaches that class on double clicking a file but the problem is I am not able to extract the path of the file from the args which gets passed as an argument.

When it try to print the arguments passed it prints the following :

com.apple.eawt.ApplicationEvent[source=sun.lwawt.macosx.LWCToolkit@7bcb0da4]

Please let me know how to extract the arguments from the method called inside the invoke method.





Aucun commentaire:

Enregistrer un commentaire