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/01 02:55:47 UTC
cvs commit: jakarta-hivemind/framework/src/test/org/apache/hivemind/impl TestJavaTypeUtils.java
hlship 2004/10/31 17:55:47
Modified: framework/src/java/org/apache/hivemind ClassResolver.java
. status.xml
framework/src/java/org/apache/hivemind/impl
DefaultClassResolver.java
Added: framework/src/java/org/apache/hivemind/impl
JavaTypeUtils.java
framework/src/test/org/apache/hivemind/impl
TestJavaTypeUtils.java
Log:
Move some logic related to primitive Java types and arrays from Tapestry directly into DefaultClassResolver.
Revision Changes Path
1.3 +5 -1 jakarta-hivemind/framework/src/java/org/apache/hivemind/ClassResolver.java
Index: ClassResolver.java
===================================================================
RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/ClassResolver.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ClassResolver.java 17 Jun 2004 15:16:11 -0000 1.2
+++ ClassResolver.java 1 Nov 2004 01:55:47 -0000 1.3
@@ -44,11 +44,15 @@
* Forwarded, to the the method
* <code>Class.forName(String, boolean, ClassLoader)</code>, using
* the resolver's class loader.
+ *
+ * <p>
+ * Since 1.1, the type may include primitive types and arrays
+ * (of primitives or of objects).
*
* @throws ApplicationRuntimeException on any error.
*/
- public Class findClass(String name);
+ public Class findClass(String type);
/**
* Returns a {@link java.lang.ClassLoader} that can see
1.71 +3 -0 jakarta-hivemind/status.xml
Index: status.xml
===================================================================
RCS file: /home/cvs/jakarta-hivemind/status.xml,v
retrieving revision 1.70
retrieving revision 1.71
diff -u -r1.70 -r1.71
--- status.xml 31 Oct 2004 14:44:25 -0000 1.70
+++ status.xml 1 Nov 2004 01:55:47 -0000 1.71
@@ -68,6 +68,9 @@
<action type="fix" dev="HLS" fixes-bug="HIVEMIND-68">
Properly report the actual method name when invoking a service initializer method.
</action>
+ <action type="add" dev="HLS">
+ Move some logic related to primitive Java types and arrays from Tapestry directly into DefaultClassResolver.
+ </action>
</release>
<release version="1.0" date="Sep 22 2004">
1.4 +30 -19 jakarta-hivemind/framework/src/java/org/apache/hivemind/impl/DefaultClassResolver.java
Index: DefaultClassResolver.java
===================================================================
RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/impl/DefaultClassResolver.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DefaultClassResolver.java 29 Jul 2004 13:18:49 -0000 1.3
+++ DefaultClassResolver.java 1 Nov 2004 01:55:47 -0000 1.4
@@ -15,6 +15,8 @@
package org.apache.hivemind.impl;
import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -22,10 +24,9 @@
import org.apache.hivemind.ClassResolver;
/**
- * Default implementation of {@link org.apache.hivemind.ClassResolver} based
- * around {@link Thread#getContextClassLoader()} (which is set by the
- * servlet container).
- *
+ * Default implementation of {@link org.apache.hivemind.ClassResolver}based around
+ * {@link Thread#getContextClassLoader()}(which is set by the servlet container).
+ *
* @author Howard Lewis Ship
*/
public class DefaultClassResolver implements ClassResolver
@@ -35,10 +36,8 @@
private ClassLoader _loader;
/**
- * Constructs a new instance using
- * {@link Thread#getContextClassLoader()}.
- *
- **/
+ * Constructs a new instance using {@link Thread#getContextClassLoader()}.
+ */
public DefaultClassResolver()
{
@@ -82,25 +81,37 @@
/**
* Invokes {@link Class#forName(java.lang.String, boolean, java.lang.ClassLoader)}.
- *
- * @param name the complete class name to locate and load
- * @return The loaded class
- * @throws ApplicationRuntimeException if loading the class throws an exception
- * (typically {@link ClassNotFoundException} or a security exception)
*
+ * @param type
+ * the complete class name to locate and load; alternately, may be a primitive name
+ * or an array type (primitive or object)
+ * @return The loaded class
+ * @throws ApplicationRuntimeException
+ * if loading the class throws an exception (typically
+ * {@link ClassNotFoundException}or a security exception)
+ * @see JavaTypeUtils
*/
- public Class findClass(String name)
+ public Class findClass(String type)
{
+ Class result = JavaTypeUtils.getPrimtiveClass(type);
+
+ if (result != null)
+ return result;
+
+ // This does some magic to handle arrays of primitives or objects in the
+ // format needed by Class.forName().
+
+ String jvmName = JavaTypeUtils.getJVMClassName(type);
+
try
{
- return Class.forName(name, true, _loader);
+ return Class.forName(jvmName, true, _loader);
}
catch (Throwable t)
{
- throw new ApplicationRuntimeException(
- ImplMessages.unableToLoadClass(name, _loader, t),
- t);
+ throw new ApplicationRuntimeException(ImplMessages.unableToLoadClass(type, _loader, t),
+ t);
}
}
@@ -109,4 +120,4 @@
return _loader;
}
-}
+}
\ No newline at end of file
1.1 jakarta-hivemind/framework/src/java/org/apache/hivemind/impl/JavaTypeUtils.java
Index: JavaTypeUtils.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.hivemind.impl;
import java.util.HashMap;
import java.util.Map;
/**
* Holds a utility method that converts java type names (as they might appear in source code) into
* JVM class names.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class JavaTypeUtils
{
/**
* Mapping between a primitive type and its Java VM representation Used for the encoding of
* array types
*/
private static Map PRIMITIVE_TYPE_CODES = new HashMap();
static
{
PRIMITIVE_TYPE_CODES.put("boolean", "Z");
PRIMITIVE_TYPE_CODES.put("short", "S");
PRIMITIVE_TYPE_CODES.put("int", "I");
PRIMITIVE_TYPE_CODES.put("long", "J");
PRIMITIVE_TYPE_CODES.put("float", "F");
PRIMITIVE_TYPE_CODES.put("double", "D");
PRIMITIVE_TYPE_CODES.put("char", "C");
PRIMITIVE_TYPE_CODES.put("byte", "B");
}
/**
* Map from Java type name to Class.
*/
private static final Map PRIMITIVE_CLASSES = new HashMap();
static
{
PRIMITIVE_CLASSES.put("boolean", boolean.class);
PRIMITIVE_CLASSES.put("short", short.class);
PRIMITIVE_CLASSES.put("char", char.class);
PRIMITIVE_CLASSES.put("byte", byte.class);
PRIMITIVE_CLASSES.put("int", int.class);
PRIMITIVE_CLASSES.put("long", long.class);
PRIMITIVE_CLASSES.put("float", float.class);
PRIMITIVE_CLASSES.put("double", double.class);
}
private JavaTypeUtils()
{
// Prevent instantiation
}
/**
* Translates types from standard Java format to Java VM format. For example, java.util.Locale
* remains java.util.Locale, but int[][] is translated to [[I and java.lang.Object[] to
* [Ljava.lang.Object;
*/
public static String getJVMClassName(String type)
{
// if it is not an array, just return the type itself
if (!type.endsWith("[]"))
return type;
// if it is an array, convert it to JavaVM-style format
StringBuffer buffer = new StringBuffer();
while (type.endsWith("[]"))
{
buffer.append("[");
type = type.substring(0, type.length() - 2);
}
String primitiveIdentifier = (String) PRIMITIVE_TYPE_CODES.get(type);
if (primitiveIdentifier != null)
buffer.append(primitiveIdentifier);
else
{
buffer.append("L");
buffer.append(type);
buffer.append(";");
}
return buffer.toString();
}
/**
* Translates a primitive type ("boolean", "char", etc.) to the corresponding
* Class.
*
* @return the corresponding class, or null if type is not a primitive type.
*
*/
public static Class getPrimtiveClass(String type)
{
return (Class) PRIMITIVE_CLASSES.get(type);
}
}
1.1 jakarta-hivemind/framework/src/test/org/apache/hivemind/impl/TestJavaTypeUtils.java
Index: TestJavaTypeUtils.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.hivemind.impl;
import junit.framework.TestCase;
/**
* Tests for {@link org.apache.hivemind.impl.JavaTypeUtils}.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class TestJavaTypeUtils extends TestCase
{
private void test(String expected, String input)
{
String actual = JavaTypeUtils.getJVMClassName(input);
assertEquals(expected, actual);
}
public void testNonArrayUnchanged()
{
test("java.lang.Object", "java.lang.Object");
test("int", "int");
}
public void testPrimitiveArray()
{
test("[I", "int[]");
}
public void testObjectArray()
{
test("[Ljava.lang.Throwable;", "java.lang.Throwable[]");
}
public void testPrimitiveMultiArray()
{
test("[[B", "byte[][]");
}
// TODO: Test that all the primitives are correct
public void testObjectMultiArray()
{
test("[[Ljava.lang.Runnable;", "java.lang.Runnable[][]");
}
public void testGetPrimitive()
{
assertSame(boolean.class, JavaTypeUtils.getPrimtiveClass("boolean"));
assertSame(char.class, JavaTypeUtils.getPrimtiveClass("char"));
assertNull(JavaTypeUtils.getPrimtiveClass("java.lang.Object"));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-cvs-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-cvs-help@jakarta.apache.org