User: neales
Date: 02/03/07 09:03:53
Modified: src/main/org/jboss/proxy/compiler Proxies.java
ProxyCompiler.java
Added: src/main/org/jboss/proxy/compiler
ProxyImplementationFactory.java Runtime.java
Utility.java package.html
Log:
o Replace org.jboss.proxy.compiler with a BCEL implementation
o Add a test for this.
Revision Changes Path
1.2 +13 -14 jboss/src/main/org/jboss/proxy/compiler/Proxies.java
Index: Proxies.java
===================================================================
RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/proxy/compiler/Proxies.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Proxies.java 7 Dec 2001 17:33:22 -0000 1.1
+++ Proxies.java 7 Mar 2002 17:03:53 -0000 1.2
@@ -54,16 +54,15 @@
* // x1 == x2
* MyInterface x3 = (MyInterface) Proxies.newTarget(i);
* // x1 != x3, but calls to x3 are forwarded via i to x1
- * </code>
- */
+ * </code> */
public static ProxyTarget newTarget(ClassLoader parent,
- InvocationHandler InvocationHandler,
- Class targetTypes[])
+ InvocationHandler invocationHandler,
+ Class targetTypes[])
{
- return Impl.getImpl(targetTypes).newTarget(InvocationHandler, parent);
+ return Impl.getImpl(targetTypes).newTarget(invocationHandler, parent);
}
- // public static ProxyTarget newTarget(InvocationHandler InvocationHandler) {
+ // public static ProxyTarget newTarget(InvocationHandler invocationHandler) {
// return newTarget(InvocationHandler, InvocationHandler.getTargetTypes());
// }
@@ -143,7 +142,7 @@
* it is a proxy for some original target object; extract and return
* that object. Otherwise, just call <tt>newTarget</tt>.
*/
- public static Object getTarget(InvocationHandler InvocationHandler)
+ public static Object getTarget(InvocationHandler invocationHandler)
{
// if (InvocationHandler instanceof ProxyTargetMemo) {
// // this kind of InvocationHandler is able to memoize the
ProxyTarget we build
@@ -156,9 +155,9 @@
// return target;
// }
- if (InvocationHandler instanceof ProxyInvocationHandler)
+ if (invocationHandler instanceof ProxyInvocationHandler)
{
- Object target = ((ProxyInvocationHandler)InvocationHandler).getTarget();
+ Object target = ((ProxyInvocationHandler)invocationHandler).getTarget();
if (target != null)
{
return target;
@@ -166,7 +165,7 @@
// and fall through...
}
- //return newTarget(InvocationHandler);
+ //return newTarget(invocationHandler);
return null;
}
@@ -187,11 +186,11 @@
if (target instanceof ProxyTarget)
{
ProxyTarget tproxy = (ProxyTarget)target;
- InvocationHandler InvocationHandler = tproxy.getInvocationHandler();
+ InvocationHandler invocationHandler = tproxy.getInvocationHandler();
if (targetTypes == null ||
Impl.sameTypes(tproxy.getTargetTypes(), targetTypes))
{
- return InvocationHandler;
+ return invocationHandler;
}
// and fall through...
}
@@ -580,7 +579,7 @@
}
- ProxyTarget newTarget(InvocationHandler InvocationHandler,
+ ProxyTarget newTarget(InvocationHandler invocationHandler,
ClassLoader parent)
{
if (proxyConstructor == null)
@@ -598,7 +597,7 @@
try
{
Object arg[] =
- { InvocationHandler };
+ { invocationHandler };
return (ProxyTarget) proxyConstructor.newInstance(arg);
} catch (InvocationTargetException ee)
{
1.2 +105 -302 jboss/src/main/org/jboss/proxy/compiler/ProxyCompiler.java
Index: ProxyCompiler.java
===================================================================
RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/proxy/compiler/ProxyCompiler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ProxyCompiler.java 7 Dec 2001 17:33:22 -0000 1.1
+++ ProxyCompiler.java 7 Mar 2002 17:03:53 -0000 1.2
@@ -6,27 +6,51 @@
*/
package org.jboss.proxy.compiler;
-import java.lang.reflect.*;
+import org.jboss.logging.Logger;
+
+import org.apache.bcel.Constants;
+import org.apache.bcel.generic.ClassGen;
+import org.apache.bcel.generic.BasicType;
+import org.apache.bcel.generic.Type;
/**
* Manages bytecode assembly for dynamic proxy generation.
*
* @author Unknown
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
-class ProxyCompiler
+public class ProxyCompiler
{
+
+ // Constants -----------------------------------------------------
+
+ public final static String IMPL_SUFFIX = "$Proxy";
+
+ // Attributes ----------------------------------------------------
+
Class superclass;
Runtime runtime;
Class targetTypes[];
- Method methods[];
+ java.lang.reflect.Method methods[];
Class proxyType;
-
+
+ // Static --------------------------------------------------------
+
+ // Constructors --------------------------------------------------
+
+ /**
+ * Creates a new <code>ProxyCompiler</code> instance.
+ *
+ * @param parent a <code>ClassLoader</code> value
+ * @param superclass a <code>Class</code> value
+ * @param targetTypes[] a <code>Class</code> value
+ * @param methods[] a <code>java.lang.reflect.Method</code> value
+ */
ProxyCompiler(ClassLoader parent,
Class superclass,
Class targetTypes[],
- Method methods[])
+ java.lang.reflect.Method methods[])
{
this.superclass = superclass;
this.targetTypes = targetTypes;
@@ -39,201 +63,15 @@
runtime.makeProxyType(this);
}
- Class getProxyType() {
- return proxyType;
- }
-
- // this is the only data needed at runtime:
- public static class Runtime
- extends ClassLoader
- {
- // These members are common utilities used by ProxyTarget classes.
- // They are all public so they can be linked to from generated code.
- // I.e., they are the runtime support for the code compiled below.
- private ClassLoader parent;
-
- public Runtime( ClassLoader parent )
- {
- super( parent );
- this.parent = parent;
- }
-
- Class targetTypes[];
- Method methods[];
- ProxyCompiler compiler; // temporary!
-
- public static final Object NOARGS[] = {};
-
- public static String toString(Proxies.ProxyTarget target) {
- // This method is not used if one of the target types declare toString.
- InvocationHandler invocationHandler = target.getInvocationHandler();
- return "ProxyTarget[" + invocationHandler + "]";
- }
-
- public Class[] copyTargetTypes() {
- try {
- return (Class[]) targetTypes.clone();
- } catch (IllegalArgumentException ee) {
- return new Class[0];
- }
- }
-
- public Object invoke(InvocationHandler invocationHandler, int methodNum,
Object values[])
- throws Throwable {
- Method method = methods[methodNum];
- if (method.getName().equals( "writeReplace" ))
- {
- return new ProxyProxy( invocationHandler, copyTargetTypes() );
-
- }
- return invocationHandler.invoke(null, methods[methodNum], values);
- }
-
- public boolean invokeBoolean(InvocationHandler InvocationHandler, int
methodNum, Object values[])
- throws Throwable {
- Object result = invoke(InvocationHandler, methodNum, values);
- return ((Boolean)result).booleanValue();
- }
- public byte invokeByte(InvocationHandler InvocationHandler, int methodNum,
Object values[])
- throws Throwable {
- Object result = invoke(InvocationHandler, methodNum, values);
- return ((Number)result).byteValue();
- }
- public char invokeChar(InvocationHandler InvocationHandler, int methodNum,
Object values[])
- throws Throwable {
- Object result = invoke(InvocationHandler, methodNum, values);
- return ((Character)result).charValue();
- }
- public short invokeShort(InvocationHandler InvocationHandler, int methodNum,
Object values[])
- throws Throwable {
- Object result = invoke(InvocationHandler, methodNum, values);
- return ((Number)result).shortValue();
- }
- public int invokeInt(InvocationHandler InvocationHandler, int methodNum,
Object values[])
- throws Throwable {
- Object result = invoke(InvocationHandler, methodNum, values);
- return ((Number)result).intValue();
- }
- public long invokeLong(InvocationHandler InvocationHandler, int methodNum,
Object values[])
- throws Throwable {
- Object result = invoke(InvocationHandler, methodNum, values);
- return ((Number)result).longValue();
- }
- public float invokeFloat(InvocationHandler InvocationHandler, int methodNum,
Object values[])
- throws Throwable {
- Object result = invoke(InvocationHandler, methodNum, values);
- return ((Number)result).floatValue();
- }
- public double invokeDouble(InvocationHandler InvocationHandler, int
methodNum, Object values[])
- throws Throwable {
- Object result = invoke(InvocationHandler, methodNum, values);
- return ((Number)result).doubleValue();
- }
-
- public static Boolean wrap(boolean x) {
- return new Boolean(x);
- }
- public static Byte wrap(byte x) {
- return new Byte(x);
- }
- public static Character wrap(char x) {
- return new Character(x);
- }
- public static Short wrap(short x) {
- return new Short(x);
- }
- public static Integer wrap(int x) {
- return new Integer(x);
- }
- public static Long wrap(long x) {
- return new Long(x);
- }
- public static Float wrap(float x) {
- return new Float(x);
- }
- public static Double wrap(double x) {
- return new Double(x);
- }
- // the class loading part
-
- void makeProxyType(ProxyCompiler compiler) {
- this.compiler = compiler; // temporary, for use during loading
-
- byte code[] = compiler.getCode();
- /* ---- ---- *
- try {
- String fname = compiler.getProxyClassName();
- fname = fname.substring(1 + fname.lastIndexOf('.')) + ".class";
- fname = "/tmp/" + fname;
- java.io.OutputStream cf = new java.io.FileOutputStream(fname);
- cf.write(code);
- cf.close();
- Logger.debug("wrote "+fname);
- } catch(java.io.IOException ee) { }
- //* ---- ---- */
-
- compiler.proxyType = super.defineClass(compiler.getProxyClassName(), code,
0, code.length);
- super.resolveClass(compiler.proxyType);
- // set the Foo$Impl.info pointer to myself
- try {
- Field infoField = compiler.proxyType.getField(INFO_FIELD);
- infoField.set(null, this);
- } catch (IllegalAccessException ee) {
- throw new RuntimeException("unexpected: "+ee);
- } catch (NoSuchFieldException ee) {
- throw new RuntimeException("unexpected: "+ee);
- }
- compiler = null;
- }
-
- ClassLoader getTargetClassLoader() {
- return parent;
- /* for (int i = 0; i < targetTypes.length; i++) {
- ClassLoader cl = targetTypes[i].getClassLoader();
- if (cl != null) {
- return cl;
- }
- }
- return null; */
- }
+ // Public --------------------------------------------------------
- public synchronized Class loadClass(String name, boolean resolve)
- throws ClassNotFoundException {
- if (name.endsWith(IMPL_SUFFIX)
- && name.equals(compiler.getProxyClassName())) {
- return compiler.proxyType;
- }
- // delegate to the original class loader
- ClassLoader cl = getTargetClassLoader();
- if (cl == null) {
- return super.findSystemClass(name);
- }
- return cl.loadClass(name);
- }
-
- public java.io.InputStream getResourceAsStream(String name) {
- // delegate to the original class loader
- ClassLoader cl = getTargetClassLoader();
- if (cl == null) {
- return parent.getSystemResourceAsStream(name);
- }
- return cl.getResourceAsStream(name);
- }
-
- public java.net.URL getResource(String name) {
- // delegate to the original class loader
- ClassLoader cl = getTargetClassLoader();
- if (cl == null) {
- return parent.getSystemResource(name);
- }
- return cl.getResource(name);
- }
+
+ // Package protected ---------------------------------------------
+ Class getProxyType() {
+ return proxyType;
}
-
- // the code generation part
-
- private static String IMPL_SUFFIX = "$Proxy";
+
String getProxyClassName() {
// Note: We could reasonably put the $Impl class in either
// of two packges: The package of Proxies, or the same package
@@ -250,21 +88,16 @@
*/
return tName + IMPL_SUFFIX;
}
+
+ /**
+ * Create the implementation class for the given target.
+ *
+ * @return a <code>byte[]</code> value
+ */
+ byte[] getCode() {
- private static String INFO_FIELD = "$info";
- private static String InvocationHandler_FIELD = "$InvocationHandler";
-
- private static final Class invokeParams[] = {
- InvocationHandler.class, Integer.TYPE, Object[].class
- };
- private static final Class toStringParams[] = {
- Proxies.ProxyTarget.class
- };
-
- /** Create the implementation class for the given target. */
- private byte[] getCode() {
- String pClass = getProxyClassName();
-
+ final String proxyClassName = getProxyClassName();
+ final String superClassName = superclass.getName();
int icount = 1; // don't forget ProxyTarget
for (int i = 0; i < targetTypes.length; i++) {
Class targetType = targetTypes[i];
@@ -272,118 +105,88 @@
icount++;
}
}
- Class interfaces[] = new Class[icount];
- interfaces[0] = Proxies.ProxyTarget.class;
+ String interfaceNames[] = new String[icount];
+ interfaceNames[0] = Proxies.ProxyTarget.class.getName();
icount = 1;
for (int i = 0; i < targetTypes.length; i++) {
Class targetType = targetTypes[i];
if (targetType.isInterface()) {
- interfaces[icount++] = targetType;
+ interfaceNames[icount++] = targetType.getName();
} else if (!superclass.isAssignableFrom(targetType)) {
- throw new RuntimeException("unexpected: "+targetType);
+ throw new RuntimeException("unexpected: " + targetType);
}
}
- ProxyAssembler asm =
- new ProxyAssembler(pClass,
- Modifier.PUBLIC | Modifier.FINAL,
- superclass, interfaces);
- Class rClass = ProxyCompiler.Runtime.class;
- asm.addMember(Modifier.PUBLIC + Modifier.STATIC, rClass, null, INFO_FIELD);
+ ClassGen cg = new ClassGen(proxyClassName,
+ superClassName,
+ "<generated>",
+ Constants.ACC_PUBLIC | Constants.ACC_FINAL,
+ interfaceNames);
+
+ ProxyImplementationFactory factory = new
ProxyImplementationFactory(superClassName, proxyClassName, cg);
+
+ cg.addField(factory.createInvocationHandlerField());
+ cg.addField(factory.createRuntimeField());
+
+ cg.addMethod(factory.createConstructor());
+
// ProxyTarget implementation
- Class iClass = InvocationHandler.class;
- asm.addMember(Modifier.PRIVATE, iClass, null, InvocationHandler_FIELD);
- asm.addMember(Modifier.PUBLIC, iClass, "getInvocationHandler",
- new Class[] {}, null);
- {
- asm.pushLocal(0);
- asm.pushField(asm, InvocationHandler_FIELD);
- asm.ret();
- }
- asm.addMember(Modifier.PUBLIC, Class[].class, "getTargetTypes",
- new Class[] {}, null);
- {
- asm.pushLocal(0);
- asm.pushField(asm, INFO_FIELD);
- asm.invoke(rClass, "copyTargetTypes", new Class[] {});
- asm.ret();
- }
- boolean haveToString = false;
+ cg.addMethod(factory.createGetInvocationHandler());
+ cg.addMethod(factory.createGetTargetTypes());
+
+ boolean haveToString = false;
// Implement the methods of the target types.
for (int i = 0; i < methods.length; i++) {
- Method m = methods[i];
+
+ java.lang.reflect.Method m = methods[i];
String name = m.getName();
- Class rtype = m.getReturnType();
- Class ptypes[] = m.getParameterTypes();
- Class[] exceptions = m.getExceptionTypes();
- if (name.equals("toString") && ptypes.length == 0) {
+ Class rTypeClass = m.getReturnType();
+ String rTypeName = rTypeClass.getName();
+ Type rType = Utility.getType(rTypeClass);
+ Type[] pTypes = Utility.getTypes(m.getParameterTypes());
+
+ String[] exceptionNames = getNames(m.getExceptionTypes());
+
+ if (name.equals("toString") && pTypes.length == 0) {
haveToString = true;
- }
- asm.addMember(Modifier.PUBLIC + Modifier.FINAL,
- rtype, name, ptypes, exceptions);
- {
- // $info.invokeBoolean(InvocationHandler, i, new Object[]{ ... })
- asm.pushField(asm, INFO_FIELD);
- asm.pushLocal(0);
- asm.pushField(asm, InvocationHandler_FIELD);
- asm.pushConstant(i);
- // push the arguments
- if (ptypes.length == 0) {
- asm.pushField(rClass, "NOARGS");
- } else {
- asm.pushConstant(ptypes.length);
- asm.pushNewArray(Object.class);
- for (int j = 0; j < ptypes.length; j++) {
- Class t = ptypes[j];
- asm.dup();
- asm.pushConstant(j);
- asm.pushLocal(1 + j);
- if (t.isPrimitive()) {
- asm.invoke(rClass, "wrap", new Class[]{ t });
- }
- asm.setElement(Object.class);
- }
- }
- // call the InvocationHandler
- String invoke = "invoke";
- if (rtype.isPrimitive() && rtype != Void.TYPE) {
- String tn = rtype.getName();
- invoke += Character.toUpperCase(tn.charAt(0))
- + tn.substring(1);
- }
- asm.invoke(rClass, invoke, invokeParams);
- if (!rtype.isPrimitive() && rtype != Object.class) {
- asm.checkCast(rtype);
- }
- asm.ret();
- }
+ }
+
+ cg.addMethod(factory.createProxyMethod(name,
+ i,
+ rType,
+ pTypes,
+ exceptionNames));
}
if (!haveToString) {
- asm.addMember(Modifier.PUBLIC, String.class, "toString",
- new Class[] {}, null);
- {
- asm.pushLocal(0);
- asm.invoke(rClass, "toString", toStringParams);
- asm.ret();
- }
+ cg.addMethod(factory.createToString());
}
- // Put in the constructor:
- asm.addMember(Modifier.PUBLIC, Void.TYPE, "<init>",
- new Class[] { iClass }, null);
- {
- asm.pushLocal(0);
- asm.invoke(superclass, "<init>", new Class[0]);
- asm.pushLocal(0);
- asm.pushLocal(1);
- asm.setField(asm, InvocationHandler_FIELD);
- asm.ret();
+ /*
+ try {
+ cg.getJavaClass().dump("/tmp/" + proxyClassName + ".class");
+ } catch ( java.io.IOException e ) {
+ }
+ */
+
+ return cg.getJavaClass().getBytes();
+ }
+
+ // Protected -----------------------------------------------------
+
+ // Private -------------------------------------------------------
+ private String[] getNames(Class[] classes) {
+ String[] names = new String[classes.length];
+ for ( int i = 0; i < classes.length; i++ ) {
+ names[i] = classes[i].getName();
}
- return asm.getCode();
+ return names;
}
+
+ // Inner classes -------------------------------------------------
+
}
1.1
jboss/src/main/org/jboss/proxy/compiler/ProxyImplementationFactory.java
Index: ProxyImplementationFactory.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.proxy.compiler;
import org.apache.bcel.Constants;
import org.apache.bcel.classfile.Field;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.ArrayType;
import org.apache.bcel.generic.BasicType;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.FieldGen;
import org.apache.bcel.generic.Instruction;
import org.apache.bcel.generic.InstructionFactory;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.MethodGen;
import org.apache.bcel.generic.ObjectType;
import org.apache.bcel.generic.PUSH;
import org.apache.bcel.generic.ReferenceType;
import org.apache.bcel.generic.Type;
/**
* Factory to create the bytecode implementation of various methods
* required by the ProxyCompiler
*
* @author <a href="mailto:[EMAIL PROTECTED]">Neale Swinnerton</a>
* @version $Revision: 1.1 $
*/
public class ProxyImplementationFactory {
// Constants -----------------------------------------------------
// Class Names
private final static String RUNTIME_CN = Runtime.class.getName();
private final static String INVOCATION_HANDLER_CN =
InvocationHandler.class.getName();
private final static String STRING_BUFFER_CN =
StringBuffer.class.getName();
//Types
private final static ObjectType RUNTIME_T =
(ObjectType)Utility.getType(Runtime.class);
private final static ObjectType INVOCATION_HANDLER_T =
(ObjectType)Utility.getType(InvocationHandler.class);
private final static ArrayType ARRAY_OF_CLASS_T = new
ArrayType("java.lang.Class", 1);
private final static ObjectType OBJECT_T = new
ObjectType("java.lang.Object");
private final static ArrayType ARRAY_OF_OBJECT_T = new
ArrayType("java.lang.Object", 1);
private final static ObjectType STRING_T = new
ObjectType("java.lang.String");
private final static ObjectType STRING_BUFFER_T = new
ObjectType("java.lang.StringBuffer");
private final static ObjectType PROXY_TARGET_T = new
ObjectType(Proxies.ProxyTarget.class.getName());
private final static Type[] INVOKE_ARGS = new
Type[]{INVOCATION_HANDLER_T,
Type.INT,
ARRAY_OF_OBJECT_T};
// Method Names
private final static String GET_INVOCATION_HANDLER_MN = "getInvocationHandler";
private final static String GET_TARGET_TYPES_MN = "getTargetTypes";
private final static String TO_STRING_MN = "toString";
private final static String APPEND_MN = "append";
private final static String CTOR_MN = "<init>";
//Field Names
private final static String INVOCATION_HANDLER_FN = "invocationHandler";
// Attributes ----------------------------------------------------
private static Type PROXY_CLASS_T; // need to assign this in the ctor
private InstructionList il = new InstructionList();
private String proxyClassName;
private String superClassName;
private ConstantPoolGen constPool;
private InstructionFactory iFactory;
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
/**
* Creates a new <code>ProxyImplementationFactory</code> instance.
*
* @param superClassName a <code>String</code> value
* @param proxyClassName a <code>String</code> value
* @param cg a <code>ClassGen</code> value
*/
public ProxyImplementationFactory(String superClassName, String proxyClassName,
ClassGen cg) {
this.superClassName = superClassName;
this.proxyClassName = proxyClassName;
PROXY_CLASS_T = new ObjectType(proxyClassName);
constPool = cg.getConstantPool();
iFactory = new InstructionFactory(cg, constPool);
}
// Public --------------------------------------------------------
/**
* generate an implementation of
* <pre>
*
* <code>
* public InvocationHandler getInvocationHandler() {
* return this.invocationHandler;
* }
* </code>
*
* </pre>
*/
public Method createGetInvocationHandler() {
MethodGen mg = new MethodGen(Constants.ACC_PUBLIC,
INVOCATION_HANDLER_T,
Type.NO_ARGS,
null, GET_INVOCATION_HANDLER_MN, proxyClassName,
il, constPool);
il.append(iFactory.createLoad(PROXY_CLASS_T, 0));
il.append(iFactory.createGetField(proxyClassName, INVOCATION_HANDLER_FN,
INVOCATION_HANDLER_T));
il.append(iFactory.createReturn(INVOCATION_HANDLER_T));
mg.setMaxStack();
mg.setMaxLocals();
return getMethodAndTidyup(mg);
}
/**
* generate an implementation of
* <pre>
*
* <code>
* public Class[] getTargetTypes {
* return this.invocationHandler.copyTargetTypes();
* }
* </code>
*
* </pre>
*
* @return the method
*
*/
public Method createGetTargetTypes() {
MethodGen mg = new MethodGen(Constants.ACC_PUBLIC,
ARRAY_OF_CLASS_T,
Type.NO_ARGS,
null,
GET_TARGET_TYPES_MN,
proxyClassName,
il,
constPool);
il.append(iFactory.createLoad(PROXY_CLASS_T, 0));
il.append(iFactory.createGetField(proxyClassName, Runtime.RUNTIME_FN,
RUNTIME_T));
il.append(iFactory.createInvoke(RUNTIME_CN,
"copyTargetTypes",
ARRAY_OF_CLASS_T,
Type.NO_ARGS,
Constants.INVOKEVIRTUAL));
il.append(iFactory.createReturn(ARRAY_OF_CLASS_T));
mg.setMaxStack(1);
mg.setMaxLocals();
Method m = mg.getMethod();
il.dispose();
return m;
}
/**
* generate an implementation of
* <pre>
*
* <code>
* public String toString() {
* return "ProxyTarget[" + invocationHnadler + "]";
* }
* </code>
*
* </pre>
*
* @return the method
*
*/
public Method createToString() {
MethodGen mg = new MethodGen(Constants.ACC_PUBLIC, STRING_T,
Type.NO_ARGS,
null, TO_STRING_MN, proxyClassName, il,
constPool);
/*
il.append(iFactory.createLoad(PROXY_CLASS_T, 0));
il.append(iFactory.createInvoke(RUNTIME_CN,
TO_STRING_MN,
STRING_T,
new Type[]{PROXY_TARGET_T},
Constants.INVOKESTATIC));
il.append(iFactory.createReturn(STRING_T));
*/
il.append(iFactory.createNew(STRING_BUFFER_T));
il.append(iFactory.createDup(1));
il.append(iFactory.createInvoke(STRING_BUFFER_CN,
CTOR_MN,
Type.VOID,
Type.NO_ARGS,
Constants.INVOKESPECIAL));
il.append(new PUSH(constPool, "ProxyTarget["));
il.append(iFactory.createInvoke(STRING_BUFFER_CN,
APPEND_MN,
STRING_BUFFER_T,
new Type[]{STRING_T},
Constants.INVOKEVIRTUAL));
il.append(iFactory.createLoad(PROXY_CLASS_T, 0));
il.append(iFactory.createGetField(proxyClassName, INVOCATION_HANDLER_FN,
INVOCATION_HANDLER_T));
il.append(iFactory.createInvoke(STRING_BUFFER_CN,
APPEND_MN,
STRING_BUFFER_T,
new Type[]{OBJECT_T},
Constants.INVOKEVIRTUAL));
il.append(new PUSH(constPool, "]"));
il.append(iFactory.createInvoke(STRING_BUFFER_CN,
APPEND_MN,
STRING_BUFFER_T,
new Type[]{STRING_T},
Constants.INVOKEVIRTUAL));
il.append(iFactory.createInvoke(STRING_BUFFER_CN,
TO_STRING_MN,
STRING_T,
Type.NO_ARGS,
Constants.INVOKEVIRTUAL));
il.append(iFactory.createReturn(STRING_T));
mg.setMaxStack();
mg.setMaxLocals();
return getMethodAndTidyup(mg);
}
/**
* generate an implementation of
* <pre>
*
* <code>
* public <proxyClassName>(InvocationHandler h) {
* this.invocationHandler = h;
* }
* </code>
*
* </pre>
*
* @return the method
*
*/
public Method createConstructor() {
MethodGen mg = new MethodGen(Constants.ACC_PUBLIC,
Type.VOID,
new Type[]{INVOCATION_HANDLER_T},
null, CTOR_MN,
proxyClassName,
il,
constPool);
il.append(iFactory.createLoad(INVOCATION_HANDLER_T, 0));
il.append(iFactory.createInvoke(superClassName,
CTOR_MN,
Type.VOID,
Type.NO_ARGS,
Constants.INVOKESPECIAL));
il.append(iFactory.createLoad(PROXY_CLASS_T, 0));
il.append(iFactory.createLoad(INVOCATION_HANDLER_T, 1));
il.append(iFactory.createPutField(proxyClassName, INVOCATION_HANDLER_FN,
INVOCATION_HANDLER_T));
il.append(iFactory.createReturn(Type.VOID));
mg.setMaxStack();
mg.setMaxLocals();
return getMethodAndTidyup(mg);
}
/**
* generate an implementation of...
* <pre>
*
* <code>
* public <return type> <method name>(<p0 type> p0, <p1
type> p1, ...)
* throws e0, e1 ... {
* return runtme.invoke(invocatioHandler, <method index>,
* new Object[]{p0, p1, ...)};
* }
* </code>
*
* </pre>
*
* @return the method
*/
public Method createProxyMethod(String name,
int methodNum,
Type rType,
Type[] pTypes,
String[] exceptionNames) {
MethodGen mg = new MethodGen(Constants.ACC_PUBLIC,
rType,
pTypes,
null, // argNames
name,
proxyClassName,
il,
constPool);
for ( int j = 0; j < exceptionNames.length; j++ ) {
mg.addException(exceptionNames[j]);
}
// implementation of this.invocationHandler.invoke<Type>(InvocationHandler, i,
new Object[]{ ... })
il.append(iFactory.createGetStatic(proxyClassName, Runtime.RUNTIME_FN,
RUNTIME_T));
il.append(iFactory.createLoad(RUNTIME_T, 0));
il.append(iFactory.createGetField(proxyClassName, INVOCATION_HANDLER_FN,
INVOCATION_HANDLER_T));
il.append(new PUSH(constPool, methodNum));
il.append(new PUSH(constPool, 1));
il.append((Instruction)iFactory.createNewArray(OBJECT_T, (short)1));
if (pTypes.length > 0) {
for (int j = 0; j < pTypes.length; j++) {
Type t = pTypes[j];
il.append(iFactory.createDup(1));
il.append(new PUSH(constPool, j));
if (t instanceof BasicType) {
// do a e.g new Boolean(b)
String wrappedClassName =
Utility.getObjectEquivalentClassName((BasicType)t);
ObjectType wrappedType = new ObjectType(wrappedClassName);
il.append(iFactory.createNew(wrappedType));
il.append(iFactory.createDup(1));
il.append(iFactory.createLoad(t, 1 + j));
il.append(iFactory.createInvoke(wrappedClassName,
CTOR_MN,
Type.VOID,
new Type[]{t},
Constants.INVOKESPECIAL));
t = wrappedType;
} else {
il.append(iFactory.createLoad(t, 1 + j));
}
il.append(iFactory.createArrayStore(t));
}
}
il.append(iFactory.createInvoke(RUNTIME_CN,
"invoke",
Type.OBJECT,
INVOKE_ARGS,
Constants.INVOKEVIRTUAL));
if (rType instanceof ReferenceType ) {
il.append(iFactory.createCheckCast((ReferenceType)rType));
} else if (rType instanceof BasicType) {
if (rType != Type.VOID) {
// we've got an Object and need the equivalent primitive
// do a e.g. (Boolean)obj.booleanValue();
String wrappedClassName =
Utility.getObjectEquivalentClassName((BasicType)rType);
ObjectType wrappedType = new ObjectType(wrappedClassName);
il.append(iFactory.createCheckCast((ReferenceType)wrappedType));
String methodName = Utility.signatureToString(rType.getSignature()) +
"Value";
il.append(iFactory.createInvoke(wrappedClassName,
methodName,
rType,
Type.NO_ARGS,
Constants.INVOKEVIRTUAL));
} else {
//Chuck away returned value if it's void
il.append(iFactory.createPop(1));
}
}
il.append(iFactory.createReturn(rType));
mg.setMaxStack();
mg.setMaxLocals();
return getMethodAndTidyup(mg);
}
/**
* generate a field declaration of the form...
* <pre>
*
* <code>
* private InvocationHandler invocationHandler;
* </code>
*
* </pre>
*
* @return the method
*
*/
public Field createInvocationHandlerField() {
FieldGen fg = new FieldGen(Constants.ACC_PRIVATE, INVOCATION_HANDLER_T,
INVOCATION_HANDLER_FN, constPool);
return fg.getField();
}
/**
* generate a field declaration of the form...
* <pre>
*
* <code>
* public static Runtime runtime;
* </code>
*
* </pre>
*
* @return the method
*
*/
public Field createRuntimeField() {
FieldGen fg = new FieldGen(Constants.ACC_PUBLIC|Constants.ACC_STATIC,
RUNTIME_T, Runtime.RUNTIME_FN, constPool);
return fg.getField();
}
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
// Private -------------------------------------------------------
private Method getMethodAndTidyup(MethodGen mg) {
Method m = mg.getMethod();
il.dispose();
return m;
}
// Inner classes -------------------------------------------------
}
1.1 jboss/src/main/org/jboss/proxy/compiler/Runtime.java
Index: Runtime.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.proxy.compiler;
import org.jboss.logging.Logger;
/**
* Manages bytecode assembly for dynamic proxy generation.
*
* @author Unknown
* @version $Revision: 1.1 $
*/
// this is the only data needed at runtime:
public class Runtime
extends ClassLoader
{
public final static String RUNTIME_FN = "runtime";
// These members are common utilities used by ProxyTarget classes.
// They are all public so they can be linked to from generated code.
// I.e., they are the runtime support for the code compiled below.
private ClassLoader parent;
public Runtime( ClassLoader parent )
{
super( parent );
this.parent = parent;
}
Class targetTypes[];
java.lang.reflect.Method methods[];
ProxyCompiler compiler; // temporary!
public Class[] copyTargetTypes() {
try {
return (Class[]) targetTypes.clone();
} catch (IllegalArgumentException ee) {
return new Class[0];
}
}
public Object invoke(InvocationHandler invocationHandler, int methodNum, Object
values[])
throws Throwable {
java.lang.reflect.Method method = methods[methodNum];
if (method.getName().equals( "writeReplace" ))
{
return new ProxyProxy( invocationHandler, copyTargetTypes() );
}
return invocationHandler.invoke(null, methods[methodNum], values);
}
// the class loading part
void makeProxyType(ProxyCompiler compiler) {
this.compiler = compiler; // temporary, for use during loading
byte code[] = compiler.getCode();
compiler.proxyType = super.defineClass(compiler.getProxyClassName(), code, 0,
code.length);
super.resolveClass(compiler.proxyType);
// set the Foo$Impl.info pointer to myself
try {
java.lang.reflect.Field infoField = compiler.proxyType.getField(RUNTIME_FN);
infoField.set(null, this);
} catch (IllegalAccessException ee) {
throw new RuntimeException("unexpected: "+ee);
} catch (NoSuchFieldException ee) {
throw new RuntimeException("unexpected: "+ee);
}
compiler = null;
}
ClassLoader getTargetClassLoader() {
return parent;
/* for (int i = 0; i < targetTypes.length; i++) {
ClassLoader cl = targetTypes[i].getClassLoader();
if (cl != null) {
return cl;
}
}
return null; */
}
public synchronized Class loadClass(String name, boolean resolve)
throws ClassNotFoundException {
if (name.endsWith("$Proxy")
&& name.equals(compiler.getProxyClassName())) {
return compiler.proxyType;
}
// delegate to the original class loader
ClassLoader cl = getTargetClassLoader();
if (cl == null) {
return super.findSystemClass(name);
}
return cl.loadClass(name);
}
public java.io.InputStream getResourceAsStream(String name) {
// delegate to the original class loader
ClassLoader cl = getTargetClassLoader();
if (cl == null) {
return parent.getSystemResourceAsStream(name);
}
return cl.getResourceAsStream(name);
}
public java.net.URL getResource(String name) {
// delegate to the original class loader
ClassLoader cl = getTargetClassLoader();
if (cl == null) {
return parent.getSystemResource(name);
}
return cl.getResource(name);
}
}
1.1 jboss/src/main/org/jboss/proxy/compiler/Utility.java
Index: Utility.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.proxy.compiler;
import org.apache.bcel.Constants;
import org.apache.bcel.generic.BasicType;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.ObjectType;
import org.apache.bcel.generic.Type;
/**
* Some Routines to convert from <code>java.lang.Class</code> to
* <code>org.apache.bcel.generic.Type</code>. These get round some
* inconsistencies with Class.getName() wrt primitives
*
* <pre>
* e.g.
*
* <code>Character.Type.getName()</code> returns char.
*
* </pre>
* I think it should return C. Don't know if this is a code bug. But there's a bug
* on Bug Parade (#4369208) about the javadoc being misleading.
*
* @see java.lang.Class#getName
*
* @author <a href="mailto:[EMAIL PROTECTED]">Neale Swinnerton</a>
* @version $Revision: 1.1 $
*/
public abstract class Utility extends org.apache.bcel.classfile.Utility {
/**
* Get the <code>org.apache.bcel.generic.Type</code> for a class.
* This handles the case where the class represents an n-dimensional
* array by relying on the fact that <code>Class.getName()</code>
* on an array returns the signature
*
* <pre>
* e.g.
*
* <code>new Object[].getClass().getName()</code> returns [Ljava.lang.Object;
*
* </pre>
*
* @see Utility
*
* @param clazz a <code>Class</code> value
* @return a <code>Type</code> value
*/
public static Type getType(Class clazz) {
if (clazz.isPrimitive() ) {
if (clazz.equals(Boolean.TYPE) ) {
return Type.BOOLEAN;
} else if (clazz.equals(Byte.TYPE) ) {
return Type.BYTE;
} else if (clazz.equals(Character.TYPE) ) {
return Type.CHAR;
} else if (clazz.equals(Double.TYPE) ) {
return Type.DOUBLE;
} else if (clazz.equals(Float.TYPE) ) {
return Type.FLOAT;
} else if (clazz.equals(Integer.TYPE) ) {
return Type.INT;
} else if (clazz.equals(Long.TYPE) ) {
return Type.LONG;
} else if (clazz.equals(Short.TYPE) ) {
return Type.SHORT;
} else if (clazz.equals(Void.TYPE) ) {
return Type.VOID;
}
} else {
String name = clazz.getName();
if (clazz.isArray() ) {
return Type.getType(name);
} else {
return new ObjectType(name);
}
}
return Type.VOID;
}
/**
* Get the <code>org.apache.bcel.generic.Type</code> for an array of Classes
*
* @param classes a <code>Class[]</code> value
* @return a <code>Type[]</code> value
*/
public static Type[] getTypes(Class[] classes) {
Type[] types = new Type[classes.length];
for ( int i = 0; i < classes.length; i++ ) {
types[i] = getType(classes[i]);
}
return types;
}
/**
* Get the Object equivalent Class name for a primitive
*
* <pre>
* e.g
*
* int <-> java.lang.Integer
* char <-> Character
*
* </pre>
*
* @param t a <code>BasicType</code> value
* @return a <code>String</code> value
*/
public static String getObjectEquivalentClassName(BasicType t) {
switch(t.getType()) {
case Constants.T_INT:
return "java.lang.Integer";
case Constants.T_SHORT:
return "java.lang.Short";
case Constants.T_BOOLEAN:
return "java.lang.Boolean";
case Constants.T_CHAR:
return "java.lang.Character";
case Constants.T_BYTE:
return "java.lang.Byte";
case Constants.T_FLOAT:
return "java.lang.Float";
case Constants.T_DOUBLE:
return "java.lang.Double";
case Constants.T_LONG:
return "java.lang.Long";
default:
throw new RuntimeException("Unexpected Type: " + t);
}
}
}
1.1 jboss/src/main/org/jboss/proxy/compiler/package.html
Index: package.html
===================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!--
JBoss, the OpenSource J2EE webOS
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
-->
<!-- $Id: package.html,v 1.1 2002/03/07 17:03:53 neales Exp $ -->
</head>
<body bgcolor="white">
<p><em>Package description goes here</em>.
<h2>Package Specification</h2>
<ul>
<li><a href="javascript: alert('not available')">Not Available</a>
</ul>
<h2>Related Documentation</h2>
<ul>
<li><a href="javascript: alert('not available')">Not Available</a>
</ul>
<h2>Package Status</h2>
<ul>
<li><font color="green"><b>STABLE</b></font>
</ul>
<!-- Put @see and @since tags down here. -->
</body>
</html>
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development