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