Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSarika Sinha2020-11-23 07:42:07 +0000
committerSarika Sinha2020-11-23 07:42:07 +0000
commit92d33b49059ac5880a87d8b64016293328c56a92 (patch)
treea1cb3730407f30dcad66b67a8996e75ddbf37787
parente630d940429f56b09511af009b4734afe9ef6bf9 (diff)
downloadeclipse.jdt.core-I20201201-1800.tar.gz
eclipse.jdt.core-I20201201-1800.tar.xz
eclipse.jdt.core-I20201201-1800.zip
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java44
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/NaiveASTFlattener.java4
2 files changed, 46 insertions, 2 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java
index c38256a95a..494242cd0a 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java
@@ -893,6 +893,50 @@ public class ASTConverter_15Test extends ConverterTestSetup {
}
}
+ public void testPatternInstanceOfExpression003() throws JavaModelException {
+ if (!isJRE15) {
+ System.err.println("Test "+getName()+" requires a JRE 15");
+ return;
+ }
+ String contents =
+ "public class X {\n" +
+ " public String test001(Object o) {\n" +
+ " if (o instanceof String s){\n" +
+ " System.out.println(s);\n" +
+ " return s;\n" +
+ " }\n" +
+ " return null;\n" +
+ " }\n" +
+ "}" ;
+ this.workingCopy = getWorkingCopy("/Converter_15/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, 0, 0);
+ assertEquals("Not an if statement", ASTNode.IF_STATEMENT, node.getNodeType());
+ IfStatement ifStatement = (IfStatement) node;
+ Expression expression = ifStatement.getExpression();
+ checkSourceRange(expression, "o instanceof String s", contents);
+ assertEquals("Not an instanceof expression", ASTNode.INSTANCEOF_EXPRESSION, expression.getNodeType());
+ InstanceofExpression instanceofExpression = (InstanceofExpression) expression;
+ SimpleName var = instanceofExpression.getPatternVariable();
+ checkSourceRange(var, "s", contents);
+ String instanceofExpressionString = instanceofExpression.toString();
+ assertEquals("o instanceof String s", instanceofExpressionString);
+ }finally {
+ javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
+ }
+ }
+
public void testSealed001() throws CoreException {
if (!isJRE15) {
System.err.println("Test "+getName()+" requires a JRE 15");
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 06575f5a47..adfd67b652 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
@@ -874,10 +874,10 @@ public class NaiveASTFlattener extends ASTVisitor {
public boolean visit(InstanceofExpression node) {
node.getLeftOperand().accept(this);
this.buffer.append(" instanceof ");//$NON-NLS-1$
+ node.getRightOperand().accept(this);
if (DOMASTUtil.isInstanceofExpressionPatternSupported(node.getAST()) && node.getPatternVariable()!= null) {
+ this.buffer.append(" "); //$NON-NLS-1$
node.getPatternVariable().accept(this);
- } else {
- node.getRightOperand().accept(this);
}
return false;
}

Back to the top