Skip to content

Commit 7465f4a

Browse files
committed
fast sync for config value
fix the switch for file tree type fix the optional recompiler environment
1 parent c50e834 commit 7465f4a

File tree

15 files changed

+164
-115
lines changed

15 files changed

+164
-115
lines changed

src/main/java/cn/enaium/joe/config/ConfigManager.java

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import cn.enaium.joe.util.MessageUtil;
2222
import com.google.gson.*;
2323

24-
import javax.swing.*;
2524
import java.io.File;
2625
import java.io.IOException;
2726
import java.lang.reflect.Field;
@@ -70,12 +69,20 @@ public Map<Class<? extends Config>, Config> getConfig() {
7069
return configMap;
7170
}
7271

72+
public Map<String, String> getConfigMapStrings(Config config) {
73+
return getConfigMapStrings(config.getClass());
74+
}
75+
7376
public Map<String, String> getConfigMapStrings(Class<? extends Config> config) {
7477
return getConfigMap(config).entrySet().stream()
7578
.filter(entry -> entry.getValue().getValue() != null)
7679
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getValue().toString()));
7780
}
7881

82+
public Map<String, Value<?>> getConfigMap(Config config) {
83+
return this.getConfigMap(config.getClass());
84+
}
85+
7986
public Map<String, Value<?>> getConfigMap(Class<? extends Config> config) {
8087
Map<String, Value<?>> map = new HashMap<>();
8188
for (Field declaredField : config.getDeclaredFields()) {
@@ -93,6 +100,7 @@ public Map<String, Value<?>> getConfigMap(Class<? extends Config> config) {
93100
}
94101

95102
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
103+
96104
private Gson gson() {
97105
return GSON;
98106
}
@@ -105,33 +113,37 @@ public void load() {
105113
try {
106114
File file = new File(System.getProperty("."), config.getName() + ".json");
107115
if (file.exists()) {
108-
JsonObject jsonObject = gson().fromJson(Files.readString(file.toPath()), JsonObject.class);
109-
for (Field configField : klass.getDeclaredFields()) {
110-
configField.setAccessible(true);
111-
if (!jsonObject.has(configField.getName())) {
112-
continue;
113-
}
114-
115-
if (!jsonObject.get(configField.getName()).isJsonObject()) {
116-
continue;
117-
}
118-
119-
if (!jsonObject.get(configField.getName()).getAsJsonObject().has("value")) {
120-
continue;
121-
}
122-
123-
JsonElement valueJsonElement = jsonObject.get(configField.getName()).getAsJsonObject().get("value");
124-
125-
Object valueObject = configField.get(config);
126-
if (valueObject instanceof Value<?>) {
127-
Value<?> value = (Value<?>) valueObject;
128-
value.decode(valueJsonElement);
116+
if (JsonParser.parseString(Files.readString(file.toPath())) instanceof JsonObject jsonObject) {
117+
for (Field configField : klass.getDeclaredFields()) {
118+
configField.setAccessible(true);
119+
if (!jsonObject.has(configField.getName())) {
120+
continue;
121+
}
122+
123+
if (!jsonObject.get(configField.getName()).isJsonObject()) {
124+
continue;
125+
}
126+
127+
if (!jsonObject.get(configField.getName()).getAsJsonObject().has("value")) {
128+
continue;
129+
}
130+
131+
JsonElement valueJsonElement = jsonObject.get(configField.getName()).getAsJsonObject().get("value");
132+
133+
Object valueObject = configField.get(config);
134+
if (valueObject instanceof Value<?>) {
135+
Value<?> value = (Value<?>) valueObject;
136+
value.decode(valueJsonElement);
137+
}
129138
}
139+
} else {
140+
MessageUtil.error("Could not read the config '" + classConfigEntry.getValue().getName() + "'");
130141
}
131142
}
132143
} catch (Throwable e) {
133-
MessageUtil.error(e);
144+
MessageUtil.error("Could not read the config '" + classConfigEntry.getValue().getName() + "'", e);
134145
}
146+
classConfigEntry.getValue().update();
135147
}
136148
}
137149

src/main/java/cn/enaium/joe/config/extend/ApplicationConfig.java

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

1717
package cn.enaium.joe.config.extend;
1818

19+
import cn.enaium.joe.JavaOctetEditor;
1920
import cn.enaium.joe.annotation.NoUI;
2021
import cn.enaium.joe.config.Config;
21-
import cn.enaium.joe.config.value.EnableValue;
22-
import cn.enaium.joe.config.value.IntegerValue;
23-
import cn.enaium.joe.config.value.ModeValue;
24-
import cn.enaium.joe.config.value.StringSetValue;
22+
import cn.enaium.joe.config.value.*;
23+
import cn.enaium.joe.util.LangUtil;
24+
import cn.enaium.joe.util.compiler.environment.RecompileEnvironment;
2525

2626
import java.util.Arrays;
2727
import java.util.HashSet;
@@ -42,5 +42,10 @@ public class ApplicationConfig extends Config {
4242
public EnableValue makeDemoRecompileEnvironment = new EnableValue("makeDemoRecompileEnvironment", false, "[EXP] Make the Demo Recompile Symbol environment.");
4343
public ApplicationConfig() {
4444
super("Application");
45+
this.language.addListener(LangUtil.locales);
46+
this.packagePresentation.addListener((instance, oldValue, newValue) -> {
47+
if (!oldValue.equals(newValue) && JavaOctetEditor.getInstance().getJar() != null) JavaOctetEditor.getInstance().fileTree.refresh(JavaOctetEditor.getInstance().getJar());
48+
});
49+
this.makeDemoRecompileEnvironment.addListener(RecompileEnvironment.environment);
4550
}
4651
}

src/main/java/cn/enaium/joe/config/extend/CFRConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,6 @@ public class CFRConfig extends Config {
326326
"Propagate bytecode location info.");
327327

328328
public CFRConfig() {
329-
super("CFR", Set.of((value) -> CFRDecompiler.update()));
329+
super("CFR", Set.of(CFRDecompiler.options));
330330
}
331331
}

src/main/java/cn/enaium/joe/config/extend/FernFlowerConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ public class FernFlowerConfig extends Config {
8888

8989

9090
public FernFlowerConfig() {
91-
super("FernFlower", Set.of((config)-> FernFlowerDecompiler.updateCustomProperties()));
91+
super("FernFlower", Set.of(FernFlowerDecompiler.customProperties));
9292
}
9393
}

src/main/java/cn/enaium/joe/config/extend/ProcyonConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,6 @@ public class ProcyonConfig extends Config {
155155
public ModeValue ArrayInitializerBraceStyle = new ModeValue("ArrayInitializerBraceStyle", "EndOfLine", "ArrayInitializerBraceStyle", Arrays.asList("DoNotChange", "EndOfLine", "EndOfLineWithoutSpace", "NextLine", "NextLineShifted", "NextLineShifted2", "BannerStyle"));
156156

157157
public ProcyonConfig() {
158-
super("Procyon", Set.of((config)-> ProcyonDecompiler.create()));
158+
super("Procyon", Set.of(ProcyonDecompiler.cachedFormattingOptions));
159159
}
160160
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.enaium.joe.config.util;
2+
3+
import cn.enaium.joe.config.value.ConfigValueListener;
4+
import cn.enaium.joe.config.value.Value;
5+
6+
import java.util.function.BiFunction;
7+
8+
public class CachedConfigValue<T, E> implements ConfigValueListener<E> {
9+
10+
protected T value;
11+
protected BiFunction<E, E, T> processor;
12+
13+
public CachedConfigValue(BiFunction<E, E, T> processor){
14+
this.processor = processor;
15+
this.value = null;
16+
}
17+
18+
@Override
19+
public void update(Value<E> instance, E oldValue, E newValue) {
20+
this.value = this.processor.apply(oldValue, newValue);
21+
}
22+
23+
public T getValue() {
24+
return value;
25+
}
26+
27+
public void setValue(T value){
28+
this.value = value;
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.enaium.joe.config.util;
2+
3+
import cn.enaium.joe.config.Config;
4+
5+
import java.util.function.Consumer;
6+
import java.util.function.Function;
7+
8+
public class CachedGlobalValue<T> implements Consumer<Config> {
9+
protected T value;
10+
protected Function<Config, T> processor;
11+
12+
public CachedGlobalValue(Function<Config, T> processor){
13+
this.processor = processor;
14+
this.value = null;
15+
}
16+
17+
@Override
18+
public void accept(Config config) {
19+
this.value = this.processor.apply(config);
20+
}
21+
22+
public T getValue() {
23+
return value;
24+
}
25+
26+
public void setValue(T value){
27+
this.value = value;
28+
}
29+
}

src/main/java/cn/enaium/joe/config/value/Value.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public abstract class Value<T> {
3636
public Value(Type type, String name, T value, String description) {
3737
this.type = type;
3838
this.name = name;
39-
this.value = value;
39+
this.setValue(value);
4040
this.description = description;
4141
}
4242

src/main/java/cn/enaium/joe/service/DecompileService.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
public class DecompileService {
2929
public static IDecompiler getService() {
3030
ModeValue decompilerMode = JavaOctetEditor.getInstance().config.getByClass(ApplicationConfig.class).decompilerMode;
31-
switch (decompilerMode.getValue()) {
32-
case "CFR": return new CFRDecompiler();
33-
case "Procyon": return new ProcyonDecompiler();
34-
case "FernFlower": return new FernFlowerDecompiler();
35-
}
36-
throw new NullPointerException("Not found decompiler");
31+
return switch (decompilerMode.getValue()) {
32+
case "CFR" -> new CFRDecompiler();
33+
case "Procyon" -> new ProcyonDecompiler();
34+
case "FernFlower" -> new FernFlowerDecompiler();
35+
default -> throw new NullPointerException("Not found decompiler");
36+
};
3737
}
3838
}

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

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

1919
import cn.enaium.joe.JavaOctetEditor;
20-
import cn.enaium.joe.config.extend.CFRConfig;
20+
import cn.enaium.joe.config.util.CachedGlobalValue;
2121
import cn.enaium.joe.util.MessageUtil;
2222
import cn.enaium.joe.util.classes.ClassNode;
2323
import org.benf.cfr.reader.Main;
@@ -40,15 +40,12 @@
4040
* @since 0.7.0
4141
*/
4242
public class CFRDecompiler implements IDecompiler {
43-
public static Options options;
4443

45-
public static void update(){
46-
options = OptionsImpl.getFactory().create(JavaOctetEditor.getInstance().config.getConfigMapStrings(CFRConfig.class));
47-
}
44+
public static final CachedGlobalValue<Options> options = new CachedGlobalValue<>(config -> OptionsImpl.getFactory().create(JavaOctetEditor.getInstance().config.getConfigMapStrings(config)));
4845

4946
@Override
5047
public String decompile(final ClassNode classNode) {
51-
DCCommonState state = new DCCommonState(options, new ClassFileSource2() {
48+
DCCommonState state = new DCCommonState(options.getValue(), new ClassFileSource2() {
5249
@Override
5350
public Pair<byte[], String> getClassFileContent(String path) {
5451
String name = path.substring(0, path.length() - 6);
@@ -90,13 +87,13 @@ public PluginDumperFactory() {
9087
}
9188

9289
public Dumper getNewTopLevelDumper(JavaTypeInstance classType, SummaryDumper summaryDumper, TypeUsageInformation typeUsageInformation, IllegalIdentifierDump illegalIdentifierDump) {
93-
return new StringStreamDumper(new MethodErrorCollector.SummaryDumperMethodErrorCollector(classType, summaryDumper), this.outBuffer, typeUsageInformation, CFRDecompiler.options, this.illegalIdentifierDump);
90+
return new StringStreamDumper(new MethodErrorCollector.SummaryDumperMethodErrorCollector(classType, summaryDumper), this.outBuffer, typeUsageInformation, CFRDecompiler.options.getValue(), this.illegalIdentifierDump);
9491
}
9592
public Dumper wrapLineNoDumper(Dumper dumper) {
9693
return dumper;
9794
}
9895
public SummaryDumper getSummaryDumper() {
99-
return !CFRDecompiler.options.optionIsSet(OptionsImpl.OUTPUT_DIR) ? new NopSummaryDumper() : new FileSummaryDumper(CFRDecompiler.options.getOption(OptionsImpl.OUTPUT_DIR), CFRDecompiler.options, null);
96+
return !CFRDecompiler.options.getValue().optionIsSet(OptionsImpl.OUTPUT_DIR) ? new NopSummaryDumper() : new FileSummaryDumper(CFRDecompiler.options.getValue().getOption(OptionsImpl.OUTPUT_DIR), CFRDecompiler.options.getValue(), null);
10097
}
10198
public ProgressDumper getProgressDumper() {
10299
return ProgressDumperNop.INSTANCE;
@@ -114,7 +111,4 @@ public String getResult(){
114111
}
115112
}
116113

117-
static {
118-
update();
119-
}
120114
}

0 commit comments

Comments
 (0)