Skip to content

Commit

Permalink
Merge pull request #45 from stuartwdouglas/44
Browse files Browse the repository at this point in the history
Add instanceOf check
  • Loading branch information
gsmet authored Jul 3, 2020
2 parents 1d79895 + e9b1cce commit f3c84c0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/io/quarkus/gizmo/BytecodeCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,26 @@ default AssignableResultHandle createVariable(final Class<?> type) {
*/
BranchResult ifIntegerLessThan(ResultHandle value1, ResultHandle value2);

/**
* Checks if the given resultHandle is an instance of the target type
*
* @param resultHandle the result handle
* @param testType the cast target class
* @return a boolean result handle with the result of the instanceof call
*/
default ResultHandle instanceOf(ResultHandle resultHandle, Class<?> testType) {
return instanceOf(resultHandle, testType.getName());
}

/**
* Checks if the given resultHandle is an instance of the target type
*
* @param resultHandle the result handle
* @param testType the cast target class
* @return a boolean result handle with the result of the instanceof call
*/
ResultHandle instanceOf(ResultHandle resultHandle, String testType);

/**
* An if statement.
* <p>
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/io/quarkus/gizmo/BytecodeCreatorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,41 @@ public BranchResult ifIntegerLessThan(ResultHandle value1, ResultHandle value2)
return ifValues(value1, value2, Opcodes.IF_ICMPLT, "I");
}

@Override
public ResultHandle instanceOf(ResultHandle resultHandle, String castTarget) {
Objects.requireNonNull(resultHandle);
Objects.requireNonNull(castTarget);
final ResultHandle result = allocateResult("I");
// seems like a waste of local vars but it's the safest approach since result type can't be mutated
final ResultHandle resolvedResultHandle = resolve(checkScope(resultHandle));
assert result != null;
String intName = castTarget.replace('.', '/');
operations.add(new Operation() {
@Override
void writeBytecode(final MethodVisitor methodVisitor) {
loadResultHandle(methodVisitor, resolvedResultHandle, BytecodeCreatorImpl.this, resultHandle.getType(),true);
methodVisitor.visitTypeInsn(Opcodes.INSTANCEOF, intName);
storeResultHandle(methodVisitor, result);
}

@Override
Set<ResultHandle> getInputResultHandles() {
return Collections.singleton(resolvedResultHandle);
}

@Override
ResultHandle getTopResultHandle() {
return resolvedResultHandle;
}

@Override
ResultHandle getOutgoingResultHandle() {
return result;
}
});
return result;
}

@Override
public BranchResult ifIntegerLessEqual(ResultHandle value1, ResultHandle value2) {
return ifValues(value1, value2, Opcodes.IF_ICMPLE, "I");
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/io/quarkus/gizmo/BooleanInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.quarkus.gizmo;

public interface BooleanInterface {

boolean test(Object object);
}
21 changes: 21 additions & 0 deletions src/test/java/io/quarkus/gizmo/InstanceofTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.gizmo;

import org.junit.Assert;
import org.junit.Test;

public class InstanceofTestCase {

@Test
public void testInstanceof() throws Exception {
TestClassLoader cl = new TestClassLoader(getClass().getClassLoader());
try (ClassCreator creator = ClassCreator.builder().classOutput(cl).className("com.MyTest").interfaces(BooleanInterface.class).build()) {
MethodCreator method = creator.getMethodCreator("test", boolean.class, Object.class);
method.returnValue(method.instanceOf(method.getMethodParam(0), String.class));

}
BooleanInterface myInterface = (BooleanInterface) cl.loadClass("com.MyTest").newInstance();
Assert.assertTrue(myInterface.test("PARAM"));
Assert.assertFalse(myInterface.test(this));
}

}

0 comments on commit f3c84c0

Please sign in to comment.