This class may be removed in the future. Consider the replacements in JEP 193 'Variable Handles': http://openjdk.java.net/jeps/193
/** * Allocates instance of given class. * * @param cls Class. * @return Allocated instance. */ public static Object allocateInstance(Class cls) throws InstantiationException { return UNSAFE.allocateInstance(cls); }
public Object allocateInstance(Class<?> c) throws InstantiationException { return this.unsafe.allocateInstance(c); }
@Override public <E> E createProxy(Class<E> targetClass, E target) { try { Object proxy = UNSAFE.allocateInstance(proxyClass); field.set(proxy, target); return targetClass.cast(proxy); } catch (Throwable t) { throw new AssertionError(t); } } };
public T newInstance() { try { return type.cast(unsafe.allocateInstance(type)); } catch (InstantiationException e) { throw new ObjenesisException(e); } } }
@Override public Object newInstance(Class clazz, Constructor cons, boolean doesRequireInit, boolean unsafeAsLastResort) { try { if (!doesRequireInit && FSTUtil.unFlaggedUnsafe != null) { // no performance improvement here, keep for nasty constructables .. return FSTUtil.unFlaggedUnsafe.allocateInstance(clazz); } if ( cons == null ) // no suitable constructor found { if ( unsafeAsLastResort ) { // best effort. use Unsafe to instantiate. // Warning: if class contains transient fields which have default values assigned ('transient int x = 3'), // those will not be assigned after deserialization as unsafe instantiation does not execute any default // construction code. // Define a public no-arg constructor to avoid this behaviour (rarely an issue, but there are cases). if ( FSTUtil.unFlaggedUnsafe != null ) { return FSTUtil.unFlaggedUnsafe.allocateInstance(clazz); } throw new RuntimeException("no suitable constructor found and no Unsafe instance avaiable. Can't instantiate "+ clazz.getName()); } } return cons.newInstance(); } catch (Throwable ignored) { ignored.printStackTrace(); return null; } }
/** * Create a new DirectByteBuffer that wraps the given address and has the given capacity. * <p> * The ByteBuffer does NOT create a Cleaner, or otherwise register the pointer for freeing. */ public static ByteBuffer newDirectByteBuffer( long addr, int cap ) throws Exception { if ( directByteBufferCtor == null ) { // Simulate the JNI NewDirectByteBuffer(void*, long) invocation. Object dbb = unsafe.allocateInstance( directByteBufferClass ); initDirectByteBuffer( dbb, addr, cap ); return (ByteBuffer) dbb; } // Reflection based fallback code. return (ByteBuffer) directByteBufferCtor.newInstance( addr, cap ); }
public T newInstance() { try { return type.cast(unsafe.allocateInstance(type)); } catch (InstantiationException e) { throw new ObjenesisException(e); } } }
@Override public E read(Excerpt bytes) { E e; try { e = (E) UnsafeExcerpt.UNSAFE.allocateInstance(classMarshaled); e.readExternal(bytes); } catch (Exception e2) { throw new IllegalStateException(e2); } return e; }
/** * Returns a new {@link String} backed by the given {@code chars}. The char array should not * be mutated any more after calling this function. */ public static String moveToString(char[] chars) { if (STRING_VALUE_OFFSET == -1) { // In the off-chance that this JDK does not implement String as we'd expect, just do a copy. return new String(chars); } final String str; try { str = (String) unsafe.allocateInstance(String.class); } catch (InstantiationException e) { // This should never happen, but return a copy as a fallback just in case. return new String(chars); } unsafe.putObject(str, STRING_VALUE_OFFSET, chars); return str; }
/** * Returns a new {@link String} backed by the given {@code chars}. The char array should not * be mutated any more after calling this function. */ public static String moveToString(char[] chars) { if (STRING_VALUE_OFFSET == -1) { // In the off-chance that this JDK does not implement String as we'd expect, just do a copy. return new String(chars); } final String str; try { str = (String) unsafe.allocateInstance(String.class); } catch (InstantiationException e) { // This should never happen, but return a copy as a fallback just in case. return new String(chars); } unsafe.putObject(str, STRING_VALUE_OFFSET, chars); return str; }
/** * Returns a new {@link String} backed by the given {@code chars}. The char array should not * be mutated any more after calling this function. */ static String moveToString(char[] chars) { if (STRING_VALUE_OFFSET == -1) { // In the off-chance that this JDK does not implement String as we'd expect, just do a copy. return new String(chars); } final String str; try { str = (String) UNSAFE.allocateInstance(String.class); } catch (InstantiationException e) { // This should never happen, but return a copy as a fallback just in case. return new String(chars); } putObject(str, STRING_VALUE_OFFSET, chars); return str; }
@Override public Object newInstance(Class clazz, Constructor cons, boolean doesRequireInit, boolean unsafeAsLastResort) { try { if (!doesRequireInit && FSTUtil.unFlaggedUnsafe != null) { // no performance improvement here, keep for nasty constructables .. return FSTUtil.unFlaggedUnsafe.allocateInstance(clazz); } if ( cons == null ) // no suitable constructor found { if ( unsafeAsLastResort ) { // best effort. use Unsafe to instantiate. // Warning: if class contains transient fields which have default values assigned ('transient int x = 3'), // those will not be assigned after deserialization as unsafe instantiation does not execute any default // construction code. // Define a public no-arg constructor to avoid this behaviour (rarely an issue, but there are cases). if ( FSTUtil.unFlaggedUnsafe != null ) { return FSTUtil.unFlaggedUnsafe.allocateInstance(clazz); } throw new RuntimeException("no suitable constructor found and no Unsafe instance avaiable. Can't instantiate "+ clazz.getName()); } } return cons.newInstance(); } catch (Throwable ignored) { logger.log(FSTLogger.Level.INFO, "Failed to construct new instance", ignored); return null; } }
public <T extends FSTStruct> T createWrapper(Class<T> onHeap, Bytez bytes, long index) throws Exception { Class proxy = getProxyClass(onHeap); T res = (T) FSTUtil.getUnsafe().allocateInstance(proxy); res.baseOn(bytes, index, this); return res; }
protected static <C extends FSTStruct> Object allocInstance(Class<C> clazz) throws Exception { return FSTUtil.getUnsafe().allocateInstance(FSTStructFactory.getInstance().getProxyClass(clazz)); // return FSTStructFactory.getInstance().getProxyClass(clazz).newInstance(); }
public <T extends FSTStruct> T createWrapper(Class<T> onHeap, Bytez bytes, long index) throws Exception { Class proxy = getProxyClass(onHeap); T res = (T) FSTUtil.getUnsafe().allocateInstance(proxy); res.baseOn(bytes, index, this); return res; }
protected static <C extends FSTStruct> Object allocInstance(Class<C> clazz) throws Exception { return FSTUtil.getUnsafe().allocateInstance(FSTStructFactory.getInstance().getProxyClass(clazz)); // return FSTStructFactory.getInstance().getProxyClass(clazz).newInstance(); }
public Object newInstance(Class type) { if (exception != null) { ObjectAccessException ex = new ObjectAccessException("Cannot construct type", exception); ex.add("construction-type", type.getName()); throw ex; } ErrorWritingException ex = null; if (type == void.class || type == Void.class) { ex = new ConversionException("Type void cannot have an instance"); } else { try { return unsafe.allocateInstance(type); } catch (final SecurityException e) { ex = new ObjectAccessException("Cannot construct type", e); } catch (final InstantiationException e) { ex = new ConversionException("Cannot construct type", e); } catch (final IllegalArgumentException e) { ex = new ObjectAccessException("Cannot construct type", e); } } ex.add("construction-type", type.getName()); throw ex; }
@SuppressWarnings("restriction") protected Object instantiate() throws Exception { return _unsafe.allocateInstance(_type); }
public Object newInstance() { try { return theUnsafe.allocateInstance(clazz); } catch (InstantiationException e) { throw new Sql2oException("Could not create a new instance of class " + clazz, e); } } };
public Object newInstance() { try { return theUnsafe.allocateInstance(clazz); } catch (InstantiationException e) { throw new Sql2oException("Could not create a new instance of class " + clazz, e); } } };