public ClassDescriptor getDescriptor(Class<?> cls) { if (cls == null || cls == Object.class || !isRegularClass(cls)) return null; ClassDescriptor descriptor = descriptorCache.get(cls); if (descriptor == null) { descriptor = new ClassDescriptor(this, cls); ClassDescriptor previousDescriptor = descriptorCache.putIfAbsent(cls, descriptor); if (previousDescriptor != null) descriptor = previousDescriptor; } return descriptor; }
public ClassDescriptor(Reflection reflection, Class<?> cls) { this.cls = cls; this.instantiator = getInstantiator(reflection, cls); if (!Externalizable.class.isAssignableFrom(cls)) { this.writeObjectMethod = getPrivateMethod(cls, "writeObject", WRITE_OBJECT_PARAMS, Void.TYPE); this.readObjectMethod = getPrivateMethod(cls, "readObject", READ_OBJECT_PARAMS, Void.TYPE); } else { this.writeObjectMethod = null; this.readObjectMethod = null; } this.writeReplaceMethod = getInheritedMethod(cls, "writeReplace", null, Object.class); this.readResolveMethod = getInheritedMethod(cls, "readResolve", null, Object.class); this.properties = getSerializableProperties(reflection, cls); this.parent = reflection.getDescriptor(cls.getSuperclass()); }
public List<Property> findSerializableProperties(Class<?> cls) throws SecurityException { ClassDescriptor descriptor = getDescriptor(cls); if (descriptor == null) return Collections.emptyList(); return descriptor.getInheritedSerializableProperties(); }
protected void encodeSerializable(OutputContext ctx, Serializable v, ClassDescriptor desc) throws IOException, IllegalAccessException, InvocationTargetException { ClassDescriptor parentDesc = desc.getParent(); if (parentDesc != null) encodeSerializable(ctx, v, parentDesc); if (desc.hasWriteObjectMethod()) desc.invokeWriteObjectMethod(new JMFObjectOutputStream(ctx, desc, v), v); else { for (Property property : desc.getSerializableProperties()) ctx.getAndWriteProperty(v, property); } }
protected void decodeSerializable(InputContext ctx, Serializable v, ClassDescriptor desc) throws IOException, ClassNotFoundException, IllegalAccessException, InvocationTargetException { ClassDescriptor parentDesc = desc.getParent(); if (parentDesc != null) decodeSerializable(ctx, v, parentDesc); if (desc.hasReadObjectMethod()) desc.invokeReadObjectMethod(new JMFObjectInputStream(ctx, desc, v), v); else { for (Property property : desc.getSerializableProperties()) ctx.readAndSetProperty(v, property); } }
Class<?> cls = desc.getCls(); v = desc.newInstance(); if (desc == null || !desc.hasReadResolveMethod()) { ctx.addToObjects(v); Object resolved = desc.invokeReadResolveMethod(v); if (resolved == null) throw new JMFEncodingException(desc.getCls() + ".readResolve() method returned null"); if (resolved.getClass() == v.getClass()) throw new JMFEncodingException(desc.getCls() + ".readResolve() method returned an instance of the same class"); if (resolvedDesc == null || !resolvedDesc.hasWriteReplaceMethod()) { throw new JMFEncodingException( desc.getCls() + ".readResolve() method returned an object that has no writeReplace() method: " + (resolvedDesc == null ? "null" : resolvedDesc.getCls()) ); desc = resolvedDesc; while (desc.hasReadResolveMethod());
ClassDescriptor desc = ctx.getReflection().getDescriptor(v.getClass()); while (desc != null && desc.hasWriteReplaceMethod()) { Object replacement = desc.invokeWriteReplaceMethod(v); if (replacement == null) throw new JMFEncodingException(desc.getCls() + ".writeReplace() method returned null"); if (replacement.getClass() == v.getClass()) throw new JMFEncodingException(desc.getCls() + ".writeReplace() method returned an instance of the same class"); if (replacementDesc == null || !replacementDesc.hasReadResolveMethod()) { throw new JMFEncodingException( desc.getCls() + ".writeReplace() method returned an object that has no readResolve() method: " + (replacementDesc == null ? "null" : replacementDesc.getCls()) );
@Override public Object decode(InputContext ctx, int parameterizedJmfType) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException { int indexOrLength = IntegerUtil.decodeInteger(ctx, (parameterizedJmfType >>> INDEX_OR_LENGTH_BYTE_COUNT_OFFSET) & 0x03); if ((parameterizedJmfType & 0x80) != 0) return ctx.getObject(indexOrLength); String className = ctx.getAlias(ClassNameUtil.decodeClassName(ctx)); ClassDescriptor desc = ctx.getClassDescriptor(className); Class<?> cls = desc.getCls(); if (!Serializable.class.isAssignableFrom(cls)) throw new NotSerializableException(cls.getName()); @SuppressWarnings("unchecked") Collection<Object> v = (Collection<Object>)desc.newInstance(); ctx.addToObjects(v); for (int index = 0; index < indexOrLength; index++) v.add(ctx.readObject()); return v; }
@Override public void defaultWriteObject() throws IOException { for (Property property : desc.getSerializableProperties()) { try { out.getAndWriteProperty(v, property); } catch (IllegalAccessException e) { throw new IOException(e); } catch (InvocationTargetException e) { throw new IOException(e); } } }
Class<?> cls = desc.getCls(); v = desc.newInstance(); if (desc == null || !desc.hasReadResolveMethod()) { ctx.addToObjects(v); Object resolved = desc.invokeReadResolveMethod(v); if (resolved == null) throw new JMFEncodingException(desc.getCls() + ".readResolve() method returned null"); if (resolved.getClass() == v.getClass()) throw new JMFEncodingException(desc.getCls() + ".readResolve() method returned an instance of the same class"); if (resolvedDesc == null || !resolvedDesc.hasWriteReplaceMethod()) { throw new JMFEncodingException( desc.getCls() + ".readResolve() method returned an object that has no writeReplace() method: " + (resolvedDesc == null ? "null" : resolvedDesc.getCls()) ); desc = resolvedDesc; while (desc.hasReadResolveMethod());
protected void encodeSerializable(OutputContext ctx, Serializable v, ClassDescriptor desc) throws IOException, IllegalAccessException, InvocationTargetException { ClassDescriptor parentDesc = desc.getParent(); if (parentDesc != null) encodeSerializable(ctx, v, parentDesc); if (desc.hasWriteObjectMethod()) desc.invokeWriteObjectMethod(new JMFObjectOutputStream(ctx, desc, v), v); else { for (Property property : desc.getSerializableProperties()) ctx.getAndWriteProperty(v, property); } }
protected void decodeSerializable(InputContext ctx, Serializable v, ClassDescriptor desc) throws IOException, ClassNotFoundException, IllegalAccessException, InvocationTargetException { ClassDescriptor parentDesc = desc.getParent(); if (parentDesc != null) decodeSerializable(ctx, v, parentDesc); if (desc.hasReadObjectMethod()) desc.invokeReadObjectMethod(new JMFObjectInputStream(ctx, desc, v), v); else { for (Property property : desc.getSerializableProperties()) ctx.readAndSetProperty(v, property); } }
ClassDescriptor desc = ctx.getReflection().getDescriptor(v.getClass()); while (desc != null && desc.hasWriteReplaceMethod()) { Object replacement = desc.invokeWriteReplaceMethod(v); if (replacement == null) throw new JMFEncodingException(desc.getCls() + ".writeReplace() method returned null"); if (replacement.getClass() == v.getClass()) throw new JMFEncodingException(desc.getCls() + ".writeReplace() method returned an instance of the same class"); if (replacementDesc == null || !replacementDesc.hasReadResolveMethod()) { throw new JMFEncodingException( desc.getCls() + ".writeReplace() method returned an object that has no readResolve() method: " + (replacementDesc == null ? "null" : replacementDesc.getCls()) );
@Override public Object decode(InputContext ctx, int parameterizedJmfType) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException { int indexOrLength = IntegerUtil.decodeInteger(ctx, (parameterizedJmfType >>> INDEX_OR_LENGTH_BYTE_COUNT_OFFSET) & 0x03); if ((parameterizedJmfType & 0x80) != 0) return ctx.getObject(indexOrLength); String className = ctx.getAlias(ClassNameUtil.decodeClassName(ctx)); ClassDescriptor desc = ctx.getClassDescriptor(className); Class<?> cls = desc.getCls(); if (!Serializable.class.isAssignableFrom(cls)) throw new NotSerializableException(cls.getName()); @SuppressWarnings("unchecked") Collection<Object> v = (Collection<Object>)desc.newInstance(); ctx.addToObjects(v); for (int index = 0; index < indexOrLength; index++) v.add(ctx.readObject()); return v; }
@Override public void defaultReadObject() throws IOException, ClassNotFoundException { for (Property property : desc.getSerializableProperties()) { try { in.readAndSetProperty(v, property); } catch (IllegalAccessException e) { throw new IOException(e); } catch (InvocationTargetException e) { throw new IOException(e); } } }
Class<?> cls = desc.getCls(); v = desc.newInstance(); if (desc == null || !desc.hasReadResolveMethod()) { ctx.addToObjects(v); Object resolved = desc.invokeReadResolveMethod(v); if (resolved == null) throw new JMFEncodingException(desc.getCls() + ".readResolve() method returned null"); if (resolved.getClass() == v.getClass()) throw new JMFEncodingException(desc.getCls() + ".readResolve() method returned an instance of the same class"); if (resolvedDesc == null || !resolvedDesc.hasWriteReplaceMethod()) { throw new JMFEncodingException( desc.getCls() + ".readResolve() method returned an object that has no writeReplace() method: " + (resolvedDesc == null ? "null" : resolvedDesc.getCls()) ); desc = resolvedDesc; while (desc.hasReadResolveMethod());
protected void encodeSerializable(OutputContext ctx, Serializable v, ClassDescriptor desc) throws IOException, IllegalAccessException, InvocationTargetException { ClassDescriptor parentDesc = desc.getParent(); if (parentDesc != null) encodeSerializable(ctx, v, parentDesc); if (desc.hasWriteObjectMethod()) desc.invokeWriteObjectMethod(new JMFObjectOutputStream(ctx, desc, v), v); else { for (Property property : desc.getSerializableProperties()) ctx.getAndWriteProperty(v, property); } }
protected void decodeSerializable(InputContext ctx, Serializable v, ClassDescriptor desc) throws IOException, ClassNotFoundException, IllegalAccessException, InvocationTargetException { ClassDescriptor parentDesc = desc.getParent(); if (parentDesc != null) decodeSerializable(ctx, v, parentDesc); if (desc.hasReadObjectMethod()) desc.invokeReadObjectMethod(new JMFObjectInputStream(ctx, desc, v), v); else { for (Property property : desc.getSerializableProperties()) ctx.readAndSetProperty(v, property); } }
public ClassDescriptor(Reflection reflection, Class<?> cls) { this.cls = cls; this.instantiator = getInstantiator(reflection, cls); if (!Externalizable.class.isAssignableFrom(cls)) { this.writeObjectMethod = getPrivateMethod(cls, "writeObject", WRITE_OBJECT_PARAMS, Void.TYPE); this.readObjectMethod = getPrivateMethod(cls, "readObject", READ_OBJECT_PARAMS, Void.TYPE); } else { this.writeObjectMethod = null; this.readObjectMethod = null; } this.writeReplaceMethod = getInheritedMethod(cls, "writeReplace", null, Object.class); this.readResolveMethod = getInheritedMethod(cls, "readResolve", null, Object.class); this.properties = getSerializableProperties(reflection, cls); this.parent = reflection.getDescriptor(cls.getSuperclass()); }
ClassDescriptor desc = ctx.getReflection().getDescriptor(v.getClass()); while (desc != null && desc.hasWriteReplaceMethod()) { Object replacement = desc.invokeWriteReplaceMethod(v); if (replacement == null) throw new JMFEncodingException(desc.getCls() + ".writeReplace() method returned null"); if (replacement.getClass() == v.getClass()) throw new JMFEncodingException(desc.getCls() + ".writeReplace() method returned an instance of the same class"); if (replacementDesc == null || !replacementDesc.hasReadResolveMethod()) { throw new JMFEncodingException( desc.getCls() + ".writeReplace() method returned an object that has no readResolve() method: " + (replacementDesc == null ? "null" : replacementDesc.getCls()) );