You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2004/12/02 23:01:08 UTC
cvs commit: jakarta-tapestry/framework/src/java/org/apache/tapestry/test ScriptStrings.properties ScriptMessages.java Creator.java
hlship 2004/12/02 14:01:08
Modified: framework/src/test/org/apache/tapestry/test TestCreator.java
framework/src/java/org/apache/tapestry/test
ScriptStrings.properties ScriptMessages.java
Creator.java
Log:
Simplify Creator by making use the existing class enhancement subsystem.
Revision Changes Path
1.2 +0 -15 jakarta-tapestry/framework/src/test/org/apache/tapestry/test/TestCreator.java
Index: TestCreator.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/test/TestCreator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestCreator.java 29 Oct 2004 17:13:40 -0000 1.1
+++ TestCreator.java 2 Dec 2004 22:01:08 -0000 1.2
@@ -14,7 +14,6 @@
package org.apache.tapestry.test;
-import java.util.ArrayList;
import java.util.List;
import org.apache.hivemind.test.HiveMindTestCase;
@@ -27,20 +26,6 @@
*/
public class TestCreator extends HiveMindTestCase
{
-
- public void testNonAbstract() throws Exception
- {
- interceptLogging("org.apache.tapestry.test");
-
- Creator i = new Creator();
-
- Object result = i.getInstance(ArrayList.class);
-
- assertEquals(ArrayList.class, result.getClass());
-
- assertLoggedMessage("Class java.util.ArrayList is not an abstract class.");
-
- }
public void testInterface() throws Exception
{
1.2 +0 -2 jakarta-tapestry/framework/src/java/org/apache/tapestry/test/ScriptStrings.properties
Index: ScriptStrings.properties
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/test/ScriptStrings.properties,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ScriptStrings.properties 3 Sep 2004 17:46:15 -0000 1.1
+++ ScriptStrings.properties 2 Dec 2004 22:01:08 -0000 1.2
@@ -23,6 +23,4 @@
invalid-int-attribute=Attribute ''{0}'' (of element {1}, at {2}) is ''{3}'', which is not an integer value.
wrong-type-for-enhancement=Can not create instance of {0}. Interfaces, arrays and primitive types may not be enhanced.
-class-not-abstract=Class {0} is not an abstract class.
-unable-to-introspect=Unable to introspect class {0}: {1}
unable-to-instantiate=Unable to instantiate enhanced subclass of {0}: {2}
1.3 +25 -51 jakarta-tapestry/framework/src/java/org/apache/tapestry/test/ScriptMessages.java
Index: ScriptMessages.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/test/ScriptMessages.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ScriptMessages.java 16 Sep 2004 00:26:37 -0000 1.2
+++ ScriptMessages.java 2 Dec 2004 22:01:08 -0000 1.3
@@ -19,23 +19,22 @@
import org.apache.hivemind.service.ClassFabUtils;
/**
- * Container of static methods to format logging and exception messages, used
- * within the org.apache.tapesty.test package (and a few sub-packages).
+ * Container of static methods to format logging and exception messages, used within the
+ * org.apache.tapesty.test package (and a few sub-packages).
+ * <p>
+ * Technically, these are messages for the test package, and this class should be called
+ * TestMessages ... but that's always a bad idea (it makes the class look like a JUnit test suite).
+ * <p>
+ * This class is public, not package private, because some related sub-packages make use of it as
+ * well.
*
- * <p>Technically, these are messages for the test package, and this class
- * should be called TestMessages ... but that's always a bad idea (it makes
- * the class look like a JUnit test suite).
- *
- * <p>This class is public, not package private, because some related
- * sub-packages make use of it as well.
- *
* @author Howard Lewis Ship
* @since 3.1
*/
public final class ScriptMessages
{
- private static final MessageFormatter _formatter =
- new MessageFormatter(ScriptMessages.class, "ScriptStrings");
+ private static final MessageFormatter _formatter = new MessageFormatter(ScriptMessages.class,
+ "ScriptStrings");
public static String expectedSubstringMissing(String substring, Location location)
{
@@ -57,59 +56,34 @@
return _formatter.format("missing-required-attribute", attributeName, elementName);
}
- public static String invalidIntAttribute(
- String attributeName,
- String elementName,
- Location location,
- String attributeValue)
- {
- return _formatter.format(
- "invalid-int-attribute",
- new Object[] { attributeName, elementName, location, attributeValue });
- }
-
- public static String incorrectRegexpMatch(
- String expectedMatch,
- Location location,
- String actualMatch)
+ public static String invalidIntAttribute(String attributeName, String elementName,
+ Location location, String attributeValue)
{
- return _formatter.format("incorrect-regexp-match", expectedMatch, location, actualMatch);
+ return _formatter.format("invalid-int-attribute", new Object[]
+ { attributeName, elementName, location, attributeValue });
}
- public static String incorrectRegexpMatchCount(
- String pattern,
- Location location,
- int expectedCount,
- int actualCount)
- {
- return _formatter.format(
- "incorrect-regexp-match-count",
- new Object[] {
- pattern,
- location,
- new Integer(expectedCount),
- new Integer(actualCount)});
- }
-
- public static String wrongTypeForEnhancement(Class type)
+ public static String incorrectRegexpMatch(String expectedMatch, Location location,
+ String actualMatch)
{
- return _formatter.format(
- "wrong-type-for-enhancement",
- ClassFabUtils.getJavaClassName(type));
+ return _formatter.format("incorrect-regexp-match", expectedMatch, location, actualMatch);
}
- public static String classNotAbstract(Class type)
+ public static String incorrectRegexpMatchCount(String pattern, Location location,
+ int expectedCount, int actualCount)
{
- return _formatter.format("class-not-abstract", type.getName());
+ return _formatter.format("incorrect-regexp-match-count", new Object[]
+ { pattern, location, new Integer(expectedCount), new Integer(actualCount) });
}
- public static String unableToIntrospect(Class type, Throwable cause)
+ public static String wrongTypeForEnhancement(Class type)
{
- return _formatter.format("unable-to-introspect", type.getName(), cause);
+ return _formatter
+ .format("wrong-type-for-enhancement", ClassFabUtils.getJavaClassName(type));
}
public static String unableToInstantiate(Class abstractClass, Throwable cause)
{
return _formatter.format("unable-to-instantiate", abstractClass.getName(), cause);
}
-}
+}
\ No newline at end of file
1.4 +34 -132 jakarta-tapestry/framework/src/java/org/apache/tapestry/test/Creator.java
Index: Creator.java
===================================================================
RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/test/Creator.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Creator.java 16 Sep 2004 00:26:37 -0000 1.3
+++ Creator.java 2 Dec 2004 22:01:08 -0000 1.4
@@ -28,20 +28,27 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hivemind.ApplicationRuntimeException;
+import org.apache.hivemind.ClassResolver;
+import org.apache.hivemind.impl.DefaultClassResolver;
import org.apache.hivemind.service.ClassFab;
import org.apache.hivemind.service.ClassFabUtils;
+import org.apache.hivemind.service.ClassFactory;
import org.apache.hivemind.service.MethodSignature;
import org.apache.hivemind.service.impl.ClassFabImpl;
+import org.apache.hivemind.service.impl.ClassFactoryImpl;
import org.apache.hivemind.service.impl.CtClassSource;
import org.apache.hivemind.service.impl.HiveMindClassPool;
+import org.apache.tapestry.enhance.AbstractPropertyWorker;
+import org.apache.tapestry.enhance.EnhancementOperationImpl;
+import org.apache.tapestry.services.ComponentConstructor;
+import org.apache.tapestry.spec.ComponentSpecification;
/**
- * A utility class that is used to instantiate abstract Tapestry pages and
- * components. It creates, at runtime, a subclass where all abstract properties
- * are filled in (complete with instance variable, accessor and mutator methods).
- * This isn't the same as how the class is enhanced at runtime, but is sufficient
- * to unit test the class, especially listener methods.
- *
+ * A utility class that is used to instantiate abstract Tapestry pages and components. It creates,
+ * at runtime, a subclass where all abstract properties are filled in (complete with instance
+ * variable, accessor and mutator methods). This isn't the same as how the class is enhanced at
+ * runtime, but is sufficient to unit test the class, especially listener methods.
+ *
* @author Howard Lewis Ship
* @since 3.1
*/
@@ -50,164 +57,59 @@
private static final Log LOG = LogFactory.getLog(Creator.class);
/**
- * Keyed on Class, value is another class (fully enhanced).
+ * Keyed on Class, value is an {@link ComponentConstructor}.
*/
- private Map _classes = new HashMap();
- private final CtClassSource _classSource;
+ private Map _constructors = new HashMap();
- public Creator()
- {
- this(Thread.currentThread().getContextClassLoader());
- }
-
- public Creator(ClassLoader loader)
- {
- HiveMindClassPool pool = new HiveMindClassPool();
-
- pool.appendClassLoader(loader);
-
- _classSource = new CtClassSource(pool);
- }
-
- private void addAccessorMethod(ClassFab classFab, PropertyDescriptor pd, String attributeName)
- {
- String methodName = getMethodName(pd.getReadMethod(), "get", pd.getName());
-
- MethodSignature sig = new MethodSignature(pd.getPropertyType(), methodName, null, null);
-
- classFab.addMethod(Modifier.PUBLIC, sig, "return " + attributeName + ";");
- }
-
- private void addField(ClassFab classFab, String fieldName, Class fieldType)
- {
- classFab.addField(fieldName, fieldType);
- }
-
- private void addMissingProperties(ClassFab classFab, BeanInfo info)
- {
- PropertyDescriptor[] pd = info.getPropertyDescriptors();
-
- for (int i = 0; i < pd.length; i++)
- addMissingProperty(classFab, pd[i]);
- }
-
- private void addMissingProperty(ClassFab classFab, PropertyDescriptor pd)
- {
- Method readMethod = pd.getReadMethod();
- Method writeMethod = pd.getWriteMethod();
-
- boolean abstractRead = isAbstract(readMethod);
- boolean abstractWrite = isAbstract(writeMethod);
-
- if (!(abstractRead || abstractWrite))
- return;
-
- String attributeName = "_$" + pd.getName();
- Class propertyType = pd.getPropertyType();
-
- addField(classFab, attributeName, propertyType);
-
- if (abstractRead)
- addAccessorMethod(classFab, pd, attributeName);
-
- if (abstractWrite)
- addMutatorMethod(classFab, pd, attributeName);
+ private ClassFactory _classFactory = new ClassFactoryImpl();
- }
+ private ClassResolver _classResolver = new DefaultClassResolver();
- private void addMutatorMethod(ClassFab classFab, PropertyDescriptor pd, String attributeName)
- {
- String methodName = getMethodName(pd.getWriteMethod(), "set", pd.getName());
-
- MethodSignature sig =
- new MethodSignature(void.class, methodName, new Class[] { pd.getPropertyType()}, null);
-
- classFab.addMethod(Modifier.PUBLIC, sig, attributeName + " = $1;");
- }
-
- private Class createEnhancedClass(Class inputClass)
+ private ComponentConstructor createComponentConstructor(Class inputClass)
{
if (inputClass.isInterface() || inputClass.isPrimitive() || inputClass.isArray())
throw new IllegalArgumentException(ScriptMessages.wrongTypeForEnhancement(inputClass));
- if (!Modifier.isAbstract(inputClass.getModifiers()))
- {
- LOG.error(ScriptMessages.classNotAbstract(inputClass));
- return inputClass;
- }
-
- BeanInfo info = null;
+ EnhancementOperationImpl op = new EnhancementOperationImpl(_classResolver,
+ new ComponentSpecification(), inputClass, _classFactory);
- try
- {
- info = Introspector.getBeanInfo(inputClass, Object.class);
- }
- catch (IntrospectionException ex)
- {
- throw new ApplicationRuntimeException(
- ScriptMessages.unableToIntrospect(inputClass, ex));
- }
+ new AbstractPropertyWorker().performEnhancement(op);
- String name = ClassFabUtils.generateClassName("Enhance");
-
- CtClass newClass = _classSource.newClass(name, inputClass);
-
- ClassFab classFab = new ClassFabImpl(_classSource, newClass);
-
- addMissingProperties(classFab, info);
-
- return classFab.createClass();
+ return op.getConstructor();
}
- public Class getEnhancedClass(Class inputClass)
+ private ComponentConstructor getComponentConstructor(Class inputClass)
{
- Class result = (Class) _classes.get(inputClass);
+ ComponentConstructor result = (ComponentConstructor) _constructors.get(inputClass);
if (result == null)
{
- result = createEnhancedClass(inputClass);
+ result = createComponentConstructor(inputClass);
- _classes.put(inputClass, result);
+ _constructors.put(inputClass, result);
}
return result;
}
/**
- * Given a particular abstract class; will create an instance of that class. A subclass
- * is created with all abstract properties filled in with ordinary implementations.
+ * Given a particular abstract class; will create an instance of that class. A subclass is
+ * created with all abstract properties filled in with ordinary implementations.
*/
public Object getInstance(Class abstractClass)
{
- Class enhancedClass = getEnhancedClass(abstractClass);
+ ComponentConstructor constructor = getComponentConstructor(abstractClass);
try
{
- return enhancedClass.newInstance();
+ return constructor.newInstance();
}
catch (Exception ex)
{
- throw new ApplicationRuntimeException(
- ScriptMessages.unableToInstantiate(abstractClass, ex));
+ throw new ApplicationRuntimeException(ScriptMessages.unableToInstantiate(
+ abstractClass,
+ ex));
}
}
-
- private String getMethodName(Method m, String prefix, String propertyName)
- {
- if (m != null)
- return m.getName();
-
- StringBuffer buffer = new StringBuffer(prefix);
-
- buffer.append(propertyName.substring(0, 1).toUpperCase());
- buffer.append(propertyName.substring(1));
-
- return buffer.toString();
- }
-
- private boolean isAbstract(Method m)
- {
- return m == null || Modifier.isAbstract(m.getModifiers());
- }
-}
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org