本文整理匯總了Java中sun.reflect.ReflectionFactory類的典型用法代碼示例。如果您正苦於以下問題:Java ReflectionFactory類的具體用法?Java ReflectionFactory怎麽用?Java ReflectionFactory使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ReflectionFactory類屬於sun.reflect包,在下文中一共展示了ReflectionFactory類的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: asNMSIngredient
import sun.reflect.ReflectionFactory; //導入依賴的package包/類
public RecipeItemStack asNMSIngredient() {
try {
//not usable with java 9
// RecipeItemStack recipeItemStackInjected = (RecipeItemStack) unsafe.allocateInstance(recipeItemStackInjectedClass);
// ReflectionUtil.setDeclaredFieldValue(recipeItemStackInjected, "predicate", tester);
// ReflectionUtil.setFinalFieldValue(recipeItemStackInjected, "choices", new ItemStack[0]);
// return recipeItemStackInjected;
//not usable to to illegal access exception
// Constructor<? extends RecipeItemStack> constructor = recipeItemStackInjectedClass.getConstructor(Predicate.class);
// RecipeItemStack recipeItemStackInjected = constructor.newInstance(tester);
// return recipeItemstackInjected;
Constructor<? extends RecipeItemStack> constructor = (Constructor<? extends RecipeItemStack>) ReflectionFactory.getReflectionFactory()
.newConstructorForSerialization(recipeItemStackInjectedClass, Object.class.getConstructor()); //TODO generates NPE?
RecipeItemStack recipeItemStackInjected = constructor.newInstance();
ReflectionUtil.setDeclaredFieldValue(recipeItemStackInjected, "predicate", tester);
ReflectionUtil.setFinalFieldValue(recipeItemStackInjected, "choices", new ItemStack[0]);
return recipeItemStackInjected;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例2: setFailsafeFieldValue
import sun.reflect.ReflectionFactory; //導入依賴的package包/類
public static void setFailsafeFieldValue(Field field, Object target, Object value)
throws NoSuchFieldException, IllegalAccessException {
// let's make the field accessible
field.setAccessible(true);
// next we change the modifier in the Field instance to
// not be final anymore, thus tricking reflection into
// letting us modify the static final field
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
int modifiers = modifiersField.getInt(field);
// blank out the final bit in the modifiers int
modifiers &= ~Modifier.FINAL;
modifiersField.setInt(field, modifiers);
FieldAccessor fa = ReflectionFactory.getReflectionFactory().newFieldAccessor(field, false);
fa.set(target, value);
}
示例3: createPristineURL
import sun.reflect.ReflectionFactory; //導入依賴的package包/類
private static URL createPristineURL() {
// Creates a pristine, incomplete URL object, that gets created when we
// step into new URL(), for instance.
ReflectionFactory rf = ReflectionFactory.getReflectionFactory();
try {
Constructor constructor = rf.newConstructorForSerialization(URL.class, Object.class.getDeclaredConstructor());
Object newInstance = constructor.newInstance();
return (URL) newInstance;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
示例4: createWithConstructor
import sun.reflect.ReflectionFactory; //導入依賴的package包/類
@SuppressWarnings ( {"unchecked"} )
public static <T> T createWithConstructor ( Class<T> classToInstantiate, Class<? super T> constructorClass, Class<?>[] consArgTypes, Object[] consArgs )
throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
Constructor<? super T> objCons = constructorClass.getDeclaredConstructor(consArgTypes);
objCons.setAccessible(true);
Constructor<?> sc = ReflectionFactory.getReflectionFactory().newConstructorForSerialization(classToInstantiate, objCons);
sc.setAccessible(true);
return (T)sc.newInstance(consArgs);
}
示例5: run
import sun.reflect.ReflectionFactory; //導入依賴的package包/類
@Override
public Constructor<ServletPrintWriterDelegate> run() {
try {
return (Constructor)ReflectionFactory.getReflectionFactory().newConstructorForSerialization(ServletPrintWriterDelegate.class, Object.class.getDeclaredConstructor());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
示例6: Bridge
import sun.reflect.ReflectionFactory; //導入依賴的package包/類
private Bridge()
{
latestUserDefinedLoaderMethod = getLatestUserDefinedLoaderMethod();
unsafe = getUnsafe() ;
reflectionFactory = (ReflectionFactory)AccessController.doPrivileged(
new ReflectionFactory.GetReflectionFactoryAction());
}
示例7: copyFields
import sun.reflect.ReflectionFactory; //導入依賴的package包/類
private static Field[] copyFields(Field[] arg) {
Field[] out = new Field[arg.length];
ReflectionFactory fact = getReflectionFactory();
for (int i = 0; i < arg.length; i++) {
out[i] = fact.copyField(arg[i]);
}
return out;
}
示例8: copyMethods
import sun.reflect.ReflectionFactory; //導入依賴的package包/類
private static Method[] copyMethods(Method[] arg) {
Method[] out = new Method[arg.length];
ReflectionFactory fact = getReflectionFactory();
for (int i = 0; i < arg.length; i++) {
out[i] = fact.copyMethod(arg[i]);
}
return out;
}
示例9: copyConstructors
import sun.reflect.ReflectionFactory; //導入依賴的package包/類
private static <U> Constructor<U>[] copyConstructors(Constructor<U>[] arg) {
Constructor<U>[] out = arg.clone();
ReflectionFactory fact = getReflectionFactory();
for (int i = 0; i < out.length; i++) {
out[i] = fact.copyConstructor(out[i]);
}
return out;
}
示例10: getReflectionFactory
import sun.reflect.ReflectionFactory; //導入依賴的package包/類
private static ReflectionFactory getReflectionFactory() {
if (reflectionFactory == null) {
reflectionFactory =
java.security.AccessController.doPrivileged
(new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
}
return reflectionFactory;
}
示例11: createEmptyObject
import sun.reflect.ReflectionFactory; //導入依賴的package包/類
public static <T> Optional<T> createEmptyObject(Class<T> clazz) {
ReflectionFactory factory = ReflectionFactory.getReflectionFactory();
Optional<T> optional;
try {
Constructor objDef = Object.class.getDeclaredConstructor();
Constructor constr = factory.newConstructorForSerialization(clazz, objDef);
optional = Optional.ofNullable(clazz.cast(constr.newInstance()));
} catch(NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
e.printStackTrace();
optional = Optional.empty();
}
return optional;
}
示例12: createWithConstructor
import sun.reflect.ReflectionFactory; //導入依賴的package包/類
@SuppressWarnings ( {
"unchecked"
} )
public static <T> T createWithConstructor ( Class<T> classToInstantiate, Class<? super T> constructorClass, Class<?>[] consArgTypes,
Object[] consArgs ) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
Constructor<? super T> objCons = constructorClass.getDeclaredConstructor(consArgTypes);
objCons.setAccessible(true);
Constructor<?> sc = ReflectionFactory.getReflectionFactory().newConstructorForSerialization(classToInstantiate, objCons);
sc.setAccessible(true);
return (T) sc.newInstance(consArgs);
}
示例13: getConstructor
import sun.reflect.ReflectionFactory; //導入依賴的package包/類
private static Constructor<?> getConstructor(Class<?> type)
throws NoSuchMethodException
{
ReflectionFactory factory = ReflectionFactory.getReflectionFactory();
Constructor<?> objectConstructor = type.getConstructor((Class[]) null);
@SuppressWarnings("unchecked")
Constructor<?> c = (Constructor<?>) factory
.newConstructorForSerialization(type, objectConstructor);
return c;
}