Java Code Examples for sun.reflect.ReflectionFactory

The following examples show how to use sun.reflect.ReflectionFactory. These examples are extracted from open source projects. 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 Project: sylph   Source File: PluginConfigFactory.java    License: 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
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 Project: jdk1.8-source-analysis   Source File: Class.java    License: 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 4
Source Project: jdk1.8-source-analysis   Source File: Class.java    License: Apache License 2.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 5
Source Project: ysoserial   Source File: Reflections.java    License: 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);
 setAccessible(objCons);
    Constructor<?> sc = ReflectionFactory.getReflectionFactory().newConstructorForSerialization(classToInstantiate, objCons);
 setAccessible(sc);
    return (T)sc.newInstance(consArgs);
}
 
Example 6
Source Project: jdk1.8-source-analysis   Source File: Class.java    License: 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 7
Source Project: JNDI-Injection-Exploit   Source File: Reflections.java    License: 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 8
Source Project: dragonwell8_jdk   Source File: Class.java    License: 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 9
Source Project: dragonwell8_jdk   Source File: Class.java    License: 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 Project: dragonwell8_jdk   Source File: Class.java    License: 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 11
Source Project: dragonwell8_jdk   Source File: Class.java    License: 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 12
Source Project: TencentKona-8   Source File: Bridge.java    License: 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 13
Source Project: TencentKona-8   Source File: Class.java    License: 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 14
Source Project: TencentKona-8   Source File: Class.java    License: 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 Project: TencentKona-8   Source File: Class.java    License: 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 16
Source Project: TencentKona-8   Source File: Class.java    License: 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 17
Source Project: jdk8u60   Source File: Bridge.java    License: 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 18
Source Project: jdk8u60   Source File: Class.java    License: 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 19
Source Project: jdk8u60   Source File: Class.java    License: 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 20
Source Project: jdk8u60   Source File: Class.java    License: 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 21
Source Project: jdk8u60   Source File: Class.java    License: 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 22
Source Project: learnjavabug   Source File: Reflections.java    License: 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 23
Source Project: learnjavabug   Source File: Reflections.java    License: 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 24
Source Project: learnjavabug   Source File: Reflections.java    License: 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);
 setAccessible(objCons);
    Constructor<?> sc = ReflectionFactory
        .getReflectionFactory().newConstructorForSerialization(classToInstantiate, objCons);
 setAccessible(sc);
    return (T)sc.newInstance(consArgs);
}
 
Example 25
Source Project: learnjavabug   Source File: Reflections.java    License: 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
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 27
Source Project: JDKSourceCode1.8   Source File: Class.java    License: MIT License 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 28
Source Project: JDKSourceCode1.8   Source File: Class.java    License: MIT License 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 29
Source Project: JDKSourceCode1.8   Source File: Class.java    License: MIT License 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 30
Source Project: JDKSourceCode1.8   Source File: Class.java    License: MIT License 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;
}