You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hivemind.apache.org by hl...@apache.org on 2004/11/04 04:19:20 UTC
cvs commit: jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl ClassFabImpl.java MethodFabImpl.java
hlship 2004/11/03 19:19:20
Modified: . status.xml
framework/src/java/org/apache/hivemind/service/impl
ClassFabImpl.java MethodFabImpl.java
Log:
Add toString() support to ClassFab and MethodFab.
Revision Changes Path
1.75 +3 -0 jakarta-hivemind/status.xml
Index: status.xml
===================================================================
RCS file: /home/cvs/jakarta-hivemind/status.xml,v
retrieving revision 1.74
retrieving revision 1.75
diff -u -r1.74 -r1.75
--- status.xml 2 Nov 2004 22:32:16 -0000 1.74
+++ status.xml 4 Nov 2004 03:19:20 -0000 1.75
@@ -82,6 +82,9 @@
<action type="update" dev="HLS">
Switch Javassist to version 3.0-rc-1.
</action>
+ <action type="add" dev="HLS">
+ Add toString() support to ClassFab and MethodFab.
+ </action>
</release>
<release version="1.0" date="Sep 22 2004">
1.6 +212 -17 jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl/ClassFabImpl.java
Index: ClassFabImpl.java
===================================================================
RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl/ClassFabImpl.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ClassFabImpl.java 12 Jun 2004 18:43:41 -0000 1.5
+++ ClassFabImpl.java 4 Nov 2004 03:19:20 -0000 1.6
@@ -15,7 +15,10 @@
package org.apache.hivemind.service.impl;
import java.lang.reflect.Modifier;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import javassist.CannotCompileException;
@@ -23,6 +26,7 @@
import javassist.CtConstructor;
import javassist.CtField;
import javassist.CtMethod;
+import javassist.NotFoundException;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.service.ClassFab;
@@ -30,21 +34,99 @@
import org.apache.hivemind.service.MethodSignature;
/**
- * Implementation of {@link org.apache.hivemind.service.ClassFab}. Hides,
- * as much as possible, the underlying library (Javassist).
- *
+ * Implementation of {@link org.apache.hivemind.service.ClassFab}. Hides, as much as possible, the
+ * underlying library (Javassist).
+ *
* @author Howard Lewis Ship
*/
public class ClassFabImpl implements ClassFab
{
- private CtClass _ctClass;
- private CtClassSource _source;
+ private final CtClass _ctClass;
+
+ private final CtClassSource _source;
+
+ /**
+ * Stores information about a constructor; used by toString().
+ *
+ * @since 1.1
+ */
+
+ private class AddedConstructor
+ {
+ private Class[] _parameterTypes;
+
+ private Class[] _exceptionTypes;
+
+ private String _body;
+
+ AddedConstructor(Class[] parameterTypes, Class[] exceptionTypes, String body)
+ {
+ _parameterTypes = parameterTypes;
+ _exceptionTypes = exceptionTypes;
+ _body = body;
+ }
+
+ public String toString()
+ {
+ StringBuffer buffer = new StringBuffer();
+
+ buffer.append("public ");
+ buffer.append(_ctClass.getName());
+
+ buffer.append("(");
+
+ int count = size(_parameterTypes);
+ for (int i = 0; i < count; i++)
+ {
+ if (i > 0)
+ buffer.append(", ");
+
+ buffer.append(_parameterTypes[i].getName());
+
+ buffer.append(" $");
+ buffer.append(i + 1);
+ }
+
+ buffer.append(")");
+
+ count = size(_exceptionTypes);
+ for (int i = 0; i < count; i++)
+ {
+ if (i == 0)
+ buffer.append("\n throws ");
+ else
+ buffer.append(", ");
+
+ buffer.append(_exceptionTypes[i].getName());
+ }
+
+ buffer.append("\n");
+ buffer.append(_body);
+
+ buffer.append("\n");
+
+ return buffer.toString();
+ }
+
+ private int size(Object[] array)
+ {
+ return array == null ? 0 : array.length;
+ }
+ }
/**
- * Map of {@link MethodFab} keyed on {@link MethodSignature}.
+ * Map of {@link MethodFab}keyed on {@link MethodSignature}.
*/
private Map _methods = new HashMap();
+ /**
+ * List of {@link AddedConstructor}.
+ *
+ * @since 1.1
+ */
+
+ private List _constructors = new ArrayList();
+
public ClassFabImpl(CtClassSource source, CtClass ctClass)
{
_source = source;
@@ -52,6 +134,115 @@
}
/**
+ * Returns a representation of the fabricated class, including inheritance, fields,
+ * constructors, methods and method bodies.
+ *
+ * @since 1.1
+ */
+ public String toString()
+ {
+ StringBuffer buffer = new StringBuffer("ClassFab[\n");
+
+ try
+ {
+ buildClassAndInheritance(buffer);
+
+ buildFields(buffer);
+
+ buildConstructors(buffer);
+
+ buildMethods(buffer);
+
+ }
+ catch (Exception ex)
+ {
+ buffer.append(" *** ");
+ buffer.append(ex);
+ }
+
+ buffer.append("\n]");
+
+ return buffer.toString();
+ }
+
+ /** @since 1.1 */
+ private void buildMethods(StringBuffer buffer)
+ {
+ Iterator i = _methods.values().iterator();
+ while (i.hasNext())
+ {
+
+ MethodFab mf = (MethodFab) i.next();
+
+ buffer.append("\n");
+ buffer.append(mf);
+ buffer.append("\n");
+ }
+ }
+
+ /** @since 1.1 */
+ private void buildConstructors(StringBuffer buffer)
+ {
+ Iterator i = _constructors.iterator();
+
+ while (i.hasNext())
+ {
+ buffer.append("\n");
+ buffer.append(i.next());
+ }
+ }
+
+ /** @since 1.1 */
+ private void buildFields(StringBuffer buffer) throws NotFoundException
+ {
+ CtField fields[] = _ctClass.getDeclaredFields();
+
+ for (int i = 0; i < fields.length; i++)
+ {
+ buffer.append("\n");
+ buffer.append(modifiers(fields[i].getModifiers()));
+ buffer.append(" ");
+ buffer.append(fields[i].getType().getName());
+ buffer.append(" ");
+ buffer.append(fields[i].getName());
+ buffer.append(";\n");
+ }
+ }
+
+ /** @since 1.1 */
+ private void buildClassAndInheritance(StringBuffer buffer) throws NotFoundException
+ {
+ buffer.append(modifiers(_ctClass.getModifiers()));
+ buffer.append(" class ");
+ buffer.append(_ctClass.getName());
+ buffer.append(" extends ");
+ buffer.append(_ctClass.getSuperclass().getName());
+ buffer.append("\n");
+
+ CtClass[] interfaces = _ctClass.getInterfaces();
+
+ if (interfaces.length > 0)
+ {
+ buffer.append(" implements ");
+
+ for (int i = 0; i < interfaces.length; i++)
+ {
+ if (i > 0)
+ buffer.append(", ");
+
+ buffer.append(interfaces[i].getName());
+ }
+
+ buffer.append("\n");
+ }
+ }
+
+ private String modifiers(int modifiers)
+ {
+ return Modifier.toString(modifiers);
+ }
+
+ /**
* Returns the name of the class fabricated by this instance.
*/
String getName()
@@ -79,9 +270,10 @@
}
catch (CannotCompileException ex)
{
- throw new ApplicationRuntimeException(
- ServiceMessages.unableToAddField(name, _ctClass, ex),
- ex);
+ throw new ApplicationRuntimeException(ServiceMessages.unableToAddField(
+ name,
+ _ctClass,
+ ex), ex);
}
}
@@ -107,14 +299,15 @@
}
catch (Exception ex)
{
- throw new ApplicationRuntimeException(
- ServiceMessages.unableToAddMethod(ms, _ctClass, ex),
- ex);
+ throw new ApplicationRuntimeException(ServiceMessages.unableToAddMethod(
+ ms,
+ _ctClass,
+ ex), ex);
}
// Return a MethodFab so the caller can add catches.
- MethodFab result = new MethodFabImpl(_source, ms, method);
+ MethodFab result = new MethodFabImpl(_source, ms, method, body);
_methods.put(ms, result);
@@ -138,12 +331,14 @@
constructor.setBody(body);
_ctClass.addConstructor(constructor);
+
+ _constructors.add(new AddedConstructor(parameterTypes, exceptions, body));
}
catch (Exception ex)
{
- throw new ApplicationRuntimeException(
- ServiceMessages.unableToAddConstructor(_ctClass, ex),
- ex);
+ throw new ApplicationRuntimeException(ServiceMessages.unableToAddConstructor(
+ _ctClass,
+ ex), ex);
}
}
@@ -170,4 +365,4 @@
return _source.createClass(_ctClass);
}
-}
+}
\ No newline at end of file
1.5 +88 -13 jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl/MethodFabImpl.java
Index: MethodFabImpl.java
===================================================================
RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl/MethodFabImpl.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- MethodFabImpl.java 12 Jun 2004 18:43:41 -0000 1.4
+++ MethodFabImpl.java 4 Nov 2004 03:19:20 -0000 1.5
@@ -14,6 +14,8 @@
package org.apache.hivemind.service.impl;
+import java.lang.reflect.Modifier;
+
import javassist.CtClass;
import javassist.CtMethod;
@@ -22,24 +24,86 @@
import org.apache.hivemind.service.MethodSignature;
/**
- * Implementation of {@link org.apache.hivemind.service.MethodFab},
- * which is returned
- * from {@link org.apache.hivemind.service.ClassFab#addMethod(int, String, Class, Class[], Class[], String)},
+ * Implementation of {@link org.apache.hivemind.service.MethodFab}, which is returned from
+ * {@link org.apache.hivemind.service.ClassFab#addMethod(int, String, Class, Class[], Class[], String)},
* so that additional exception handlers may be attached to the added method.
- *
+ *
* @author Howard Lewis Ship
*/
class MethodFabImpl implements MethodFab
{
private CtClassSource _source;
+
private MethodSignature _signature;
+
private CtMethod _method;
- public MethodFabImpl(CtClassSource source, MethodSignature signature, CtMethod method)
+ private StringBuffer _descriptionBody = new StringBuffer();
+
+ public MethodFabImpl(CtClassSource source, MethodSignature signature, CtMethod method,
+ String body)
{
_source = source;
_signature = signature;
_method = method;
+
+ _descriptionBody.append(body);
+ }
+
+ /**
+ * Returns a a text representation of the method, parameters and method body.
+ */
+ public String toString()
+ {
+ StringBuffer buffer = new StringBuffer();
+
+ try
+ {
+ buffer.append(Modifier.toString(_method.getModifiers()));
+
+ buffer.append(" ");
+ buffer.append(_method.getReturnType().getName());
+
+ buffer.append(" ");
+ buffer.append(_method.getName());
+ buffer.append("(");
+
+ CtClass[] params = _method.getParameterTypes();
+
+ for (int i = 0; i < params.length; i++)
+ {
+ if (i > 0)
+ buffer.append(", ");
+
+ buffer.append(params[i].getName());
+
+ buffer.append(" $");
+ buffer.append(i + 1);
+ }
+ buffer.append(")");
+
+ CtClass[] exceptions = _method.getExceptionTypes();
+
+ for (int i = 0; i < exceptions.length; i++)
+ {
+ if (i == 0)
+ buffer.append("\n throws ");
+ else
+ buffer.append(", ");
+
+ buffer.append(exceptions[i].getName());
+ }
+
+ buffer.append("\n");
+ buffer.append(_descriptionBody);
+ }
+ catch (Exception ex)
+ {
+ buffer.append(" *** ");
+ buffer.append(ex);
+ }
+
+ return buffer.toString();
}
public void addCatch(Class exceptionClass, String catchBody)
@@ -52,10 +116,16 @@
}
catch (Exception ex)
{
- throw new ApplicationRuntimeException(
- ServiceMessages.unableToAddCatch(exceptionClass, _method, ex),
- ex);
+ throw new ApplicationRuntimeException(ServiceMessages.unableToAddCatch(
+ exceptionClass,
+ _method,
+ ex), ex);
}
+
+ _descriptionBody.append("\ncatch(");
+ _descriptionBody.append(exceptionClass.getName());
+ _descriptionBody.append(" $e)\n");
+ _descriptionBody.append(catchBody);
}
public void extend(String body, boolean asFinally)
@@ -66,13 +136,18 @@
}
catch (Exception ex)
{
- throw new ApplicationRuntimeException(
- ServiceMessages.unableToExtendMethod(
+ throw new ApplicationRuntimeException(ServiceMessages.unableToExtendMethod(
_signature,
_method.getDeclaringClass().getName(),
- ex),
- ex);
+ ex), ex);
}
+
+ _descriptionBody.append("\n");
+
+ if (asFinally)
+ _descriptionBody.append("finally\n");
+
+ _descriptionBody.append(body);
}
-}
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-cvs-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-cvs-help@jakarta.apache.org