diff --git a/resources/bytecode/javFiles/InstanceOf.jav b/resources/bytecode/javFiles/InstanceOf.jav index 4cee01f2..e7b1cbd8 100644 --- a/resources/bytecode/javFiles/InstanceOf.jav +++ b/resources/bytecode/javFiles/InstanceOf.jav @@ -1,18 +1,18 @@ -import java.lang.Number; +import java.lang.Object; import java.lang.Integer; -import java.lang.Double; -import java.lang.String; +import java.lang.Boolean; + +interface Interface {} + +class Test implements Interface { +} +class Test2 { +} public class InstanceOf { - main(n) { - if (n instanceof Integer i) { - takes(i); - return "Integer"; - } else if (n instanceof Double d) { - takes(d); - return "Double"; - } - } + a = new Test(); - takes(i) {} // Should be overloaded + test1() { return this.a instanceof Test; } + test2() { return this.a instanceof Interface; } + test3() { return this.a instanceof Integer; } } \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index dffcebeb..d7ced3bf 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -1078,6 +1078,7 @@ public class Codegen { var mv = state.mv; if (instanceOf.right() instanceof TargetTypePattern right && right.name() == null) { + generate(state, instanceOf.left()); mv.visitTypeInsn(INSTANCEOF, right.type().getInternalName()); return; } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index e1c82e94..a776f81c 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -157,7 +157,7 @@ public class TYPEStmt implements StatementVisitor { @Override public void visit(InstanceOf instanceOf) { - //throw new NotImplementedException(); + instanceOf.getExpression().accept(this); } @Override diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 34571c82..0ab8a7ec 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -788,4 +788,15 @@ public class TestComplete { var clazz = classFiles.get("SuperCall"); var instance = clazz.getDeclaredConstructor().newInstance(); } + + @Test + public void testInstanceOf() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "InstanceOf.jav"); + var clazz = classFiles.get("InstanceOf"); + var instance = clazz.getDeclaredConstructor().newInstance(); + + assertTrue((Boolean) clazz.getDeclaredMethod("test1").invoke(instance)); + assertTrue((Boolean) clazz.getDeclaredMethod("test2").invoke(instance)); + assertFalse((Boolean) clazz.getDeclaredMethod("test3").invoke(instance)); + } }