Skip to content
This repository was archived by the owner on Jan 2, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# JavaOctetEditor

![](https://user-images.githubusercontent.com/32991121/186328031-4bf5cddd-3926-4a8b-b675-10da8839b016.png)
![](https://user-images.githubusercontent.com/32991121/186328036-7ab4de70-592b-4b9a-a48b-48c847f178c0.png)
![Snipaste_2022-08-30_09-02-03](https://user-images.githubusercontent.com/32991121/187325154-249f6d48-a22e-4472-951a-bb4f69301802.png)
![Snipaste_2022-08-30_09-02-22](https://user-images.githubusercontent.com/32991121/187325158-37d291fa-daeb-445d-9418-472254a32ea6.png)
14 changes: 10 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.text.SimpleDateFormat
import org.gradle.internal.jvm.Jvm

plugins {
id 'java'
Expand All @@ -7,7 +8,7 @@ plugins {
}

group 'cn.enaium'
version '1.0.1'
version '1.1.0'

sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8

Expand All @@ -18,7 +19,11 @@ jar {
"Implementation-Title": "${project.name}",
"Implementation-Version": "${project.version}",
"Implementation-Vendor": "${project.group}",
"Implementation-Timestamp": new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
"Implementation-Timestamp": new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),
"Premain-Class": "cn.enaium.joe.Agent",
"Agent-Class": "cn.enaium.joe.Agent",
"Can-Redefine-Classes": true,
"Can-Retransform-Classes": true
)
}

Expand All @@ -45,9 +50,10 @@ dependencies {
implementation 'org.quiltmc:quiltflower:1.8.1'
implementation 'org.javassist:javassist:3.29.0-GA'
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'org.tinylog:tinylog-api:2.5.0'
implementation 'org.tinylog:tinylog-impl:2.5.0'
implementation 'org.tinylog:tinylog:1.3.6'
implementation 'com.github.FabricMC:mapping-io:597f0722d6'

compileOnly files(Jvm.current().getToolsJar())
}

test {
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/cn/enaium/joe/Agent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2022 Enaium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.enaium.joe;

import cn.enaium.joe.jar.Jar;
import cn.enaium.joe.util.ReflectUtil;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;

import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

/**
* @author Enaium
* @since 1.1.0
*/
public class Agent {
public static void premain(String agentArgs, Instrumentation inst) throws Exception {
agent(agentArgs, inst);
}

public static void agentmain(String agentArgs, Instrumentation inst) throws Exception {
agent(agentArgs, inst);
}

private static void agent(String agentArgs, Instrumentation inst) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
URLClassLoader loader = new URLClassLoader(new URL[]{Agent.class.getProtectionDomain().getCodeSource().getLocation()}, ClassLoader.getSystemClassLoader().getParent());
Class<?> main = loader.loadClass("cn.enaium.joe.Main");
Method agent = ReflectUtil.getMethod(main, "agent", Instrumentation.class);
agent.invoke(null, inst);
}
}
10 changes: 10 additions & 0 deletions src/main/java/cn/enaium/joe/JavaOctetEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
import cn.enaium.joe.task.TaskManager;
import cn.enaium.joe.util.LangUtil;
import cn.enaium.joe.util.MessageUtil;
import cn.enaium.joe.util.ReflectUtil;
import com.sun.tools.attach.VirtualMachine;
import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
import org.pmw.tinylog.Logger;

import javax.swing.*;
import java.awt.*;
Expand Down Expand Up @@ -77,6 +80,13 @@ public void run() {
window.setJMenuBar(new JMenuBar() {{
add(new FileMenu());
add(new SearchMenu());

AttachMenu attachMenu = new AttachMenu() {{
if (!ReflectUtil.classHas("com.sun.tools.attach.VirtualMachine")) {
setEnabled(false);
}
}};
add(attachMenu);
add(new MappingMenu());
add(new ConfigMenu());
add(new HelpMenu());
Expand Down
71 changes: 70 additions & 1 deletion src/main/java/cn/enaium/joe/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,85 @@

package cn.enaium.joe;

import cn.enaium.joe.jar.Jar;
import cn.enaium.joe.util.IOUtil;
import cn.enaium.joe.util.MessageUtil;
import cn.enaium.joe.util.ReflectUtil;
import com.formdev.flatlaf.FlatDarkLaf;
import org.tinylog.Logger;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;
import org.pmw.tinylog.Configurator;
import org.pmw.tinylog.Logger;
import org.pmw.tinylog.writers.ConsoleWriter;
import org.pmw.tinylog.writers.FileWriter;

import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

/**
* @author Enaium
*/
public final class Main {
public static void main(String[] args) {
loadTools();
launch();
}


private static void agent(Instrumentation inst) throws IOException {
launch();
Jar jar = new Jar();
for (Class<?> allLoadedClass : inst.getAllLoadedClasses()) {
String name = allLoadedClass.getName().replace('.', '/');
if (name.contains("$$") || name.contains("[") || !inst.isModifiableClass(allLoadedClass)
|| allLoadedClass.getClassLoader() == Main.class.getClassLoader()
|| name.matches("(java|sun|javax|com.sun|jdk|javafx).+")) {
continue;
}

ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(IOUtil.getBytes(Objects.requireNonNull(ClassLoader.getSystemClassLoader().getResourceAsStream(name + ".class"))));
classReader.accept(classNode, 0);
jar.classes.put(allLoadedClass.getName(), classNode);
}
JavaOctetEditor.getInstance().setJar(jar);
}

private static void launch() {
Configurator.currentConfig().writer(new ConsoleWriter(), "[{date: HH:mm:ss.SSS}] {level} > {message}").addWriter(new FileWriter("latest.log"), "[{date: HH:mm:ss.SSS}] {level} > {message}").activate();

Logger.info("DIR:{}", System.getProperty("user.dir"));
FlatDarkLaf.setup();
new JavaOctetEditor().run();
}

private static void loadTools() {
if (!ReflectUtil.classHas("com.sun.tools.attach.VirtualMachine")) {
Path toolsPath = Paths.get("lib", "tools.jar");
Path jrePath = Paths.get(System.getProperty("java.home"));
Path tool = jrePath.getParent().resolve(toolsPath);
if (Files.notExists(tool)) {
Logger.warn("Please use jdk to run");
return;
}
try {
Method addURL = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
addURL.setAccessible(true);
addURL.invoke(Main.class.getClassLoader(), tool.toUri().toURL());
} catch (NoSuchMethodException | MalformedURLException | InvocationTargetException |
IllegalAccessException e) {
MessageUtil.error(e);
}
}
}
}
57 changes: 37 additions & 20 deletions src/main/java/cn/enaium/joe/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import cn.enaium.joe.config.extend.ApplicationConfig;
import cn.enaium.joe.config.extend.CFRConfig;
import cn.enaium.joe.config.value.Value;
import cn.enaium.joe.config.extend.FernFlowerConfig;
import cn.enaium.joe.config.extend.ProcyonConfig;
import cn.enaium.joe.config.value.*;
import cn.enaium.joe.util.MessageUtil;
import cn.enaium.joe.util.ReflectUtil;
import com.google.gson.*;
Expand All @@ -29,19 +31,20 @@
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

/**
* @author Enaium
* @since 0.7.0
*/
public class ConfigManager {
private final Map<Class<? extends Config>, Config> configMap = new HashMap<>();
private final Map<Class<? extends Config>, Config> configMap = new LinkedHashMap<>();

public ConfigManager() {
setByClass(new ApplicationConfig());
setByClass(new CFRConfig());
setByClass(new FernFlowerConfig());
setByClass(new ProcyonConfig());
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -70,7 +73,7 @@ public Map<String, String> getConfigMap(Class<? extends Config> config) {
if (o instanceof Value<?>) {
Object value = ((Value<?>) o).getValue();
if (value != null) {
map.put(((Value<?>) o).getName(), value.toString());
map.put(declaredField.getName(), value.toString());
}
}
} catch (IllegalAccessException e) {
Expand All @@ -92,34 +95,48 @@ public void load() {
try {
File file = new File(System.getProperty("."), config.getName() + ".json");
if (file.exists()) {
//Step.1 get json object
JsonObject jsonObject = gson().fromJson(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8), JsonObject.class);

//Step.2 get all field of the config
for (Field configField : klass.getDeclaredFields()) {
configField.setAccessible(true);
if (!jsonObject.has(configField.getName())) {
continue;
}

//Step.3 deserialize
Object setting = gson().fromJson(jsonObject.get(configField.getName()).toString(), configField.getType());
if (!jsonObject.has(configField.getName())) {
continue;
}

//Step.3.1 get all field of the setting
for (Field settingField : setting.getClass().getDeclaredFields()) {
settingField.setAccessible(true);
if (!jsonObject.get(configField.getName()).getAsJsonObject().has("value")) {
continue;
}

if (settingField.isAnnotationPresent(Expose.class)) {
if (!settingField.getAnnotation(Expose.class).deserialize()) {
Field declaredField = ReflectUtil.getField(setting.getClass(), settingField.getName());
//Step.3.2 use the value from config to set the value of setting
declaredField.set(setting, ReflectUtil.getFieldValue(ReflectUtil.getFieldValue(config, configField.getName()), settingField.getName()));
JsonElement valueJsonElement = jsonObject.get(configField.getName()).getAsJsonObject().get("value");

Object valueObject = configField.get(config);
if (valueObject instanceof Value<?>) {
Value<?> value = (Value<?>) valueObject;
if (value instanceof EnableValue) {
((EnableValue) value).setValue(valueJsonElement.getAsBoolean());
} else if (value instanceof IntegerValue) {
((IntegerValue) value).setValue(valueJsonElement.getAsInt());
} else if (value instanceof ModeValue) {
ModeValue modeValue = (ModeValue) value;
if (modeValue.getMode().contains(valueJsonElement.getAsString())) {
modeValue.setValue(valueJsonElement.getAsString());
} else {
modeValue.setValue(modeValue.getMode().get(0));
}
} else if (value instanceof StringSetValue) {
Set<String> strings = new HashSet<>();
for (JsonElement jsonElement : valueJsonElement.getAsJsonArray()) {
strings.add(jsonElement.getAsString());
}
((StringSetValue) value).setValue(strings);
} else if (value instanceof StringValue) {
((StringValue) value).setValue(valueJsonElement.getAsString());
}
}

//Step.4 use the value from step 3 to set the value of config
configField.set(config, setting);
}
}
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
public class ApplicationConfig extends Config {
public ModeValue decompilerMode = new ModeValue("Decompiler", "CFR", "Java Decompiler", Arrays.asList("CFR", "Procyon", "FernFlower"));
public ModeValue language = new ModeValue("Language", "System", "UI language", Arrays.asList("System", "zh_CN", "en_US"));
public ModeValue packagePresentation = new ModeValue("Package Presentation", "Hierarchical", "Package Mode", Arrays.asList("Flat", "Hierarchical"));
@NoUI
public final StringSetValue loadRecent = new StringSetValue("Load Recent", new HashSet<>(), "");

Expand Down
Loading