hammant 02/01/04 13:09:15
Modified: armi build.xml test.xml
armi/src/java/org/apache/commons/armi/server/impl
BCELClassRetriever.java
Added: armi/src/java/org/apache/commons/armi/test DirectTestC.java
Log:
More BCEL work
Revision Changes Path
1.4 +2 -2 jakarta-commons-sandbox/armi/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/armi/build.xml,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- build.xml 4 Jan 2002 13:15:30 -0000 1.3
+++ build.xml 4 Jan 2002 21:09:14 -0000 1.4
@@ -3,7 +3,7 @@
<!--
"Alt (to) RMI" component of the Jakarta Commons Subproject
- $Id: build.xml,v 1.3 2002/01/04 13:15:30 hammant Exp $
+ $Id: build.xml,v 1.4 2002/01/04 21:09:14 hammant Exp $
-->
@@ -92,7 +92,7 @@
<pathelement location="${build.home}/classes"/>
<pathelement location="${build.home}/tests"/>
<pathelement location="${commons-collections.jar}"/>
- <pathelement location="${junit.jar}"/>
+ <pathelement location="${junit.jar}"/>
</path>
<!-- Should all tests fail if one does? -->
1.4 +7 -1 jakarta-commons-sandbox/armi/test.xml
Index: test.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/armi/test.xml,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- test.xml 30 Dec 2001 01:12:04 -0000 1.3
+++ test.xml 4 Jan 2002 21:09:14 -0000 1.4
@@ -3,7 +3,7 @@
<!--
"Alt (to) RMI" component of the Jakarta Commons Subproject
- $Id: test.xml,v 1.3 2001/12/30 01:12:04 hammant Exp $
+ $Id: test.xml,v 1.4 2002/01/04 21:09:14 hammant Exp $
-->
@@ -43,6 +43,7 @@
<path id="testA.classpath">
<pathelement location="${build.home}/classes"/>
<pathelement location="${build.home}/classes2"/>
+ <pathelement location="lib/bcel.jar"/>
</path>
<path id="testB1.classpath">
@@ -195,5 +196,10 @@
</java>
</target>
+ <target name="test4c" depends="generate">
+ <java classname="org.apache.commons.armi.test.DirectTestC" fork="true">
+ <classpath refid="testA.classpath"/>
+ </java>
+ </target>
</project>
1.3 +123 -165
jakarta-commons-sandbox/armi/src/java/org/apache/commons/armi/server/impl/BCELClassRetriever.java
Index: BCELClassRetriever.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/armi/src/java/org/apache/commons/armi/server/impl/BCELClassRetriever.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- BCELClassRetriever.java 4 Jan 2002 13:27:23 -0000 1.2
+++ BCELClassRetriever.java 4 Jan 2002 21:09:14 -0000 1.3
@@ -1,8 +1,8 @@
/*
- * $Header:
/home/cvs/jakarta-commons-sandbox/armi/src/java/org/apache/commons/armi/server/impl/BCELClassRetriever.java,v
1.2 2002/01/04 13:27:23 hammant Exp $
- * $Revision: 1.2 $
- * $Date: 2002/01/04 13:27:23 $
+ * $Header:
/home/cvs/jakarta-commons-sandbox/armi/src/java/org/apache/commons/armi/server/impl/BCELClassRetriever.java,v
1.3 2002/01/04 21:09:14 hammant Exp $
+ * $Revision: 1.3 $
+ * $Date: 2002/01/04 21:09:14 $
*
* ====================================================================
*
@@ -71,6 +71,14 @@
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.ConstantPoolGen;
+import org.apache.bcel.generic.MethodGen;
+import org.apache.bcel.generic.Type;
+import org.apache.bcel.generic.ArrayType;
+import org.apache.bcel.generic.LocalVariableGen;
+import org.apache.bcel.generic.ObjectType;
+import org.apache.bcel.generic.InstructionConstants;
+import org.apache.bcel.generic.InstructionFactory;
+import org.apache.bcel.generic.ASTORE;
import java.net.URLClassLoader;
import java.net.URL;
@@ -80,6 +88,7 @@
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.PrintWriter;
+import java.io.IOException;
import java.util.HashMap;
import java.util.Vector;
import java.lang.reflect.Method;
@@ -90,7 +99,7 @@
*
*
* @author Paul Hammant <a
href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class BCELClassRetriever extends AbstractMethodHandler implements
ClassRetriever {
@@ -127,7 +136,7 @@
private HashMap mClasses = new HashMap();
- PrintWriter mClassSource = null; // TODO delete this from source when completed
+ PrintWriter mClassSource = new PrintWriter(System.out); // TODO delete this
from source when completed
public void generate(String asName, Class[] interfacesToPublish) {
String[] interfaces = new String[interfacesToPublish.length];
@@ -140,7 +149,7 @@
interfaces);
ConstantPoolGen cp = cg.getConstantPool(); // cg creates constant pool
- InstructionList il = new InstructionList();
+ InstructionFactory ifactory = new InstructionFactory(cg);
cg.addEmptyConstructor(Constants.ACC_PUBLIC);
Vector methodsDone = new Vector();
@@ -153,189 +162,138 @@
String methodSignature = super.getMethodSignature(method);
if (!methodsDone.contains(methodSignature)) {
methodsDone.add(methodSignature);
- generate(methodSignature, asName, cg, cp, il, method);
+ generate(methodSignature, asName, cg, cp, ifactory, method);
}
}
}
mClasses.put(asName, cg.getJavaClass().getBytes());
+ try {
+ cg.getJavaClass().dump("Abcdabcd.class");
+ } catch (IOException ioe) {}
}
// Much of the following taken from PrimarySourceGenerator.
// Lots to convert before this will work.
- private void generate(String methodSignature, String asName, ClassGen cg,
ConstantPoolGen cpg, InstructionList il, Method method) {
-
- String rClass = method.getReturnType().getName();
- String mName = method.getName();
-
- mClassSource.print(" public " + rClass + " " + mName + " (");
-
- Class[] argTypes = method.getParameterTypes();
-
- for (int i = 0; i < argTypes.length; i++) {
- String cn = argTypes[i].getName();
-
- generateParameter(cn, argTypes, i);
- }
-
- mClassSource.print(") ");
-
- Class[] throwsTypes = method.getExceptionTypes();
-
- for (int i = 0; i < throwsTypes.length; i++) {
- generateThrows(i, throwsTypes);
- }
-
- mClassSource.println("{");
- mClassSource.println(" Object[] args = new Object[" +
argTypes.length
- + "];");
-
- for (int i = 0; i < argTypes.length; i++) {
- String cn = argTypes[i].getName();
-
- generateAssignLine(cn, i);
- }
-
- mClassSource.println(" try {");
-
- if (rClass.equals("void")) {
- mClassSource.println(" armiProcessVoidRequest(\""
- + methodSignature.toString() +
"\",args);");
- } else {
- mClassSource.println(" Object retVal =
armiProcessObjectRequest(\""
- + methodSignature.toString() +
"\",args);");
- generateReturnLine(rClass);
- }
-
- mClassSource.println(" } catch (Throwable t) {");
-
- throwsTypes = method.getExceptionTypes();
-
- for (int i = 0; i < throwsTypes.length; i++) {
- generateThrowHandler(i, throwsTypes);
- }
-
- mClassSource.println(" if (t instanceof RuntimeException)
{ ");
- mClassSource.println(" throw (RuntimeException) t;");
- mClassSource.println(" } else if (t instanceof Error) { ");
- mClassSource.println(" throw (Error) t;");
- mClassSource.println(" } else { ");
- mClassSource.println(
- " throw new
org.apache.commons.armi.common.ArmiInvocationException(\"Should never get here\" +
t.getMessage()); ");
- mClassSource.println(" }");
- mClassSource.println(" }");
- mClassSource.println(" }");
-
- }
-
- private void generateParameter(String cn, Class[] argTypes, int i) {
-
- if (cn.startsWith("L")) {
- mClassSource.print(argTypes[i].getName());
- } else if (cn.startsWith("[L")) {
- mClassSource.print(argTypes[i].getName() + "[]");
- } else if (cn.equals("[B")) {
- mClassSource.print("byte[]");
- } else if (cn.equals("[C")) {
- mClassSource.print("char[]");
- } else if (cn.equals("[D")) {
- mClassSource.print("double[]");
- } else if (cn.equals("[F")) {
- mClassSource.print("float[]");
- } else if (cn.equals("[I")) {
- mClassSource.print("int[]");
- } else if (cn.equals("[J")) {
- mClassSource.print("long[]");
- } else if (cn.equals("[S")) {
- mClassSource.print("short[]");
- } else if (cn.equals("[Z")) {
- mClassSource.print("boolean[]");
- } else {
- mClassSource.print(cn);
- }
-
- mClassSource.print(" v" + i);
+ private void generate(String methodSignature, String asName, ClassGen cg,
ConstantPoolGen cp, InstructionFactory iFactory, Method method) {
- if (i + 1 < argTypes.length) {
- mClassSource.print(", ");
- }
- }
-
- private void generateAssignLine(String cn, int i) {
-
- if (cn.equals("int")) {
- mClassSource.println(" args[" + i + "] = new Integer(v" + i + ");");
- } else if (cn.equals("short")) {
- mClassSource.println(" args[" + i + "] = new Short(v" + i + ");");
- } else if (cn.equals("float")) {
- mClassSource.println(" args[" + i + "] = new Float(v" + i + ");");
- } else if (cn.equals("double")) {
- mClassSource.println(" args[" + i + "] = new Double(v" + i + ");");
- } else if (cn.equals("long")) {
- mClassSource.println(" args[" + i + "] = new Long(v" + i + ");");
- } else if (cn.equals("char")) {
- mClassSource.println(" args[" + i + "] = new Character(v" + i +
");");
- } else if (cn.equals("boolean")) {
- mClassSource.println(" args[" + i + "] = new Boolean(v" + i + ");");
- } else if (cn.equals("byte")) {
- mClassSource.println(" args[" + i + "] = new Byte(v" + i + ");");
+ String rClass = method.getReturnType().getName();
+ Type rType = null;
+ if (rClass.equals("void")) {
+ rType = Type.VOID;
} else {
- mClassSource.println(" args[" + i + "] = v" + i + ";");
+ rType = new ObjectType(rClass);
+ //Type.getType(rClass);
}
- }
+ String name = method.getName();
- private void generateReturnLine(String rClass) {
-
- if (rClass.equals("boolean")) {
- mClassSource.println(" return ((Boolean) retVal).booleanValue();");
- } else if (rClass.equals("integer")) {
- mClassSource.println(" return ((Integer) retVal).intValue();");
- } else if (rClass.equals("short")) {
- mClassSource.println(" return ((Short) retVal).shortValue();");
- } else if (rClass.equals("float")) {
- mClassSource.println(" return ((Float) retVal).floatValue();");
- } else if (rClass.equals("double")) {
- mClassSource.println(" return ((Double) retVal).doubleValue();");
- } else if (rClass.equals("long")) {
- mClassSource.println(" return ((Long) retVal).longValue();");
- } else if (rClass.equals("char")) {
- mClassSource.println(" return ((Character) retVal).charValue();");
- } else if (rClass.equals("byte")) {
- mClassSource.println(" return ((Byte) retVal).byteValue();");
- } else {
- mClassSource.println(" return (" + rClass + ") retVal;");
+ Class[] pClasses = method.getParameterTypes();
+ Type[] pTypes = new Type[pClasses.length];
+ String[] pNames = new String[pClasses.length];
+ for (int i = 0; i < pClasses.length; i++) {
+ System.out.println("pC " + pClasses[i].getName());
+ pTypes[i] = new ObjectType(pClasses[i].getName());
+ //Type.getType(pClasses[i].getName());
+ pNames[i] = "v" + i;
}
- }
- private void generateThrowHandler(int i, Class[] throwsTypes) {
-
- if (i == 0) {
- mClassSource.println(" if (t instanceof " +
throwsTypes[i].getName() + ") { ");
- } else {
- mClassSource.println(" } else if (t instanceof " +
throwsTypes[i].getName()
- + ") { ");
- }
+ InstructionList il = new InstructionList();
- mClassSource.println(" throw (" + throwsTypes[i].getName() + ") t;");
+ MethodGen mg = new MethodGen(Constants.ACC_PUBLIC,// access flags
+ rType, // return type
+ pTypes, // argument types
+ pNames, // arg names
+ name, "ArmiGenerated" + asName, // method, class
+ il, cp);
+
+ Type argArrayType = new ArrayType("java.lang.Object", 1);
+
+ // Create Argument Array.
+ LocalVariableGen lg = mg.addLocalVariable("args",
+ argArrayType,
+ null, null);
+ int argsIx = lg.getIndex();
+ lg.setStart(il.append(new ASTORE(argsIx))); // `i' valid from here
+
+ // Instantiate Argument Array.
+
+ for (int i = 0; i < pClasses.length; i++) {
+ lg = mg.addLocalVariable("V" + i,
+ new ObjectType("java.io.BufferedReader"),
+ null, null);
+ int vX = lg.getIndex();
+ lg.setStart(il.append(new ASTORE(vX))); // `vX' valid from here
- if (i + 1 == throwsTypes.length) {
- mClassSource.print(" } else");
}
- }
- private void generateThrows(int i, Class[] throwsTypes) {
+ il.append(iFactory.createPrintln("Hello?!"));
+ il.append(InstructionConstants.RETURN);
- if (i == 0) {
- mClassSource.print("throws ");
- }
+ mg.stripAttributes(true);
+ mg.setMaxStack();
+ mg.setMaxLocals();
+
+ //mClassSource.print(" public " + rClass + " " + mName + " (");
+ //Class[] argTypes = method.getParameterTypes();
+
+ //for (int i = 0; i < argTypes.length; i++) {
+ // String cn = argTypes[i].getName();
+ //
+ // generateParameter(cn, argTypes, i);
+ //}
+
+ //mClassSource.print(") ");
+
+ //Class[] throwsTypes = method.getExceptionTypes();
+
+ //for (int i = 0; i < throwsTypes.length; i++) {
+ // generateThrows(i, throwsTypes);
+ //}
+
+ //mClassSource.println("{");
+ //mClassSource.println(" Object[] args = new Object[" + argTypes.length
+ // + "];");
+
+ //for (int i = 0; i < argTypes.length; i++) {
+ // String cn = argTypes[i].getName();
+ //
+ // generateAssignLine(cn, i);
+ //}
+
+ //mClassSource.println(" try {");
+
+ //if (rClass.equals("void")) {
+ // mClassSource.println(" armiProcessVoidRequest(\""
+ // + methodSignature.toString() + "\",args);");
+ //} else {
+ // mClassSource.println(" Object retVal =
armiProcessObjectRequest(\""
+ // + methodSignature.toString() + "\",args);");
+ // generateReturnLine(rClass);
+ //}
+
+ //mClassSource.println(" } catch (Throwable t) {");
+
+ //throwsTypes = method.getExceptionTypes();
+
+ //for (int i = 0; i < throwsTypes.length; i++) {
+ // generateThrowHandler(i, throwsTypes);
+ //}
+
+ //mClassSource.println(" if (t instanceof RuntimeException) { ");
+ //mClassSource.println(" throw (RuntimeException) t;");
+ //mClassSource.println(" } else if (t instanceof Error) { ");
+ //mClassSource.println(" throw (Error) t;");
+ //mClassSource.println(" } else { ");
+ //mClassSource.println(
+ // " throw new
org.apache.commons.armi.common.ArmiInvocationException(\"Should never get here\" +
t.getMessage()); ");
+ //mClassSource.println(" }");
+ //mClassSource.println(" }");
+ //mClassSource.println(" }");
- mClassSource.print(throwsTypes[i].getName());
+ cg.addMethod(mg.getMethod());
- if (i + 1 < throwsTypes.length) {
- mClassSource.print(", ");
- }
}
/**
1.1
jakarta-commons-sandbox/armi/src/java/org/apache/commons/armi/test/DirectTestC.java
Index: DirectTestC.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/armi/src/java/org/apache/commons/armi/test/DirectTestC.java,v
1.1 2002/01/04 21:09:14 hammant Exp $
* $Revision: 1.1 $
* $Date: 2002/01/04 21:09:14 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.armi.test;
import org.apache.commons.armi.server.impl.direct.DirectServer;
import org.apache.commons.armi.server.impl.direct.DirectArmiInovcationAdapter;
import org.apache.commons.armi.server.impl.BCELClassRetriever;
import org.apache.commons.armi.server.ClassRetriever;
import org.apache.commons.armi.common.ArmiInvocationHandler;
import org.apache.commons.armi.common.ArmiConnectionException;
import org.apache.commons.armi.client.ArmiHostContext;
import org.apache.commons.armi.client.ArmiFactory;
import org.apache.commons.armi.client.impl.direct.DirectArmiHostContext;
import org.apache.commons.armi.client.impl.ServerClassArmiFactory;
import org.apache.commons.armi.client.impl.ClientClassArmiFactory;
/**
* Class DirectTest
*
*
* @author Paul Hammant <a
href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
* @version $Revision: 1.1 $
*/
public class DirectTestC extends DirectTest {
private static String mServerOrClientFactory;
/**
* Method main
*
*
* @param args
*
* @throws Exception
*
*/
public static void main(String[] args) throws Exception {
System.out.println("Direct Test BCEL");
mServerOrClientFactory = args[0];
DirectServer ds = new DirectServer();
BCELClassRetriever bcr = new BCELClassRetriever();
bcr.generate("Hello", new Class[] { TestInterface.class });
ds.setClassRetriever(bcr);
TestInterfaceImpl ti = new TestInterfaceImpl();
ds.publish(ti, "Hello", TestInterface.class);
ds.start();
DirectTestClient dtc = new DirectTestClient(new
DirectArmiInovcationAdapter(ds));
Thread thread = new Thread(dtc);
thread.start();
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>