Skip to content

Commit

Permalink
Handle records correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
litetex committed Aug 31, 2024
1 parent d4cfc71 commit 5e0a856
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 76 deletions.
2 changes: 0 additions & 2 deletions src/main/java/javgent/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExecutorConfig> optionalExecutorConfig = Parser.parse(args);
if (optionalExecutorConfig.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
16 changes: 13 additions & 3 deletions src/main/java/javgent/executor/bytecode/clazz/BCClassVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static Optional<RemoteMethodResolverResult> 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);

Expand All @@ -37,7 +37,7 @@ public static Optional<RemoteMethodResolverResult> resolve(CurrentClassControlle
result.Name = Optional.ofNullable(remotePatchMethod.Name);
}

return Optional.ofNullable(result);
return Optional.of(result);
}

@SuppressWarnings({"squid:ClassVariableVisibilityCheck", "squid:S00116"})
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 5e0a856

Please sign in to comment.