[FIX] Фиксы InjectClassAcceptor

This commit is contained in:
Gravit 2020-01-24 09:57:04 +07:00
parent b4a75b9076
commit 667fb21e9b
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
3 changed files with 11 additions and 3 deletions

View file

@ -49,14 +49,15 @@ private static void visit(ClassNode cn, Map<String, Object> object) {
return ret;
});
List<MethodNode> constructors = cn.methods.stream().filter(e -> "<init>".equals(e.name)).collect(Collectors.toList());
MethodNode init = constructors.stream().filter(e -> e.invisibleAnnotations.stream().filter(f -> INJ_C_DESC.equals(f.desc)).findFirst()
.isPresent()).findFirst().orElseGet(() -> constructors.stream().filter(e -> e.desc.equals("()V")).findFirst().orElse(null));
MethodNode init = constructors.stream().filter(e -> e != null && e.invisibleAnnotations != null && e.invisibleAnnotations.stream().anyMatch(f -> INJ_C_DESC.equals(f.desc))).findFirst()
.orElseGet(() -> constructors.stream().filter(e -> e.desc.equals("()V")).findFirst().orElse(null));
cn.fields.stream().filter(e -> e.invisibleAnnotations != null)
.filter(e -> !e.invisibleAnnotations.isEmpty() && e.invisibleAnnotations.stream().anyMatch(f -> f.desc.equals(INJ_DESC))).forEach(e -> {
// Notice that fields that will be used with this algo should not have default
// value by = ...;
AnnotationNode n = e.invisibleAnnotations.stream().filter(f -> INJ_DESC.equals(f.desc)).findFirst()
.get();
e.invisibleAnnotations.removeIf(f -> INJ_DESC.equals(f.desc));
AtomicReference<String> valueName = new AtomicReference<>(null);
n.accept(new AnnotationVisitor(Opcodes.ASM7) {
@Override

View file

@ -12,6 +12,7 @@
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.asm.ClassMetadataReader;
import pro.gravit.launchserver.asm.ConfigGenerator;
import pro.gravit.launchserver.asm.InjectClassAcceptor;
import pro.gravit.launchserver.binary.BuildContext;
import pro.gravit.launchserver.binary.LauncherConfigurator;
import pro.gravit.utils.HookException;
@ -123,9 +124,13 @@ public void transform(ClassNode cn, String classname, BuildContext context) {
public IOHookSet<BuildContext> preBuildHook = new IOHookSet<>();
public IOHookSet<BuildContext> postBuildHook = new IOHookSet<>();
public Map<String, Object> properties = new HashMap<>();
public MainBuildTask(LaunchServer srv) {
server = srv;
reader = new ClassMetadataReader();
InjectClassAcceptor injectClassAcceptor = new InjectClassAcceptor(properties);
transformers.add(injectClassAcceptor);
}
@Override

View file

@ -8,6 +8,7 @@
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import pro.gravit.launcher.LauncherInject;
import pro.gravit.launcher.LauncherInjectionConstructor;
import pro.gravit.launchserver.asm.InjectClassAcceptor;
import pro.gravit.utils.PublicURLClassLoader;
import pro.gravit.utils.helper.JarHelper;
@ -48,7 +49,8 @@ void testASM() throws Exception
node.name = "ASMTestClass";
Map<String, Object> map = new HashMap<>();
map.put("testprop", 1234);
InjectClassAcceptor.visit(node, map);
InjectClassAcceptor injectClassAcceptor = new InjectClassAcceptor(map);
injectClassAcceptor.transform(node, "ASMTestClass", null);
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
node.accept(writer);
byte[] bytes = writer.toByteArray();