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);
}
}
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);
}
}
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);
}
}
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();
}
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();
}
Aggregations