diff options
author | Sarika Sinha | 2020-09-09 10:15:09 +0000 |
---|---|---|
committer | Sarika Sinha | 2020-09-09 11:18:03 +0000 |
commit | 4e604f9a678a1911e2601e4482365185b17fe6f1 (patch) | |
tree | 1f8a501fa74c8fe417147540e31680bc097127fc | |
parent | e228581ad4ceeeff9172ca1936e5981c4ca32e0f (diff) | |
download | eclipse.jdt.core-4e604f9a678a1911e2601e4482365185b17fe6f1.tar.gz eclipse.jdt.core-4e604f9a678a1911e2601e4482365185b17fe6f1.tar.xz eclipse.jdt.core-4e604f9a678a1911e2601e4482365185b17fe6f1.zip |
Bug 566703 - [dom ast] NullPointerException on InstanceofExpression when
preview features are enabled
Change-Id: I269e52e884a51ad6de08eb05063c2399db64bb95
-rw-r--r-- | org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter_15Test.java | 63 |
1 files changed, 53 insertions, 10 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 f3a66c5d99..800823c577 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 @@ -37,6 +37,8 @@ import org.eclipse.jdt.core.dom.IBinding; import org.eclipse.jdt.core.dom.IMethodBinding; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.IVariableBinding; +import org.eclipse.jdt.core.dom.IfStatement; +import org.eclipse.jdt.core.dom.InstanceofExpression; import org.eclipse.jdt.core.dom.MarkerAnnotation; import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.Modifier; @@ -791,16 +793,57 @@ public class ASTConverter_15Test extends ConverterTestSetup { CompilationUnit compilationUnit = (CompilationUnit) node; assertProblemsSize(compilationUnit, 0); node = getASTNode(compilationUnit, 0, 0, 0); - assertEquals("Text block statement", node.getNodeType(), ASTNode.VARIABLE_DECLARATION_STATEMENT); - List fragments = ((VariableDeclarationStatement) node).fragments(); - assertEquals("Incorrect no of fragments", 1, fragments.size()); - node = (ASTNode) fragments.get(0); - assertEquals("Switch statement", node.getNodeType(), ASTNode.VARIABLE_DECLARATION_FRAGMENT); - VariableDeclarationFragment fragment = (VariableDeclarationFragment) node; - Expression initializer = fragment.getInitializer(); - assertTrue("Initializer is not a TextBlock", initializer instanceof TextBlock); - ITypeBinding binding = initializer.resolveTypeBinding(); - assertNotNull("No binding", binding); + 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; + SingleVariableDeclaration var = instanceofExpression.getPatternVariable(); + checkSourceRange(var, "String s", contents); + }finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + + public void _testPatternInstanceOfExpression002() 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){\n" + + " String s = (String)o;\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", contents); + assertEquals("Not an instanceof expression", ASTNode.INSTANCEOF_EXPRESSION, expression.getNodeType()); + InstanceofExpression instanceofExpression = (InstanceofExpression) expression; + SingleVariableDeclaration var = instanceofExpression.getPatternVariable(); + assertNull(var); }finally { javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); } |