1 20 38 package gov.nasa.jpf.jvm; 39 40 43 public class TestJavaLangClass { 44 static String clsName = "gov.nasa.jpf.jvm.TestJavaLangClass"; 45 46 int data = 42; 48 public static void main (String [] args) { 49 TestJavaLangClass t = new TestJavaLangClass(); 50 51 if (args.length > 0) { 52 for (int i = 0; i < args.length; i++) { 54 String func = args[i]; 55 56 if ("testClassForName".equals(func)) { 59 t.testClassForName(); 60 } else if ("testClassField".equals(func)) { 61 t.testClassField(); 62 } else if ("testGetClass".equals(func)) { 63 t.testGetClass(); 64 } else if ("testIdentity".equals(func)) { 65 t.testIdentity(); 66 } else if ("testNewInstance".equals(func)) { 67 t.testNewInstance(); 68 } else { 69 throw new IllegalArgumentException ("unknown test function"); 70 } 71 } 72 } else { 73 t.testClassForName(); 75 t.testClassField(); 76 t.testGetClass(); 77 t.testIdentity(); 78 t.testNewInstance(); 79 } 80 } 81 82 public void testClassField () { 83 Class clazz = TestJavaLangClass.class; 84 85 if (clazz == null) { 86 throw new RuntimeException ("class field not set"); 87 } 88 89 if (!clsName.equals(clazz.getName())) { 90 throw new RuntimeException ("getName() wrong for class field"); 91 } 92 } 93 94 95 public void testClassForName () { 96 Class clazz = null; 97 98 try { 99 clazz = Class.forName(clsName); 100 } catch (Exception x) { 101 } 102 103 if (clazz == null) { 104 throw new RuntimeException ("Class.forName() returned null object"); 105 } 106 107 if (!clsName.equals(clazz.getName())) { 108 throw new RuntimeException ( 109 "getName() wrong for Class.forName() acquired class"); 110 } 111 } 112 113 public void testGetClass () { 114 Class clazz = this.getClass(); 115 116 if (clazz == null) { 117 throw new RuntimeException ("Object.getClass() failed"); 118 } 119 120 if (!clsName.equals(clazz.getName())) { 121 throw new RuntimeException ( 122 "getName() wrong for getClass() acquired class"); 123 } 124 } 125 126 public void testIdentity () { 127 Class clazz1 = null; 128 Class clazz2 = TestJavaLangClass.class; 129 Class clazz3 = this.getClass(); 130 131 try { 132 clazz1 = Class.forName(clsName); 133 } catch (Exception x) { 134 } 135 136 if (clazz1 != clazz2) { 137 throw new RuntimeException ( 138 "Class.forName() and class field not identical"); 139 } 140 141 if (clazz2 != clazz3) { 142 throw new RuntimeException ( 143 "Object.getClass() and class field not identical"); 144 } 145 } 146 147 public void testNewInstance () { 148 try { 149 Class clazz = TestJavaLangClass.class; 150 TestJavaLangClass o = (TestJavaLangClass) clazz.newInstance(); 151 152 if (o.data != 42) { 153 throw new RuntimeException ( 154 "Class.newInstance() failed to call default ctor"); 155 } 156 } catch (Exception e) { 157 throw new RuntimeException ( 158 "Class.newInstance() caused exception: " + e); 159 } 160 } 161 } | Popular Tags |