001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.bootjar;
005:
006: import com.tc.asm.ClassReader;
007: import com.tc.asm.tree.ClassNode;
008: import com.tc.asm.tree.MethodNode;
009: import com.tc.object.tools.BootJar;
010: import com.tc.test.TCTestCase;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.lang.reflect.Type;
015: import java.net.URL;
016: import java.util.Iterator;
017: import java.util.List;
018: import java.util.Set;
019:
020: public class ParameterizedTypesTest extends TCTestCase {
021:
022: public void testParameterizedTypesTest() throws Exception {
023: BootJar bj = BootJar.getDefaultBootJarForReading();
024: Set specs = bj.getAllPreInstrumentedClasses();
025: for (Iterator iter = specs.iterator(); iter.hasNext();) {
026: String className = (String) iter.next();
027: checkParmeterizedType(className);
028: }
029:
030: }
031:
032: private void checkParmeterizedType(String className)
033: throws Exception {
034: Class klass = Class.forName(className);
035: Type gsc = klass.getGenericSuperclass();
036: System.err.println("GenericSuperClass for " + className
037: + " is " + gsc);
038: }
039:
040: public void testHashMap() throws Exception {
041: assertMethodSignatures("java/util/HashMap.class");
042: }
043:
044: public void testHashtable() throws Exception {
045: assertMethodSignatures("java/util/Hashtable.class");
046: }
047:
048: public void testLinkedHashMap() throws Exception {
049: assertMethodSignatures("java/util/LinkedHashMap.class");
050: }
051:
052: private void assertMethodSignatures(String className)
053: throws IOException {
054: ClassNode jcn = getOriginalClass(className);
055: ClassNode tcn = getCurrentClass(className);
056:
057: for (Iterator it = tcn.methods.iterator(); it.hasNext();) {
058: MethodNode tnode = (MethodNode) it.next();
059: MethodNode jnode = findMethod(jcn.methods, tnode);
060: if (jnode != null) {
061: assertEquals("Invalid signature for " + tnode.name
062: + tnode.desc, jnode.signature, tnode.signature);
063: }
064: }
065: }
066:
067: private MethodNode findMethod(List methods, MethodNode jnode) {
068: for (Iterator it = methods.iterator(); it.hasNext();) {
069: MethodNode tnode = (MethodNode) it.next();
070: if (tnode.name.equals(jnode.name)
071: && tnode.desc.equals(jnode.desc)) {
072: return tnode;
073: }
074: }
075: return null;
076: }
077:
078: private ClassNode getCurrentClass(String className)
079: throws IOException {
080: InputStream is = getClass()
081: .getResourceAsStream("/" + className);
082: ClassReader cr = new ClassReader(is);
083: ClassNode cn = new ClassNode();
084: cr.accept(cn, ClassReader.SKIP_FRAMES);
085: return cn;
086: }
087:
088: private ClassNode getOriginalClass(String className)
089: throws IOException {
090: // We're working with several possible core classes now to search in the
091: // JVM jars since some JVMs have split their classes accross several jars.
092: // The original jar for the Sun VM is:
093: // jar:file:/C:/jdk1.5.0_08/jre/lib/rt.jar!/java/lang/Void.class
094: String[] core_jvm_classes = new String[] {
095: "/java/lang/Void.class", "/java/lang/Object.class" };
096: for (int i = 0; i < core_jvm_classes.length; i++) {
097: URL resource = Void.class.getResource(core_jvm_classes[i]);
098: assertNotNull("Unable to find class " + core_jvm_classes[i]
099: + " in an original JVM jar", resource);
100:
101: String path = resource.toString();
102: String classPath = path.substring(0, path
103: .indexOf(core_jvm_classes[i]) + 1)
104: + className;
105:
106: URL classUrl = new URL(classPath);
107: InputStream is = null;
108: try {
109: is = classUrl.openStream();
110: } catch (Exception e) {
111: continue;
112: }
113:
114: if (is != null) {
115: ClassReader cr = new ClassReader(is);
116: ClassNode cn = new ClassNode();
117: cr.accept(cn, ClassReader.SKIP_FRAMES);
118: return cn;
119: }
120: }
121: fail("Unable to find zip entry " + className);
122: return null;
123: }
124: }
|