[java] Remove java12 break-with-expression support
This preview language feature has been replaced with the yield statement in java 13.
This commit is contained in:
@ -416,12 +416,6 @@ public class JavaParser {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkForBreakExpression() {
|
||||
if (jdkVersion < 12) {
|
||||
throwParseException("Expressions in break statements are only supported with Java 12");
|
||||
}
|
||||
}
|
||||
|
||||
private void checkForYieldStatement() {
|
||||
if (jdkVersion < 13) {
|
||||
throwParseException("Yield statements are only supported with Java 13");
|
||||
@ -2666,7 +2660,7 @@ void ForUpdate() :
|
||||
void BreakStatement() :
|
||||
{Token t;}
|
||||
{
|
||||
"break" [ LOOKAHEAD(<IDENTIFIER> ";") t=<IDENTIFIER> {jjtThis.setImage(t.image);} | Expression() {checkForBreakExpression();} ] ";"
|
||||
"break" [ t=<IDENTIFIER> {jjtThis.setImage(t.image);} ] ";"
|
||||
}
|
||||
|
||||
void ContinueStatement() :
|
||||
|
@ -24,13 +24,4 @@ public class ASTBreakStatement extends AbstractJavaNode {
|
||||
public Object jjtAccept(JavaParserVisitor visitor, Object data) {
|
||||
return visitor.visit(this, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getImage() {
|
||||
String result = super.getImage();
|
||||
if (result == null && hasDescendantOfType(ASTName.class)) {
|
||||
result = getFirstDescendantOfType(ASTName.class).getImage();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -1186,7 +1186,7 @@ public class ClassTypeResolver extends JavaParserVisitorAdapter {
|
||||
super.visit(node, data);
|
||||
|
||||
JavaTypeDefinition type = null;
|
||||
// first try to determine the type based on the first expression/break/yield of a switch rule
|
||||
// first try to determine the type based on the first expression/yield of a switch rule
|
||||
List<ASTSwitchLabeledRule> rules = node.findChildrenOfType(ASTSwitchLabeledRule.class);
|
||||
for (ASTSwitchLabeledRule rule : rules) {
|
||||
Node body = rule.jjtGetChild(1); // second child is either Expression, Block, ThrowStatement
|
||||
@ -1194,14 +1194,6 @@ public class ClassTypeResolver extends JavaParserVisitorAdapter {
|
||||
type = ((ASTExpression) body).getTypeDefinition();
|
||||
break;
|
||||
} else if (body instanceof ASTBlock) {
|
||||
List<ASTBreakStatement> breaks = body.findDescendantsOfType(ASTBreakStatement.class);
|
||||
if (!breaks.isEmpty()) {
|
||||
ASTExpression expression = breaks.get(0).getFirstChildOfType(ASTExpression.class);
|
||||
if (expression != null) {
|
||||
type = expression.getTypeDefinition();
|
||||
break;
|
||||
}
|
||||
}
|
||||
List<ASTYieldStatement> yields = body.findDescendantsOfType(ASTYieldStatement.class);
|
||||
if (!yields.isEmpty()) {
|
||||
ASTExpression expression = yields.get(0).getFirstChildOfType(ASTExpression.class);
|
||||
@ -1213,18 +1205,10 @@ public class ClassTypeResolver extends JavaParserVisitorAdapter {
|
||||
}
|
||||
}
|
||||
if (type == null) {
|
||||
// now check the labels and their expressions of break/yield statements
|
||||
// now check the labels and their expressions of yield statements
|
||||
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
|
||||
Node child = node.jjtGetChild(i);
|
||||
if (child instanceof ASTBlockStatement) {
|
||||
List<ASTBreakStatement> breaks = child.findDescendantsOfType(ASTBreakStatement.class);
|
||||
if (!breaks.isEmpty()) {
|
||||
ASTExpression expression = breaks.get(0).getFirstChildOfType(ASTExpression.class);
|
||||
if (expression != null) {
|
||||
type = expression.getTypeDefinition();
|
||||
break;
|
||||
}
|
||||
}
|
||||
List<ASTYieldStatement> yields = child.findDescendantsOfType(ASTYieldStatement.class);
|
||||
if (!yields.isEmpty()) {
|
||||
ASTExpression expression = yields.get(0).getFirstChildOfType(ASTExpression.class);
|
||||
|
@ -77,7 +77,8 @@ public class Java12Test {
|
||||
|
||||
@Test
|
||||
public void testSwitchExpressions() {
|
||||
ASTCompilationUnit compilationUnit = ParserTstUtil.parseAndTypeResolveJava("12",
|
||||
// note: this uses java13 as we need to use the yield statement
|
||||
ASTCompilationUnit compilationUnit = ParserTstUtil.parseAndTypeResolveJava("13",
|
||||
loadSource("SwitchExpressions.java"));
|
||||
Assert.assertNotNull(compilationUnit);
|
||||
|
||||
@ -91,26 +92,4 @@ public class Java12Test {
|
||||
Assert.assertEquals(Integer.TYPE, localVarDecl.getType());
|
||||
Assert.assertEquals(Integer.TYPE, switchExpression.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwitchExpressionsBreak() {
|
||||
ASTCompilationUnit compilationUnit = ParserTstUtil.parseAndTypeResolveJava("12",
|
||||
loadSource("SwitchExpressionsBreak.java"));
|
||||
Assert.assertNotNull(compilationUnit);
|
||||
|
||||
ASTSwitchExpression switchExpression = compilationUnit.getFirstDescendantOfType(ASTSwitchExpression.class);
|
||||
Assert.assertEquals(11, switchExpression.jjtGetNumChildren());
|
||||
Assert.assertTrue(switchExpression.jjtGetChild(0) instanceof ASTExpression);
|
||||
Assert.assertEquals(5, switchExpression.findChildrenOfType(ASTSwitchLabel.class).size());
|
||||
|
||||
ASTBreakStatement breakStatement = switchExpression.getFirstDescendantOfType(ASTBreakStatement.class);
|
||||
Assert.assertEquals("SwitchExpressionsBreak.SIX", breakStatement.getImage());
|
||||
Assert.assertTrue(breakStatement.jjtGetChild(0) instanceof ASTExpression);
|
||||
|
||||
ASTLocalVariableDeclaration localVar = compilationUnit.findDescendantsOfType(ASTLocalVariableDeclaration.class).get(1);
|
||||
ASTVariableDeclarator localVarDecl = localVar.getFirstChildOfType(ASTVariableDeclarator.class);
|
||||
Assert.assertEquals(Integer.TYPE, localVarDecl.getType());
|
||||
Assert.assertEquals(Integer.TYPE, switchExpression.getType());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ public class SwitchExpressions {
|
||||
default -> {
|
||||
int k = day * 2;
|
||||
int result = f(k);
|
||||
break result;
|
||||
yield result;
|
||||
}
|
||||
};
|
||||
System.out.printf("NumLetters: %d%n", numLetters);
|
||||
|
@ -1,36 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* @see <a href="https://openjdk.java.net/jeps/325">JEP 325: Switch Expressions (Preview)</a>
|
||||
*/
|
||||
public class SwitchExpressionsBreak {
|
||||
private static final int MONDAY = 1;
|
||||
private static final int TUESDAY = 2;
|
||||
private static final int WEDNESDAY = 3;
|
||||
private static final int THURSDAY = 4;
|
||||
private static final int FRIDAY = 5;
|
||||
private static final int SATURDAY = 6;
|
||||
private static final int SUNDAY = 7;
|
||||
|
||||
private static final int SIX = 6;
|
||||
|
||||
public static void main(String[] args) {
|
||||
int day = FRIDAY;
|
||||
|
||||
var numLetters = switch (day) {
|
||||
case MONDAY, FRIDAY, SUNDAY: break SwitchExpressionsBreak.SIX;
|
||||
case TUESDAY : break 7;
|
||||
case THURSDAY, SATURDAY : break 8;
|
||||
case WEDNESDAY : break 9;
|
||||
default : {
|
||||
int k = day * 2;
|
||||
int result = f(k);
|
||||
break result;
|
||||
}
|
||||
};
|
||||
System.out.printf("NumLetters: %d%n", numLetters);
|
||||
}
|
||||
|
||||
private static int f(int k) {
|
||||
return k*3;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user