/** * Will convert a class name to its class loader resource name (e.g {@code org.objenesis.EmptyClass} * becomes {@code org/objenesis/EmptyClass.class}) * * @param className full class name including the package * @return the resource name */ public static String classNameToResource(String className) { return classNameToInternalClassName(className) + ".class"; }
@Override public int read() throws IOException { int result = data[pointer++]; if(pointer >= data.length) { advanceBuffer(); } return result; }
public ProxyingInstantiator(Class<T> type) { byte[] classBytes = writeExtendingClass(type, SUFFIX); try { newType = ClassDefinitionUtils.defineClass(type.getName() + SUFFIX, classBytes, type.getClassLoader()); } catch (Exception e) { throw new ObjenesisException(e); } }
return new ObjectInputStreamInstantiator<T>(type); return new SunReflectionFactorySerializationInstantiator<T>(type); return new ObjectStreamClassInstantiator<T>(type); return new ObjectStreamClassInstantiator<T>(type); return new ObjectStreamClassInstantiator<T>(type);
if(PlatformDescription.isGoogleAppEngine()) { if(Serializable.class.isAssignableFrom(type)) { return new ObjectInputStreamInstantiator<T>(type); return new AccessibleInstantiator<T>(type);
public ObjectStreamClassInstantiator(Class<T> type) { initialize(); objStreamClass = ObjectStreamClass.lookup(type); }
public ObjectInputStreamInstantiator(Class<T> clazz) { if(Serializable.class.isAssignableFrom(clazz)) { try { this.inputStream = new ObjectInputStream(new MockStream(clazz)); } catch(IOException e) { throw new Error("IOException: " + e.getMessage()); } } else { throw new ObjenesisException(new NotSerializableException(clazz + " not serializable")); } }
public ProxyingInstantiator(Class<T> type) { byte[] classBytes = writeExtendingClass(type); try { newType = ClassDefinitionUtils.defineClass(type.getName() + SUFFIX, classBytes, type, type.getClassLoader()); } catch (Exception e) { throw new ObjenesisException(e); } }
private <T> ObjectInstantiator<T> newInstantiatorOf(Class<T> type) { String suffix = type.getSimpleName(); String className = getClass().getName() + "$$$" + suffix; Class<ObjectInstantiator<T>> clazz = getExistingClass(getClass().getClassLoader(), className); if(clazz == null) { byte[] classBytes = writeExtendingClass(type, className); try { clazz = ClassDefinitionUtils.defineClass(className, classBytes, getClass().getClassLoader()); } catch (Exception e) { throw new ObjenesisException(e); } } try { return clazz.newInstance(); } catch (InstantiationException e) { throw new ObjenesisException(e); } catch (IllegalAccessException e) { throw new ObjenesisException(e); } }
/** * Read the bytes of a class from the classpath * * @param className full class name including the package * @return the bytes representing the class * @throws IllegalArgumentException if the class is longer than 2500 bytes * @throws IOException if we fail to read the class */ public static byte[] readClass(String className) throws IOException { // convert to a resource className = classNameToResource(className); byte[] b = new byte[2500]; // I'm assuming that I'm reading class that are not too big int length; InputStream in = ClassDefinitionUtils.class.getClassLoader().getResourceAsStream(className); try { length = in.read(b); } finally { in.close(); } if(length >= 2500) { throw new IllegalArgumentException("The class is longer that 2500 bytes which is currently unsupported"); } byte[] copy = new byte[length]; System.arraycopy(b, 0, copy, 0, length); return copy; }
return new ObjectInputStreamInstantiator<>(type); return new ObjectStreamClassInstantiator<>(type);
@Override public int read() { int result = data[pointer++]; if(pointer >= data.length) { advanceBuffer(); } return result; }
return new ObjectInputStreamInstantiator<>(type); return new AccessibleInstantiator<>(type);
public ObjectStreamClassInstantiator(Class<T> type) { initialize(); objStreamClass = ObjectStreamClass.lookup(type); }
public ObjectInputStreamInstantiator(Class<T> clazz) { if(Serializable.class.isAssignableFrom(clazz)) { try { this.inputStream = new ObjectInputStream(new MockStream(clazz)); } catch(IOException e) { throw new Error("IOException: " + e.getMessage()); } } else { throw new ObjenesisException(new NotSerializableException(clazz + " not serializable")); } }
String parentClazz = classNameToInternalClassName(type.getName()); String clazz = parentClazz + suffix;
@Override public int read(byte[] b, int off, int len) throws IOException { int left = len; int remaining = data.length - pointer; while(remaining <= left) { System.arraycopy(data, pointer, b, off, remaining); off += remaining; left -= remaining; advanceBuffer(); remaining = data.length - pointer; } if(left > 0) { System.arraycopy(data, pointer, b, off, left); pointer += left; } return len; } }
String clazz = classNameToInternalClassName(className); in.writeUTF(classNameToInternalClassName(type.getName()));
@Override public int read(byte[] b, int off, int len) { int left = len; int remaining = data.length - pointer; while(remaining <= left) { System.arraycopy(data, pointer, b, off, remaining); off += remaining; left -= remaining; advanceBuffer(); remaining = data.length - pointer; } if(left > 0) { System.arraycopy(data, pointer, b, off, left); pointer += left; } return len; } }
public int read() throws IOException { int result = data[pointer++]; if(pointer >= data.length) { advanceBuffer(); } return result; }