本文整理了Java中sun.misc.Unsafe.allocateInstance()
方法的一些代码示例,展示了Unsafe.allocateInstance()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Unsafe.allocateInstance()
方法的具体详情如下:
包路径:sun.misc.Unsafe
类名称:Unsafe
方法名:allocateInstance
[英]Allocates an instance of the given class without running the constructor. The class' will be run, if necessary.
[中]在不运行构造函数的情况下分配给定类的实例。如有必要,该课程将运行。
代码示例来源:origin: apache/ignite
/**
* Allocates instance of given class.
*
* @param cls Class.
* @return Allocated instance.
*/
public static Object allocateInstance(Class cls) throws InstantiationException {
return UNSAFE.allocateInstance(cls);
}
代码示例来源:origin: apache/geode
public Object allocateInstance(Class<?> c) throws InstantiationException {
return this.unsafe.allocateInstance(c);
}
代码示例来源:origin: robolectric/robolectric
@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);
}
}
};
代码示例来源:origin: redisson/redisson
public T newInstance() {
try {
return type.cast(unsafe.allocateInstance(type));
} catch (InstantiationException e) {
throw new ObjenesisException(e);
}
}
}
代码示例来源:origin: redisson/redisson
@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;
}
}
代码示例来源:origin: neo4j/neo4j
/**
* 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 );
}
代码示例来源:origin: org.objenesis/objenesis
public T newInstance() {
try {
return type.cast(unsafe.allocateInstance(type));
} catch (InstantiationException e) {
throw new ObjenesisException(e);
}
}
}
代码示例来源:origin: peter-lawrey/Java-Chronicle
@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;
}
代码示例来源:origin: fengjiachun/Jupiter
/**
* 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;
}
代码示例来源:origin: fengjiachun/Jupiter
/**
* 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;
}
代码示例来源:origin: com.google.protobuf/protobuf-java
/**
* 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;
}
代码示例来源:origin: RuedigerMoeller/fast-serialization
@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;
}
}
代码示例来源:origin: redisson/redisson
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;
}
代码示例来源:origin: redisson/redisson
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();
}
代码示例来源:origin: RuedigerMoeller/fast-serialization
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;
}
代码示例来源:origin: RuedigerMoeller/fast-serialization
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();
}
代码示例来源:origin: com.thoughtworks.xstream/xstream
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;
}
代码示例来源:origin: webx/citrus
@SuppressWarnings("restriction")
protected Object instantiate()
throws Exception {
return _unsafe.allocateInstance(_type);
}
代码示例来源:origin: aaberg/sql2o
public Object newInstance() {
try {
return theUnsafe.allocateInstance(clazz);
} catch (InstantiationException e) {
throw new Sql2oException("Could not create a new instance of class " + clazz, e);
}
}
};
代码示例来源:origin: aaberg/sql2o
public Object newInstance() {
try {
return theUnsafe.allocateInstance(clazz);
} catch (InstantiationException e) {
throw new Sql2oException("Could not create a new instance of class " + clazz, e);
}
}
};
内容来源于网络,如有侵权,请联系作者删除!