1 19 20 package org.netbeans.core.registry.oldformats; 21 22 import org.openide.ErrorManager; 23 import org.openide.filesystems.FileObject; 24 import org.openide.util.Lookup; 25 import org.openide.util.SharedClassObject; 26 import org.openide.util.Utilities; 27 28 import java.io.*; 29 import java.lang.reflect.Method ; 30 31 35 public class InstanceUtils { 36 private InstanceUtils() {} 37 38 public static Object newValue(String val) throws ClassNotFoundException { 39 val = Utilities.translate(val); 40 try { 41 Class c = findClass(val); 42 if (SharedClassObject.class.isAssignableFrom(c)) { 43 Object o = SharedClassObject.findObject(c, false); 44 if (null != o) { 45 try { 47 Method method = SharedClassObject.class.getDeclaredMethod("reset", new Class [0]); method.setAccessible(true); 49 method.invoke(o, new Object [0]); 50 } catch (Exception e) { 51 ErrorManager.getDefault().notify(e); 52 } 53 } else { 54 o = SharedClassObject.findObject(c, true); 55 } 56 return o; 57 } else { 58 return c.newInstance(); 59 } 60 } catch (Exception e) { 61 ClassNotFoundException e2 = new ClassNotFoundException ("Cannot instantiate class "+val); 62 ErrorManager.getDefault().annotate(e2, e); 63 throw e2; 64 } 65 } 66 67 public static Object serialValue(String val) throws ClassNotFoundException , IOException { 68 if ((val == null) ||(val.length() == 0)) { 69 return null; 70 } 71 72 byte[] bytes = new byte[val.length()/2]; 73 int tempI; 74 int count = 0; 75 try { 76 for (int i = 0; i < val.length(); i++) { 77 if (Character.isWhitespace(val.charAt(i))) { 78 continue; 79 } 80 tempI = Integer.parseInt(val.substring(i,i+2),16); 81 if (tempI > 127) { 82 tempI -=256; 83 } 84 bytes[count++] = (byte) tempI; 85 i++; 86 } 87 } catch (NumberFormatException e) { 88 e.printStackTrace(); 89 IOException e2 = new IOException("Cannot read value of <serialvalue> attribute from file XXXX"); 90 ErrorManager.getDefault().annotate(e2, e); 91 throw e2; 92 } 93 94 ByteArrayInputStream bis = new ByteArrayInputStream(bytes, 0, count); 95 try { 96 ObjectInputStream ois = new SpecialObjectInputStream(bis); 97 return ois.readObject(); 98 } catch (Exception e) { 99 IOException e2 = new IOException("Cannot read value of <serialvalue> attribute from file XXXXX"); 100 ErrorManager.getDefault().annotate(e2, e); 101 throw e2; 102 } 103 } 104 105 106 public static Object methodValue(String className, String methodName, FileObject fo) throws ClassNotFoundException , IOException { 107 int idx = methodName.lastIndexOf("."); 108 Class cls = findClass(((idx > 0) ? methodName.substring(0,idx) : className)); 109 methodName = (idx > 0 ) ? methodName.substring(idx+1) : methodName; 110 111 Object [] paramArray = new Object [] { 112 new Class [] {FileObject.class}, 113 new Class [] {} 114 }; 115 116 Object [] objectsList = new Object [] { 117 new Object [] {fo}, 118 new Object [] {} 119 }; 120 121 for (int i = 0; i < paramArray.length; i++) { 122 try { 123 if (objectsList[i] == null) { 124 continue; 125 } 126 Method method = cls.getDeclaredMethod(methodName, (Class [])paramArray [i]); 127 if (method != null) { 128 method.setAccessible(true); 129 if (objectsList[i] != null) { 130 return method.invoke(null,(Object [])objectsList[i]); 131 } 132 } 133 } catch (Exception nsmExc) { 134 continue; 135 } 136 } 137 throw new IOException("Cannot instantiate object by method "+className+"."+methodName+". "); 138 } 139 140 private static Class findClass(String name) throws ClassNotFoundException { 141 ClassLoader c = (ClassLoader )Lookup.getDefault().lookup(ClassLoader .class); 142 if (c == null) { 143 return Class.forName(name); 144 } else { 145 return Class.forName(name, true, c); 146 } 147 } 148 149 150 154 private static class SpecialObjectInputStream extends java.io.ObjectInputStream { 155 156 public SpecialObjectInputStream(InputStream is) throws IOException { 158 super(is); 159 try { 160 enableResolveObject (true); 161 } catch (SecurityException ex) { 162 throw new IOException (ex.toString ()); 163 } 164 } 165 166 protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException { 168 ClassLoader cl = getNBClassLoader(); 169 try { 170 return Class.forName(v.getName(), false, cl); 171 } catch (ClassNotFoundException cnfe) { 172 String msg = "Offending classloader: " + cl; ErrorManager.getDefault ().annotate(cnfe, ErrorManager.INFORMATIONAL, msg, null, null, null); 174 throw cnfe; 175 } 176 } 177 178 184 protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { 185 ObjectStreamClass ose = super.readClassDescriptor(); 186 187 String name = ose.getName(); 188 String newN = org.openide.util.Utilities.translate(name); 189 190 if (name == newN) { 191 return ose; 193 } 194 195 ClassLoader cl = getNBClassLoader(); 196 try { 197 Class origCl = Class.forName(name, false, cl); 200 return ObjectStreamClass.lookup(origCl); 201 } catch (ClassNotFoundException ex) { 202 } 204 205 Class clazz = Class.forName(newN, false, cl); 207 ObjectStreamClass newOse = ObjectStreamClass.lookup(clazz); 208 209 if (newOse == null) { 212 throw new java.io.NotSerializableException (newN); 213 } 214 215 return newOse; 216 } 217 218 private static ClassLoader getNBClassLoader() { 220 ClassLoader c = (ClassLoader ) org.openide.util.Lookup.getDefault().lookup(ClassLoader .class); 221 return c != null ? c : ClassLoader.getSystemClassLoader(); 222 } 223 224 } 225 226 } 227 | Popular Tags |