1 19 20 package org.netbeans; 21 22 import junit.framework.AssertionFailedError; 23 import junit.textui.TestRunner; 24 import org.netbeans.junit.*; 25 import java.io.InputStream ; 26 import java.lang.reflect.*; 27 import java.util.*; 28 29 32 public class PatchByteCodeTest extends NbTestCase { 33 34 public PatchByteCodeTest(String name) { 35 super(name); 36 } 37 38 public static void main(String [] args) { 39 TestRunner.run(new NbTestSuite(PatchByteCodeTest.class)); 40 } 41 42 protected void setUp() throws Exception { 43 super.setUp(); 44 } 45 46 public void testBeanTreeViewLoad () throws Exception { 47 checkPatching ( 48 "org.openide.explorer.view.BeanTreeView", 49 "data/BeanTreeView.clazz", 50 null, null 51 , null); 52 } 53 54 84 85 public void testClassCanBeAlsoInstantiated () throws Exception { 86 Class c = checkPatching ( 87 Sample.class.getName (), 88 "Sample.class", 89 "java.lang.Throwable", 90 null, 91 null 92 ); 93 94 c.newInstance (); 95 } 96 97 public void testConstructorCanBeAlsoInstantiated () throws Exception { 98 Class c = checkPatching ( 99 Sample2.class.getName (), 100 "Sample2.class", 101 "java.lang.Throwable", 102 null, 103 new String [] { "<init>" } 104 ); 105 106 c.newInstance (); 107 } 108 109 public void testChangingSetOfSuperInterfaces () throws Exception { 110 Class c = checkPatching ( 111 Sample.class.getName (), 112 "Sample.class", 113 "java.lang.Throwable", 114 new String [] { "org.openide.nodes.Node$Cookie", "java.lang.Cloneable" } 115 , null); 116 117 assertEquals ("Super class is throwable", Throwable .class, c.getSuperclass()); 118 Class [] ifaces = c.getInterfaces(); 119 assertEquals ("Two of them", 2, ifaces.length); 120 121 122 Object obj = c.newInstance (); 123 assertTrue ("Is instance of Cookie", obj instanceof org.openide.nodes.Node.Cookie); 124 assertTrue ("Is instance of Cloneable", obj instanceof Cloneable ); 125 } 126 127 128 public void testPatchingOfFieldsAndMethodsToPublicAndNonFinal () throws Exception { 129 InputStream is = getClass ().getResourceAsStream ("Sample.class"); 130 assertNotNull ("Class has not been found", is); 131 132 byte[] arr = new byte[is.available ()]; 133 int l = is.read (arr); 134 assertEquals ("Read exactly as much as expected", l, arr.length); 135 136 137 138 HashMap args = new HashMap (); 139 args.put ("netbeans.public", Arrays.asList(new String [] { "member", "field", "method", "staticmethod" }) ); 140 byte[] res = PatchByteCode.enhanceClass(arr, args); 141 PatchClassLoader loader = new PatchClassLoader (Sample.class.getName (), res, ClassLoader.getSystemClassLoader()); 142 143 Class c = loader.loadClass (Sample.class.getName ()); 144 145 assertTrue ("Class should be public", Modifier.isPublic (c.getModifiers())); 146 147 Method m = c.getDeclaredMethod("method", new Class [0]); 148 assertNotNull ("Mehtod method is there", m); 149 assertTrue ("And is public", Modifier.isPublic (m.getModifiers())); 150 assertTrue ("And is not final", !Modifier.isFinal(m.getModifiers ())); 151 assertTrue ("And is not static", !Modifier.isStatic(m.getModifiers())); 152 assertTrue ("And is not synchronzied", !Modifier.isSynchronized(m.getModifiers())); 153 154 m = c.getDeclaredMethod("member", new Class [] { Object .class }); 155 assertNotNull ("Member method is there", m); 156 assertTrue ("And is public", Modifier.isPublic (m.getModifiers())); 157 assertTrue ("And is not final", !Modifier.isFinal(m.getModifiers ())); 158 assertTrue ("And is not static", !Modifier.isStatic(m.getModifiers())); 159 assertTrue ("And is synchronzied", Modifier.isSynchronized(m.getModifiers())); 160 161 m = c.getDeclaredMethod("staticmethod", new Class [] { }); 162 assertNotNull ("Member method is there", m); 163 assertTrue ("And is public", Modifier.isPublic (m.getModifiers())); 164 assertTrue ("And is not final", !Modifier.isFinal(m.getModifiers ())); 165 assertTrue ("And is not static", Modifier.isStatic(m.getModifiers())); 166 assertTrue ("And is not synchronzied", !Modifier.isSynchronized(m.getModifiers())); 167 168 java.lang.reflect.Field f; 169 170 f = c.getDeclaredField("member"); 171 assertNotNull ("Really exists", f); 172 assertTrue ("Is public", Modifier.isPublic (f.getModifiers ())); 173 assertTrue ("Is not final", !Modifier.isFinal (f.getModifiers ())); 174 assertTrue ("Is static", Modifier.isStatic (f.getModifiers ())); 175 176 f = c.getDeclaredField("field"); 177 assertNotNull ("Really exists", f); 178 assertTrue ("Is public", Modifier.isPublic (f.getModifiers ())); 179 assertTrue ("Is not final", !Modifier.isFinal (f.getModifiers ())); 180 assertTrue ("Is static", !Modifier.isStatic (f.getModifiers ())); 181 182 } 183 184 public void testRenameOfAMember () throws Exception { 185 InputStream is = getClass ().getResourceAsStream ("Sample.class"); 186 assertNotNull ("Class has not been found", is); 187 188 byte[] arr = new byte[is.available ()]; 189 int l = is.read (arr); 190 assertEquals ("Read exactly as much as expected", l, arr.length); 191 192 193 194 HashMap args = new HashMap (); 195 args.put ("netbeans.rename", Arrays.asList(new String [] { "staticmethod", "StaticMethod" }) ); 196 byte[] res = PatchByteCode.enhanceClass(arr, args); 197 198 PatchClassLoader loader = new PatchClassLoader (Sample.class.getName (), res, ClassLoader.getSystemClassLoader()); 199 200 Class c = loader.loadClass (Sample.class.getName ()); 201 202 try { 203 c.getDeclaredMethod("staticmethod", new Class [] { }); 204 fail ("The old method is still present"); 205 } catch (NoSuchMethodException ex) { 206 } 208 209 java.lang.reflect.Method m = c.getDeclaredMethod("StaticMethod", new Class [] { }); 210 assertNotNull ("Renamed method found", m); 211 } 212 213 private Class checkPatching ( 214 String className, 215 String resource, 216 String superclass, 217 String [] interfaces, 218 String [] publc 219 ) throws Exception { 220 if (superclass == null) { 221 superclass = PatchByteCodeTest.class.getName (); 222 } 223 224 InputStream is = getClass ().getResourceAsStream (resource); 225 assertNotNull ("Resource has been found " + resource, is); 226 227 byte[] arr = new byte[is.available ()]; 228 int l = is.read (arr); 229 assertEquals ("Read exactly as much as expected", l, arr.length); 230 231 HashMap args = new HashMap (); 232 args.put ("netbeans.superclass", superclass.replace ('.', '/')); 233 234 if (interfaces != null) { 235 StringBuffer sb = new StringBuffer (); 236 String ap = ""; 237 for (int i = 0; i < interfaces.length; i++) { 238 sb.append (ap); 239 sb.append (interfaces[i]); 240 ap = ","; 241 } 242 args.put ("netbeans.interfaces", sb.toString().replace('.', '/')); 243 } 244 245 if (publc != null) { 246 args.put ("netbeans.public", Arrays.asList(publc)); 247 } 248 249 byte[] res = PatchByteCode.enhanceClass(arr, args); 250 PatchClassLoader loader = new PatchClassLoader (className, res); 251 252 Class c = loader.loadClass (className); 253 254 assertEquals ( 255 "Superclass changed appropriately", 256 superclass, 257 c.getSuperclass().getName () 258 ); 259 260 return c; 261 } 262 263 264 private static final class PatchClassLoader extends ClassLoader { 265 private String res; 266 private byte[] arr; 267 268 public PatchClassLoader (String res, byte[] arr) { 269 this (res, arr, PatchClassLoader.class.getClassLoader ()); 270 } 271 272 public PatchClassLoader (String res, byte[] arr, ClassLoader c) { 273 super (c); 274 275 this.res = res; 276 this.arr = arr; 277 } 278 279 protected synchronized Class loadClass(String name, boolean resolve) 280 throws ClassNotFoundException { 281 if (res.equals (name)) { 282 byte[] patch = PatchByteCode.patch(arr, name); 283 284 return defineClass (name, patch, 0, patch.length); 285 } else { 286 return super.loadClass (name, resolve); 287 } 288 } 289 } 290 } 291 | Popular Tags |