diff --git a/src/main/java/javgent/App.java b/src/main/java/javgent/App.java index 8156577..d8c143a 100644 --- a/src/main/java/javgent/App.java +++ b/src/main/java/javgent/App.java @@ -21,8 +21,6 @@ public static void main(String[] args) { System.exit(-1); }); - LOG.info("project and license information: https://github.com/BaseMC/javgent"); - try { Optional optionalExecutorConfig = Parser.parse(args); if (optionalExecutorConfig.isEmpty()) diff --git a/src/main/java/javgent/executor/bytecode/abstractdefault/AbstractClassBasedSignatureWriter.java b/src/main/java/javgent/executor/bytecode/abstractdefault/AbstractClassBasedSignatureWriter.java index 189e8a1..dd3e706 100644 --- a/src/main/java/javgent/executor/bytecode/abstractdefault/AbstractClassBasedSignatureWriter.java +++ b/src/main/java/javgent/executor/bytecode/abstractdefault/AbstractClassBasedSignatureWriter.java @@ -3,11 +3,11 @@ import javgent.executor.bytecode.clazz.CurrentClassController; import org.objectweb.asm.signature.SignatureWriter; -public class AbstractClassBasedSignatureWriter extends SignatureWriter { +public abstract class AbstractClassBasedSignatureWriter extends SignatureWriter { protected CurrentClassController controller; - public AbstractClassBasedSignatureWriter(CurrentClassController controller) { + protected AbstractClassBasedSignatureWriter(CurrentClassController controller) { this.controller = controller; } diff --git a/src/main/java/javgent/executor/bytecode/abstractdefault/AbstractDefaultSignatureWriter.java b/src/main/java/javgent/executor/bytecode/abstractdefault/AbstractDefaultSignatureWriter.java new file mode 100644 index 0000000..0285a58 --- /dev/null +++ b/src/main/java/javgent/executor/bytecode/abstractdefault/AbstractDefaultSignatureWriter.java @@ -0,0 +1,25 @@ +package javgent.executor.bytecode.abstractdefault; + +import javgent.executor.bytecode.clazz.CurrentClassController; +import javgent.executor.bytecode.clazz.util.InnerClassTypeResolver; + +public abstract class AbstractDefaultSignatureWriter extends AbstractClassBasedSignatureWriter { + + protected AbstractDefaultSignatureWriter(CurrentClassController controller) { + super(controller); + } + + @Override + public void visitInnerClassType(String name) { + var newName = InnerClassTypeResolver.resolve(controller, name); + + super.visitInnerClassType(newName); + } + + @Override + public void visitTypeVariable(String name) { + var newName = controller.findNameByObfNameOrReturn(name); + + super.visitTypeVariable(newName); + } +} diff --git a/src/main/java/javgent/executor/bytecode/abstractdefault/AbstractDescriptorWriter.java b/src/main/java/javgent/executor/bytecode/abstractdefault/AbstractDescriptorWriter.java new file mode 100644 index 0000000..0ecc7e1 --- /dev/null +++ b/src/main/java/javgent/executor/bytecode/abstractdefault/AbstractDescriptorWriter.java @@ -0,0 +1,41 @@ +package javgent.executor.bytecode.abstractdefault; + +import javgent.executor.bytecode.clazz.CurrentClassController; +import org.objectweb.asm.signature.SignatureWriter; +import org.slf4j.Logger; + +public abstract class AbstractDescriptorWriter extends SignatureWriter { + + protected CurrentClassController controller; + protected Logger log; + + protected AbstractDescriptorWriter(CurrentClassController controller, Logger log) { + this.controller = controller; + this.log = log; + } + + @Override + public void visitClassType(String name) { + var newName = controller.findNameByObfNameOrReturn(name); + + super.visitClassType(newName); + } + + @Override + public void visitFormalTypeParameter(String name) { + log.warn("Visiting not implemented method 'visitFormalTypeParameter'! name='{}'", name); + super.visitFormalTypeParameter(name); + } + + @Override + public void visitInnerClassType(String name) { + log.warn("Visiting not implemented method 'visitInnerClassType'! name='{}'", name); + super.visitInnerClassType(name); + } + + @Override + public void visitTypeVariable(String name) { + log.warn("Visiting not implemented method 'visitTypeVariable'! name='{}'", name); + super.visitTypeVariable(name); + } +} diff --git a/src/main/java/javgent/executor/bytecode/clazz/BCClassVisitor.java b/src/main/java/javgent/executor/bytecode/clazz/BCClassVisitor.java index f9e770c..8740951 100644 --- a/src/main/java/javgent/executor/bytecode/clazz/BCClassVisitor.java +++ b/src/main/java/javgent/executor/bytecode/clazz/BCClassVisitor.java @@ -8,6 +8,8 @@ import javgent.executor.bytecode.clazz.sub.method.ModifierForMethod; import javgent.executor.bytecode.clazz.sub.method.visitor.MethodSignatureWriter; import javgent.executor.bytecode.clazz.util.RemoteMethodResolver; +import javgent.executor.bytecode.clazz.visitor.RecordDescriptorWriter; +import javgent.executor.bytecode.clazz.visitor.RecordSignatureWriter; import javgent.executor.bytecode.clazz.writers.ClassDescriptorWriter; import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.Attribute; @@ -129,7 +131,7 @@ public void visitOuterClass(String owner, String name, String descriptor) { //name of a method (likely) var optNewController = controller.getRegistry().getByObfName(owner); - if (!optNewController.isPresent()) { + if (optNewController.isEmpty()) { Log.debug("Unable to get controller for owner='{}'", owner); super.visitOuterClass(owner, name, descriptor); return; @@ -142,7 +144,7 @@ public void visitOuterClass(String owner, String name, String descriptor) { name, descriptor); - if (!optResult.isPresent()) { + if (optResult.isEmpty()) { Log.debug("Unable to get result for owner='{}', name='{}'", owner, name); super.visitOuterClass(owner, name, descriptor); return; @@ -223,7 +225,15 @@ public void visitAttribute(Attribute attribute) { @Override public RecordComponentVisitor visitRecordComponent(String name, String descriptor, String signature) { - Log.warn("Visiting not implemented 'visitRecordComponent'! name='{},descriptor='{}'", name, descriptor); + if (!controllerPresent()) { + return super.visitRecordComponent(name, descriptor, signature); + } + + name = controller.getFieldsController().findNameByObfNameOrReturn(name); + descriptor = RecordDescriptorWriter.convert(controller, descriptor); + if (signature != null) + signature = RecordSignatureWriter.convert(controller, signature); + return super.visitRecordComponent(name, descriptor, signature); } } diff --git a/src/main/java/javgent/executor/bytecode/clazz/CurrentClassController.java b/src/main/java/javgent/executor/bytecode/clazz/CurrentClassController.java index a6365a1..b98e0fd 100644 --- a/src/main/java/javgent/executor/bytecode/clazz/CurrentClassController.java +++ b/src/main/java/javgent/executor/bytecode/clazz/CurrentClassController.java @@ -26,7 +26,7 @@ public PatchClass getCurrentPatchClass() { public String findNameByObfNameOrReturn(String obfName) { var opt = findPatchClassByObfName(obfName); - if (!opt.isPresent()) + if (opt.isEmpty()) return obfName; return opt.get().Name; } diff --git a/src/main/java/javgent/executor/bytecode/clazz/sub/method/BCMethodVisitor.java b/src/main/java/javgent/executor/bytecode/clazz/sub/method/BCMethodVisitor.java index f0a58c7..833c537 100644 --- a/src/main/java/javgent/executor/bytecode/clazz/sub/method/BCMethodVisitor.java +++ b/src/main/java/javgent/executor/bytecode/clazz/sub/method/BCMethodVisitor.java @@ -59,7 +59,8 @@ public void visitFieldInsn(int opcode, String owner, String name, String descrip if (optName.isPresent()) name = optName.get(); else - Log.info("Failed to find name: {}, {}, {} ", owner, name, descriptor); + Log.debug("Failed to find name: {} aka {}, {}, {} - Assuming that name needs no translation", + owner, methodFieldResolverResult.Owner, name, descriptor); owner = methodFieldResolverResult.Owner; } else @@ -208,6 +209,12 @@ public void visitAttribute(Attribute attribute) { @Override public void visitParameter(String name, int access) { + if(name == null) + { + super.visitParameter(null, access); + return; + } + Log.warn("Visiting not implemented 'visitParameter'! name='{}'", name); super.visitParameter(name, access); } diff --git a/src/main/java/javgent/executor/bytecode/clazz/sub/method/visitor/MethodDescriptorWriter.java b/src/main/java/javgent/executor/bytecode/clazz/sub/method/visitor/MethodDescriptorWriter.java index fcbe167..a91a998 100644 --- a/src/main/java/javgent/executor/bytecode/clazz/sub/method/visitor/MethodDescriptorWriter.java +++ b/src/main/java/javgent/executor/bytecode/clazz/sub/method/visitor/MethodDescriptorWriter.java @@ -1,44 +1,18 @@ package javgent.executor.bytecode.clazz.sub.method.visitor; +import javgent.executor.bytecode.abstractdefault.AbstractDescriptorWriter; import javgent.executor.bytecode.clazz.CurrentClassController; import org.objectweb.asm.signature.SignatureReader; import org.objectweb.asm.signature.SignatureWriter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class MethodDescriptorWriter extends SignatureWriter { +public class MethodDescriptorWriter extends AbstractDescriptorWriter { private static final Logger Log = LoggerFactory.getLogger(MethodDescriptorWriter.class); - private CurrentClassController controller; - public MethodDescriptorWriter(CurrentClassController controller) { - this.controller = controller; - } - - @Override - public void visitClassType(String name) { - var newName = controller.findNameByObfNameOrReturn(name); - - super.visitClassType(newName); - } - - @Override - public void visitFormalTypeParameter(String name) { - Log.warn("Visiting not implemented method 'visitFormalTypeParameter'! name='{}'", name); - super.visitFormalTypeParameter(name); - } - - @Override - public void visitInnerClassType(String name) { - Log.warn("Visiting not implemented method 'visitInnerClassType'! name='{}'", name); - super.visitInnerClassType(name); - } - - @Override - public void visitTypeVariable(String name) { - Log.warn("Visiting not implemented method 'visitTypeVariable'! name='{}'", name); - super.visitTypeVariable(name); + super(controller, Log); } public static String convert(CurrentClassController controller, String descriptor) { diff --git a/src/main/java/javgent/executor/bytecode/clazz/sub/method/visitor/MethodSignatureWriter.java b/src/main/java/javgent/executor/bytecode/clazz/sub/method/visitor/MethodSignatureWriter.java index d94f0c3..dad05a7 100644 --- a/src/main/java/javgent/executor/bytecode/clazz/sub/method/visitor/MethodSignatureWriter.java +++ b/src/main/java/javgent/executor/bytecode/clazz/sub/method/visitor/MethodSignatureWriter.java @@ -1,46 +1,13 @@ package javgent.executor.bytecode.clazz.sub.method.visitor; +import javgent.executor.bytecode.abstractdefault.AbstractDefaultSignatureWriter; import javgent.executor.bytecode.clazz.CurrentClassController; -import javgent.executor.bytecode.clazz.util.InnerClassTypeResolver; import org.objectweb.asm.signature.SignatureReader; -import org.objectweb.asm.signature.SignatureWriter; -public class MethodSignatureWriter extends SignatureWriter { - - private CurrentClassController controller; +public class MethodSignatureWriter extends AbstractDefaultSignatureWriter { public MethodSignatureWriter(CurrentClassController controller) { - this.controller = controller; - } - - @Override - public void visitClassType(String name) { - - String newName = controller.findNameByObfNameOrReturn(name); - - super.visitClassType(newName); - } - - @Override - public void visitFormalTypeParameter(String name) { - - String newName = controller.findNameByObfNameOrReturn(name); - - super.visitFormalTypeParameter(newName); - } - - @Override - public void visitInnerClassType(String name) { - var newName = InnerClassTypeResolver.resolve(controller, name); - - super.visitInnerClassType(newName); - } - - @Override - public void visitTypeVariable(String name) { - String newName = controller.findNameByObfNameOrReturn(name); - - super.visitTypeVariable(newName); + super(controller); } public static String convert(CurrentClassController currentClassController, String signature) { diff --git a/src/main/java/javgent/executor/bytecode/clazz/util/RemoteMethodResolver.java b/src/main/java/javgent/executor/bytecode/clazz/util/RemoteMethodResolver.java index 6b54d57..07c29ce 100644 --- a/src/main/java/javgent/executor/bytecode/clazz/util/RemoteMethodResolver.java +++ b/src/main/java/javgent/executor/bytecode/clazz/util/RemoteMethodResolver.java @@ -26,7 +26,7 @@ public static Optional resolve(CurrentClassControlle result.Owner = currentClassController.findNameByObfNameOrReturn(owner); if(obfName == null && desc == null) - return Optional.ofNullable(result); + return Optional.of(result); var selector = MethodSelectorVistiorDetector.findSelector(remoteController, desc, null); @@ -37,7 +37,7 @@ public static Optional resolve(CurrentClassControlle result.Name = Optional.ofNullable(remotePatchMethod.Name); } - return Optional.ofNullable(result); + return Optional.of(result); } @SuppressWarnings({"squid:ClassVariableVisibilityCheck", "squid:S00116"}) diff --git a/src/main/java/javgent/executor/bytecode/clazz/visitor/RecordDescriptorWriter.java b/src/main/java/javgent/executor/bytecode/clazz/visitor/RecordDescriptorWriter.java new file mode 100644 index 0000000..f65a5f6 --- /dev/null +++ b/src/main/java/javgent/executor/bytecode/clazz/visitor/RecordDescriptorWriter.java @@ -0,0 +1,25 @@ +package javgent.executor.bytecode.clazz.visitor; + +import javgent.executor.bytecode.abstractdefault.AbstractDescriptorWriter; +import javgent.executor.bytecode.clazz.CurrentClassController; +import org.objectweb.asm.signature.SignatureReader; +import org.objectweb.asm.signature.SignatureWriter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RecordDescriptorWriter extends AbstractDescriptorWriter { + + private static final Logger Log = LoggerFactory.getLogger(RecordDescriptorWriter.class); + + public RecordDescriptorWriter(CurrentClassController controller) { + super(controller, Log); + } + + public static String convert(CurrentClassController controller, String descriptor) { + var writer = new RecordDescriptorWriter(controller); + + new SignatureReader(descriptor).accept(writer); + + return writer.toString(); + } +} diff --git a/src/main/java/javgent/executor/bytecode/clazz/visitor/RecordSignatureWriter.java b/src/main/java/javgent/executor/bytecode/clazz/visitor/RecordSignatureWriter.java new file mode 100644 index 0000000..ebbacc3 --- /dev/null +++ b/src/main/java/javgent/executor/bytecode/clazz/visitor/RecordSignatureWriter.java @@ -0,0 +1,23 @@ +package javgent.executor.bytecode.clazz.visitor; + +import javgent.executor.bytecode.abstractdefault.AbstractDefaultSignatureWriter; +import javgent.executor.bytecode.clazz.CurrentClassController; +import org.objectweb.asm.signature.SignatureReader; + +public class RecordSignatureWriter extends AbstractDefaultSignatureWriter { + + public RecordSignatureWriter(CurrentClassController controller) { + super(controller); + } + + public static String convert(CurrentClassController currentClassController, String signature) { + if(signature == null) + return null; + + var writer = new RecordSignatureWriter(currentClassController); + + new SignatureReader(signature).accept(writer); + + return writer.toString(); + } +}