@@ -1013,36 +1013,83 @@ private TreeNode convertIdent(IdentifierTree node, TreePath parent) {
1013
1013
private TreeNode convertIf (IfTree node , TreePath parent ) {
1014
1014
TreePath path = getTreePath (parent , node );
1015
1015
Expression condition = convertWithoutParens (node .getCondition (), path );
1016
- Statement thenStatement = (Statement ) convert (node .getThenStatement (), path );
1017
- Statement newNode = new IfStatement ()
1018
- .setExpression (condition )
1019
- .setThenStatement (thenStatement )
1020
- .setElseStatement ((Statement ) convert (node .getElseStatement (), path ));
1016
+ Pattern pattern = null ;
1021
1017
InstanceofExpression instanceofExpr = null ;
1018
+ Expression subExpr = null ;
1022
1019
if (condition .getKind () == TreeNode .Kind .INSTANCEOF_EXPRESSION ) {
1023
1020
instanceofExpr = (InstanceofExpression ) condition ;
1021
+ pattern = instanceofExpr .getPattern ();
1022
+ instanceofExpr .setPattern (null );
1024
1023
} else if (condition .getKind () == TreeNode .Kind .INFIX_EXPRESSION ) {
1025
- for (Expression operand : ((InfixExpression ) condition ).getOperands ()) {
1024
+ InfixExpression infixExpr = (InfixExpression ) condition ;
1025
+ List <Expression > operands = infixExpr .getOperands ();
1026
+ for (int i = 0 ; i < operands .size (); i ++) {
1027
+ Expression operand = operands .get (i );
1026
1028
if (operand .getKind () == TreeNode .Kind .INSTANCEOF_EXPRESSION ) {
1027
1029
instanceofExpr = (InstanceofExpression ) operand ;
1028
- break ;
1030
+ pattern = instanceofExpr .getPattern ();
1031
+ if (pattern != null ) {
1032
+ instanceofExpr .setPattern (null );
1033
+ condition = instanceofExpr .copy ();
1034
+ List <Expression > subOperands = new ArrayList <>();
1035
+ while (++i < operands .size ()) {
1036
+ subOperands .add (operands .get (i ));
1037
+ }
1038
+ if (subOperands .size () == 1 ) {
1039
+ subExpr = subOperands .get (0 ).copy ();
1040
+ } else if (subOperands .size () > 1 ) {
1041
+ InfixExpression newInfix =
1042
+ new InfixExpression (infixExpr .getTypeMirror (), infixExpr .getOperator ());
1043
+ for (Expression subOperand : subOperands ) {
1044
+ newInfix .addOperand (subOperand .copy ());
1045
+ }
1046
+ subExpr = newInfix ;
1047
+ }
1048
+ break ;
1049
+ } else {
1050
+ // No pattern, reset instanceofExpr and pattern.
1051
+ instanceofExpr = null ;
1052
+ pattern = null ;
1053
+ }
1029
1054
}
1030
1055
}
1031
1056
}
1032
- Pattern pattern = instanceofExpr != null ? instanceofExpr .getPattern () : null ;
1057
+
1058
+ Statement thenStatement = (Statement ) convert (node .getThenStatement (), path );
1033
1059
if (pattern != null ) {
1034
1060
// Create local variable with pattern variable element.
1035
1061
VariableElement localVar =
1036
1062
((Pattern .BindingPattern ) pattern ).getVariable ().getVariableElement ();
1037
- CastExpression castExpr = new CastExpression (localVar .asType (),
1038
- instanceofExpr .getLeftOperand ().copy ());
1063
+ CastExpression castExpr =
1064
+ new CastExpression (localVar .asType (), instanceofExpr .getLeftOperand ().copy ());
1065
+ castExpr .setNeedsCastChk (false );
1039
1066
VariableDeclarationStatement localVarDecl =
1040
1067
new VariableDeclarationStatement (localVar , castExpr );
1041
- Block block = new Block ()
1042
- .addStatement (localVarDecl )
1043
- .addStatement (newNode );
1044
- newNode = block ;
1068
+ Block thenBlock ;
1069
+ if (thenStatement .getKind () == TreeNode .Kind .BLOCK ) {
1070
+ thenBlock = (Block ) thenStatement ;
1071
+ } else {
1072
+ thenBlock = new Block ();
1073
+ thenBlock .addStatement (thenStatement );
1074
+ }
1075
+ thenBlock .addStatement (0 , localVarDecl );
1076
+ if (subExpr != null ) {
1077
+ // Move statements inside of if statement with subExpr condition.
1078
+ IfStatement subIf = new IfStatement ().setExpression (subExpr );
1079
+ Block subIfBlock = new Block ();
1080
+ subIf .setThenStatement (subIfBlock );
1081
+ while (thenBlock .getStatements ().size () > 1 ) {
1082
+ subIfBlock .addStatement (thenBlock .getStatements ().remove (1 ));
1083
+ }
1084
+ thenBlock .addStatement (subIf );
1085
+ }
1086
+ thenStatement = thenBlock ;
1045
1087
}
1088
+ Statement newNode =
1089
+ new IfStatement ()
1090
+ .setExpression (condition )
1091
+ .setThenStatement (thenStatement )
1092
+ .setElseStatement ((Statement ) convert (node .getElseStatement (), path ));
1046
1093
return newNode ;
1047
1094
}
1048
1095
@@ -1201,6 +1248,7 @@ private void maybeAddUnreachableDirective(Block body) {
1201
1248
}
1202
1249
}
1203
1250
1251
+ @ SuppressWarnings ("StatementSwitchToExpressionSwitch" )
1204
1252
private static String getMemberName (ExpressionTree node ) {
1205
1253
switch (node .getKind ().name ()) {
1206
1254
case "IDENTIFIER" :
@@ -1883,6 +1931,7 @@ private TreeNode getAssociatedJavaDoc(Tree node, TreePath path) {
1883
1931
return comment != null && comment .isDocComment () ? comment : null ;
1884
1932
}
1885
1933
1934
+ @ SuppressWarnings ("StatementSwitchToExpressionSwitch" )
1886
1935
private Comment convertAssociatedComment (Tree node , TreePath path ) {
1887
1936
boolean docCommentsEnabled = newUnit .getEnv ().options ().docCommentsEnabled ();
1888
1937
DocCommentTable docComments = ((JCCompilationUnit ) unit ).docComments ;
0 commit comments