Skip to content

Commit

Permalink
Add support for instanceof to groovy parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Jun 12, 2023
1 parent 186a1a0 commit 7c182ff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.Statement;
import org.openrewrite.java.tree.*;
import org.openrewrite.java.marker.Semicolon;
import org.openrewrite.marker.Markers;

import java.math.BigDecimal;
Expand Down Expand Up @@ -691,6 +692,7 @@ public void visitBinaryExpression(BinaryExpression binary) {

Space opPrefix = whitespace();
boolean assignment = false;
boolean instanceOf = false;
J.AssignmentOperation.Type assignOp = null;
J.Binary.Type binaryOp = null;
G.Binary.Type gBinaryOp = null;
Expand Down Expand Up @@ -752,6 +754,9 @@ public void visitBinaryExpression(BinaryExpression binary) {
case ">>>":
binaryOp = J.Binary.Type.UnsignedRightShift;
break;
case "instanceof":
instanceOf = true;
break;
case "=":
assignment = true;
break;
Expand Down Expand Up @@ -809,6 +814,10 @@ public void visitBinaryExpression(BinaryExpression binary) {
queue.add(new J.Assignment(randomId(), fmt, Markers.EMPTY,
left, JLeftPadded.build(right).withBefore(opPrefix),
typeMapping.type(binary.getType())));
} else if (instanceOf) {
queue.add(new J.InstanceOf(randomId(), fmt, Markers.EMPTY,
JRightPadded.build(left).withAfter(opPrefix), right, null,
typeMapping.type(binary.getType())));
} else if (assignOp != null) {
queue.add(new J.AssignmentOperation(randomId(), fmt, Markers.EMPTY,
left, JLeftPadded.build(assignOp).withBefore(opPrefix),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ public J visitTernary(J.Ternary ternary, PrintOutputCapture<P> p) {

@Override
public <M extends Marker> M visitMarker(Marker marker, PrintOutputCapture<P> p) {
if (marker instanceof Semicolon) {
//noinspection deprecation
if (marker instanceof Semicolon || marker instanceof org.openrewrite.java.marker.Semicolon) {
p.append(';');
}
return super.visitMarker(marker, p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import static org.openrewrite.groovy.Assertions.groovy;

@SuppressWarnings({"GroovyUnusedAssignment", "GrUnnecessarySemicolon"})
@SuppressWarnings({"GroovyUnusedAssignment", "GrUnnecessarySemicolon", "UnnecessaryQualifiedReference"})
class BinaryTest implements RewriteTest {

@Test
Expand Down Expand Up @@ -140,4 +140,13 @@ void bitwiseXOr() {
)
);
}

@Test
void instanceOf() {
rewriteRun(
groovy("""
def isString = "" instanceof java.lang.String
""")
);
}
}

0 comments on commit 7c182ff

Please sign in to comment.