Skip to content

Commit b9ec36a

Browse files
committed
cleanup ReflectionHelper
1 parent 987ef34 commit b9ec36a

File tree

10 files changed

+54
-97
lines changed

10 files changed

+54
-97
lines changed

src/main/java/cn/enaium/joe/Agent.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package cn.enaium.joe;
1818

19-
import cn.enaium.joe.util.ReflectUtil;
20-
2119
import java.lang.instrument.Instrumentation;
2220
import java.lang.reflect.InvocationTargetException;
2321
import java.lang.reflect.Method;
@@ -40,7 +38,7 @@ public static void agentmain(String agentArgs, Instrumentation inst) throws Exce
4038
private static void agent(String agentArgs, Instrumentation inst) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
4139
URLClassLoader loader = new URLClassLoader(new URL[]{Agent.class.getProtectionDomain().getCodeSource().getLocation()}, ClassLoader.getSystemClassLoader().getParent());
4240
Class<?> main = loader.loadClass("cn.enaium.joe.Main");
43-
Method agent = ReflectUtil.getMethod(main, "agent", Instrumentation.class);
41+
Method agent = main.getMethod( "agent", Instrumentation.class);
4442
agent.invoke(null, inst);
4543
}
4644
}

src/main/java/cn/enaium/joe/JavaOctetEditor.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import cn.enaium.joe.jar.Jar;
2929
import cn.enaium.joe.task.TaskManager;
3030
import cn.enaium.joe.util.*;
31+
import cn.enaium.joe.util.reflection.ReflectionHelper;
3132
import com.formdev.flatlaf.FlatDarkLaf;
3233
import com.formdev.flatlaf.extras.FlatSVGIcon;
3334
import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
@@ -41,7 +42,6 @@
4142
* @author Enaium
4243
*/
4344
public class JavaOctetEditor {
44-
public static final ClassLoader classLoader = JavaOctetEditor.class.getClassLoader();
4545
private static JavaOctetEditor instance;
4646

4747
public static final String TITLE = "JavaOctetEditor";
@@ -100,7 +100,7 @@ public void run() {
100100
add(new SearchMenu());
101101

102102
AttachMenu attachMenu = new AttachMenu() {{
103-
if (!ReflectUtil.classHas("com.sun.tools.attach.VirtualMachine")) {
103+
if (!ReflectionHelper.isClassExist("com.sun.tools.attach.VirtualMachine")) {
104104
setEnabled(false);
105105
}
106106
}};
@@ -144,13 +144,4 @@ public void setJar(Jar jar) {
144144
public static JavaOctetEditor getInstance() {
145145
return instance;
146146
}
147-
148-
public static boolean isClassExist(String canonicalName){
149-
try {
150-
Class.forName(canonicalName, false, classLoader);
151-
return true;
152-
} catch (ClassNotFoundException e) {
153-
return false;
154-
}
155-
}
156147
}

src/main/java/cn/enaium/joe/Main.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@
1616

1717
package cn.enaium.joe;
1818

19-
import cn.enaium.joe.config.extend.ApplicationConfig;
2019
import cn.enaium.joe.jar.Jar;
2120
import cn.enaium.joe.util.*;
22-
import com.formdev.flatlaf.FlatDarkLaf;
21+
import cn.enaium.joe.util.reflection.ReflectionHelper;
2322
import org.objectweb.asm.ClassReader;
2423
import org.objectweb.asm.tree.ClassNode;
2524
import org.pmw.tinylog.Configurator;
2625
import org.pmw.tinylog.Logger;
2726
import org.pmw.tinylog.writers.ConsoleWriter;
2827
import org.pmw.tinylog.writers.FileWriter;
2928

30-
import javax.swing.*;
3129
import java.io.IOException;
3230
import java.lang.instrument.Instrumentation;
3331
import java.lang.reflect.InvocationTargetException;
@@ -47,6 +45,8 @@
4745
* @author Enaium
4846
*/
4947
public final class Main {
48+
public static final ClassLoader classLoader = Main.class.getClassLoader();
49+
5050
public static void main(String[] args) {
5151
ImagineBreakerHelper.boot();
5252
loadTools();
@@ -82,7 +82,7 @@ private static void launch() {
8282
}
8383

8484
private static void loadTools() {
85-
if (!ReflectUtil.classHas("com.sun.tools.attach.VirtualMachine")) {
85+
if (!ReflectionHelper.isClassExist("com.sun.tools.attach.VirtualMachine")) {
8686
Path toolsPath = Paths.get("lib", "tools.jar");
8787
Path jrePath = Paths.get(System.getProperty("java.home"));
8888
Path tool = jrePath.getParent().resolve(toolsPath);

src/main/java/cn/enaium/joe/dialog/AboutDialog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package cn.enaium.joe.dialog;
1818

1919
import cn.enaium.joe.util.LangUtil;
20-
import cn.enaium.joe.util.ReflectUtil;
20+
import cn.enaium.joe.util.reflection.ReflectionHelper;
2121
import net.miginfocom.swing.MigLayout;
2222

2323
import javax.swing.*;
@@ -58,7 +58,7 @@ public AboutDialog() {
5858
add(new JLabel(LangUtil.i18n("about.java.supportCompiler")));
5959
add(new JLabel(String.valueOf(ToolProvider.getSystemJavaCompiler() != null)), "wrap");
6060
add(new JLabel(LangUtil.i18n("about.java.supportAttach")));
61-
add(new JLabel(String.valueOf(ReflectUtil.classHas("com.sun.tools.attach.VirtualMachine"))), "wrap");
61+
add(new JLabel(String.valueOf(ReflectionHelper.isClassExist("com.sun.tools.attach.VirtualMachine"))), "wrap");
6262
}});
6363
setResizable(false);
6464
pack();

src/main/java/cn/enaium/joe/gui/panel/InheritPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import cn.enaium.joe.jar.Jar;
2626
import cn.enaium.joe.util.ASMUtil;
2727
import cn.enaium.joe.util.JTreeUtil;
28-
import cn.enaium.joe.util.ReflectUtil;
28+
import cn.enaium.joe.util.reflection.ReflectionHelper;
2929
import org.objectweb.asm.ClassReader;
3030
import org.objectweb.asm.tree.ClassNode;
3131

@@ -102,7 +102,7 @@ private void recursion(ClassTreeNode classTreeNode, boolean parent) {
102102
ClassTreeNode newChild = null;
103103
if (classes.containsKey(s + ".class")) {
104104
newChild = new ClassTreeNode(classes.get(s + ".class"));
105-
} else if (ReflectUtil.classHas(s.replace("/", "."))) {
105+
} else if (ReflectionHelper.isClassExist(s.replace("/", "."))) {
106106
try {
107107
newChild = new ClassTreeNode(ASMUtil.acceptClassNode(new ClassReader(s)));
108108
} catch (IOException ignored) {

src/main/java/cn/enaium/joe/gui/panel/file/tabbed/tab/classes/ASMifierTablePanel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import cn.enaium.joe.gui.panel.CodeAreaPanel;
2424
import cn.enaium.joe.util.*;
2525
import cn.enaium.joe.util.classes.ASMClassLoader;
26+
import cn.enaium.joe.util.reflection.ReflectionHelper;
2627
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
2728
import org.objectweb.asm.ClassReader;
2829
import org.objectweb.asm.tree.ClassNode;
@@ -69,7 +70,7 @@ public ASMifierTablePanel(ClassNode classNode) {
6970
"}";
7071

7172
byte[] dumps = (byte[])new ASMClassLoader().defineClass(className, Compiler.compileSingle(className, stringBuilder)).getMethod("dump").invoke(null);
72-
ReflectUtil.copyAllMember(classNode, ASMUtil.acceptClassNode(new ClassReader(dumps)));
73+
ReflectionHelper.copyAllMember(classNode, ASMUtil.acceptClassNode(new ClassReader(dumps)));
7374
MessageUtil.info(LangUtil.i18n("success"));
7475
EditSaveSuccessEvent.trigger(classNode.name);
7576
} catch (Throwable e) {

src/main/java/cn/enaium/joe/service/decompiler/ProcyonDecompiler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818

1919
import cn.enaium.joe.JavaOctetEditor;
2020
import cn.enaium.joe.config.extend.ProcyonConfig;
21-
import cn.enaium.joe.util.ReflectUtil;
2221
import cn.enaium.joe.util.classes.ClassNode;
22+
import cn.enaium.joe.util.reflection.FieldAccessor;
23+
import cn.enaium.joe.util.reflection.ReflectionHelper;
2324
import com.strobel.assembler.InputTypeLoader;
2425
import com.strobel.assembler.metadata.Buffer;
2526
import com.strobel.assembler.metadata.ITypeLoader;
@@ -31,7 +32,6 @@
3132
import org.pmw.tinylog.Logger;
3233

3334
import java.io.StringWriter;
34-
import java.lang.reflect.Field;
3535

3636
/**
3737
* @author Enaium
@@ -46,14 +46,14 @@ public static void create() {
4646
JavaOctetEditor.getInstance().config.getConfigMap(ProcyonConfig.class).forEach(
4747
(s, value) -> {
4848
try {
49-
Field f = ReflectUtil.getField(aDefault.getClass(), value.getName());
50-
Object defaultValue = ReflectUtil.getFieldValue(aDefault, value.getName());
49+
FieldAccessor<Object, JavaFormattingOptions> f = ReflectionHelper.getFieldAccessor(JavaFormattingOptions.class, value.getName());
50+
Object defaultValue = f.get(aDefault);
5151
if (defaultValue instanceof Enum<?>) {
5252
f.set(aDefault, Enum.valueOf(((Enum<?>) defaultValue).getDeclaringClass(), (String) value.getValue()));
5353
} else {
5454
f.set(aDefault, value.getValue());
5555
}
56-
} catch (NoSuchFieldException | IllegalAccessException e) {
56+
} catch (Throwable e) {
5757
Logger.error(e);
5858
}
5959
}

src/main/java/cn/enaium/joe/util/ASMUtil.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
* @since 1.2.0
3131
*/
3232
public class ASMUtil {
33+
34+
public static String resolvePkgName(String className){
35+
if (className.indexOf('.') > 0){
36+
return className.substring(0, className.lastIndexOf('.') -1);
37+
} else return className.substring(0, className.lastIndexOf('/') -1);
38+
}
39+
3340
/**
3441
* converts a string value to a value of that type
3542
*

src/main/java/cn/enaium/joe/util/ReflectUtil.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/main/java/cn/enaium/joe/util/reflection/ReflectionHelper.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cn.enaium.joe.util.reflection;
22

3+
import cn.enaium.joe.Main;
4+
35
import java.io.Serial;
46
import java.lang.reflect.Constructor;
57
import java.lang.reflect.Field;
@@ -235,4 +237,31 @@ public static<T> ConstructorAccessor<T> getConstructorAccessor(Class<?> clazz, C
235237
Constructor<T> constructor = (Constructor<T>) findConstructor(clazz, args);
236238
return new ConstructorAccessor<>(constructor);
237239
}
240+
241+
public static boolean isClassExist(String canonicalName){
242+
try {
243+
Class.forName(canonicalName, false, Main.classLoader);
244+
return true;
245+
} catch (ClassNotFoundException e) {
246+
return false;
247+
}
248+
}
249+
250+
/**
251+
* copy all class member from the old class object to the new class object
252+
*
253+
* @param oldObject old object
254+
* @param newObject new object
255+
* @param <T> type of two classes
256+
* @throws NoSuchFieldException e
257+
* @throws IllegalAccessException e
258+
*/
259+
@Deprecated
260+
public static <T> void copyAllMember(T oldObject, T newObject) throws NoSuchFieldException, IllegalAccessException {
261+
for (Field oldField : oldObject.getClass().getDeclaredFields()) {
262+
oldField.setAccessible(true);
263+
Field declaredField = newObject.getClass().getDeclaredField(oldField.getName());
264+
oldField.set(oldObject, declaredField.get(newObject));
265+
}
266+
}
238267
}

0 commit comments

Comments
 (0)