1 package org.apache.harmony.tests.java.lang.reflect; 2 3 import dalvik.system.DexFile; 4 5 import junit.framework.TestCase; 6 7 import java.io.File; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.io.InputStream; 11 import java.io.OutputStream; 12 import java.lang.reflect.GenericSignatureFormatError; 13 import java.lang.reflect.TypeVariable; 14 15 import tests.support.Support_ClassLoader; 16 17 public class GenericSignatureFormatErrorTest extends TestCase{ 18 test_Constructor()19 public void test_Constructor() { 20 assertNotNull(new GenericSignatureFormatError()); 21 } 22 test_readResource()23 public void test_readResource() throws Exception { 24 File tf = File.createTempFile("classes", ".dex"); 25 // System.out.println("GenericSignatureFormatErrorTest:" 26 // +tf.getAbsolutePath()+", canRead: "+tf.canRead() 27 // +", canWrite: "+tf.canWrite()); 28 InputStream is = this.getClass().getResourceAsStream("/resources/dex1.bytes"); 29 assertNotNull(is); 30 } 31 32 33 // SideEffect: strange issue (exception: 'could not open dex file', 34 // dalvikvm: 'waitpid failed' log msg - only occurs when @SideEffect is removed 35 // and this test is run via running tests.luni.AllTestsLang TestSuite test_signatureFormatError()36 public void test_signatureFormatError() throws Exception { 37 /* 38 * dex1.bytes is a jar file with a classes.dex in it. 39 * the classes.dex was javac'ed, dx'ed and patched 40 * with the following java file: 41 * 42 * package demo; 43 * public class HelloWorld<U> { 44 * public HelloWorld(U t) {} 45 * } 46 * 47 * patch: 48 * the string constant (class generics signature string) 49 * "<U:" was changed to "<<:" 50 * 51 */ 52 53 File tf = File.createTempFile("classes", ".dex"); 54 // System.out.println("GenericSignatureFormatErrorTest:" + 55 // tf.getAbsolutePath() + ", canRead: " + tf.canRead() + 56 // ", canWrite: "+tf.canWrite()); 57 InputStream is = this.getClass().getResourceAsStream("/resources/dex1.bytes"); 58 assertNotNull(is); 59 OutputStream fos = new FileOutputStream(tf); 60 copy(is, fos); 61 fos.flush(); 62 fos.close(); 63 64 65 // class signature string "<U:" was changed to "<<:" 66 //System.out.println("file length:"+tf.length()); 67 try { 68 // Was: 69 // DexFile df = new DexFile(tf); 70 // Class clazz = df.loadClass("demo/HelloWorld", this.getClass().getClassLoader()); 71 72 ClassLoader cl = Support_ClassLoader.getInstance(tf.toURL(), 73 getClass().getClassLoader()); 74 75 Class clazz = cl.loadClass("demo/HelloWorld"); 76 TypeVariable[] tvs = clazz.getTypeParameters(); 77 fail("expecting a GenericSignatureFormatError"); 78 // for (TypeVariable tv : tvs) { 79 // System.out.println("tv:"+tv.toString()); 80 // } 81 } catch (GenericSignatureFormatError gsfe) { 82 // expected 83 } 84 } 85 copy(InputStream is, OutputStream os)86 private void copy(InputStream is, OutputStream os) { 87 try { 88 int b; 89 while ((b = is.read()) != -1) { 90 os.write(b); 91 } 92 is.close(); 93 } catch (IOException ex) { 94 throw new RuntimeException("io error", ex); 95 } 96 } 97 } 98