diff options
| author | Noopur Gupta | 2019-03-06 17:43:37 +0000 |
|---|---|---|
| committer | Noopur Gupta | 2019-03-06 17:43:37 +0000 |
| commit | 3ccfaaed9e85323fc58c3622149cfd4691028aca (patch) | |
| tree | 25f27a9475afe5846e84da47b4b47ae633241ce8 | |
| parent | 5f38ded6c00993ba75f3834c25de97962dfef1cc (diff) | |
| download | eclipse.jdt.ui-3ccfaaed9e85323fc58c3622149cfd4691028aca.tar.gz eclipse.jdt.ui-3ccfaaed9e85323fc58c3622149cfd4691028aca.tar.xz eclipse.jdt.ui-3ccfaaed9e85323fc58c3622149cfd4691028aca.zip | |
Change-Id: I4de0f7c7a407be8c728664289b401af5fd6c46ae
17 files changed, 272 insertions, 63 deletions
diff --git a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/ImportReferencesCollector.java b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/ImportReferencesCollector.java index d42514b1dd..d52a84c7ae 100644 --- a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/ImportReferencesCollector.java +++ b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/ImportReferencesCollector.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -305,6 +309,10 @@ public class ImportReferencesCollector extends GenericVisitor { @Override public boolean visit(BreakStatement node) { + int apiLevel= node.getAST().apiLevel(); + if (apiLevel >= AST.JLS12) { + evalQualifyingExpression(node.getExpression(), null); + } return false; } diff --git a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/SharedASTProviderCore.java b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/SharedASTProviderCore.java index 7d0103c858..e7d346d16b 100644 --- a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/SharedASTProviderCore.java +++ b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/SharedASTProviderCore.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2018 IBM Corporation and others. + * Copyright (c) 2007, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -33,7 +37,7 @@ import org.eclipse.jdt.core.manipulation.CoreASTProvider; * <p>Clients can make the following assumptions about the AST: * <dl> * <li>the AST has a {@link ITypeRoot} as source: {@link CompilationUnit#getTypeRoot()} is not null.</li> - * <li>the {@link AST#apiLevel() AST API level} is {@link AST#JLS11 API level 11} or higher</li> + * <li>the {@link AST#apiLevel() AST API level} is {@link AST#JLS12 API level 12} or higher</li> * <li>the AST has bindings resolved ({@link AST#hasResolvedBindings()})</li> * <li>{@link AST#hasStatementsRecovery() statement} and {@link AST#hasBindingsRecovery() bindings} * recovery are enabled diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/core/manipulation/dom/ASTResolving.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/core/manipulation/dom/ASTResolving.java index f181b1633c..5f7d30a780 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/core/manipulation/dom/ASTResolving.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/core/manipulation/dom/ASTResolving.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -7,6 +7,10 @@ * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 + * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. * * Contributors: * IBM Corporation - initial API and implementation @@ -73,6 +77,7 @@ import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; import org.eclipse.jdt.core.dom.SuperConstructorInvocation; import org.eclipse.jdt.core.dom.SuperMethodInvocation; import org.eclipse.jdt.core.dom.SwitchCase; +import org.eclipse.jdt.core.dom.SwitchExpression; import org.eclipse.jdt.core.dom.SwitchStatement; import org.eclipse.jdt.core.dom.TagElement; import org.eclipse.jdt.core.dom.TryStatement; @@ -294,8 +299,15 @@ public class ASTResolving { } break; case ASTNode.SWITCH_CASE: - if (node.equals(((SwitchCase) parent).getExpression()) && parent.getParent() instanceof SwitchStatement) { - return ((SwitchStatement) parent.getParent()).getExpression().resolveTypeBinding(); + SwitchCase switchCase= (SwitchCase) parent; + if (node.equals(switchCase.getExpression()) || (switchCase.getAST().apiLevel() >= AST.JLS12 && switchCase.expressions().contains(node))) { + ASTNode caseParent= switchCase.getParent(); + if (caseParent instanceof SwitchStatement) { + return ((SwitchStatement) caseParent).getExpression().resolveTypeBinding(); + } + if (caseParent instanceof SwitchExpression) { + return ((SwitchExpression) caseParent).getExpression().resolveTypeBinding(); + } } break; case ASTNode.ASSERT_STATEMENT: diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/core/manipulation/dom/NecessaryParenthesesChecker.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/core/manipulation/dom/NecessaryParenthesesChecker.java index 0a66a7623d..426e48e905 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/core/manipulation/dom/NecessaryParenthesesChecker.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/core/manipulation/dom/NecessaryParenthesesChecker.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2018 IBM Corporation and others. + * Copyright (c) 2011, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation * Nikolay Metchev <nikolaymetchev@gmail.com> - [inline] Inline local variable with initializer generates assignment where left-hand side is not a variable - https://bugs.eclipse.org/394721 @@ -92,6 +96,7 @@ public class NecessaryParenthesesChecker { || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == SwitchStatement.EXPRESSION_PROPERTY || locationInParent == SwitchCase.EXPRESSION_PROPERTY + || locationInParent == SwitchCase.EXPRESSIONS2_PROPERTY || locationInParent == ArrayAccess.INDEX_PROPERTY || locationInParent == ThrowStatement.EXPRESSION_PROPERTY || locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/ASTFlattener.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/ASTFlattener.java index 6eef2273be..475d4cee6b 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/ASTFlattener.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/ASTFlattener.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -7,7 +7,11 @@ * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 - * + * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -47,6 +51,8 @@ public class ASTFlattener extends GenericVisitor { @Deprecated private static final int JLS9= AST.JLS9; + private static final int JLS12= AST.JLS12; + /** * The string buffer into which the serialized representation of the AST is * written. @@ -374,6 +380,10 @@ public class ASTFlattener extends GenericVisitor { this.fBuffer.append(" ");//$NON-NLS-1$ node.getLabel().accept(this); } + if (node.getAST().apiLevel() >= JLS12 && node.getExpression() != null) { + this.fBuffer.append(" ");//$NON-NLS-1$ + node.getExpression().accept(this); + } this.fBuffer.append(";");//$NON-NLS-1$ return false; } @@ -1528,36 +1538,78 @@ public class ASTFlattener extends GenericVisitor { return false; } - /* - * @see ASTVisitor#visit(SwitchCase) - */ @Override public boolean visit(SwitchCase node) { - if (node.isDefault()) { - this.fBuffer.append("default :");//$NON-NLS-1$ + if (node.getAST().apiLevel() >= JLS12) { + if (!node.expressions().isEmpty()) { + for (Iterator<Expression> it= node.expressions().iterator(); it.hasNext();) { + Expression caseExpr= it.next(); + if (node.isDefault()) { + if (node.isSwitchLabeledRule()) { + this.fBuffer.append("default ->");//$NON-NLS-1$ + } else { + this.fBuffer.append("default :");//$NON-NLS-1$ + } + } else { + this.fBuffer.append("case ");//$NON-NLS-1$ + caseExpr.accept(this); + if (it.hasNext()) { + this.fBuffer.append(", ");//$NON-NLS-1$ + } else { + if (node.isSwitchLabeledRule()) { + this.fBuffer.append("->");//$NON-NLS-1$ + } else { + this.fBuffer.append(":");//$NON-NLS-1$ + } + } + } + } + } } else { - this.fBuffer.append("case ");//$NON-NLS-1$ - node.getExpression().accept(this); - this.fBuffer.append(":");//$NON-NLS-1$ + if (node.isDefault()) { + this.fBuffer.append("default :");//$NON-NLS-1$ + } else { + this.fBuffer.append("case ");//$NON-NLS-1$ + node.getExpression().accept(this); + this.fBuffer.append(":");//$NON-NLS-1$ + } } return false; } - /* - * @see ASTVisitor#visit(SwitchStatement) - */ @Override public boolean visit(SwitchStatement node) { + visitSwitch(node); + return false; + } + + @Override + public boolean visit(SwitchExpression node) { + visitSwitch(node); + return false; + } + + private void visitSwitch(ASTNode node) { this.fBuffer.append("switch (");//$NON-NLS-1$ - node.getExpression().accept(this); + if (node instanceof SwitchExpression) { + ((SwitchExpression) node).getExpression().accept(this); + } else if (node instanceof SwitchStatement) { + ((SwitchStatement) node).getExpression().accept(this); + } this.fBuffer.append(") ");//$NON-NLS-1$ this.fBuffer.append("{");//$NON-NLS-1$ - for (Iterator<Statement> it= node.statements().iterator(); it.hasNext();) { - Statement s= it.next(); - s.accept(this); + if (node instanceof SwitchExpression) { + for (Iterator<Statement> it= ((SwitchExpression) node).statements().iterator(); it.hasNext();) { + Statement s= it.next(); + s.accept(this); + } + } else if (node instanceof SwitchStatement) { + for (Iterator<Statement> it= ((SwitchStatement) node).statements().iterator(); it.hasNext();) { + Statement s= it.next(); + s.accept(this); + } } - this.fBuffer.append("}");//$NON-NLS-1$ - return false; + this.fBuffer.append("}\n");//$NON-NLS-1$ } /* diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/ASTNodes.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/ASTNodes.java index 5026a45f6e..42ac0b0d9c 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/ASTNodes.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/ASTNodes.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation * Dmitry Stalnov (dstalnov@fusionone.com) - contributed fix for @@ -58,6 +62,7 @@ import org.eclipse.jdt.core.dom.ArrayInitializer; import org.eclipse.jdt.core.dom.ArrayType; import org.eclipse.jdt.core.dom.Assignment; import org.eclipse.jdt.core.dom.BodyDeclaration; +import org.eclipse.jdt.core.dom.BreakStatement; import org.eclipse.jdt.core.dom.CastExpression; import org.eclipse.jdt.core.dom.CharacterLiteral; import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor; @@ -399,7 +404,8 @@ public class ASTNodes { public static boolean isLabel(SimpleName name) { int parentType= name.getParent().getNodeType(); return parentType == ASTNode.LABELED_STATEMENT || - parentType == ASTNode.BREAK_STATEMENT || parentType != ASTNode.CONTINUE_STATEMENT; + (parentType == ASTNode.BREAK_STATEMENT && name.getLocationInParent() == BreakStatement.LABEL_PROPERTY) || + parentType != ASTNode.CONTINUE_STATEMENT; } public static boolean isStatic(BodyDeclaration declaration) { diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/GenericVisitor.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/GenericVisitor.java index 7c252a8751..a6fb915f0f 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/GenericVisitor.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/GenericVisitor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2017 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -7,6 +7,10 @@ * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 + * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. * * Contributors: * IBM Corporation - initial API and implementation @@ -382,6 +386,10 @@ public class GenericVisitor extends ASTVisitor { endVisitNode(node); } @Override + public void endVisit(SwitchExpression node) { + endVisitNode(node); + } + @Override public void endVisit(SwitchStatement node) { endVisitNode(node); } @@ -782,6 +790,10 @@ public class GenericVisitor extends ASTVisitor { return visitNode(node); } @Override + public boolean visit(SwitchExpression node) { + return visitNode(node); + } + @Override public boolean visit(SwitchStatement node) { return visitNode(node); } diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/HierarchicalASTVisitor.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/HierarchicalASTVisitor.java index 8ea0239e2b..574c9999be 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/HierarchicalASTVisitor.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/HierarchicalASTVisitor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2017 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -469,6 +473,16 @@ public abstract class HierarchicalASTVisitor extends ASTVisitor { endVisit((Expression)node); } + @Override + public boolean visit(SwitchExpression node) { + return visit((Expression)node); + } + + @Override + public void endVisit(SwitchExpression node) { + endVisit((Expression)node); + } + //---- Begin MethodReference Hierarchy ---------------------------------- public boolean visit(MethodReference node) { return visit((Expression)node); diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/IASTSharedValues.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/IASTSharedValues.java index 10d7cbec5f..44c2014780 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/IASTSharedValues.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/IASTSharedValues.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2016, 2018 IBM Corporation and others. + * Copyright (c) 2016, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -21,7 +25,7 @@ public interface IASTSharedValues { * This value is subject to change with every release. JDT-UI-internal code typically supports * the latest available {@link AST#apiLevel() AST level} exclusively. */ - public static final int SHARED_AST_LEVEL= AST.JLS11; + public static final int SHARED_AST_LEVEL= AST.JLS12; public static final boolean SHARED_AST_STATEMENT_RECOVERY= true; diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/ScopeAnalyzer.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/ScopeAnalyzer.java index 57cf10e593..f607d12f9e 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/ScopeAnalyzer.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/dom/ScopeAnalyzer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2017 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -18,6 +22,7 @@ import java.util.Collection; import java.util.HashSet; import java.util.List; +import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; @@ -42,9 +47,11 @@ import org.eclipse.jdt.core.dom.Modifier; import org.eclipse.jdt.core.dom.QualifiedName; import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.Statement; +import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; import org.eclipse.jdt.core.dom.SuperFieldAccess; import org.eclipse.jdt.core.dom.SuperMethodInvocation; import org.eclipse.jdt.core.dom.SwitchCase; +import org.eclipse.jdt.core.dom.SwitchExpression; import org.eclipse.jdt.core.dom.SwitchStatement; import org.eclipse.jdt.core.dom.Type; import org.eclipse.jdt.core.dom.TypeDeclarationStatement; @@ -375,8 +382,15 @@ public class ScopeAnalyzer { public IBinding[] getDeclarationsInScope(SimpleName selector, int flags) { try { // special case for switch on enum - if (selector.getLocationInParent() == SwitchCase.EXPRESSION_PROPERTY) { - ITypeBinding binding= ((SwitchStatement) selector.getParent().getParent()).getExpression().resolveTypeBinding(); + StructuralPropertyDescriptor locationInParent= selector.getLocationInParent(); + if (locationInParent == SwitchCase.EXPRESSION_PROPERTY || locationInParent == SwitchCase.EXPRESSIONS2_PROPERTY) { + ASTNode caseParent= selector.getParent().getParent(); + ITypeBinding binding= null; + if (caseParent instanceof SwitchStatement) { + binding= ((SwitchStatement) caseParent).getExpression().resolveTypeBinding(); + } else if (caseParent instanceof SwitchExpression) { + binding= ((SwitchExpression) caseParent).getExpression().resolveTypeBinding(); + } if (binding != null && binding.isEnum()) { return getEnumContants(binding); } @@ -466,8 +480,15 @@ public class ScopeAnalyzer { public boolean isDeclaredInScope(IBinding declaration, SimpleName selector, int flags) { try { // special case for switch on enum - if (selector.getLocationInParent() == SwitchCase.EXPRESSION_PROPERTY) { - ITypeBinding binding= ((SwitchStatement) selector.getParent().getParent()).getExpression().resolveTypeBinding(); + StructuralPropertyDescriptor locationInParent= selector.getLocationInParent(); + if (locationInParent == SwitchCase.EXPRESSION_PROPERTY || locationInParent == SwitchCase.EXPRESSIONS2_PROPERTY) { + ASTNode caseParent= selector.getParent().getParent(); + ITypeBinding binding= null; + if (caseParent instanceof SwitchStatement) { + binding= ((SwitchStatement) caseParent).getExpression().resolveTypeBinding(); + } else if (caseParent instanceof SwitchExpression) { + binding= ((SwitchExpression) caseParent).getExpression().resolveTypeBinding(); + } if (binding != null && binding.isEnum()) { return hasEnumContants(declaration, binding.getTypeDeclaration()); } @@ -698,9 +719,31 @@ public class ScopeAnalyzer { @Override public boolean visit(SwitchCase node) { // switch on enum allows to use enum constants without qualification - if (hasFlag(VARIABLES, fFlags) && !node.isDefault() && isInside(node.getExpression())) { - SwitchStatement switchStatement= (SwitchStatement) node.getParent(); - ITypeBinding binding= switchStatement.getExpression().resolveTypeBinding(); + if (hasFlag(VARIABLES, fFlags) && !node.isDefault()) { + if (node.getAST().apiLevel() >= AST.JLS12) { + List<Expression> expressions= node.expressions(); + for (Expression expression : expressions) { + visitExpression(node, expression); + } + } else { + Expression expression= node.getExpression(); + visitExpression(node, expression); + } + } + return false; + } + + private void visitExpression(SwitchCase node, Expression expression) { + if (isInside(expression)) { + ASTNode caseParent= node.getParent(); + ITypeBinding binding= null; + if (caseParent instanceof SwitchStatement) { + SwitchStatement switchStatement= (SwitchStatement) caseParent; + binding= switchStatement.getExpression().resolveTypeBinding(); + } else if (caseParent instanceof SwitchExpression) { + SwitchExpression switchExpression= (SwitchExpression) caseParent; + binding= switchExpression.getExpression().resolveTypeBinding(); + } if (binding != null && binding.isEnum()) { IVariableBinding[] declaredFields= binding.getDeclaredFields(); for (int i= 0; i < declaredFields.length; i++) { @@ -708,12 +751,11 @@ public class ScopeAnalyzer { if (curr.isEnumConstant()) { fBreak= fRequestor.acceptBinding(curr); if (fBreak) - return false; + return; } } } } - return false; } @Override diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/Checks.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/Checks.java index 5a4e34571b..bf09a487ce 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/Checks.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/Checks.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -48,6 +52,7 @@ import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.Signature; +import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.Annotation; import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; @@ -386,14 +391,28 @@ public class Checks { public static boolean isEnumCase(ASTNode node) { if (node instanceof SwitchCase) { final SwitchCase caze= (SwitchCase) node; - final Expression expression= caze.getExpression(); - if (expression instanceof Name) { - final Name name= (Name) expression; - final IBinding binding= name.resolveBinding(); - if (binding instanceof IVariableBinding) { - IVariableBinding variableBinding= (IVariableBinding) binding; - return variableBinding.isEnumConstant(); + if (node.getAST().apiLevel() >= AST.JLS12) { + List<Expression> expressions= caze.expressions(); + boolean isEnumConst= true; + for (Expression expression : expressions) { + isEnumConst= isEnumConst && isEnumConst(expression); } + return isEnumConst; + } else { + Expression expression= caze.getExpression(); + return isEnumConst(expression); + } + } + return false; + } + + private static boolean isEnumConst(final Expression expression) { + if (expression instanceof Name) { + final Name name= (Name) expression; + final IBinding binding= name.resolveBinding(); + if (binding instanceof IVariableBinding) { + IVariableBinding variableBinding= (IVariableBinding) binding; + return variableBinding.isEnumConstant(); } } return false; diff --git a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnit4TestFinder.java b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnit4TestFinder.java index 947e8ff315..588d1bd7d4 100644 --- a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnit4TestFinder.java +++ b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnit4TestFinder.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2018 IBM Corporation and others. + * Copyright (c) 2006, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * David Saff (saff@mit.edu) - initial API and implementation * (bug 102632: [JUnit] Support for JUnit 4.) @@ -208,7 +212,7 @@ public class JUnit4TestFinder implements ITestFinder { if (CoreTestSearchEngine.hasSuiteMethod(type)) { // since JUnit 4.3.1 return true; } - ASTParser parser= ASTParser.newParser(AST.JLS11); + ASTParser parser= ASTParser.newParser(AST.JLS12); /* TODO: When bug 156352 is fixed: parser.setProject(type.getJavaProject()); IBinding[] bindings= parser.createBindings(new IJavaElement[] { type }, monitor); diff --git a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnit5TestFinder.java b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnit5TestFinder.java index 6477fbb407..51a1aa656a 100644 --- a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnit5TestFinder.java +++ b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnit5TestFinder.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2016, 2018 IBM Corporation and others. + * Copyright (c) 2016, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -257,7 +261,7 @@ public class JUnit5TestFinder implements ITestFinder { if (CoreTestSearchEngine.hasSuiteMethod(type)) { // since JUnit 4.3.1 return true; } - ASTParser parser= ASTParser.newParser(AST.JLS11); + ASTParser parser= ASTParser.newParser(AST.JLS12); if (type.getCompilationUnit() != null) { parser.setSource(type.getCompilationUnit()); } else if (!isAvailable(type.getSourceRange())) { // class file with no source diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/CodeStyleFix.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/CodeStyleFix.java index 797520554b..81903ff6e2 100644 --- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/CodeStyleFix.java +++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/CodeStyleFix.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -165,7 +169,7 @@ public class CodeStyleFix extends CompilationUnitRewriteOperationsFix { return; StructuralPropertyDescriptor parentDescription= node.getLocationInParent(); - if (parentDescription == VariableDeclarationFragment.NAME_PROPERTY || parentDescription == SwitchCase.EXPRESSION_PROPERTY) + if (parentDescription == VariableDeclarationFragment.NAME_PROPERTY || parentDescription == SwitchCase.EXPRESSION_PROPERTY || parentDescription == SwitchCase.EXPRESSIONS2_PROPERTY) return; IBinding binding= node.resolveBinding(); diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractMethodAnalyzer.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractMethodAnalyzer.java index ea65fe137d..870e84e08a 100644 --- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractMethodAnalyzer.java +++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractMethodAnalyzer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation * Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056 @@ -265,7 +269,7 @@ import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider; ASTNode node= nodes[0]; if (node instanceof Type) { status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_type_reference, JavaStatusContext.create(fCUnit, node)); - } else if (node.getLocationInParent() == SwitchCase.EXPRESSION_PROPERTY) { + } else if (node.getLocationInParent() == SwitchCase.EXPRESSION_PROPERTY || node.getLocationInParent() == SwitchCase.EXPRESSIONS2_PROPERTY) { status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_switch_case, JavaStatusContext.create(fCUnit, node)); } else if (node instanceof Annotation || ASTNodes.getParent(node, Annotation.class) != null) { status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_from_annotation, JavaStatusContext.create(fCUnit, node)); diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/UnresolvedElementsSubProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/UnresolvedElementsSubProcessor.java index 47c6873f0d..660b329428 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/UnresolvedElementsSubProcessor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/UnresolvedElementsSubProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation * Renaud Waldura <renaud+eclipse@waldura.com> - New class/interface with wizard @@ -101,6 +105,7 @@ import org.eclipse.jdt.core.dom.SuperConstructorInvocation; import org.eclipse.jdt.core.dom.SuperFieldAccess; import org.eclipse.jdt.core.dom.SuperMethodInvocation; import org.eclipse.jdt.core.dom.SwitchCase; +import org.eclipse.jdt.core.dom.SwitchExpression; import org.eclipse.jdt.core.dom.SwitchStatement; import org.eclipse.jdt.core.dom.ThisExpression; import org.eclipse.jdt.core.dom.ThrowStatement; @@ -234,8 +239,14 @@ public class UnresolvedElementsSubProcessor { typeKind= TypeKinds.REF_TYPES; suggestVariableProposals= false; } - } else if (locationInParent == SwitchCase.EXPRESSION_PROPERTY) { - ITypeBinding switchExp= ((SwitchStatement) node.getParent().getParent()).getExpression().resolveTypeBinding(); + } else if (locationInParent == SwitchCase.EXPRESSION_PROPERTY || locationInParent == SwitchCase.EXPRESSIONS2_PROPERTY) { + ASTNode caseParent= node.getParent().getParent(); + ITypeBinding switchExp= null; + if (caseParent instanceof SwitchStatement) { + switchExp= ((SwitchStatement) caseParent).getExpression().resolveTypeBinding(); + } else if (caseParent instanceof SwitchExpression) { + switchExp= ((SwitchExpression) caseParent).getExpression().resolveTypeBinding(); + } if (switchExp != null && switchExp.isEnum()) { binding= switchExp; } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/SharedASTProvider.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/SharedASTProvider.java index b6c9fa4935..729e5eb5ae 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/SharedASTProvider.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/SharedASTProvider.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2018 IBM Corporation and others. + * Copyright (c) 2007, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -8,6 +8,10 @@ * * SPDX-License-Identifier: EPL-2.0 * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -33,7 +37,7 @@ import org.eclipse.jdt.core.manipulation.CoreASTProvider; * <p>Clients can make the following assumptions about the AST: * <dl> * <li>the AST has a {@link ITypeRoot} as source: {@link CompilationUnit#getTypeRoot()} is not null.</li> - * <li>the {@link AST#apiLevel() AST API level} is {@link AST#JLS11 API level 11} or higher</li> + * <li>the {@link AST#apiLevel() AST API level} is {@link AST#JLS12 API level 12} or higher</li> * <li>the AST has bindings resolved ({@link AST#hasResolvedBindings()})</li> * <li>{@link AST#hasStatementsRecovery() statement} and {@link AST#hasBindingsRecovery() bindings} * recovery are enabled |
