Skip to content

Commit

Permalink
reflection proxy based approach for swing/awt proxy object
Browse files Browse the repository at this point in the history
  • Loading branch information
jussimalinen committed Sep 4, 2013
1 parent 2de2c60 commit 3d38297
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 91 deletions.
50 changes: 50 additions & 0 deletions src/main/java/org/robotframework/swing/common/ProxyHandler.java
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,102 +16,25 @@
package org.robotframework.swing.textcomponent;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public interface TextComponentOperator {

public class TextComponentOperator {

public void setText(String text);

private final SwingTextComponentOperator swingOperator;
private final AWTTextComponentOperator awtOperator;
public boolean isEditable();

public TextComponentOperator(SwingTextComponentOperator swing) {
swingOperator = swing;
awtOperator = null;
}
public boolean isEnabled();

public TextComponentOperator(AWTTextComponentOperator awt) {
swingOperator = null;
awtOperator = awt;
}
public void setEnabled(boolean enabled);

public void setText(String text) {
if (swingOperator != null)
swingOperator.setText(text);
else
awtOperator.setText(text);
}
public String getText();

public boolean isEditable() {
if (swingOperator != null)
return swingOperator.isEditable();
else
return awtOperator.isEditable();
}
public void typeText(String text);

public boolean isEnabled() {
if (swingOperator != null)
return swingOperator.isEnabled();
else
return awtOperator.isEnabled();
}
public void clearText();

public void setEnabled(boolean enabled) {
if (swingOperator != null)
swingOperator.setEnabled(enabled);
else
awtOperator.setEnabled(enabled);
}

public String getText() {
if (swingOperator!=null)
return swingOperator.getText();
else
return awtOperator.getText();
}

public void typeText(String text) {
if (swingOperator!=null)
swingOperator.typeText(text);
else
awtOperator.typeText(text);
}

public void clearText() {
if (swingOperator!=null)
swingOperator.clearText();
else
awtOperator.clearText();
}

public void makeComponentVisible() {
if (swingOperator!=null)
swingOperator.makeComponentVisible();
else
awtOperator.makeComponentVisible();
}

public void selectAll() {
if (swingOperator!=null)
swingOperator.selectAll();
else
awtOperator.selectAll();
}

private Object operator;

public Object invoke(String methodName, Object... arguments) {
Class[] classes = new Class[arguments.length];
for (int i=0; i < classes.length; i++) {
classes[i] = arguments[i].getClass();
}
try {
Method m = operator.getClass().getMethod(methodName, classes);
return m.invoke(operator, new Object[] {});
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void makeComponentVisible();

public void selectAll();

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,34 @@

import org.netbeans.jemmy.operators.ContainerOperator;
import org.robotframework.swing.chooser.ByNameComponentChooser;
import org.robotframework.swing.common.ProxyHandler;
import org.robotframework.swing.context.Context;
import org.robotframework.swing.factory.DefaultContextVerifyingOperatorFactory;


public class TextComponentOperatorFactory extends DefaultContextVerifyingOperatorFactory<TextComponentOperator> {
@Override
public TextComponentOperator createOperatorByIndex(int index) {
return new TextComponentOperator(new SwingTextComponentOperator((ContainerOperator) Context.getContext(), index));
return createProxy(new SwingTextComponentOperator((ContainerOperator) Context.getContext(), index));
}

@Override
public TextComponentOperator createOperatorByName(String name) {
return new TextComponentOperator(new SwingTextComponentOperator((ContainerOperator) Context.getContext(), new ByNameComponentChooser(name)));
return createProxy(new SwingTextComponentOperator((ContainerOperator) Context.getContext(), new ByNameComponentChooser(name)));
}

@Override
public TextComponentOperator indexAWTArgument(int index) {
return new TextComponentOperator(new AWTTextComponentOperator((ContainerOperator) Context.getContext(), index));
return createProxy(new AWTTextComponentOperator((ContainerOperator) Context.getContext(), index));
}

@Override
public TextComponentOperator nameAWTArgument(String name) {
return new TextComponentOperator(new AWTTextComponentOperator((ContainerOperator) Context.getContext(), new ByNameComponentChooser(name)));
return createProxy(new AWTTextComponentOperator((ContainerOperator) Context.getContext(), new ByNameComponentChooser(name)));
}

private TextComponentOperator createProxy(Object operator) {
return (TextComponentOperator) ProxyHandler.createProxy(TextComponentOperator.class,
operator);
}
}

0 comments on commit 3d38297

Please sign in to comment.