Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/services/ClassFabUtils.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/services/ClassFabUtils.java?rev=651797&r1=651796&r2=651797&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/services/ClassFabUtils.java (original) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/services/ClassFabUtils.java Fri Apr 25 20:00:55 2008 @@ -21,32 +21,32 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Map; +import java.util.concurrent.atomic.AtomicLong; /** * Handy method useful when creating new classes using [EMAIL PROTECTED] org.apache.tapestry.ioc.services.ClassFab}. */ public final class ClassFabUtils { - private static long _uid = System.currentTimeMillis(); + private static final AtomicLong UID_GENERATOR = new AtomicLong(System.currentTimeMillis()); - private ClassFabUtils() + private static String nextUID() { + return Long.toHexString(UID_GENERATOR.getAndIncrement()); } /** * Generates a unique class name, which will be in the default package. */ - public static synchronized String generateClassName(String baseName) { - return "$" + baseName + "_" + Long.toHexString(_uid++); + return "$" + baseName + "_" + nextUID(); } /** * Returns a class name derived from the provided interfaceClass. The package part of the interface name is stripped * out, and the result passed to [EMAIL PROTECTED] #generateClassName(String)}. */ - public static String generateClassName(Class interfaceClass) { return generateClassName(interfaceClass.getSimpleName()); @@ -77,6 +77,11 @@ return method.getReturnType().equals(String.class); } + public static Class getPrimitiveType(String primitiveTypeName) + { + return PRIMITIVE_TYPE_NAME_TO_PRIMITIVE_INFO.get(primitiveTypeName).getPrimitiveType(); + } + private static class PrimitiveInfo { private final Class _primitiveType; @@ -172,47 +177,62 @@ } /** - * Given one of the primitive types, returns the name of the method that will unwrap the wrapped type to the - * primitive type. + * Given a wrapper type, determines the corresponding primitive type. */ - public static String getUnwrapMethodName(String primitiveTypeName) + public static Class getPrimitiveType(Class wrapperType) { - return PRIMITIVE_TYPE_NAME_TO_PRIMITIVE_INFO.get(primitiveTypeName).getUnwrapMethod(); + return WRAPPER_TYPE_TO_PRIMITIVE_INFO.get(wrapperType).getPrimitiveType(); } - public static String getUnwrapMethodName(Class wrapperType) + /** + * Returns the wrapper type for an input type; for most types, this is the type. For primitive types, it is the + * corresponding wrapper type. + * + * @param type type to check + * @return type or corresponding wrapper type + */ + public static Class getWrapperType(Class type) { - PrimitiveInfo info = WRAPPER_TYPE_TO_PRIMITIVE_INFO.get(wrapperType); + PrimitiveInfo info = PRIMITIVE_TYPE_NAME_TO_PRIMITIVE_INFO.get(type.getName()); - return info.getUnwrapMethod(); + return info == null ? type : info.getWrapperType(); } /** - * Given the name of a primitive type, returns the name of the corresponding wrapper class. + * Takes a reference and casts it to the desired type. If the desired type is a primitive type, then the reference + * is cast to the correct wrapper type and a call to the correct unwrapper method is added. The end result is code + * that can be assigned to a field or parameter of the desired type (even if desired type is a primitive). + * + * @param reference to be cast + * @param desiredType desired object or primitive type + * @return Javassist code to peform the cast */ - - public static String getWrapperTypeName(String primitiveType) + public static String castReference(String reference, String desiredType) { - return PRIMITIVE_TYPE_NAME_TO_PRIMITIVE_INFO.get(primitiveType).getWrapperType().getName(); - } + if (isPrimitiveType(desiredType)) + { + PrimitiveInfo info = PRIMITIVE_TYPE_NAME_TO_PRIMITIVE_INFO.get(desiredType); - public static Class getPrimitiveType(Class wrapperType) - { - return WRAPPER_TYPE_TO_PRIMITIVE_INFO.get(wrapperType).getPrimitiveType(); + return String.format("((%s)%s).%s()", + info.getWrapperType().getName(), reference, + info.getUnwrapMethod()); + } + + return String.format("(%s)%s", desiredType, reference); } + /** - * Given some type (possibly a primitive) returns the corresponding primitive type. For non-primitives, the provided - * type is returned. + * Given a type name, determines if that is the name of a primitive type. */ - public static Class getWrapperType(Class primitiveType) + public static boolean isPrimitiveType(String typeName) { - if (primitiveType.isPrimitive()) - return PRIMITIVE_TYPE_NAME_TO_PRIMITIVE_INFO.get(primitiveType.getName()).getWrapperType(); - - return primitiveType; // Not a primitive! + return PRIMITIVE_TYPE_NAME_TO_PRIMITIVE_INFO.containsKey(typeName); } + /** + * Converts a Class to a JVM type code (the way class information is expressed in a class file). + */ public static String getTypeCode(Class type) { if (type.equals(void.class)) return "V"; @@ -225,9 +245,9 @@ } /** - * Creates a proxy for a given service interface around an [EMAIL PROTECTED] ObjectCreator} that can provide (on demand) an - * object (implementing the service interface) to delegate to. The ObjectCreator will be invoked on every method - * invocation ( if it is caching, that should be internal to its implementation). + * Creates a proxy for a given service interface around an [EMAIL PROTECTED] org.apache.tapestry.ioc.ObjectCreator} that can + * provide (on demand) an object (implementing the service interface) to delegate to. The ObjectCreator will be + * invoked on every method invocation (if it is caching, that should be internal to its implementation). * * @param <T> * @param classFab used to create the new class @@ -241,7 +261,7 @@ { classFab.addField("_creator", Modifier.PRIVATE | Modifier.FINAL, ObjectCreator.class); - classFab.addConstructor(new Class[]{ObjectCreator.class}, null, "_creator = $1;"); + classFab.addConstructor(new Class[] { ObjectCreator.class }, null, "_creator = $1;"); String body = format("return (%s) _creator.createObject();", serviceInterface.getName());
Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/decorator.apt URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/decorator.apt?rev=651797&r1=651796&r2=651797&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/decorator.apt (original) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/decorator.apt Fri Apr 25 20:00:55 2008 @@ -300,7 +300,7 @@ } +---+ - Most of the logging logic occurs inside the ServiceLogger object, the MethodAdvise exists to call the right methods at + Most of the logging logic occurs inside the ServiceLogger object, the MethodAdvice exists to call the right methods at the right time. A Logger doesn't <change> parameter values (or thrown exceptions, or the result), it just captures and logs the data. @@ -309,6 +309,6 @@ For checked exceptions, we use isFail() and getThrown(). The AspectDecorator service can also be used in more complicated ways: it is possible to - only advise some of the methods and not others, or use different advise for different methods. Check the + only advise some of the methods and not others, or use different advice for different methods. Check the JavaDoc for more details. Added: tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/upgrade.apt URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/upgrade.apt?rev=651797&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/upgrade.apt (added) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/upgrade.apt Fri Apr 25 20:00:55 2008 @@ -0,0 +1,20 @@ + ---- + Upgrade Notes + ---- + +Upgrade Notes + + This is a quick guide to changes between releases of Tapestry. This is meant to provide information + on any additions or changes that developers will face after upgrading to the latest version of Tapestry. + + It is always advised to perform a full and complete build after upgrading. + + You should also check the {{{../release-notes.html}project-wide release notes}} for information + about bugs fixes and other improvements. + +Release 5.0.12 + + Several methods of {{{../apidocs/org/apache/tapestry/ioc/services/ClassFabUtils.html}ClassFabUtils}} + have been removed. The new method {{{castReference()}}} is an improved replacement for the removed + methods. These methods were largely used when decorating services, and the new + {{{../apidocs/org/apache/tapestry/ioc/services/AspectDecorator.html}AspectDecorator}} is even easier. \ No newline at end of file Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/site/site.xml URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/site/site.xml?rev=651797&r1=651796&r2=651797&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/site/site.xml (original) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/site/site.xml Fri Apr 25 20:00:55 2008 @@ -43,6 +43,7 @@ <menu name="Quick Links"> <item name="Download" href="http://tapestry.apache.org/download.html"/> + <item name="Upgrade Notes" href="upgrade.html"/> </menu> <menu name="Tapestry IoC Container"> Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry/ioc/services/ClassFabUtilsTest.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry/ioc/services/ClassFabUtilsTest.java?rev=651797&r1=651796&r2=651797&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry/ioc/services/ClassFabUtilsTest.java (original) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry/ioc/services/ClassFabUtilsTest.java Fri Apr 25 20:00:55 2008 @@ -32,17 +32,17 @@ @DataProvider(name = "provider") public Object[][] createInputs() { - return new Object[][]{{"java.lang.Object", "java.lang.Object"}, + return new Object[][] { { "java.lang.Object", "java.lang.Object" }, - {"int", "int"}, + { "int", "int" }, - {"int[]", "[I"}, + { "int[]", "[I" }, - {"java.lang.Throwable[]", "[Ljava.lang.Throwable;"}, + { "java.lang.Throwable[]", "[Ljava.lang.Throwable;" }, - {"byte[][]", "[[B"}, + { "byte[][]", "[[B" }, - {"java.lang.Runnable[][]", "[[Ljava.lang.Runnable;"}}; + { "java.lang.Runnable[][]", "[[Ljava.lang.Runnable;" } }; } @Test(dataProvider = "typeCodeProvider") @@ -54,47 +54,43 @@ @DataProvider(name = "typeCodeProvider") public Object[][] get_type_code_provider() { - return new Object[][]{{int.class, "I"}, + return new Object[][] { { int.class, "I" }, - {int[].class, "[I"}, + { int[].class, "[I" }, - {Thread.class, "Ljava/lang/Thread;"}, + { Thread.class, "Ljava/lang/Thread;" }, - {Thread[].class, "[Ljava/lang/Thread;"}, + { Thread[].class, "[Ljava/lang/Thread;" }, - {Double[][].class, "[[Ljava/lang/Double;"}, + { Double[][].class, "[[Ljava/lang/Double;" }, - {void.class, "V"}}; + { void.class, "V" } }; } @Test - public void unwrap_method() + public void primitive_type_from_wrapper_type() { - assertEquals(ClassFabUtils.getUnwrapMethodName("int"), "intValue"); + assertSame(ClassFabUtils.getPrimitiveType(Boolean.class), boolean.class); } @Test - public void wrapper_type_name() + public void get_primitive_type_from_name() { - assertEquals(ClassFabUtils.getWrapperTypeName("int"), "java.lang.Integer"); + assertSame(ClassFabUtils.getPrimitiveType("int"), int.class); } @Test - public void wrapper_type() + public void cast_reference_to_object_type() { - assertEquals(ClassFabUtils.getWrapperType(int.class), Integer.class); - assertEquals(ClassFabUtils.getWrapperType(getClass()), getClass()); + assertEquals(ClassFabUtils.castReference("ref", "java.lang.String"), + "(java.lang.String)ref"); } @Test - public void unwrap_method_from_wrapper_type() + public void cast_reference_to_primitive_type() { - assertEquals(ClassFabUtils.getUnwrapMethodName(Boolean.class), "booleanValue"); - } + assertEquals(ClassFabUtils.castReference("ref", "short"), + "((java.lang.Short)ref).shortValue()"); - @Test - public void primitive_type_from_wrapper_type() - { - assertSame(ClassFabUtils.getPrimitiveType(Boolean.class), boolean.class); } }
