1 19 20 package org.openide.loaders; 21 22 23 import java.io.*; 24 import java.lang.reflect.*; 25 import org.openide.cookies.InstanceCookie; 26 import org.openide.filesystems.FileObject; 27 import org.openide.util.*; 28 29 33 public class InstanceSupport extends Object implements InstanceCookie.Of { 34 35 private MultiDataObject.Entry entry; 36 37 38 private Throwable clazzException; 39 40 41 private Class clazz; 42 43 44 private Boolean applet; 45 46 private Boolean bean; 47 48 52 public InstanceSupport(MultiDataObject.Entry entry) { 53 this.entry = entry; 54 } 55 56 59 MultiDataObject.Entry entry () { 60 return entry; 61 } 62 63 64 66 69 public String instanceName () { 70 String p = instanceOrigin().getPath(); 72 int x = p.lastIndexOf('.'); 73 if (x != -1 && x > p.lastIndexOf('/')) { 74 p = p.substring(0, x); 75 } 76 return p.replace('/', '.'); 77 } 78 79 94 public Class <?> instanceClass() 95 throws java.io.IOException , ClassNotFoundException { 96 return instanceClass(null); 97 } 98 99 final Class <?> instanceClass(ClassLoader cl) 100 throws java.io.IOException , ClassNotFoundException { 101 if (clazzException != null) { 102 if (clazzException instanceof IOException) 103 throw (IOException)clazzException; 104 else if (clazzException instanceof ClassNotFoundException ) 105 throw (ClassNotFoundException )clazzException; 106 else 107 throw (ThreadDeath )clazzException; 108 } 109 if (clazz != null) return clazz; 110 try { 112 if (isSerialized ()) { InputStream is = instanceOrigin ().getInputStream (); 115 try { 116 clazz = readClass (is); 117 return clazz; 118 } finally { 119 is.close (); 120 } 121 } else { 122 clazz = findClass (instanceName (), cl); 124 if (clazz == null) throw new ClassNotFoundException (instanceName()); 125 return clazz; 126 } 127 } catch (IOException ex) { 128 Exceptions.attachMessage(ex, "From file: " + entry.getFile()); clazzException = ex; 130 throw ex; 131 } catch (ClassNotFoundException ex) { 132 Exceptions.attachMessage(ex, "From file: " + entry.getFile()); clazzException = ex; 134 throw ex; 135 } catch (RuntimeException re) { 136 clazzException = new ClassNotFoundException ("From file: " + entry.getFile() + " due to: " + re.toString()); clazzException.initCause(re); 139 throw (ClassNotFoundException ) clazzException; 140 } catch (LinkageError le) { 141 clazzException = new ClassNotFoundException ("From file: " + entry.getFile() + " due to: " + le.toString()); clazzException.initCause(le); 143 throw (ClassNotFoundException ) clazzException; 144 } 145 } 146 147 157 public boolean instanceOf(Class <?> type) { 158 try { 159 return type.isAssignableFrom (instanceClass ()); 160 } catch (IOException ex) { 161 return false; 162 } catch (ClassNotFoundException ex) { 163 return false; 164 } 165 } 166 167 170 public FileObject instanceOrigin () { 171 return entry.getFile (); 173 } 174 175 180 public Object instanceCreate () 181 throws java.io.IOException , ClassNotFoundException { 182 try { 183 if (isSerialized ()) { 184 BufferedInputStream bis = new BufferedInputStream(instanceOrigin().getInputStream(), 1024); 186 org.openide.util.io.NbObjectInputStream nbis = new org.openide.util.io.NbObjectInputStream(bis); 187 Object o = nbis.readObject(); 188 nbis.close(); 189 return o; 190 } else { 191 Class <?> c = instanceClass (); 192 if (SharedClassObject.class.isAssignableFrom (c)) { 193 return SharedClassObject.findObject (c.asSubclass(SharedClassObject.class), true); 195 } else { 196 return c.newInstance(); 198 } 199 } 200 } catch (IOException ex) { 201 Exceptions.attachLocalizedMessage(ex, instanceName()); 203 throw ex; 204 } catch (ClassNotFoundException ex) { 205 throw ex; 206 } catch (Exception e) { 207 throw new ClassNotFoundException ("Cannot instantiate " + instanceName() + " for " + entry.getFile(), e); } catch (LinkageError e) { 210 throw new ClassNotFoundException ("Cannot instantiate " + instanceName() + " for " + entry.getFile(), e); } 212 } 213 214 222 @Deprecated 223 public boolean isApplet () { 224 if (applet != null) return applet.booleanValue (); 225 boolean b = instanceOf (java.applet.Applet .class); 226 applet = b ? Boolean.TRUE : Boolean.FALSE; 227 return b; 228 } 229 230 239 @Deprecated 240 public boolean isExecutable () { 241 try { 242 Method main = instanceClass ().getDeclaredMethod ("main", new Class [] { String [].class 244 }); 245 246 int m = main.getModifiers (); 247 return Modifier.isPublic (m) && Modifier.isStatic (m) && Void.TYPE.equals ( 248 main.getReturnType () 249 ); 250 } catch (Exception ex) { 251 return false; 252 } catch (LinkageError re) { 253 return false; 255 } 256 } 257 258 266 @Deprecated 267 public boolean isJavaBean () { 268 if (bean != null) return bean.booleanValue (); 269 270 if (isSerialized ()) { 272 bean = Boolean.TRUE; 273 return true; 274 } 275 276 try { 278 Class clazz = instanceClass(); 279 int modif = clazz.getModifiers (); 280 if (!Modifier.isPublic (modif) || Modifier.isAbstract (modif)) { 281 bean = Boolean.FALSE; 282 return false; 283 } 284 Constructor c; 285 try { 286 c = clazz.getConstructor (new Class [0]); 287 } catch (NoSuchMethodException e) { 288 bean = Boolean.FALSE; 289 return false; 290 } 291 if ((c == null) || !Modifier.isPublic (c.getModifiers ())) { 292 bean = Boolean.FALSE; 293 return false; 294 } 295 298 for (Class outer = clazz.getDeclaringClass(); outer != null; outer = outer.getDeclaringClass()) { 299 if (!Modifier.isStatic(modif)) 301 return false; 302 modif = outer.getModifiers(); 303 if (!Modifier.isPublic(modif)) 305 return false; 306 } 307 } catch (Exception ex) { 308 bean = Boolean.FALSE; 309 return true; 310 } catch (LinkageError e) { 311 bean = Boolean.FALSE; 313 return false; 314 } 315 bean = Boolean.TRUE; 318 return true; 319 } 320 321 329 @Deprecated 330 public boolean isInterface () { 331 try { 332 return instanceClass ().isInterface (); 333 } catch (IOException ex) { 334 return false; 335 } catch (ClassNotFoundException cnfe) { 336 return false; 337 } 338 } 339 340 public String toString () { 341 return instanceName (); 342 } 343 344 367 @Deprecated 368 public static HelpCtx findHelp (InstanceCookie instance) { 369 try { 370 Class clazz = instance.instanceClass(); 371 if (java.awt.Component .class.isAssignableFrom (clazz) || java.awt.MenuComponent .class.isAssignableFrom (clazz)) { 375 String name = clazz.getName (); 376 String [] pkgs = new String [] { "java.awt.", "javax.swing.", "javax.swing.border." }; for (int i = 0; i < pkgs.length; i++) { 378 if (name.startsWith (pkgs[i]) && name.substring (pkgs[i].length ()).indexOf ('.') == -1) 379 return new HelpCtx (name); 380 } 381 } 382 Object o = instance.instanceCreate(); 383 if (o != null && o != instance) { 384 HelpCtx h = HelpCtx.findHelp(o); 385 if (h != HelpCtx.DEFAULT_HELP) { 386 return h; 387 } 388 } 389 } catch (Exception e) { 390 } 392 return null; 393 } 394 395 399 private boolean isSerialized () { 400 return instanceOrigin ().getExt ().equals ("ser"); } 402 403 409 private Class readClass (InputStream is) throws IOException, ClassNotFoundException { 410 411 class OIS extends ObjectInputStream { 412 public OIS (InputStream iss) throws IOException { 413 super (iss); 414 } 415 416 418 public Class resolveClass (ObjectStreamClass osc) 419 throws IOException, ClassNotFoundException { 420 Class c = findClass (osc.getName (), null); 421 if (c == writeRepl) { 422 return c; 425 } 426 427 throw new ClassEx (c); 430 } 431 }; 432 433 ObjectInputStream ois = new OIS (new BufferedInputStream (is)); 434 435 try { 436 ois.readObject (); 437 throw new ClassNotFoundException (); 439 } catch (ClassEx ex) { 440 return ex.clazz; 442 } 443 } 444 445 446 private static Class writeRepl; 447 static { 448 try { 449 writeRepl = Class.forName ("org.openide.util.SharedClassObject$WriteReplace"); } catch (Exception ex) { 451 ex.printStackTrace(); 452 } 453 } 454 455 460 private Class findClass (String name, ClassLoader customLoader) throws ClassNotFoundException { 461 try { 462 Class c; 463 try { 464 if (customLoader != null) { 465 c = customLoader.loadClass(name); 466 } else { 467 ClassLoader loader = (ClassLoader )Lookup.getDefault().lookup(ClassLoader .class); 469 if (loader == null) { 470 loader = getClass ().getClassLoader (); 471 } 472 c = loader.loadClass (name); 473 } 474 } catch (ClassNotFoundException ex) { 475 c = createClassLoader().loadClass(name); 477 } 478 return c; 479 } catch (ClassNotFoundException ex) { 480 throw ex; 481 } catch (RuntimeException ex) { 482 throw ex; 483 } catch (LinkageError le) { 484 throw new ClassNotFoundException (le.toString(), le); 485 } 486 } 487 488 495 protected ClassLoader createClassLoader() { 496 ClassLoader l = (ClassLoader )Lookup.getDefault().lookup(ClassLoader .class); 497 return l; 498 } 499 500 502 public static class Instance extends Object implements InstanceCookie.Of { 503 504 private Object obj; 505 506 507 510 public Instance (Object obj) { 511 this.obj = obj; 512 } 513 514 517 public String instanceName () { 518 return obj.getClass ().getName (); 519 } 520 521 527 public Class <?> instanceClass() { 528 return obj.getClass (); 529 } 530 531 534 public Object instanceCreate () { 535 return obj; 536 } 537 538 public boolean instanceOf(Class <?> type) { 539 return type.isAssignableFrom (instanceClass ()); 540 } 541 } 542 543 546 private static class ClassEx extends IOException { 547 548 public Class clazz; 549 550 static final long serialVersionUID =4810039297880922426L; 551 553 public ClassEx (Class c) { 554 clazz = c; 555 } 556 } 557 } 558 | Popular Tags |