sun.reflect.ReflectionFactory Java Examples

The following examples show how to use sun.reflect.ReflectionFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PluginConfigFactory.java    From sylph with Apache License 2.0 6 votes vote down vote up
public static <T extends PluginConfig> T pluginConfigInstance(Class<T> type)
        throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException
{
    checkState(!Modifier.isAbstract(type.getModifiers()), "%s is Interface or Abstract, unable to inject", type);

    //Ignore the constructor in the configuration class
    try {
        Constructor<T> pluginConfigConstructor = type.getDeclaredConstructor();
        logger.debug("find 'no parameter' constructor with [{}]", type);
        pluginConfigConstructor.setAccessible(true);
        return pluginConfigConstructor.newInstance();
    }
    catch (NoSuchMethodException e) {
        logger.warn("Not find 'no parameter' constructor, use javassist inject with [{}]", type);
        // copy proxyConfig field value to pluginConfig ...
        Constructor superCons = Object.class.getConstructor();
        ReflectionFactory reflFactory = ReflectionFactory.getReflectionFactory();
        Constructor<?> c = reflFactory.newConstructorForSerialization(type, superCons);
        // or use unsafe, demo: PluginConfig pluginConfig = (PluginConfig) unsafe.allocateInstance(type)
        @SuppressWarnings("unchecked")
        T pluginConfig = (T) c.newInstance();
        return pluginConfig;
    }
}
 
Example #2
Source File: ReflectionUtils.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
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);

    try {
        FieldAccessor fa = ReflectionFactory.getReflectionFactory().newFieldAccessor(field, false);
        fa.set(target, value);
    } catch (NoSuchMethodError error) {
        field.set(target, value);
    }
}
 
Example #3
Source File: DynamicCodec.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static Constructor<?> getConstructor(Class<?> type) throws ReflectiveOperationException {
  Constructor<?> constructor =
      ReflectionFactory.getReflectionFactory()
          .newConstructorForSerialization(type, Object.class.getDeclaredConstructor());
  constructor.setAccessible(true);
  return constructor;
}
 
Example #4
Source File: Class.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #5
Source File: Class.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static ReflectionFactory getReflectionFactory() {
    if (reflectionFactory == null) {
        reflectionFactory =
            java.security.AccessController.doPrivileged
                (new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
    }
    return reflectionFactory;
}
 
Example #6
Source File: DynamicEnumType.java    From Carbon-2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void setField(Field field, Object target, Object value) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    field.setAccessible(true);
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

    FieldAccessor fieldAccessor = ReflectionFactory.getReflectionFactory().newFieldAccessor(field, true);
    fieldAccessor.set(target, value);
}
 
Example #7
Source File: DynamicEnumType.java    From Carbon-2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Object makeEnum(Class<?> enumClass, String name, int ordinal, Class<?>[] paramTypes, Object[] paramValues) throws Exception {
    ArrayList<Class<?>> allParamTypes = new ArrayList<Class<?>>();
    allParamTypes.add(String.class);
    allParamTypes.add(Integer.TYPE);
    allParamTypes.addAll(Arrays.asList(paramTypes));

    ArrayList<Object> allParamValues = new ArrayList<Object>();
    allParamValues.add(name);
    allParamValues.add(Integer.valueOf(ordinal));
    allParamValues.addAll(Arrays.asList(paramValues));

    Constructor<?> enumConstructor = enumClass.getDeclaredConstructor((Class[]) allParamTypes.toArray(new Class[0]));
    ConstructorAccessor constructorAccessor = ReflectionFactory.getReflectionFactory().newConstructorAccessor(enumConstructor);
    return constructorAccessor.newInstance(allParamValues.toArray(new Object[0]));
}
 
Example #8
Source File: Class.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static ReflectionFactory getReflectionFactory() {
    if (reflectionFactory == null) {
        reflectionFactory =
            java.security.AccessController.doPrivileged
                (new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
    }
    return reflectionFactory;
}
 
Example #9
Source File: Class.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #10
Source File: Class.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static ReflectionFactory getReflectionFactory() {
    if (reflectionFactory == null) {
        reflectionFactory =
            java.security.AccessController.doPrivileged
                (new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
    }
    return reflectionFactory;
}
 
Example #11
Source File: Class.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #12
Source File: Class.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #13
Source File: DynamicEnumType.java    From Carbon with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Object makeEnum(Class<?> enumClass, String name, int ordinal, Class<?>[] paramTypes, Object[] paramValues) throws Exception {
	ArrayList<Class<?>> allParamTypes = new ArrayList<Class<?>>();
	allParamTypes.add(String.class);
	allParamTypes.add(Integer.TYPE);
	allParamTypes.addAll(Arrays.asList(paramTypes));

	ArrayList<Object> allParamValues = new ArrayList<Object>();
	allParamValues.add(name);
	allParamValues.add(Integer.valueOf(ordinal));
	allParamValues.addAll(Arrays.asList(paramValues));

	Constructor<?> enumConstructor = enumClass.getDeclaredConstructor((Class[]) allParamTypes.toArray(new Class[0]));
	ConstructorAccessor constructorAccessor = ReflectionFactory.getReflectionFactory().newConstructorAccessor(enumConstructor);
	return constructorAccessor.newInstance(allParamValues.toArray(new Object[0]));
}
 
Example #14
Source File: Class.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #15
Source File: Class.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #16
Source File: Bridge.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private Bridge()
{
    latestUserDefinedLoaderMethod = getLatestUserDefinedLoaderMethod();
    unsafe = getUnsafe() ;
    reflectionFactory = (ReflectionFactory)AccessController.doPrivileged(
        new ReflectionFactory.GetReflectionFactoryAction());
}
 
Example #17
Source File: Class.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
private static ReflectionFactory getReflectionFactory() {
    if (reflectionFactory == null) {
        reflectionFactory =
            java.security.AccessController.doPrivileged
                (new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
    }
    return reflectionFactory;
}
 
Example #18
Source File: Class.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #19
Source File: Class.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static ReflectionFactory getReflectionFactory() {
    if (reflectionFactory == null) {
        reflectionFactory =
            java.security.AccessController.doPrivileged
                (new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
    }
    return reflectionFactory;
}
 
Example #20
Source File: ReferenceCleanup.java    From Carbon with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void setField(Field field, Object target, Object value) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
	field.setAccessible(true);
	Field modifiersField = Field.class.getDeclaredField("modifiers");
	modifiersField.setAccessible(true);
	modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

	FieldAccessor fieldAccessor = ReflectionFactory.getReflectionFactory().newFieldAccessor(field, true);
	fieldAccessor.set(target, value);
}
 
Example #21
Source File: Injector.java    From Carbon with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void setStaticFinalField(Field field, Object newValue) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    field.setAccessible(true);
    Field fieldModifiers = Field.class.getDeclaredField("modifiers");
    fieldModifiers.setAccessible(true);
    fieldModifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
 FieldAccessor fieldAccessor = ReflectionFactory.getReflectionFactory().newFieldAccessor(field, true);
 fieldAccessor.set(null, newValue);
}
 
Example #22
Source File: Class.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #23
Source File: Class.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #24
Source File: DynamicEnumType.java    From Carbon-2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Object makeEnum(Class<?> enumClass, String name, int ordinal, Class<?>[] paramTypes, Object[] paramValues) throws Exception {
    ArrayList<Class<?>> allParamTypes = new ArrayList<Class<?>>();
    allParamTypes.add(String.class);
    allParamTypes.add(Integer.TYPE);
    allParamTypes.addAll(Arrays.asList(paramTypes));

    ArrayList<Object> allParamValues = new ArrayList<Object>();
    allParamValues.add(name);
    allParamValues.add(Integer.valueOf(ordinal));
    allParamValues.addAll(Arrays.asList(paramValues));

    Constructor<?> enumConstructor = enumClass.getDeclaredConstructor((Class[]) allParamTypes.toArray(new Class[0]));
    ConstructorAccessor constructorAccessor = ReflectionFactory.getReflectionFactory().newConstructorAccessor(enumConstructor);
    return constructorAccessor.newInstance(allParamValues.toArray(new Object[0]));
}
 
Example #25
Source File: Reflections.java    From ysoserial-modified with MIT License 5 votes vote down vote up
@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);
}
 
Example #26
Source File: ReflectionUtils.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
private static ConstructorAccessor getConstructorAccessor(Class<?> enumClass,
                                                          Class<?>[] additionalParameterTypes) throws NoSuchMethodException {
    Class<?>[] parameterTypes = new Class[additionalParameterTypes.length + 2];
    parameterTypes[0] = String.class;
    parameterTypes[1] = int.class;
    System.arraycopy(additionalParameterTypes, 0,
            parameterTypes, 2, additionalParameterTypes.length);
    return ReflectionFactory.getReflectionFactory().newConstructorAccessor(enumClass.getDeclaredConstructor(parameterTypes));
}
 
Example #27
Source File: Class.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static ReflectionFactory getReflectionFactory() {
    if (reflectionFactory == null) {
        reflectionFactory =
            java.security.AccessController.doPrivileged
                (new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
    }
    return reflectionFactory;
}
 
Example #28
Source File: Class.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #29
Source File: Class.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #30
Source File: Class.java    From jdk-1.7-annotated with Apache License 2.0 5 votes vote down vote up
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;
}