Hi, I do a simple work for annotation, based printString sample.
Code:
//slice definition
module demo{
interface Printer{
void printString(string s);
};
};
//Java annotation
Code:
package ice;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* @author qinxian
* @since 3.0
*/
@Retention(RUNTIME)
@Target(TYPE)
public @interface IceIdentity {
String value();
}
Code:
package demo;
import ice.IceIdentity;
@IceIdentity("::demo::Printer")
public interface Printer extends Ice.Object, _PrinterOperations, _PrinterOperationsNC {
}
Code:
//It's server tie style disp implemantation, just handler PrintString
package ice;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import Ice.Current;
import Ice.ObjectImpl;
import Ice.TieBase;
/**
* @author qinxian
* @since 3.0
*/
public class ServerProxy implements InvocationHandler {
private ServerTie<?> tie;
private Class<?> interfaceClass;
/**
* @param tie
*/
@SuppressWarnings("unchecked")
public ServerProxy(Class interfaceClass, Object ice_delegate, String[] ids, String[] all) {
super();
this.interfaceClass = interfaceClass;
tie = new ServerTie(ice_delegate, ids, all);
}
class ServerTie<T> extends ObjectImpl implements TieBase {
private T ice_delegate;
private String[] __ids;
/**
* @param ice_delegate
* @param __ids
* @param __all
*/
public ServerTie(T ice_delegate, String[] ids, String[] all) {
super();
this.ice_delegate = ice_delegate;
this.__ids = ids;
this.__all = all;
}
public Object ice_delegate() {
return ice_delegate;
}
@SuppressWarnings("unchecked")
public void ice_delegate(Object delegate) {
this.ice_delegate = (T) delegate;
}
@Override
public boolean equals(java.lang.Object rhs) {
if (this == rhs) {
return true;
}
if (!(rhs instanceof ServerTie)) {
return false;
}
return ice_delegate.equals(((ServerTie) rhs).ice_delegate);
}
@Override
public int hashCode() {
return ice_delegate.hashCode();
}
protected void ice_copyStateFrom(Ice.Object __obj) throws java.lang.CloneNotSupportedException {
throw new java.lang.CloneNotSupportedException();
}
@Override
public boolean ice_isA(String s) {
return java.util.Arrays.binarySearch(__ids, s) >= 0;
}
@Override
public boolean ice_isA(String s, Ice.Current __current) {
return java.util.Arrays.binarySearch(__ids, s) >= 0;
}
@Override
public String[] ice_ids() {
return __ids;
}
@Override
public String[] ice_ids(Ice.Current __current) {
return __ids;
}
@Override
public String ice_id() {
return __ids[__ids.length - 1];
}
@Override
public String ice_id(Ice.Current __current) {
return __ids[__ids.length - 1];
}
private String[] __all;
@Override
public IceInternal.DispatchStatus __dispatch(IceInternal.Incoming in, Ice.Current __current) {
System.out.println(Arrays.asList(__all));
System.out.printf("%s, %s\n", __current.operation, __current.ctx);
int pos = java.util.Arrays.binarySearch(__all, __current.operation);
System.out.println(pos);
if (pos < 0) {
return IceInternal.DispatchStatus.DispatchOperationNotExist;
}
switch (pos) {
case 0 :
return ___ice_id(this, in, __current);
case 1 :
return ___ice_ids(this, in, __current);
case 2 :
return ___ice_isA(this, in, __current);
case 3 :
return ___ice_ping(this, in, __current);
default :
__checkMode(Ice.OperationMode.Normal, __current.mode);
IceInternal.BasicStream __is = in.is();
String s;
s = __is.readString();
try {
Method method = interfaceClass.getMethod(__all[pos], String.class, Current.class);
System.out.println(method);
method.invoke(ice_delegate, s, __current);
return IceInternal.DispatchStatus.DispatchOK;
} catch (Exception e) {
e.printStackTrace();
}
}
assert (false);
return IceInternal.DispatchStatus.DispatchOperationNotExist;
}
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Class declaringClass = method.getDeclaringClass();
String methodName = method.getName();
System.out.printf("%s.%s: args:%s\n", declaringClass, methodName, Arrays.asList(args));
if (declaringClass == TieBase.class || declaringClass == Ice.Object.class)
return method.invoke(tie, args);
if (declaringClass == interfaceClass) {
Object lastArg = null;
int len = args.length;
if (len > 0)
lastArg = args[len - 1];
if (lastArg instanceof Current) {
}
Object[] newArgs = new Object[len + 1];
System.arraycopy(args, 0, newArgs, 0, len);
newArgs[len] = lastArg;
return method.invoke(tie.ice_delegate, newArgs);
}
return method.invoke(tie.ice_delegate, args);
}
@SuppressWarnings("unchecked")
public static <T> Ice.Object proxy(Object impl, Class<T> interfaceClass) {
ClassLoader loader = impl.getClass().getClassLoader();
String[] ids = new String[2];
ids[0] = "::Ice::Object";
ids[1] = interfaceClass.getAnnotation(IceIdentity.class).value();
List<String> allMethods = new ArrayList<String>();
allMethods.addAll(Arrays.asList("ice_id", "ice_ids", "ice_isA", "ice_ping"));
String methodName = null;
for (Method method : interfaceClass.getMethods()) {
if (method.getDeclaringClass() == Ice.Object.class) {
} else {
methodName = method.getName();
if (allMethods.contains(methodName)) {
} else
allMethods.add(methodName);
}
}
ServerProxy h = new ServerProxy(interfaceClass, impl, ids, allMethods.toArray(new String[0]));
Class[] allInterfaces = new Class[3];
allInterfaces[0] = Ice.Object.class;
allInterfaces[1] = TieBase.class;
allInterfaces[2] = interfaceClass;
return (Ice.Object) Proxy.newProxyInstance(loader, allInterfaces, h);
}
}
To be continue: