Search in sources :

Example 11 with ClassData

use of org.fakereplace.data.ClassData 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 ClassData

use of org.fakereplace.data.ClassData 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)

Aggregations

ClassData (org.fakereplace.data.ClassData)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 MethodData (org.fakereplace.data.MethodData)8 ArrayList (java.util.ArrayList)6 FieldData (org.fakereplace.data.FieldData)4 Constructor (java.lang.reflect.Constructor)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 ModifiedMethod (org.fakereplace.data.ModifiedMethod)2