package org.jboss.proxy.compiler;
import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.lang.reflect.Member;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
public final class Proxies
{
private Proxies()
{
super();
}
public static ProxyTarget newTarget(ClassLoader parent,
InvocationHandler invocationHandler,
Class targetTypes[])
throws Exception
{
return Impl.getImpl(targetTypes).newTarget(invocationHandler, parent);
}
public interface ProxyTarget
extends Serializable
{
InvocationHandler getInvocationHandler();
Class[] getTargetTypes();
}
public static ProxyInvocationHandler newInvocationHandler(Object target,
Class targetType)
{
return Impl.getImpl(targetType).newInvocationHandler(target);
}
public static ProxyInvocationHandler newInvocationHandler(Object target,
Class targetTypes[])
{
return Impl.getImpl(targetTypes).newInvocationHandler(target);
}
public interface ProxyInvocationHandler
extends InvocationHandler, Serializable
{
Object getTarget();
}
public static Object getTarget(InvocationHandler invocationHandler)
{
if (invocationHandler instanceof ProxyInvocationHandler)
{
Object target = ((ProxyInvocationHandler)invocationHandler).getTarget();
if (target != null)
{
return target;
}
}
return null;
}
public static InvocationHandler getInvocationHandler(Object target,
Class targetTypes[])
{
if (target instanceof ProxyTarget)
{
ProxyTarget tproxy = (ProxyTarget)target;
InvocationHandler invocationHandler = tproxy.getInvocationHandler();
if (targetTypes == null ||
Impl.sameTypes(tproxy.getTargetTypes(), targetTypes))
{
return invocationHandler;
}
}
return newInvocationHandler(target, targetTypes);
}
public static InvocationHandler getInvocationHandler(Object target,
Class targetType)
{
if (targetType == null)
{
return getInvocationHandler(target, (Class[])null);
}
return getInvocationHandler(target, new Class[] { targetType });
}
public static Method[] getMethods(Class targetType)
{
return Impl.getImpl(targetType).copyMethods();
}
public static Method[] getMethods(Class targetTypes[])
{
return Impl.getImpl(targetTypes).copyMethods();
}
public static void forgetProxyForClass(Class clazz)
{
Impl.forgetProxyForClass(clazz);
}
static class Impl
implements Serializable
{
static final long serialVersionUID = 2911933124627738645L;
static Hashtable impls = new Hashtable();
Class targetTypes[];
Method methods[];
Impl more;
Class superclass = Object.class;
String proxyString;
Constructor proxyConstructor;
Impl(Class targetTypes[])
{
this.targetTypes = targetTypes;
Method methodLists[][] = new Method[targetTypes.length][];
for (int i = 0; i < targetTypes.length; i++)
{
methodLists[i] = checkTargetType(targetTypes[i]);
}
checkSuperclass();
this.methods = combineMethodLists(methodLists);
}
static synchronized Impl getImpl(Class targetType)
{
Impl impl = (Impl) impls.get(targetType);
if (impl == null)
{
impl = new Impl(new Class[] { targetType });
impls.put(targetType, impl);
}
return impl;
}
static synchronized Impl getImpl(Class targetTypes[])
{
int n = targetTypes.length;
if (n == 1)
{
return getImpl(targetTypes[0]);
}
for (int i = 0; i < n; ++i)
{
for (Impl impl = (Impl) impls.get(targetTypes[i]); impl != null; impl = impl.more)
{
if (sameTypes(targetTypes, impl.targetTypes))
return impl;
}
}
targetTypes = copyAndUniquify(targetTypes);
Impl impl1 = getImpl(new Class[] { targetTypes[0] });
Impl impl = new Impl(targetTypes);
impl.more = impl1.more;
impl1.more = impl;
return impl;
}
static synchronized void forgetProxyForClass(Class clazz)
{
impls.remove(clazz);
}
static boolean sameTypes(Class tt1[], Class tt2[])
{
if (tt1.length == 1 && tt2.length == 1)
{
return tt1[0] == tt2[0];
}
int totalSeen2 = 0;
each_type:
for (int i = tt1.length; --i >= 0; )
{
Class c = tt1[i];
for (int j = i; --j >= 0; )
{
if (c == tt1[j])
{
continue each_type;
}
}
int seen2 = 0;
for (int j = tt2.length; --j >= 0; )
{
if (c == tt2[j])
{
++seen2;
}
}
if (seen2 == 0)
{
return false;
}
totalSeen2 += seen2;
}
return totalSeen2 == tt2.length;
}
static Class[] copyAndUniquify(Class targetTypes[])
{
int n = targetTypes.length;
Class tt[] = new Class[n];
int k = 0;
each_type:
for (int i = 0; i < n; i++)
{
Class c = targetTypes[i];
for (int j = i; --j >= 0; )
{
if (c == targetTypes[j])
{
continue each_type;
}
}
tt[k++] = c;
}
if (k < n)
{
Class tt0[] = new Class[k];
for (int i = 0; i < k; i++)
{
tt0[i] = tt[i];
}
tt = tt0;
}
return tt;
}
Method[] checkTargetType(Class targetType)
{
if (targetType.isArray())
{
throw new IllegalArgumentException
("cannot subclass an array type: " + targetType.getName());
}
if (targetType.isPrimitive())
{
throw new IllegalArgumentException
("cannot subclass a primitive type: " + targetType);
}
int tmod = targetType.getModifiers();
if (Modifier.isFinal(tmod))
{
throw new IllegalArgumentException
("cannot subclass a final type: " + targetType);
}
if (!Modifier.isPublic(tmod))
{
throw new IllegalArgumentException
("cannot subclass a non-public type: " + targetType);
}
if (!targetType.isInterface())
{
if (!targetType.isAssignableFrom(superclass))
{
if (superclass.isAssignableFrom(targetType))
{
superclass = targetType;
}
else {
throw new IllegalArgumentException
("inconsistent superclass: " + targetType);
}
}
}
Method methodList[] = targetType.getMethods();
int nm = 0;
for (int i = 0; i < methodList.length; i++)
{
Method m = methodList[i];
if (eligibleForInvocationHandler(m))
{
methodList[nm++] = m; }
}
while (nm < methodList.length)
{
methodList[nm++] = null; }
return methodList;
}
void checkSuperclass()
{
Constructor constructors[] = superclass.getConstructors();
for (int i = 0; i < constructors.length; i++)
{
Constructor c = constructors[i];
int mod = c.getModifiers();
if (Modifier.isPublic(mod)
&& c.getParameterTypes().length == 0)
{
return; }
}
throw new IllegalArgumentException
("cannot subclass without nullary constructor: "
+superclass.getName());
}
static boolean eligibleForInvocationHandler(Method m)
{
int mod = m.getModifiers();
if (Modifier.isStatic(mod) || Modifier.isFinal(mod))
{
return false;
}
if (!Modifier.isAbstract(mod))
{
return false;
}
return true;
}
static boolean areEqual(Method m1, Method m2)
{
if( ! m1.getName().equals(m2.getName()) )
return false;
Class a1[] = m1.getParameterTypes();
Class a2[] = m2.getParameterTypes();
if( a1.length != a2.length )
return false;
for( int i=0; i < a1.length; i++)
if( !a1[i].equals(a1[i]) )
return false;
return true;
}
static Method[] combineMethodLists(Method methodLists[][])
{
int nm = 0;
for (int i = 0; i < methodLists.length; i++)
{
nm += methodLists[i].length;
}
Method methods[] = new Method[nm];
nm=0;
for (int i = 0; i < methodLists.length; i++)
for (int j = 0; j < methodLists[i].length; j++)
methods[nm++]=methodLists[i][j];
for( int i=0; i < methods.length; i++ )
{
if( methods[i] == null )
continue;
for( int j=i+1; j < methods.length; j++ )
{
if( methods[j] == null )
continue;
if( areEqual(methods[i], methods[j]) )
{
methods[j]=null;
nm--;
}
}
}
ArrayList tmp = new ArrayList();
for (int i = 0; i < methods.length; i++)
{
if( methods[i] != null )
tmp.add(methods[i]);
}
Method methodsCopy[] = new Method[tmp.size()];
tmp.toArray(methodsCopy);
return methodsCopy;
}
Method[] copyMethods()
{
return (Method[])methods.clone();
}
Class[] copyTargetTypes()
{
return (Class[])targetTypes.clone();
}
ProxyTarget newTarget(InvocationHandler invocationHandler,
ClassLoader parent)
throws Exception
{
if (proxyConstructor == null)
{
ProxyCompiler pc = new ProxyCompiler(parent,
superclass,
targetTypes,
methods);
Class type[] = { InvocationHandler.class };
proxyConstructor = pc.getProxyType().getConstructor(type);
}
Object args[] = { invocationHandler };
return (ProxyTarget)proxyConstructor.newInstance(args);
}
ProxyInvocationHandler newInvocationHandler(final Object target)
{
if (proxyString == null)
{
String s = "InvocationHandler@" + targetTypes[0].getName();
for (int i = 1; i < targetTypes.length; i++)
{
s += "," + targetTypes[i].getName();
}
proxyString = s;
}
return new ProxyInvocationHandler()
{
public Object getTarget()
{
return target;
}
public Class[] getTargetTypes()
{
return copyTargetTypes();
}
public String toString()
{
return proxyString + "[" + target + "]";
}
public Object invoke(Object dummy,
Method method,
Object values[])
throws Throwable
{
return Impl.this.invoke(target, method, values);
}
};
}
Object invoke(Object target, Member method, Object values[])
throws Throwable
{
try
{
Method methods[] = this.methods;
for (int i = methods.length; --i >= 0; )
{
if (methods[i] == method)
{
return methods[i].invoke(target, values);
}
}
if (method == null)
{
if (methods.length == 1)
{
return methods[0].invoke(target, values);
}
throw new IllegalArgumentException("non-unique method");
}
for (int i = methods.length; --i >= 0; )
{
if (methods[i].equals(method))
{
return methods[i].invoke(target, values);
}
}
}
catch (InvocationTargetException e)
{
throw e.getTargetException();
}
throw new IllegalArgumentException("method unexpected "+method);
}
}
}