-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reflection proxy based approach for swing/awt proxy object
- Loading branch information
1 parent
2de2c60
commit 3d38297
Showing
3 changed files
with
71 additions
and
91 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/main/java/org/robotframework/swing/common/ProxyHandler.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.robotframework.swing.common; | ||
|
||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
|
||
|
||
public class ProxyHandler implements InvocationHandler { | ||
|
||
private Object operator; | ||
|
||
public ProxyHandler(Object target) { | ||
operator = target; | ||
} | ||
|
||
public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable { | ||
String methodName = method.getName(); | ||
Class[] argumentTypes = method.getParameterTypes(); | ||
try { | ||
Method m = operator.getClass().getMethod(methodName, argumentTypes); | ||
return m.invoke(operator, arguments); | ||
} catch (NoSuchMethodException e) { | ||
throw new RuntimeException("This component type does not support operation "+methodName); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} catch (InvocationTargetException e) { | ||
throw e.getCause(); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Swing and AWT operator often do not inherit from the same interface, although | ||
* they implement the same methods. With createProxy method you can create | ||
* a new proxy instance for operator, so that the proxy implements the interfaceTarget. | ||
*/ | ||
public static Object createProxy(Class interfaceTarget, Object operator) { | ||
InvocationHandler handler = new ProxyHandler(operator); | ||
Class proxyClass = Proxy.getProxyClass( | ||
interfaceTarget.getClassLoader(), new Class[]{interfaceTarget}); | ||
try { | ||
return proxyClass.getConstructor(new Class[]{InvocationHandler.class}). | ||
newInstance(new Object[]{handler}); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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
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