Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSarika Sinha2019-03-13 10:30:16 -0400
committerManoj Palat2019-03-13 10:30:16 -0400
commit43541b77de66d1f23001f9a275cadab89c71232a (patch)
tree0572e230ba0e26e34faabae83ca5a019704b8f0e
parent27bd552dc69e5f85a45a8046d2287ad5a183fcfc (diff)
downloadeclipse.jdt.core-43541b77de66d1f23001f9a275cadab89c71232a.tar.gz
eclipse.jdt.core-43541b77de66d1f23001f9a275cadab89c71232a.tar.xz
eclipse.jdt.core-43541b77de66d1f23001f9a275cadab89c71232a.zip
Bug 544748 - [12] [dom ast] Flag implicit break statement
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java55
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java2
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java18
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java2
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/BreakStatement.java33
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java9
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java3
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java10
8 files changed, 128 insertions, 4 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java
index f7637c7ed0..8b0dfef6dc 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java
@@ -110,6 +110,7 @@ public class ASTConverter12Test extends ConverterTestSetup {
javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
}
}
+
public void test0002() throws JavaModelException {
String contents =
"public class X {\n" +
@@ -162,4 +163,58 @@ public class ASTConverter12Test extends ConverterTestSetup {
javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
}
}
+
+ /* test implicit break statement */
+
+ public void test0003() throws JavaModelException {
+ String contents =
+ "public class X {\n" +
+ " static enum Day {MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY, SATURDAY,SUNDAY}\n" +
+ " String aa(Day day) throws Exception {\n" +
+ " var today = \"\";\n" +
+ " switch (day) {\n" +
+ " case SATURDAY,SUNDAY ->\n" +
+ " today=\"Weekend\";\n" +
+ " case MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY ->\n" +
+ " today=\"Working\";\n" +
+ " default ->\n" +
+ " throw new Exception(\"Invalid day: \" + day.name());\n" +
+ " }\n" +
+ " return today;\n" +
+ " }\n" +
+ " \n" +
+ " String bb(Day day) throws Exception {\n" +
+ " String today = \"\";\n" +
+ " today = switch (day) {\n" +
+ " case SATURDAY,SUNDAY:\n" +
+ " break \"Weekend day\";\n" +
+ " case MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY:\n" +
+ " break \"Week day\";\n" +
+ " default:\n" +
+ " break \"Any day\";\n" +
+ " };\n" +
+ " return today;\n" +
+ " }\n" +
+ "}" ;
+ this.workingCopy = getWorkingCopy("/Converter12/src/X.java", true/*resolve*/);
+ IJavaProject javaProject = this.workingCopy.getJavaProject();
+ String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true);
+ try {
+ javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
+ javaProject.setOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
+ ASTNode node = buildAST(
+ contents,
+ this.workingCopy);
+ assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
+ CompilationUnit compilationUnit = (CompilationUnit) node;
+ assertProblemsSize(compilationUnit, 0);
+ node = getASTNode(compilationUnit, 0, 1, 1);
+ assertEquals("Switch statement", node.getNodeType(), ASTNode.SWITCH_STATEMENT);
+ SwitchStatement switchStatement = (SwitchStatement) node;
+ checkSourceRange((Statement) switchStatement.statements().get(0), "case SATURDAY,SUNDAY ->", contents);
+
+ } finally {
+ javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
+ }
+ }
}
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java
index 0cd3fd6b1f..632a7354c4 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java
@@ -1451,7 +1451,7 @@ public final class AST {
/**
* Creates an unparented break statement node owned by this AST.
- * The break statement has no label.
+ * The break statement has no label/identifier/expression and is not implicit.
*
* @return a new unparented break statement node
*/
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java
index 6d0a464a10..3bf50287af 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java
@@ -1290,7 +1290,17 @@ class ASTConverter {
public BreakStatement convert(org.eclipse.jdt.internal.compiler.ast.BreakStatement statement) {
BreakStatement breakStatement = new BreakStatement(this.ast);
- breakStatement.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 1);
+ if (this.ast.apiLevel >= AST.JLS12_INTERNAL) {
+ breakStatement.setImplicit(statement.isImplicit);
+ if (statement.isImplicit) {
+ breakStatement.setSourceRange(statement.sourceEnd -1, 0);
+ } else {
+ breakStatement.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 1);
+ }
+ }
+ else {
+ breakStatement.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 1);
+ }
if (statement.label != null) {
final SimpleName name = new SimpleName(this.ast);
name.internalSetIdentifier(new String(statement.label));
@@ -1300,6 +1310,12 @@ class ASTConverter {
else if (statement.expression != null && this.ast.apiLevel >= AST.JLS12_INTERNAL) {
final Expression expression= convert(statement.expression);
breakStatement.setExpression(expression);
+ int sourceEnd = statement.sourceEnd;
+ if (sourceEnd == -1) {
+ breakStatement.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 2);
+ } else {
+ breakStatement.setSourceRange(statement.sourceStart, sourceEnd - statement.sourceStart + 1);
+ }
}
return breakStatement;
}
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java
index 1e0259ef5f..0da2da76a8 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java
@@ -489,7 +489,7 @@ public class ASTMatcher {
}
BreakStatement o = (BreakStatement) other;
return ( node.getAST().apiLevel >= AST.JLS12_INTERNAL && node.getExpression() != null
- ? safeSubtreeMatch(node.getExpression(), o.getExpression())
+ ? safeSubtreeMatch(node.getExpression(), o.getExpression()) && node.isImplicit() == o.isImplicit()
: safeSubtreeMatch(node.getLabel(), o.getLabel()));
}
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/BreakStatement.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/BreakStatement.java
index 2441ce01dd..393a730038 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/BreakStatement.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/BreakStatement.java
@@ -65,6 +65,11 @@ public class BreakStatement extends Statement {
* or null if uninitialized.
*/
private static final List PROPERTY_DESCRIPTORS_12;
+
+ /**
+ * <code>true</code> indicates implicit and <code>false</code> indicates not implicit.
+ */
+ private boolean isImplicit = false;
static {
List properyList = new ArrayList(2);
@@ -109,7 +114,7 @@ public class BreakStatement extends Statement {
/**
* Creates a new unparented break statement node owned by the given
- * AST. By default, the break statement has no label.
+ * AST. By default, the break statement has no label/identifier/expression and is not implicit.
* <p>
* N.B. This constructor is package-private.
* </p>
@@ -245,6 +250,32 @@ public class BreakStatement extends Statement {
postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
}
+ /**
+ * Gets the isImplicit of this break statement as <code>true</code> or <code>false</code>.
+ *<code>true</code> indicates implicit and <code>false</code> indicates not implicit.
+ *
+ * @return isImplicit <code>true</code> or <code>false</code>
+ * @exception UnsupportedOperationException if this operation is used below JLS12
+ * @since 3.17 BETA_JAVA_12
+ */
+ public boolean isImplicit() {
+ unsupportedBelow12();
+ return this.isImplicit;
+ }
+
+ /**
+ * Sets the isImplicit of this break statement as <code>true</code> or <code>false</code>.
+ * <code>true</code> indicates implicit and <code>false</code> indicates not implicit.
+
+ * @param isImplicit <code>true</code> or <code>false</code>
+ * @exception UnsupportedOperationException if this operation is used below JLS12
+ * @since 3.17 BETA_JAVA_12
+ */
+ public void setImplicit(boolean isImplicit) {
+ unsupportedBelow12();
+ this.isImplicit = isImplicit;
+ }
+
@Override
int memSize() {
return super.memSize() + 2 * 4;
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java
index 001fc1fd7f..cb0ef37174 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java
@@ -464,12 +464,21 @@ public class NaiveASTFlattener extends ASTVisitor {
@Override
public boolean visit(BreakStatement node) {
+ if (node.getAST().apiLevel() >= JLS12 && node.isImplicit()) {
+ return false;
+ }
printIndent();
this.buffer.append("break");//$NON-NLS-1$
if (node.getLabel() != null) {
this.buffer.append(" ");//$NON-NLS-1$
node.getLabel().accept(this);
}
+ if (node.getAST().apiLevel() >= JLS12) {
+ if (node.getExpression() != null) {
+ this.buffer.append(" ");//$NON-NLS-1$
+ node.getExpression().accept(this);
+ }
+ }
this.buffer.append(";\n");//$NON-NLS-1$
return false;
}
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java
index ead61a2cc7..0ef10c03ce 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java
@@ -2573,6 +2573,9 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
try {
int offset= getScanner().getTokenEndOffset(TerminalTokens.TokenNamebreak, node.getStartPosition());
rewriteNode(node, BreakStatement.LABEL_PROPERTY, offset, ASTRewriteFormatter.SPACE); // space between break and label
+ if (node.getAST().apiLevel() >= JLS12_INTERNAL) {
+ rewriteNode(node, BreakStatement.EXPRESSION_PROPERTY, offset, ASTRewriteFormatter.SPACE); // space between break and label
+ }
} catch (CoreException e) {
handleException(e);
}
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java
index 8a380d2c4c..d4c7a796a1 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java
@@ -366,12 +366,22 @@ public class ASTRewriteFlattener extends ASTVisitor {
@Override
public boolean visit(BreakStatement node) {
+ if (node.getAST().apiLevel() >= JLS12 && node.isImplicit()) {
+ return false;
+ }
this.result.append("break"); //$NON-NLS-1$
ASTNode label= getChildNode(node, BreakStatement.LABEL_PROPERTY);
if (label != null) {
this.result.append(' ');
label.accept(this);
}
+ if (node.getAST().apiLevel() >= JLS12) {
+ ASTNode expression = getChildNode(node, BreakStatement.EXPRESSION_PROPERTY);
+ if (expression != null ) {
+ this.result.append(' ');
+ expression.accept(this);
+ }
+ }
this.result.append(';');
return false;
}

Back to the top