Last active
August 29, 2015 14:20
-
-
Save lidio601/42e3e5ecc8a3995883e1 to your computer and use it in GitHub Desktop.
Sample Java class to simulate the Javascript callback functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sample class that define a Callbackable object to act like a Javascript callback function | |
*/ | |
package net.fabiocigliano.test.callback; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Modifier; | |
/** | |
* @author fabiocigliano | |
*/ | |
public class Callbackable implements Runnable { | |
private Method method; | |
private Object receiver; | |
private Class<?> creceiver; | |
private Object[] params; | |
private static Object[] NIL = new Object[]{}; | |
/* | |
* Constructors | |
*/ | |
public Callbackable(Method m, Class<?> receiver) { | |
this.method = m; | |
setReceiver(receiver); | |
setParam(NIL); | |
} | |
public Callbackable(Method m, Object receiver) { | |
this.method = m; | |
setReceiver(receiver); | |
setParam(NIL); | |
} | |
public Callbackable(Method m, Class<?> receiver, Object... params) { | |
this.method = m; | |
creceiver = receiver; | |
this.receiver = null; | |
setParam(params); | |
} | |
/* | |
* Setters | |
*/ | |
public void setParam(Object...params) { | |
this.params = params; | |
} | |
public void setReceiver(Object o) { | |
if( o != null ) { | |
receiver = o; | |
creceiver = o.getClass(); | |
} | |
} | |
public void setReceiver(Class<?> c) { | |
if( c != null ) { | |
receiver = null; | |
creceiver = c; | |
} | |
} | |
/* | |
* Status methods | |
*/ | |
public boolean canRun() { | |
return this.method != null && (this.receiver!=null || this.creceiver!=null); | |
} | |
protected static void handleException(Exception e) { | |
e.printStackTrace(); | |
} | |
private static Method findMethod(Class<?> c, String methodName) { | |
Method m = null; | |
try { | |
for(Method list : c.getMethods()) { | |
System.out.println(list); | |
if( list.getName().equals(methodName) ) { | |
m = list; | |
// break; | |
} | |
} | |
} catch(SecurityException npe) { | |
handleException(npe); | |
} | |
return m; | |
} | |
/* | |
* Final callback method | |
*/ | |
@Override public void run() { | |
try { | |
if( canRun() ) { | |
if(Modifier.isStatic(method.getModifiers())) { | |
this.method.invoke(null, params); | |
} else { | |
if( receiver == null ) { | |
receiver = creceiver.newInstance(); | |
} | |
this.method.invoke(receiver, params); | |
} | |
} | |
} | |
catch(IllegalAccessException iae) { | |
handleException(iae); | |
} | |
catch(SecurityException se) { | |
handleException(se); | |
} | |
catch(InstantiationException ie) { | |
handleException(ie); | |
} | |
catch(NullPointerException npe) { | |
handleException(npe); | |
} | |
catch(IllegalArgumentException iae) { | |
handleException(iae); | |
} | |
catch(InvocationTargetException ite) { | |
handleException(ite); | |
} | |
} | |
/* | |
* Testing | |
*/ | |
// TODO: | |
/** | |
* Sample main for testing | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
final a i = new a(); | |
System.out.println(i); | |
i.c(); | |
System.out.println(i+" pre thread"); | |
final Callbackable cb; | |
// cb = new Callbackable( Callbackable.findMethod(a.class, "print"), i); | |
cb = new Callbackable( Callbackable.findMethod(a.class, "print"), a.class, i ); | |
new Thread(new Runnable() { | |
@Override public void run() { | |
for(int j=0; j<10; j++) i.c(); | |
cb.run(); | |
} | |
}).start(); | |
System.out.println(i+" thread started"); | |
} | |
/** | |
* Sample job, just for testing | |
*/ | |
public static class a { | |
private int b = 0; | |
public void c() { | |
b = b + 1; | |
} | |
@Override public String toString() { | |
return "b = "+b; | |
} | |
// public void print() { | |
// System.out.println(this); | |
// } | |
public static void print(a o) { | |
// o.print(); | |
System.out.println(o); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment