Add instanceof
This commit is contained in:
parent
700ea125fc
commit
9da763b361
@ -1,18 +1,18 @@
|
|||||||
import java.lang.Number;
|
import java.lang.Object;
|
||||||
import java.lang.Integer;
|
import java.lang.Integer;
|
||||||
import java.lang.Double;
|
import java.lang.Boolean;
|
||||||
import java.lang.String;
|
|
||||||
|
interface Interface {}
|
||||||
|
|
||||||
|
class Test implements Interface {
|
||||||
|
}
|
||||||
|
class Test2 {
|
||||||
|
}
|
||||||
|
|
||||||
public class InstanceOf {
|
public class InstanceOf {
|
||||||
main(n) {
|
a = new Test();
|
||||||
if (n instanceof Integer i) {
|
|
||||||
takes(i);
|
|
||||||
return "Integer";
|
|
||||||
} else if (n instanceof Double d) {
|
|
||||||
takes(d);
|
|
||||||
return "Double";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
takes(i) {} // Should be overloaded
|
test1() { return this.a instanceof Test; }
|
||||||
|
test2() { return this.a instanceof Interface; }
|
||||||
|
test3() { return this.a instanceof Integer; }
|
||||||
}
|
}
|
@ -1078,6 +1078,7 @@ public class Codegen {
|
|||||||
var mv = state.mv;
|
var mv = state.mv;
|
||||||
|
|
||||||
if (instanceOf.right() instanceof TargetTypePattern right && right.name() == null) {
|
if (instanceOf.right() instanceof TargetTypePattern right && right.name() == null) {
|
||||||
|
generate(state, instanceOf.left());
|
||||||
mv.visitTypeInsn(INSTANCEOF, right.type().getInternalName());
|
mv.visitTypeInsn(INSTANCEOF, right.type().getInternalName());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -157,7 +157,7 @@ public class TYPEStmt implements StatementVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(InstanceOf instanceOf) {
|
public void visit(InstanceOf instanceOf) {
|
||||||
//throw new NotImplementedException();
|
instanceOf.getExpression().accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -788,4 +788,15 @@ public class TestComplete {
|
|||||||
var clazz = classFiles.get("SuperCall");
|
var clazz = classFiles.get("SuperCall");
|
||||||
var instance = clazz.getDeclaredConstructor().newInstance();
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user