mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 01:18:03 +00:00
fix problem with new
This commit is contained in:
parent
71df6cc1d5
commit
c823894eb2
80
src/main/java/de/maishai/typedast/LoggingMethodVisitor.java
Normal file
80
src/main/java/de/maishai/typedast/LoggingMethodVisitor.java
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
package de.maishai.typedast;
|
||||||
|
|
||||||
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
import org.objectweb.asm.Opcodes;
|
||||||
|
|
||||||
|
public class LoggingMethodVisitor extends MethodVisitor {
|
||||||
|
|
||||||
|
public LoggingMethodVisitor(MethodVisitor methodVisitor) {
|
||||||
|
super(Opcodes.ASM9, methodVisitor);
|
||||||
|
System.out.println("\n--- Visiting Method ---");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String opcodeToString(int opcode) {
|
||||||
|
for (java.lang.reflect.Field field : Opcodes.class.getDeclaredFields()) {
|
||||||
|
try {
|
||||||
|
if (field.getType() == int.class && field.getInt(null) == opcode) {
|
||||||
|
return field.getName();
|
||||||
|
}
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "UNKNOWN";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
|
||||||
|
System.out.println("visitMethodInsn: " + opcodeToString(opcode) + ", " + owner + ", " + name + ", " + descriptor + ", " + isInterface);
|
||||||
|
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitVarInsn(int opcode, int varIndex) {
|
||||||
|
System.out.println("visitVarInsn: " + opcodeToString(opcode) + ", " + varIndex);
|
||||||
|
super.visitVarInsn(opcode, varIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitTypeInsn(int opcode, String type) {
|
||||||
|
System.out.println("visitTypeInsn: " + opcodeToString(opcode) + ", " + type);
|
||||||
|
super.visitTypeInsn(opcode, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitInsn(int opcode) {
|
||||||
|
System.out.println("visitInsn: " + opcodeToString(opcode));
|
||||||
|
super.visitInsn(opcode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitMethodInsn(int opcode, String owner, String name, String descriptor) {
|
||||||
|
System.out.println("visitMethodInsn: " + opcodeToString(opcode) + ", " + owner + ", " + name + ", " + descriptor);
|
||||||
|
super.visitMethodInsn(opcode, owner, name, descriptor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitFieldInsn(int opcode, String owner, String name, String descriptor) {
|
||||||
|
System.out.println("visitFieldInsn: " + opcodeToString(opcode) + ", " + owner + ", " + name + ", " + descriptor);
|
||||||
|
super.visitFieldInsn(opcode, owner, name, descriptor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitMaxs(int maxStack, int maxLocals) {
|
||||||
|
System.out.println("visitMaxs: " + maxStack + ", " + maxLocals);
|
||||||
|
super.visitMaxs(maxStack, maxLocals);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitIntInsn(int opcode, int operand) {
|
||||||
|
String opcodeString = opcode == Opcodes.BIPUSH ? "BIPUSH" : "SIPUSH";
|
||||||
|
System.out.println("visitIntInsn: " + opcodeString + ", " + operand);
|
||||||
|
super.visitIntInsn(opcode, operand);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitLdcInsn(Object value) {
|
||||||
|
System.out.println("visitLdcInsn: " + value);
|
||||||
|
super.visitLdcInsn(value);
|
||||||
|
}
|
||||||
|
}
|
@ -28,7 +28,12 @@ public class MethodContext {
|
|||||||
private final Deque<Label> continueLabels = new ArrayDeque<>();
|
private final Deque<Label> continueLabels = new ArrayDeque<>();
|
||||||
|
|
||||||
public MethodContext(ClassContext classContext, MethodVisitor mv, Type returnType) {
|
public MethodContext(ClassContext classContext, MethodVisitor mv, Type returnType) {
|
||||||
this.mv = mv;
|
// if DEBUG env variable is set, log all method calls
|
||||||
|
if (System.getenv("DEBUG") != null) {
|
||||||
|
this.mv = new LoggingMethodVisitor(mv);
|
||||||
|
} else {
|
||||||
|
this.mv = mv;
|
||||||
|
}
|
||||||
this.classContext = classContext;
|
this.classContext = classContext;
|
||||||
this.returnType = returnType;
|
this.returnType = returnType;
|
||||||
registerVariable("this", classContext.getType());
|
registerVariable("this", classContext.getType());
|
||||||
|
@ -88,7 +88,7 @@ public class TypedAssignment implements TypedStatement {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void codeGen(MethodContext ctx) {
|
public void codeGen(MethodContext ctx) {
|
||||||
if(value instanceof TypedNew || value instanceof TypedMethodCall) {
|
if(value instanceof TypedMethodCall) {
|
||||||
value.codeGen(ctx);
|
value.codeGen(ctx);
|
||||||
getOwnerChain(ctx);
|
getOwnerChain(ctx);
|
||||||
} else {
|
} else {
|
||||||
|
@ -71,6 +71,7 @@ public class TypedNew implements TypedExpression, TypedStatement {
|
|||||||
}
|
}
|
||||||
String descriptor = CodeGenUtils.generateDescriptor(args.stream().map(TypedExpression::getType).toList(), Type.VOID);
|
String descriptor = CodeGenUtils.generateDescriptor(args.stream().map(TypedExpression::getType).toList(), Type.VOID);
|
||||||
ctx.getMv().visitMethodInsn(Opcodes.INVOKESPECIAL, type.getReference(), "<init>", descriptor, false);
|
ctx.getMv().visitMethodInsn(Opcodes.INVOKESPECIAL, type.getReference(), "<init>", descriptor, false);
|
||||||
|
ctx.popStack();
|
||||||
LOGGER.log(Level.FINEST, "INVOKESPECIAL {0} <init> {1}", new Object[]{type.getReference(), descriptor});
|
LOGGER.log(Level.FINEST, "INVOKESPECIAL {0} <init> {1}", new Object[]{type.getReference(), descriptor});
|
||||||
for (int i = 0; i < args.size(); i++) {
|
for (int i = 0; i < args.size(); i++) {
|
||||||
ctx.popStack();
|
ctx.popStack();
|
||||||
|
@ -7,52 +7,50 @@ import testResources.CodeGen.BytecodeTestUtil;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ByteCode_ClassObjects {
|
class ByteCode_ClassObjects {
|
||||||
|
|
||||||
private BytecodeTestUtil util;
|
private BytecodeTestUtil util;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
try {
|
try {
|
||||||
util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/ClassObjects.java"), "ClassObjects");
|
util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/ClassObjects.java"), "ClassObjects");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testConstructorCount() {
|
void testConstructorCount() {
|
||||||
Assertions.assertEquals(2, util.getConstructorCount());
|
Assertions.assertEquals(2, util.getConstructorCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testConstructor1() {
|
void testConstructor1() {
|
||||||
Assertions.assertEquals("ClassObjects", util.getConstructorNames().get(0));
|
Assertions.assertEquals("ClassObjects", util.getConstructorNames().get(0));
|
||||||
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
|
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testConstructor2() {
|
void testConstructor2() {
|
||||||
Assertions.assertEquals("ClassObjects", util.getConstructorNames().get(1));
|
Assertions.assertEquals("ClassObjects", util.getConstructorNames().get(1));
|
||||||
Assertions.assertEquals(1, util.getConstructorParameterCount(1));
|
Assertions.assertEquals(1, util.getConstructorParameterCount(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMethodCount() {
|
void testMethodCount() {
|
||||||
Assertions.assertEquals(1, util.getMethodCount());
|
Assertions.assertEquals(1, util.getMethodCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMethodNames() {
|
void testMethodNames() {
|
||||||
Assertions.assertEquals("objectsMethod", util.getMethodNames().get(0));
|
Assertions.assertEquals("objectsMethod", util.getMethodNames().get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMethodReturnType() {
|
void testMethodReturnType() {
|
||||||
try {
|
try {
|
||||||
Assertions.assertEquals("void", util.getMethodReturnType("objectsMethod", new Class<?>[]{}));
|
Assertions.assertEquals("void", util.getMethodReturnType("objectsMethod", new Class<?>[]{}));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -61,7 +59,7 @@ public class ByteCode_ClassObjects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMethodParameters() {
|
void testMethodParameters() {
|
||||||
try {
|
try {
|
||||||
Assertions.assertEquals(0, util.getMethodParameterCount("objectsMethod", new Class<?>[]{}));
|
Assertions.assertEquals(0, util.getMethodParameterCount("objectsMethod", new Class<?>[]{}));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -70,28 +68,27 @@ public class ByteCode_ClassObjects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFieldCount() {
|
void testFieldCount() {
|
||||||
Assertions.assertEquals(3, util.getFieldCount());
|
Assertions.assertEquals(3, util.getFieldCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFieldNames() {
|
void testFieldNames() {
|
||||||
Assertions.assertEquals("object", util.getFieldNames().get(0));
|
Assertions.assertEquals("object", util.getFieldNames().get(0));
|
||||||
Assertions.assertEquals("objectWithValue", util.getFieldNames().get(1));
|
Assertions.assertEquals("objectWithValue", util.getFieldNames().get(1));
|
||||||
Assertions.assertEquals("integerValue", util.getFieldNames().get(2));
|
Assertions.assertEquals("integerValue", util.getFieldNames().get(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFieldTypes() {
|
void testFieldTypes() {
|
||||||
Assertions.assertEquals("ClassObjects", util.getFieldTypes().get(0));
|
Assertions.assertEquals("ClassObjects", util.getFieldTypes().get(0));
|
||||||
Assertions.assertEquals("ClassObjects", util.getFieldTypes().get(1));
|
Assertions.assertEquals("ClassObjects", util.getFieldTypes().get(1));
|
||||||
Assertions.assertEquals("int", util.getFieldTypes().get(2));
|
Assertions.assertEquals("int", util.getFieldTypes().get(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFieldValues() {
|
void testFieldValues() {
|
||||||
try {
|
try {
|
||||||
Assertions.assertNull(util.getFieldValue("object"));
|
Assertions.assertNull(util.getFieldValue("object"));
|
||||||
Assertions.assertNull(util.getFieldValue("objectWithValue"));
|
Assertions.assertNull(util.getFieldValue("objectWithValue"));
|
||||||
@ -102,47 +99,46 @@ public class ByteCode_ClassObjects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvokeConstructor1() {
|
void testInvokeConstructor1() {
|
||||||
try {
|
try {
|
||||||
Object constructor1ReturnValue = util.invokeConstructor(new Class<?>[]{}, new Object[]{});
|
Object constructor1ReturnValue = util.invokeConstructor(new Class<?>[]{}, new Object[]{});
|
||||||
Assertions.assertEquals(constructor1ReturnValue.getClass().getName(), "ClassObjects");
|
Assertions.assertEquals("ClassObjects", constructor1ReturnValue.getClass().getName());
|
||||||
Assertions.assertNull(util.getFieldValueOfObject(constructor1ReturnValue,"object"));
|
Assertions.assertNull(util.getFieldValueOfObject(constructor1ReturnValue, "object"));
|
||||||
Assertions.assertNull(util.getFieldValueOfObject(constructor1ReturnValue, "objectWithValue"));
|
Assertions.assertNull(util.getFieldValueOfObject(constructor1ReturnValue, "objectWithValue"));
|
||||||
Assertions.assertEquals(0,
|
Assertions.assertEquals(0,
|
||||||
(int) util.getFieldValueOfObject(constructor1ReturnValue,"integerValue"));
|
(int) util.getFieldValueOfObject(constructor1ReturnValue, "integerValue"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvokeConstructor2() {
|
void testInvokeConstructor2() {
|
||||||
try {
|
try {
|
||||||
Object constructor2ReturnValue = util.invokeConstructor(new Class<?>[]{int.class}, new Object[]{2});
|
Object constructor2ReturnValue = util.invokeConstructor(new Class<?>[]{int.class}, new Object[]{2});
|
||||||
Assertions.assertEquals(constructor2ReturnValue.getClass().getName(), "ClassObjects");
|
Assertions.assertEquals("ClassObjects", constructor2ReturnValue.getClass().getName());
|
||||||
Assertions.assertNull(util.getFieldValueOfObject(constructor2ReturnValue, "object"));
|
Assertions.assertNull(util.getFieldValueOfObject(constructor2ReturnValue, "object"));
|
||||||
Assertions.assertNull(util.getFieldValueOfObject(constructor2ReturnValue, "objectWithValue"));
|
Assertions.assertNull(util.getFieldValueOfObject(constructor2ReturnValue, "objectWithValue"));
|
||||||
Assertions.assertEquals(2,
|
Assertions.assertEquals(2,
|
||||||
(int) util.getFieldValueOfObject(constructor2ReturnValue,"integerValue"));
|
(int) util.getFieldValueOfObject(constructor2ReturnValue, "integerValue"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvokeMethod() {
|
void testInvokeMethod() {
|
||||||
try {
|
try {
|
||||||
Object fieldObject = util.getFieldValue("object");
|
Object fieldObject = util.getFieldValue("object");
|
||||||
Object fieldObjectWithValue = util.getFieldValue("objectWithValue");
|
Object fieldObjectWithValue = util.getFieldValue("objectWithValue");
|
||||||
int fieldIntegerValue = (int) util.getFieldValue("integerValue");
|
Assertions.assertEquals(0, (int) util.getFieldValue("integerValue"));
|
||||||
util.invokeMethod("objectsMethod", new Class<?>[]{}, new Object[]{});
|
util.invokeMethod("objectsMethod", null);
|
||||||
Assertions.assertNotSame(util.getFieldValue("object"), fieldObject);
|
Assertions.assertNotSame(util.getFieldValue("object"), fieldObject);
|
||||||
Assertions.assertEquals(util.getFieldValue("object").getClass().getName(), "ClassObjects");
|
Assertions.assertEquals("ClassObjects", util.getFieldValue("object").getClass().getName());
|
||||||
Assertions.assertNotSame(util.getFieldValue("objectWithValue"), fieldObjectWithValue);
|
Assertions.assertNotSame(util.getFieldValue("objectWithValue"), fieldObjectWithValue);
|
||||||
Assertions.assertEquals(util.getFieldValue("objectWithValue").getClass().getName(), "ClassObjects");
|
Assertions.assertEquals("ClassObjects", util.getFieldValue("objectWithValue").getClass().getName());
|
||||||
Assertions.assertEquals((int) util.getFieldValue("integerValue"), fieldIntegerValue);
|
Assertions.assertEquals(2, (int) util.getFieldValue("integerValue"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user