Skip to content

Commit e7ff60f

Browse files
committed
Caching > Reflection
1 parent 474fb9b commit e7ff60f

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

Diff for: build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ sourceCompatibility = 1.8
2121
targetCompatibility = 1.8
2222

2323
archivesBaseName = 'Crafty-Crashes'
24-
version = '0.3'
24+
version = '0.4'
2525

2626
repositories {
2727
maven {

Diff for: src/com/chocohead/cc/SMAPper.java

+6-33
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
11
package com.chocohead.cc;
22

3-
import java.lang.reflect.Field;
43
import java.util.HashMap;
54
import java.util.Map;
6-
import java.util.Set;
75

8-
import com.google.common.collect.Iterables;
9-
10-
import org.apache.commons.lang3.reflect.FieldUtils;
11-
12-
import org.spongepowered.asm.mixin.Mixins;
136
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
14-
import org.spongepowered.asm.mixin.transformer.ClassInfo;
157

168
import com.chocohead.cc.smap.FileInfo;
179
import com.chocohead.cc.smap.LineInfo;
1810
import com.chocohead.cc.smap.SMAP;
1911

2012
public class SMAPper {
21-
private static final Field MIXINS = FieldUtils.getDeclaredField(ClassInfo.class, "mixins", true);
22-
23-
private static boolean hasMixins(ClassInfo type) {
24-
try {
25-
return !((Set<?>) MIXINS.get(type)).isEmpty();
26-
} catch (ReflectiveOperationException e) {
27-
throw new RuntimeException("Failed to find mixins for " + type.getName(), e);
28-
}
29-
}
30-
3113
public static void apply(Throwable t, String... skippedPackages) {
3214
apply(t, new HashMap<String, SMAP>(), skippedPackages);
3315
}
@@ -68,14 +50,10 @@ private static boolean apply(StackTraceElement[] elements, Map<String, SMAP> cac
6850

6951
smap = null;
7052
if (!skip) {
71-
ClassInfo info = ClassInfo.fromCache(className);
72-
73-
if (info != null && hasMixins(info)) {
74-
String source = SourcePool.get(className);
53+
String source = SourcePool.get(className);
7554

76-
if (source != null && source.startsWith("SMAP")) {
77-
smap = SMAP.forResolved(source);
78-
}
55+
if (source != null && source.startsWith("SMAP")) {
56+
smap = SMAP.forResolved(source);
7957
}
8058
}
8159

@@ -97,13 +75,8 @@ private static boolean apply(StackTraceElement[] elements, Map<String, SMAP> cac
9775
return modified;
9876
}
9977

100-
private static String findMixin(FileInfo info) {
101-
if (info.path != null && info.path.endsWith(".java")) {//This is very silly but also useful
102-
IMixinInfo mixin = Iterables.getOnlyElement(Mixins.getMixinsForClass(info.path.substring(0, info.path.length() - 5)));
103-
104-
if (mixin != null) return " [" + mixin.getConfig().getName() + ']';
105-
}
106-
107-
return "";
78+
private static String findMixin(FileInfo file) {
79+
IMixinInfo mixin = SourcePool.findFor(file);
80+
return mixin != null ? " [" + mixin.getConfig().getName() + ']' : "";
10881
}
10982
}

Diff for: src/com/chocohead/cc/SourcePool.java

+28
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package com.chocohead.cc;
22

33
import java.util.HashMap;
4+
import java.util.IdentityHashMap;
45
import java.util.Map;
56

7+
import com.google.common.collect.Iterables;
8+
9+
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
10+
import org.spongepowered.asm.mixin.transformer.ClassInfo;
11+
12+
import com.chocohead.cc.smap.FileInfo;
13+
614
public class SourcePool {
715
private static final Map<String, String> SOURCES = new HashMap<>();
16+
private static final Map<FileInfo, IMixinInfo> MIXINS = new IdentityHashMap<>();
817

918
static void add(String owner, String source) {
1019
if (SOURCES.put(owner, source) != null) {
@@ -15,4 +24,23 @@ static void add(String owner, String source) {
1524
public static String get(String owner) {
1625
return SOURCES.get(owner);
1726
}
27+
28+
public static IMixinInfo findFor(FileInfo file) {
29+
if (!MIXINS.containsKey(file)) {
30+
if (file.path != null && file.path.endsWith(".java")) {
31+
ClassInfo info = ClassInfo.fromCache(file.path.substring(0, file.path.length() - 5));
32+
33+
if (info != null && info.isMixin()) {//This is very silly but also useful
34+
IMixinInfo out = Iterables.getOnlyElement(info.getAppliedMixins());
35+
MIXINS.put(file, out);
36+
return out;
37+
}
38+
}
39+
40+
MIXINS.put(file, null);
41+
return null;
42+
} else {
43+
return MIXINS.get(file);
44+
}
45+
}
1846
}

0 commit comments

Comments
 (0)