1
1
package xyz .wagyourtail .unimined .jarmodagent .transformer ;
2
2
3
3
import net .lenni0451 .classtransform .TransformerManager ;
4
+ import net .lenni0451 .classtransform .mappings .annotation .AnnotationRemap ;
4
5
import net .lenni0451 .classtransform .utils .ASMUtils ;
5
6
import net .lenni0451 .classtransform .utils .tree .IClassProvider ;
6
7
import org .objectweb .asm .*;
9
10
10
11
import java .io .File ;
11
12
import java .io .IOException ;
13
+ import java .lang .reflect .Method ;
12
14
import java .nio .file .Files ;
13
15
import java .util .HashMap ;
16
+ import java .util .HashSet ;
14
17
import java .util .Map ;
15
18
import java .util .Set ;
16
19
@@ -63,7 +66,7 @@ protected AnnotationStringRemappingClassVisitor(int api, ClassVisitor classVisit
63
66
@ Override
64
67
public AnnotationVisitor visitAnnotation (String descriptor , boolean visible ) {
65
68
AnnotationVisitor av = super .visitAnnotation (descriptor , visible );
66
- return av == null ? null : new RemappingAnnotationVisitor (api , av , refmap );
69
+ return av == null ? null : new RemappingAnnotationVisitor (api , av , refmap , descriptor , false );
67
70
}
68
71
69
72
@ Override
@@ -73,7 +76,7 @@ public FieldVisitor visitField(int access, String name, String descriptor, Strin
73
76
@ Override
74
77
public AnnotationVisitor visitAnnotation (String descriptor , boolean visible ) {
75
78
AnnotationVisitor av = super .visitAnnotation (descriptor , visible );
76
- return av == null ? null : new RemappingAnnotationVisitor (api , av , refmap );
79
+ return av == null ? null : new RemappingAnnotationVisitor (api , av , refmap , descriptor , false );
77
80
}
78
81
};
79
82
}
@@ -85,30 +88,76 @@ public MethodVisitor visitMethod(int access, String name, String descriptor, Str
85
88
@ Override
86
89
public AnnotationVisitor visitAnnotation (String descriptor , boolean visible ) {
87
90
AnnotationVisitor av = super .visitAnnotation (descriptor , visible );
88
- return av == null ? null : new RemappingAnnotationVisitor (api , av , refmap );
91
+ return av == null ? null : new RemappingAnnotationVisitor (api , av , refmap , descriptor , false );
89
92
}
90
93
91
94
@ Override
92
95
public AnnotationVisitor visitParameterAnnotation (int parameter , String descriptor , boolean visible ) {
93
96
AnnotationVisitor av = super .visitParameterAnnotation (parameter , descriptor , visible );
94
- return av == null ? null : new RemappingAnnotationVisitor (api , av , refmap );
97
+ return av == null ? null : new RemappingAnnotationVisitor (api , av , refmap , descriptor , false );
95
98
}
96
99
};
97
100
}
98
101
}
99
102
100
103
public static class RemappingAnnotationVisitor extends AnnotationVisitor {
104
+ private static final Set <String > failed = new HashSet <>();
105
+
101
106
Map <String , String > refmap ;
107
+ boolean allowNullNameRemap ;
108
+ Class <?> annotationType ;
109
+
102
110
103
- protected RemappingAnnotationVisitor (int api , AnnotationVisitor annotationVisitor , Map <String , String > refmap ) {
111
+ protected RemappingAnnotationVisitor (int api , AnnotationVisitor annotationVisitor , Map <String , String > refmap , String descriptor , boolean allowNullNameRemap ) {
104
112
super (api , annotationVisitor );
105
113
this .refmap = refmap ;
114
+ this .allowNullNameRemap = allowNullNameRemap ;
115
+ try {
116
+ if (descriptor != null ) {
117
+ if (descriptor .startsWith ("L" )) descriptor = descriptor .substring (1 );
118
+ if (descriptor .endsWith (";" )) descriptor = descriptor .substring (0 , descriptor .length () - 1 );
119
+ this .annotationType = Class .forName (descriptor .replace ('/' , '.' ));
120
+ }
121
+ } catch (ClassNotFoundException e ) {
122
+ if (failed .add (descriptor )) {
123
+ System .err .println ("[JarModAgent] Failed to find annotation class " + descriptor );
124
+ }
125
+ }
126
+ JarModder .debug ("remapping annotation " + descriptor );
127
+ }
128
+
129
+ public boolean testName (String name ) {
130
+ if (name == null ) {
131
+ if (allowNullNameRemap ) {
132
+ return true ;
133
+ } else {
134
+ name = "value" ;
135
+ }
136
+ }
137
+ if (annotationType != null ) {
138
+ try {
139
+ Method m = annotationType .getDeclaredMethod (name );
140
+ if (m .getAnnotation (AnnotationRemap .class ) == null ) {
141
+ JarModder .debug ("not remapping " + name + " because it doesn't have @AnnotationRemap" );
142
+ return false ;
143
+ }
144
+ } catch (NoSuchMethodException e ) {
145
+ throw new RuntimeException (e );
146
+ }
147
+ } else {
148
+ return false ;
149
+ }
150
+ return true ;
106
151
}
107
152
108
153
@ Override
109
154
public void visit (String name , Object value ) {
155
+ if (!testName (name )) {
156
+ super .visit (name , value );
157
+ return ;
158
+ }
110
159
if (value instanceof String ) {
111
- System . out . println ("remapping \" " + name + "\" \" " + value + "\" to \" " + refmap .getOrDefault (value , (String ) value ) + "\" " );
160
+ JarModder . debug ("remapping \" " + name + "\" \" " + value + "\" to \" " + refmap .getOrDefault (value , (String ) value ) + "\" " );
112
161
super .visit (name , refmap .getOrDefault (value , (String ) value ));
113
162
} else {
114
163
super .visit (name , value );
@@ -118,13 +167,13 @@ public void visit(String name, Object value) {
118
167
@ Override
119
168
public AnnotationVisitor visitAnnotation (String name , String descriptor ) {
120
169
AnnotationVisitor av = super .visitAnnotation (name , descriptor );
121
- return av == null ? null : new RemappingAnnotationVisitor (api , av , refmap );
170
+ return av == null ? null : new RemappingAnnotationVisitor (api , av , refmap , descriptor , false );
122
171
}
123
172
124
173
@ Override
125
174
public AnnotationVisitor visitArray (String name ) {
126
175
AnnotationVisitor av = super .visitArray (name );
127
- return av == null ? null : new RemappingAnnotationVisitor (api , av , refmap );
176
+ return av == null ? null : new RemappingAnnotationVisitor (api , av , refmap , null , true );
128
177
}
129
178
}
130
179
}
0 commit comments