diff options
author | Kalyan Prasad Tatavarthi | 2020-08-06 16:19:51 +0000 |
---|---|---|
committer | Kalyan Prasad Tatavarthi | 2020-08-07 12:00:57 +0000 |
commit | 8353e965720c053511312df9eb8fd1b6698bd4d2 (patch) | |
tree | 2a176b097ba73a161dcadbdd088f395088450097 | |
parent | deacf0d8fd46602b05df2535e9c6482f3d4cd0d1 (diff) | |
download | eclipse.jdt.core-8353e965720c053511312df9eb8fd1b6698bd4d2.tar.gz eclipse.jdt.core-8353e965720c053511312df9eb8fd1b6698bd4d2.tar.xz eclipse.jdt.core-8353e965720c053511312df9eb8fd1b6698bd4d2.zip |
Bug 565850 - [15] Provide offset location for "permits"Y20200809-1200Y20200808-1200Y20200807-1200P20200810-0350P20200810-0320P20200808-0230P20200807-1210P20200807-1200P20200807-1140
Change-Id: I30c22ddc2c93055b126e72ed1c74b78cf395389b
Signed-off-by: Kalyan Prasad Tatavarthi <kalyan_prasad@in.ibm.com>
6 files changed, 227 insertions, 5 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 1803fe4c23..5d5add713a 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 @@ -517,6 +517,97 @@ public class ASTConverter_15Test extends ConverterTestSetup { } } + public void testRecord011() throws CoreException { + if (!isJRE15) { + System.err.println("Test "+getName()+" requires a JRE 15"); + return; + } + String contents = + "public record X() {\n" + + " public X {\n" + + " System.out.println(\"no error\");\n" + + " }\n" + + "\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); + List<AbstractTypeDeclaration> types = compilationUnit.types(); + assertEquals("No. of Types is not 1", types.size(), 1); + AbstractTypeDeclaration type = types.get(0); + assertTrue("type not a Record", type instanceof RecordDeclaration); + RecordDeclaration recDecl = (RecordDeclaration)type; + int startPos = recDecl.getRestrictedIdentifierStartPosition(); + assertEquals("Start position of 'record' keyword is not 7", startPos, 7); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + + public void testClass001() throws CoreException { + if (!isJRE15) { + System.err.println("Test "+getName()+" requires a JRE 15"); + return; + } + String contents = + "public class X {\n" + + " public X() {\n" + + " System.out.println(\"no error\");\n" + + " }\n" + + "\n" + + "}\n"; + this.workingCopy = getWorkingCopy("/Converter_15/src/X.java", true/*resolve*/); + ASTNode node = buildAST( + contents, + this.workingCopy); + assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType()); + CompilationUnit compilationUnit = (CompilationUnit) node; + assertProblemsSize(compilationUnit, 0); + List<AbstractTypeDeclaration> types = compilationUnit.types(); + assertEquals("No. of Types is not 1", types.size(), 1); + AbstractTypeDeclaration type = types.get(0); + assertTrue("type not a type", type instanceof TypeDeclaration); + TypeDeclaration typeDecl = (TypeDeclaration)type; + assertTrue("type not a class", !typeDecl.isInterface()); + int startPos = typeDecl.getRestrictedIdentifierStartPosition(); + assertEquals("Restricter identifier position for class' is not -1", startPos, -1); + } + + public void testInterface001() throws CoreException { + if (!isJRE15) { + System.err.println("Test "+getName()+" requires a JRE 15"); + return; + } + String contents = + "public interface X {\n" + + "\n" + + "}\n"; + this.workingCopy = getWorkingCopy("/Converter_15/src/X.java", true/*resolve*/); + ASTNode node = buildAST( + contents, + this.workingCopy); + assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType()); + CompilationUnit compilationUnit = (CompilationUnit) node; + assertProblemsSize(compilationUnit, 0); + List<AbstractTypeDeclaration> types = compilationUnit.types(); + assertEquals("No. of Types is not 1", types.size(), 1); + AbstractTypeDeclaration type = types.get(0); + assertTrue("type not a type", type instanceof TypeDeclaration); + TypeDeclaration typeDecl = (TypeDeclaration)type; + assertTrue("type not an interface", typeDecl.isInterface()); + int startPos = typeDecl.getRestrictedIdentifierStartPosition(); + assertEquals("Restricter identifier position for interface' is not -1", startPos, -1); + } + public void testTextBlock001() throws JavaModelException { if (!isJRE15) { System.err.println("Test "+getName()+" requires a JRE 15"); @@ -738,7 +829,7 @@ public class ASTConverter_15Test extends ConverterTestSetup { assertEquals("Not a Type Declaration", ASTNode.TYPE_DECLARATION, node.getNodeType()); TypeDeclaration type = (TypeDeclaration)node; List modifiers = type.modifiers(); - assertEquals("Incorrect no of modfiers", 2, modifiers.size()); + assertEquals("Incorrect no of modifiers", 2, modifiers.size()); Modifier modifier = (Modifier) modifiers.get(1); assertSame("Incorrect modifier keyword", Modifier.ModifierKeyword.SEALED_KEYWORD, modifier.getKeyword()); List permittedTypes = type.permittedTypes(); @@ -792,4 +883,94 @@ public class ASTConverter_15Test extends ConverterTestSetup { javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); } } + + public void testSealed003() throws CoreException { + if (!isJRE15) { + System.err.println("Test "+getName()+" requires a JRE 15"); + return; + } + String contents = "public sealed interface X permits X1{\n" + + "\n" + + "}\n" + + "non-sealed interface X1 extends X {\n" + + "\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); + List<AbstractTypeDeclaration> types = compilationUnit.types(); + assertEquals("No. of Types is not 2", types.size(), 2); + AbstractTypeDeclaration type = types.get(0); + if (!type.getName().getIdentifier().equals("X")) { + type = types.get(1); + } + assertTrue("type not a type", type instanceof TypeDeclaration); + TypeDeclaration typeDecl = (TypeDeclaration)type; + assertTrue("type not an interface", typeDecl.isInterface()); + List modifiers = type.modifiers(); + assertEquals("Incorrect no of modifiers", 2, modifiers.size()); + Modifier modifier = (Modifier) modifiers.get(1); + assertSame("Incorrect modifier keyword", Modifier.ModifierKeyword.SEALED_KEYWORD, modifier.getKeyword()); + int startPos = modifier.getStartPosition(); + assertEquals("Restricter identifier position for sealed is not 7", startPos, contents.indexOf("sealed")); + startPos = typeDecl.getRestrictedIdentifierStartPosition(); + assertEquals("Restricter identifier position for permits is not 26", startPos, contents.indexOf("permits")); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + + public void testSealed004() throws CoreException { + if (!isJRE15) { + System.err.println("Test "+getName()+" requires a JRE 15"); + return; + } + String contents = "public sealed class X permits X1{\n" + + "\n" + + "}\n" + + "non-sealed class X1 extends X {\n" + + "\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); + List<AbstractTypeDeclaration> types = compilationUnit.types(); + assertEquals("No. of Types is not 2", types.size(), 2); + AbstractTypeDeclaration type = types.get(0); + if (!type.getName().getIdentifier().equals("X")) { + type = types.get(1); + } + assertTrue("type not a type", type instanceof TypeDeclaration); + TypeDeclaration typeDecl = (TypeDeclaration)type; + assertTrue("type not an class", !typeDecl.isInterface()); + List modifiers = type.modifiers(); + assertEquals("Incorrect no of modifiers", 2, modifiers.size()); + Modifier modifier = (Modifier) modifiers.get(1); + assertSame("Incorrect modifier keyword", Modifier.ModifierKeyword.SEALED_KEYWORD, modifier.getKeyword()); + int startPos = modifier.getStartPosition(); + assertEquals("Restricter identifier position for sealed is not 7", startPos, contents.indexOf("sealed")); + startPos = typeDecl.getRestrictedIdentifierStartPosition(); + assertEquals("Restricter identifier position for permits is not 26", startPos, contents.indexOf("permits")); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java index 2edf6a5414..9acea3d9fb 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java @@ -76,7 +76,7 @@ public class TypeDeclaration extends Statement implements ProblemSeverities, Ref public int maxFieldCount; public int declarationSourceStart; public int declarationSourceEnd; - public int restrictedIdentifierStart; // used only for records + public int restrictedIdentifierStart = -1; // used only for record and permits restricted keywords. public int bodyStart; public int bodyEnd; // doesn't include the trailing comment if any. public CompilationResult compilationResult; diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java index 829e9553c6..0f28d02192 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java @@ -2684,7 +2684,9 @@ private void consumeClassOrRecordHeaderName1(boolean isRecord) { // we want to keep the beginning position but get rid of the end position // it is only used for the ClassLiteralAccess positions. typeDecl.declarationSourceStart = this.intStack[this.intPtr--]; - typeDecl.restrictedIdentifierStart = typeDecl.declarationSourceStart; + if (isRecord) { + typeDecl.restrictedIdentifierStart = typeDecl.declarationSourceStart; + } this.intPtr--; // remove the end position of the class token typeDecl.modifiersSourceStart = this.intStack[this.intPtr--]; @@ -4745,6 +4747,7 @@ private void populatePermittedTypes() { //permitted types this.astPtr -= length; TypeDeclaration typeDecl = (TypeDeclaration) this.astStack[this.astPtr]; + typeDecl.restrictedIdentifierStart= this.intStack[this.intPtr--]; System.arraycopy( this.astStack, this.astPtr + 1, @@ -10281,6 +10284,7 @@ protected void consumeToken(int type) { case TokenNameuses: case TokenNameprovides: case TokenNameRestrictedIdentifierYield: + case TokenNameRestrictedIdentifierpermits: pushOnIntStack(this.scanner.startPosition); break; case TokenNameswitch : 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 60041161a6..b2828f6412 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 @@ -3312,6 +3312,9 @@ class ASTConverter { typeDecl.permittedTypes().add(convertType); } } + if (permittedTypes.length > 0) { + typeDecl.setRestrictedIdentifierStartPosition(typeDeclaration.restrictedIdentifierStart); + } } } buildBodyDeclarations(typeDeclaration, typeDecl, isInterface); diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/RecordDeclaration.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/RecordDeclaration.java index f1c49b0d9a..823eac5285 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/RecordDeclaration.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/RecordDeclaration.java @@ -459,7 +459,7 @@ public class RecordDeclaration extends AbstractTypeDeclaration { @Override int memSize() { - return super.memSize() + 8 * 4; + return super.memSize() + 4 * 4; } @Override diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeDeclaration.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeDeclaration.java index 260bca3d43..3cfe849f05 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeDeclaration.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeDeclaration.java @@ -495,6 +495,7 @@ public class TypeDeclaration extends AbstractTypeDeclaration { if (DOMASTUtil.isFeatureSupportedinAST(this.ast, Modifier.SEALED)) { result.permittedTypes().addAll( ASTNode.copySubtrees(target, permittedTypes())); + result.restrictedIdentifierStartPosition = getRestrictedIdentifierStartPosition(); } result.bodyDeclarations().addAll( ASTNode.copySubtrees(target, bodyDeclarations())); @@ -866,7 +867,8 @@ public class TypeDeclaration extends AbstractTypeDeclaration { @Override int memSize() { - return super.memSize() + 6 * 4; + // there are 7 fields that are either int or pointer and one boolean type + return super.memSize() + 1 + (7 * 4) ; } @Override @@ -883,5 +885,37 @@ public class TypeDeclaration extends AbstractTypeDeclaration { + (this.permittedTypes == null ? 0 : this.permittedTypes.listSize()) + this.bodyDeclarations.listSize(); } + + /** + * A character index into the original restricted identifier source string, or <code>-1</code> if no restricted + * identifier source position information is available for this node; <code>-1</code> by default. + */ + private int restrictedIdentifierStartPosition = -1; + + /** + * A character index into the original restricted identifier source string, or <code>-1</code> if no restricted + * identifier source position information is available for this node; <code>-1</code> by default. + * + * @noreference This method is not intended to be referenced by clients. + */ + public void setRestrictedIdentifierStartPosition(int restrictedIdentifierStartPosition) { + if (restrictedIdentifierStartPosition < 0) { + throw new IllegalArgumentException(); + } + // restrictedIdentifierStartPosition is not considered a structural property + // but we protect it nevertheless + checkModifiable(); + this.restrictedIdentifierStartPosition = restrictedIdentifierStartPosition; + } + + /** + * A character index into the original restricted identifier source string, or <code>-1</code> if no restricted + * identifier source position information is available for this node; <code>-1</code> by default. + * + * @noreference This method is not intended to be referenced by clients. + */ + public int getRestrictedIdentifierStartPosition() { + return this.restrictedIdentifierStartPosition; + } } |