Write tx signature to constant pool

This commit is contained in:
Victorious3 2023-03-24 15:14:17 +01:00
parent cf2d612870
commit b9f19cc105
2 changed files with 11 additions and 13 deletions

View File

@ -999,8 +999,8 @@ public class Codegen {
private void generateConstructor(TargetConstructor constructor) {
MethodVisitor mv = cw.visitMethod(constructor.access() | ACC_PUBLIC, "<init>", constructor.getDescriptor(), constructor.getSignature(), null);
if (!constructor.txGenerics().isEmpty())
mv.visitAttribute(new JavaTXSignatureAttribute(constructor.getTXSignature()));
if (constructor.txGenerics() != null)
mv.visitAttribute(new JavaTXSignatureAttribute(cw.newConst(constructor.getTXSignature())));
mv.visitCode();
var state = new State(null, mv, 1);
@ -1026,8 +1026,9 @@ public class Codegen {
private void generateMethod(TargetMethod method) {
// TODO The older codegen has set ACC_PUBLIC for all methods, good for testing but bad for everything else
MethodVisitor mv = cw.visitMethod(method.access() | ACC_PUBLIC, method.name(), method.getDescriptor(), method.getSignature(), null);
if (method.txSignature() != null)
mv.visitAttribute(new JavaTXSignatureAttribute(method.getTXSignature()));
if (method.txSignature() != null) {
mv.visitAttribute(new JavaTXSignatureAttribute(cw.newConst(method.getTXSignature())));
}
System.out.println(method.getDescriptor());
System.out.println(method.getSignature());
@ -1058,8 +1059,8 @@ public class Codegen {
generateSignature(clazz, clazz.generics()), clazz.superType() != null ? clazz.superType().getInternalName(): "java/lang/Object",
clazz.implementingInterfaces().stream().map(TargetType::toSignature).toArray(String[]::new)
);
if (!clazz.txGenerics().isEmpty())
cw.visitAttribute(new JavaTXSignatureAttribute(generateSignature(clazz, clazz.txGenerics())));
if (clazz.txGenerics() != null)
cw.visitAttribute(new JavaTXSignatureAttribute(cw.newConst(generateSignature(clazz, clazz.txGenerics()))));
clazz.fields().forEach(this::generateField);
clazz.constructors().forEach(this::generateConstructor);

View File

@ -2,13 +2,10 @@ package de.dhbwstuttgart.bytecode;
import org.objectweb.asm.*;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
public class JavaTXSignatureAttribute extends Attribute {
final String signature;
final int signature;
protected JavaTXSignatureAttribute(String signature) {
protected JavaTXSignatureAttribute(int signature) {
super("JavaTXSignature");
this.signature = signature;
}
@ -17,13 +14,13 @@ public class JavaTXSignatureAttribute extends Attribute {
protected Attribute read(ClassReader classReader, int offset, int length, char[] charBuffer, int codeAttributeOffset, Label[] labels) {
var data = new byte[length];
System.arraycopy(classReader.b, offset, data, 0, length);
return new JavaTXSignatureAttribute(new String(data, StandardCharsets.UTF_8));
return new JavaTXSignatureAttribute(data[0] << 8 | data[1]);
}
@Override
protected ByteVector write(ClassWriter classWriter, byte[] code, int codeLength, int maxStack, int maxLocals) {
var data = new ByteVector();
data.putUTF8(this.signature);
data.putShort(this.signature);
return data;
}
}