Search in sources :

Example 6 with ClassData

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

the class ConstructorReflection method getDeclaredConstructors.

public static Constructor<?>[] getDeclaredConstructors(Class<?> clazz) {
    try {
        ClassData cd = ClassDataStore.instance().getModifiedClassData(clazz.getClassLoader(), Descriptor.toJvmName(clazz.getName()));
        if (cd == null || !cd.isReplaceable()) {
            return clazz.getDeclaredConstructors();
        }
        Constructor<?>[] meth = clazz.getDeclaredConstructors();
        List<Constructor<?>> visible = new ArrayList<>(meth.length);
        for (int i = 0; i < meth.length; ++i) {
            if (meth[i].getParameterTypes().length != 3 || !meth[i].getParameterTypes()[2].equals(ConstructorArgument.class)) {
                visible.add(meth[i]);
            }
        }
        for (MethodData i : cd.getMethods()) {
            if (i.getType() == MemberType.FAKE_CONSTRUCTOR) {
                Class<?> c = clazz.getClassLoader().loadClass(i.getClassName());
                visible.add(i.getConstructor(c));
            } else if (i.getType() == MemberType.REMOVED && i.getMethodName().equals("<init>")) {
                Class<?> c = clazz.getClassLoader().loadClass(i.getClassName());
                visible.remove(i.getConstructor(c));
            }
        }
        Constructor<?>[] ret = new Constructor[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) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) MethodData(org.fakereplace.data.MethodData) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with ClassData

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

the class FieldReflection method getDeclaredFields.

public static Field[] getDeclaredFields(Class<?> clazz) {
    if (!ClassDataStore.instance().isClassReplaced(clazz)) {
        return clazz.getDeclaredFields();
    }
    try {
        ClassData cd = ClassDataStore.instance().getModifiedClassData(clazz.getClassLoader(), Descriptor.toJvmName(clazz.getName()));
        Field[] meth = clazz.getDeclaredFields();
        Collection<FieldData> fieldData = cd.getFields();
        List<Field> visible = new ArrayList<>(meth.length);
        for (int i = 0; i < meth.length; ++i) {
            for (FieldData f : fieldData) {
                if (f.getAccessFlags() == meth[i].getModifiers() && f.getName().equals(meth[i].getName())) {
                    if (f.getMemberType() == MemberType.NORMAL) {
                        visible.add(meth[i]);
                        break;
                    }
                }
            }
        }
        for (FieldData i : cd.getFields()) {
            if (i.getMemberType() == MemberType.FAKE) {
                Class<?> c = clazz.getClassLoader().loadClass(i.getClassName());
                visible.add(i.getField(c));
            }
        }
        Field[] ret = new Field[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 : FieldData(org.fakereplace.data.FieldData) Field(java.lang.reflect.Field) ClassData(org.fakereplace.data.ClassData) ArrayList(java.util.ArrayList)

Example 8 with ClassData

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

the class FieldReflection method getFields.

public static Field[] getFields(Class<?> clazz) {
    if (!ClassDataStore.instance().isClassReplaced(clazz)) {
        return clazz.getFields();
    }
    try {
        ClassData cd = ClassDataStore.instance().getModifiedClassData(clazz.getClassLoader(), Descriptor.toJvmName(clazz.getName()));
        if (cd == null) {
            return clazz.getDeclaredFields();
        }
        Field[] meth = clazz.getFields();
        Collection<FieldData> fieldData = cd.getFields();
        List<Field> visible = new ArrayList<>(meth.length);
        for (int i = 0; i < meth.length; ++i) {
            for (FieldData f : fieldData) {
                if (f.getAccessFlags() == meth[i].getModifiers() && f.getName().equals(meth[i].getName())) {
                    if (f.getMemberType() == MemberType.NORMAL) {
                        visible.add(meth[i]);
                        break;
                    }
                }
            }
        }
        ClassData cta = cd;
        while (cta != null) {
            for (FieldData i : cta.getFields()) {
                if (i.getMemberType() == MemberType.FAKE && AccessFlag.isPublic(i.getAccessFlags())) {
                    Class<?> c = clazz.getClassLoader().loadClass(i.getClassName());
                    visible.add(i.getField(c));
                }
            }
            cta = cta.getSuperClassInformation();
        }
        Field[] ret = new Field[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 : FieldData(org.fakereplace.data.FieldData) Field(java.lang.reflect.Field) ClassData(org.fakereplace.data.ClassData) ArrayList(java.util.ArrayList)

Example 9 with ClassData

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

the class FieldReflection method getField.

public static Field getField(Class<?> clazz, String name) throws NoSuchFieldException {
    if (!ClassDataStore.instance().isClassReplaced(clazz)) {
        return clazz.getField(name);
    }
    ClassData cd = ClassDataStore.instance().getModifiedClassData(clazz.getClassLoader(), Descriptor.toJvmName(clazz.getName()));
    if (cd == null) {
        return clazz.getField(name);
    }
    FieldData fd = cd.getField(name);
    if (fd == null) {
        return clazz.getField(name);
    }
    if (!AccessFlag.isPublic(fd.getAccessFlags())) {
        throw new NoSuchFieldException(clazz.getName() + "." + name);
    }
    switch(fd.getMemberType()) {
        case NORMAL:
            return clazz.getField(name);
        case FAKE:
            try {
                Class<?> c = clazz.getClassLoader().loadClass(fd.getClassName());
                return c.getField(name);
            } catch (NoSuchFieldException e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
    }
    throw new NoSuchFieldException();
}
Also used : FieldData(org.fakereplace.data.FieldData) ClassData(org.fakereplace.data.ClassData)

Example 10 with ClassData

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

the class FieldReflection method getDeclaredField.

public static Field getDeclaredField(Class<?> clazz, String name) throws NoSuchFieldException {
    if (!ClassDataStore.instance().isClassReplaced(clazz)) {
        return clazz.getDeclaredField(name);
    }
    ClassData cd = ClassDataStore.instance().getModifiedClassData(clazz.getClassLoader(), Descriptor.toJvmName(clazz.getName()));
    if (cd == null) {
        return clazz.getDeclaredField(name);
    }
    FieldData fd = cd.getField(name);
    if (fd == null) {
        return clazz.getDeclaredField(name);
    }
    switch(fd.getMemberType()) {
        case NORMAL:
            return clazz.getDeclaredField(name);
        case FAKE:
            try {
                Class<?> c = clazz.getClassLoader().loadClass(fd.getClassName());
                return c.getDeclaredField(name);
            } catch (NoSuchFieldException e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
    }
    throw new NoSuchFieldException();
}
Also used : FieldData(org.fakereplace.data.FieldData) ClassData(org.fakereplace.data.ClassData)

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