Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSarika Sinha2019-03-19 00:18:53 -0400
committerSarika Sinha2019-03-19 00:18:53 -0400
commit902a54ea511d65383bbb81e4e688af3e167b06d1 (patch)
treec4f62361b40c58dfc1122e8f979f530cdd3d04b8
parent02d5a930beb4813367fc08986e6c2813a4f82fe0 (diff)
downloadeclipse.jdt.core-902a54ea511d65383bbb81e4e688af3e167b06d1.tar.gz
eclipse.jdt.core-902a54ea511d65383bbb81e4e688af3e167b06d1.tar.xz
eclipse.jdt.core-902a54ea511d65383bbb81e4e688af3e167b06d1.zip
Bug 545497 - [12] AST Rewrite ignores the optional expression if
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java6
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java6
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java7
3 files changed, 13 insertions, 6 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java
index 6abaae5768..ecb94b013c 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java
@@ -781,7 +781,7 @@ public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest {
assertEqualString(preview, buf.toString());
}
@SuppressWarnings("rawtypes")
- public void _testSwitchExpressions_05_since_12() throws Exception {
+ public void testSwitchExpressions_05_since_12() throws Exception {
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
StringBuilder builder= new StringBuilder();
builder.append("package test1;\n");
@@ -833,6 +833,7 @@ public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest {
listRewrite.insertAt(statement1, 3, null);
}
+ // Expected output is not ideal due to formatting issue Bug 545439
String preview= evaluateRewrite(cu, rewrite);
builder= new StringBuilder();
builder.append("package test1;\n");
@@ -840,7 +841,8 @@ public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest {
builder.append(" public String foo(int i) {\n" +
" String ret = switch(i%2) {\n" +
" case 0 : \"even\";\n" +
- " case 1 : \"odd\";\n" +
+ " case 1:\n" +
+ " \"odd\";\n" +
" default : \"\";\n" +
" };\n" +
" return ret;");
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 cb0ef37174..849acb880d 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,11 +464,13 @@ public class NaiveASTFlattener extends ASTVisitor {
@Override
public boolean visit(BreakStatement node) {
- if (node.getAST().apiLevel() >= JLS12 && node.isImplicit()) {
+ if (node.getAST().apiLevel() >= JLS12 && node.isImplicit() && node.getExpression() == null) {
return false;
}
printIndent();
- this.buffer.append("break");//$NON-NLS-1$
+ if (node.getAST().apiLevel() < JLS12 || (node.getAST().apiLevel() >= JLS12 && !node.isImplicit())) {
+ this.buffer.append("break"); //$NON-NLS-1$
+ }
if (node.getLabel() != null) {
this.buffer.append(" ");//$NON-NLS-1$
node.getLabel().accept(this);
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 98f0f4807e..94e85132db 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,10 +366,13 @@ public class ASTRewriteFlattener extends ASTVisitor {
@Override
public boolean visit(BreakStatement node) {
- if (node.getAST().apiLevel() >= JLS12 && node.isImplicit()) {
+ if (node.getAST().apiLevel() >= JLS12 && node.isImplicit() && node.getExpression() == null) {
return false;
}
- this.result.append("break"); //$NON-NLS-1$
+
+ if (node.getAST().apiLevel() < JLS12 || (node.getAST().apiLevel() >= JLS12 && !node.isImplicit())) {
+ this.result.append("break"); //$NON-NLS-1$
+ }
ASTNode label= getChildNode(node, BreakStatement.LABEL_PROPERTY);
if (label != null) {
this.result.append(' ');

Back to the top