Search in sources :

Example 11 with MethodData

use of org.fakereplace.data.MethodData in project fakereplace by fakereplace.

the class MethodReflection method getDeclaredMethod.

public static Method getDeclaredMethod(Class<?> clazz, String name, Class<?>... parameters) throws NoSuchMethodException {
    ClassData cd = ClassDataStore.instance().getModifiedClassData(clazz.getClassLoader(), Descriptor.toJvmName(clazz.getName()));
    if (cd == null || !cd.isReplaceable()) {
        return clazz.getDeclaredMethod(name, parameters);
    }
    String args;
    if (parameters != null) {
        args = '(' + DescriptorUtils.classArrayToDescriptorString(parameters) + ')';
    } else {
        args = "()";
    }
    MethodData md = cd.getMethodData(name, args);
    if (md == null) {
        return clazz.getDeclaredMethod(name, parameters);
    }
    switch(md.getType()) {
        case NORMAL:
            return clazz.getDeclaredMethod(name, parameters);
        case FAKE:
            try {
                Class<?> c = clazz.getClassLoader().loadClass(md.getClassName());
                return c.getDeclaredMethod(name, parameters);
            } catch (NoSuchMethodException e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
    }
    throw new NoSuchMethodException();
}
Also used : ClassData(org.fakereplace.data.ClassData) MethodData(org.fakereplace.data.MethodData) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 12 with MethodData

use of org.fakereplace.data.MethodData in project fakereplace by fakereplace.

the class MethodReflection method getMethods.

public static Method[] getMethods(Class<?> clazz) {
    try {
        ClassData cd = ClassDataStore.instance().getModifiedClassData(clazz.getClassLoader(), Descriptor.toJvmName(clazz.getName()));
        if (cd == null) {
            return clazz.getMethods();
        }
        Method[] meth = clazz.getMethods();
        List<Method> visible = new ArrayList<>(meth.length);
        for (int i = 0; i < meth.length; ++i) {
            MethodData mData = cd.getData(meth[i]);
            if (mData == null || mData.getType() == MemberType.NORMAL) {
                visible.add(meth[i]);
            }
        }
        ClassData cta = cd;
        while (cta != null) {
            if (cta.isReplaceable()) {
                for (MethodData i : cta.getMethods()) {
                    if (i.getType() == MemberType.FAKE && AccessFlag.isPublic(i.getAccessFlags())) {
                        Class<?> c = clazz.getClassLoader().loadClass(i.getClassName());
                        visible.add(i.getMethod(c));
                    } else if (i.getType() == MemberType.REMOVED) {
                        Class<?> c = clazz.getClassLoader().loadClass(i.getClassName());
                        visible.remove(i.getMethod(c));
                    }
                }
            }
            cta = cta.getSuperClassInformation();
        }
        Method[] ret = new Method[visible.size()];
        for (int i = 0; i < visible.size(); ++i) {
            ret[i] = visible.get(i);
        }
        return ret;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ClassData(org.fakereplace.data.ClassData) ArrayList(java.util.ArrayList) MethodData(org.fakereplace.data.MethodData) ModifiedMethod(org.fakereplace.data.ModifiedMethod) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 13 with MethodData

use of org.fakereplace.data.MethodData in project fakereplace by fakereplace.

the class MethodReflection method invoke.

public static Object invoke(Method method, Object instance, Object[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    if (!Modifier.isStatic(method.getModifiers())) {
        MethodData info = ClassDataStore.instance().getMethodInformation(method.getDeclaringClass().getName());
        try {
            Method invoke = info.getMethodToInvoke(method.getDeclaringClass());
            Object[] newAgrs = prependInstanceToParams(instance, args);
            return invokeWithPermissionCheck(method, invoke, null, newAgrs);
        } catch (NoSuchMethodException | ClassNotFoundException | SecurityException e) {
            throw new RuntimeException(e);
        }
    }
    return invokeWithPermissionCheck(method, method, instance, args);
}
Also used : MethodData(org.fakereplace.data.MethodData) ModifiedMethod(org.fakereplace.data.ModifiedMethod) Method(java.lang.reflect.Method)

Aggregations

MethodData (org.fakereplace.data.MethodData)13 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 ClassData (org.fakereplace.data.ClassData)8 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)4 MethodInfo (javassist.bytecode.MethodInfo)3 BaseClassData (org.fakereplace.data.BaseClassData)3 ModifiedMethod (org.fakereplace.data.ModifiedMethod)3 Constructor (java.lang.reflect.Constructor)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Bytecode (javassist.bytecode.Bytecode)2 CodeIterator (javassist.bytecode.CodeIterator)2 ConstPool (javassist.bytecode.ConstPool)2 IOException (java.io.IOException)1 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)1 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)1 BadBytecode (javassist.bytecode.BadBytecode)1 CodeAttribute (javassist.bytecode.CodeAttribute)1