/** java.io.ObjectStreamClass#getFields() */ public void test_getFields() { ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class); ObjectStreamField[] osfArray = osc.getFields(); assertTrue( "Array of fields should be of length 2 but is instead of length: " + osfArray.length, osfArray.length == 2); }
/** java.io.ObjectStreamClass#getName() */ public void test_getName() { ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class); assertEquals( "getName returned incorrect name: " + osc.getName(), "org.apache.harmony.tests.java.io.ObjectStreamClassTest$DummyClass", osc.getName()); }
/** * Provides a special handling for renaming of serialized classes. * * <p>Often, as the time goes the serialized classes evolve. They can be moved to new packages, * renamed or changed (by a mistake) to not reflect the version of class stored in previous * sessions. * * <p>This method deals with some of this incompatibilites and provides the module owners a way * how to fix some of them. * * <p>When a class is read, the <link>Utilities.translate</link> is consulted to find out what * whether the name of the class is listed there and what new value is assigned to it. This allows * complete rename of the serialized class. For example: <code>org.netbeans.core.NbMainExplorer * </code> can be renamed to <code>org.netbeans.core.ui.NbExp</code> - of course supposing that * the new class is able to read the serialized fields of the old one. * * <p>Another useful feature of this method is the ability to supress wrong <code>serialVersionUID * </code>. This was causing us a lot of problems, because people were forgetting to specify the * <code>serialVersionUID</code> field in their sources and then it was hard to recover from it. * Right now we have a solution: Just use <link>Utilities.translate</link> framework to assing * your class <code>org.yourpackage.YourClass</code> the same name as it had e.g. <code> * org.yourpackage.YourClass</code>. This will be interpreted by this method as a hit to suppress * <code>serialVersionUID</code> and the <code>NbObjectInputStream</code> will ignore its value. * * <p>Please see <link>Utilities.translate</link> to learn how your module can provide list of * classes that changed name or want to suppress <code>serialVersionUID</code>. */ protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { ObjectStreamClass ose = super.readClassDescriptor(); String name = ose.getName(); String newN = Utilities.translate(name); if (name == newN) { // no translation return ose; } // otherwise reload the ObjectStreamClass to contain the local copy ClassLoader cl = getNBClassLoader(); Class clazz = Class.forName(newN, false, cl); ObjectStreamClass newOse = ObjectStreamClass.lookup(clazz); // #28021 - it is possible that lookup return null. In that case the conversion // table contains class which is not Serializable or Externalizable. if (newOse == null) { throw new java.io.NotSerializableException(newN); } return newOse; }
/** java.io.ObjectStreamClass#forClass() */ public void test_forClass() { // Need to test during serialization to be sure an instance is // returned ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class); assertEquals( "forClass returned an object: " + osc.forClass(), DummyClass.class, osc.forClass()); }
/** java.io.ObjectStreamClass#lookup(java.lang.Class) */ public void test_lookupLjava_lang_Class() { ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class); assertEquals( "lookup returned wrong class: " + osc.getName(), "org.apache.harmony.tests.java.io.ObjectStreamClassTest$DummyClass", osc.getName()); }
static void print(ObjectStreamClass desc) { System.out.println("Class name : " + desc.getName()); System.out.println("SerialVersionUID: " + desc.getSerialVersionUID()); System.out.println("ObjectStreamFields"); ObjectStreamField[] fields = desc.getFields(); int numPrim = 0; int numObj = 0; for (int i = 0; i < fields.length; i++) { ObjectStreamField f = fields[i]; String fieldName = "<fieldName unknown>"; try { fieldName = f.getName(); } catch (NoSuchMethodError e) { // ignore. ObjectStreamField.getName did not exist in JDK 1.1 } if (f.isPrimitive()) { numPrim++; System.out.println("" + i + ". " + f.getType().getName() + " " + fieldName); } else { numObj++; String ts = "<unknown>"; try { ts = f.getTypeString(); } catch (NoSuchMethodError e) { // ignore. ObjectStreamField.getTypeString did not exist in JDK 1.1 ts = "<field type unknown>"; } System.out.println("" + i + ". " + ts + " " + fieldName); } } System.out.println("# primitive fields:" + numPrim + " # object ref fields:" + numObj); }
@Override protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { ObjectStreamClass classDesc = super.readClassDescriptor(); String className = classDesc.getName(); return renamedClasses.containsKey(className) ? ObjectStreamClass.lookup(renamedClasses.get(className)) : classDesc; }
/** java.io.ObjectStreamClass#getSerialVersionUID() */ public void test_getSerialVersionUID() { ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class); assertTrue( "getSerialversionUID returned incorrect uid: " + osc.getSerialVersionUID() + " instead of " + DummyClass.getUID(), osc.getSerialVersionUID() == DummyClass.getUID()); }
public static String getSerialVersionUID(final Class<?> cls) { final ObjectStreamClass osc = ObjectStreamClass.lookup(cls); if (osc == null) { return ""; } return String.valueOf(osc.getSerialVersionUID()); }
/** java.io.ObjectStreamClass#toString() */ public void test_toString() { ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class); String oscString = osc.toString(); // The previous test was more specific than the spec so it was replaced // with the test below assertTrue( "toString returned incorrect string: " + osc.toString(), oscString.indexOf("serialVersionUID") >= 0 && oscString.indexOf("999999999999999L") >= 0); }
/** Only deserialize primitive or whitelisted classes */ @Override protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { Class<?> primitiveType = PRIMITIVE_TYPES.get(desc.getName()); if (primitiveType != null) { return primitiveType; } if (!isClassValid(desc.getName())) { throw new InvalidClassException("Unauthorized deserialization attempt", desc.getName()); } return super.resolveClass(desc); }
protected void updateOptions() { if (m_clusterer instanceof OptionHandler) { m_clustererOptions = Utils.joinOptions(((OptionHandler) m_clusterer).getOptions()); } else { m_clustererOptions = ""; } if (m_clusterer instanceof Serializable) { ObjectStreamClass obs = ObjectStreamClass.lookup(m_clusterer.getClass()); m_clustererVersion = "" + obs.getSerialVersionUID(); } else { m_clustererVersion = ""; } }
public static UnsafeAllocator create() { Object obj; try { obj = Class.forName("sun.misc.Unsafe"); Object obj3 = ((Class) (obj)).getDeclaredField("theUnsafe"); ((Field) (obj3)).setAccessible(true); obj3 = ((Field) (obj3)).get(null); obj = new q( ((Class) (obj)).getMethod("allocateInstance", new Class[] {java / lang / Class}), obj3); } catch (Exception exception) { Object obj1; try { obj1 = java / io / ObjectInputStream.getDeclaredMethod( "newInstance", new Class[] {java / lang / Class, java / lang / Class}); ((Method) (obj1)).setAccessible(true); obj1 = new r(((Method) (obj1))); } catch (Exception exception1) { Object obj2; try { obj2 = java / io / ObjectStreamClass.getDeclaredMethod( "getConstructorId", new Class[] {java / lang / Class}); ((Method) (obj2)).setAccessible(true); int i = ((Integer) ((Method) (obj2)).invoke(null, new Object[] {java / lang / Object})) .intValue(); obj2 = java / io / ObjectStreamClass.getDeclaredMethod( "newInstance", new Class[] {java / lang / Class, Integer.TYPE}); ((Method) (obj2)).setAccessible(true); obj2 = new s(((Method) (obj2)), i); } catch (Exception exception2) { return new t(); } return ((UnsafeAllocator) (obj2)); } return ((UnsafeAllocator) (obj1)); } return ((UnsafeAllocator) (obj)); }
/** Use the given ClassLoader rather than using the system class */ protected Class resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException { String cname = classDesc.getName(); if (cname.startsWith("[")) { // An array Class component; // component class int dcount; // dimension for (dcount = 1; cname.charAt(dcount) == '['; dcount++) ; if (cname.charAt(dcount) == 'L') { component = loader.loadClass(cname.substring(dcount + 1, cname.length() - 1)); } else { if (cname.length() != dcount + 1) { throw new ClassNotFoundException(cname); // malformed } component = primitiveType(cname.charAt(dcount)); } int dim[] = new int[dcount]; for (int i = 0; i < dcount; i++) { dim[i] = 1; } return Array.newInstance(component, dim).getClass(); } else { return loader.loadClass(cname); } }
/** * Computes the serial version UID value for the given class. The code is taken from {@link * ObjectStreamClass#computeDefaultSUID(Class)}. * * @param cls A class. * @return A serial version UID. * @throws IOException If failed. */ static long computeSerialVersionUid(Class cls) throws IOException { if (Serializable.class.isAssignableFrom(cls) && !Enum.class.isAssignableFrom(cls)) { return ObjectStreamClass.lookup(cls).getSerialVersionUID(); } MessageDigest md; try { md = MessageDigest.getInstance("SHA"); } catch (NoSuchAlgorithmException e) { throw new IOException("Failed to get digest for SHA.", e); } md.update(cls.getName().getBytes(UTF_8)); for (Field f : getFieldsForSerialization(cls)) { md.update(f.getName().getBytes(UTF_8)); md.update(f.getType().getName().getBytes(UTF_8)); } byte[] hashBytes = md.digest(); long hash = 0; // Composes a single-long hash from the byte[] hash. for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) hash = (hash << 8) | (hashBytes[i] & 0xFF); return hash; }
/** Use the given ClassLoader rather than using the system class */ @SuppressWarnings("rawtypes") protected Class resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException { String cname = classDesc.getName(); return ClassFinder.resolveClass(cname, this.loader); }
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException { Class cl = desc.forClass(); if (ProxyFactory.isProxyClass(cl)) { writeBoolean(true); Class superClass = cl.getSuperclass(); Class[] interfaces = cl.getInterfaces(); byte[] signature = ProxyFactory.getFilterSignature(cl); String name = superClass.getName(); writeObject(name); // we don't write the marker interface ProxyObject writeInt(interfaces.length - 1); for (int i = 0; i < interfaces.length; i++) { Class interfaze = interfaces[i]; if (interfaze != ProxyObject.class && interfaze != Proxy.class) { name = interfaces[i].getName(); writeObject(name); } } writeInt(signature.length); write(signature); } else { writeBoolean(false); super.writeClassDescriptor(desc); } }
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { try { return super.resolveClass(desc); } catch (ClassNotFoundException e) { return PluginClassLoader.findPluginClass(desc.getName()); } }
@Override protected Class<?> resolveClass(ObjectStreamClass objectStreamClass) throws ClassNotFoundException { String name = objectStreamClass.getName(); return ClassResolverUtil.resolve(name, _classLoader); }
private static Map<String, Long> getMessageSVUID(Type messageType) throws SJIOException, ClassNotFoundException // SJCompilerUtils has a simpler version of this routine. { HashMap<String, Long> ours = new HashMap<String, Long>(); if (messageType instanceof SJSessionType) // Should come before ordinary class cases? (But SJSessionType shouldn't be a // class type). { ours.putAll(getClassSVUIDs((SJSessionType) messageType)); } else if (messageType.isPrimitive()) { // No SVUID needed for primitive types. } else if (messageType.isClass()) // Duplicated from above. { String className = messageType.toClass().fullName(); Class<?> c = Class.forName(className); if (c.isInterface()) { // Interfaces don't have SVUIDs (so what should we do here?). // This encourages use of // abstract classes rather than interfaces for message types? } else { ObjectStreamClass osc = ObjectStreamClass.lookup(c); if (osc == null) { throw new SJIOException("Class not serializable: " + c); } ours.put( className, osc .getSerialVersionUID()); // Not possible to find a different SVUID for the same // (name) class here? // SVUIDs could be recorded in the // session type objects. // Put currently problems working // with these values at compilation time if the class // binary is not available a priori (SVUID value not built // yet?). } } else if (messageType.isArray()) { throw new SJIOException("Array types not done yet: " + messageType); } return ours; }
protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { String name = desc.getName(); try { return Class.forName(name, false, _loader); } catch (ClassNotFoundException ex) { return super.resolveClass(desc); } }
@Override public Class<?> resolveClass(java.io.ObjectStreamClass cl) throws IOException, ClassNotFoundException { try { return Class.forName(cl.getName(), false, Thread.currentThread().getContextClassLoader()); } catch (ClassNotFoundException e) { return super.resolveClass(cl); } }
@Override protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { if (loader == null) { return super.resolveClass(desc); } else { try { return LoadClassUtil.loadClass(desc.getName(), loader); } catch (ClassNotFoundException e) { Class primitiveClass = PRIMITIVE_CLASSES.get(desc.getName()); if (primitiveClass != null) { return primitiveClass; } else { throw e; } } } }
@Override protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { try { return current.loadClass(desc.getName()); } catch (ClassNotFoundException e) { return super.resolveClass(desc); } }
/* * (non-Javadoc) * @see * java.io.ObjectInputStream#resolveClass(java.io.ObjectStreamClass) */ @Override protected Class<?> resolveClass(final ObjectStreamClass desc) throws IOException, ClassNotFoundException { try { return super.resolveClass(desc); } catch (final ClassNotFoundException e) { return this.classLoader.loadClass(desc.getName()); } }
protected Class<?> resolveClass(ObjectStreamClass desc) throws java.io.IOException, ClassNotFoundException { try { String name = desc.getName(); return Class.forName(name, false, this.classLoader); } catch (ClassNotFoundException e) { } return super.resolveClass(desc); }
class TestObjectOutputStream extends ObjectOutputStream { static ObjectStreamClass fooDesc = ObjectStreamClass.lookup(Foo.class); static ObjectStreamClass integerDesc = ObjectStreamClass.lookup(Integer.class); TestObjectOutputStream(OutputStream out) throws IOException { super(out); } protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException { if (desc == fooDesc) { super.writeClassDescriptor(integerDesc); } else if (desc == integerDesc) { super.writeClassDescriptor(fooDesc); } else { super.writeClassDescriptor(desc); } } }
/* Uses NetBeans module classloader to load the class. * @param v description of the class to load */ protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException { ClassLoader cl = getNBClassLoader(); try { return Class.forName(v.getName(), false, cl); } catch (ClassNotFoundException cnfe) { String msg = "Offending classloader: " + cl; // NOI18N ErrorManager.getDefault().annotate(cnfe, ErrorManager.INFORMATIONAL, msg, null, null, null); throw cnfe; } }
@Override public Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { ClassLoader currentTccl = null; try { currentTccl = Thread.currentThread().getContextClassLoader(); return currentTccl.loadClass(desc.getName()); } catch (Exception e) { } return super.resolveClass(desc); }
/** * Overriden version that uses the parametrized <code>ClassLoader</code> or the <code> * ClassLoader</code> of the current <code>Thread</code> to resolve the class. * * @param desc An instance of class <code>ObjectStreamClass</code>. * @return A <code>Class</code> object corresponding to <code>desc</code>. * @throws IOException Any of the usual Input/Output exceptions. * @throws ClassNotFoundException If class of a serialized object cannot be found. */ @Override protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { String name = desc.getName(); try { return Class.forName(name, false, classLoader); } catch (ClassNotFoundException ex) { return Class.forName(name, false, Thread.currentThread().getContextClassLoader()); } }