diff options
author | Vikas Chandra | 2020-07-16 12:14:05 +0000 |
---|---|---|
committer | Vikas Chandra | 2020-07-16 12:15:10 +0000 |
commit | c9c562d4e975236e2d0ab29a02be2fa125733e1c (patch) | |
tree | 46e37ca6289cb2b786f1c3e27fdb096dd7a252c4 | |
parent | 598a8546c94573c0beb87f370ed82ecb958f3671 (diff) | |
download | eclipse.jdt.core-c9c562d4e975236e2d0ab29a02be2fa125733e1c.tar.gz eclipse.jdt.core-c9c562d4e975236e2d0ab29a02be2fa125733e1c.tar.xz eclipse.jdt.core-c9c562d4e975236e2d0ab29a02be2fa125733e1c.zip |
This reverts commit b9d03a730db479a0609ca2e5c726b7b99d02241e.
The earlier behavior was correct
Change-Id: I991bf821e4cfa25da59e4cf56f36d6fe75e694a3
4 files changed, 849 insertions, 798 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugs14Tests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugs14Tests.java index 1f31677b07..34fe476f66 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugs14Tests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugs14Tests.java @@ -13,9 +13,11 @@ package org.eclipse.jdt.core.tests.model; import java.io.IOException; +import java.util.Map; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IField; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; @@ -27,6 +29,11 @@ import org.eclipse.jdt.core.search.SearchEngine; import org.eclipse.jdt.core.search.SearchMatch; import org.eclipse.jdt.core.search.TypeReferenceMatch; import org.eclipse.jdt.internal.core.LocalVariable; +import org.eclipse.jdt.internal.core.ResolvedSourceField; +import org.eclipse.jdt.internal.core.ResolvedSourceType; +import org.eclipse.jdt.internal.core.SourceField; +import org.eclipse.jdt.internal.core.SourceMethod; +import org.eclipse.jdt.internal.core.SourceType; import junit.framework.Test; @@ -139,7 +146,797 @@ public class JavaSearchBugs14Tests extends AbstractJavaSearchTests { this.resultCollector.showAccuracy(true); } + // 0 reference of the component in compact constructor + public void testBug558812_001() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point {\n" + + " }\n" + + "}\n" + ); + IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies + String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); + try { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); + search("comp_", FIELD, REFERENCES); + assertSearchResults(""); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + + //1 reference of the component in compact constructor + public void testBug558812_002() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point {\n" + + " comp_=11;\n" + + " }\n" + + "}\n" + ); + IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies + String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); + try { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); + search("comp_", FIELD, REFERENCES); + assertSearchResults("src/X.java Point(int) [comp_] EXACT_MATCH"); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + + //2 reference of the component in compact constructor + public void testBug558812_003() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point {\n" + + " comp_=11;\n" + + " this.comp_=11;\n" + + " }\n" + + "}\n" + ); + IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies + String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); + try { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); + search("comp_", FIELD, REFERENCES); + assertSearchResults("src/X.java Point(int) [comp_] EXACT_MATCH\n" + + "src/X.java Point(int) [comp_] EXACT_MATCH"); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + + //3 reference of the component in compact constructor + public void testBug558812_004() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point {\n" + + " comp_=11;\n" + + " this.comp_=comp_;\n" + + " }\n" + + "}\n" + ); + IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies + String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); + try { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); + search("comp_", FIELD, REFERENCES); + assertSearchResults("src/X.java Point(int) [comp_] EXACT_MATCH\n" + + "src/X.java Point(int) [comp_] EXACT_MATCH\n" + + "src/X.java Point(int) [comp_] EXACT_MATCH"); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + + //0 reference of the component in canonical constructor + public void testBug558812_005() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point (int a) {\n" + + " }\n" + + "}\n" + ); + IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies + String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); + try { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); + search("comp_", FIELD, REFERENCES); + assertSearchResults(""); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + + //1 reference of the component in canonical constructor + public void testBug558812_006() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point (int a) {\n" + + " comp_=11;\n" + + " }\n" + + "}\n" + ); + IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies + String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); + try { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); + search("comp_", FIELD, REFERENCES); + assertSearchResults("src/X.java Point(int) [comp_] EXACT_MATCH"); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + + //1 reference of the component in canonical constructor - part2 + public void testBug558812_007() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point (int a) {\n" + + " // comp_=11;\n" + + " this.comp_=a;\n" + + " }\n" + + "}\n" + ); + IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies + String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); + try { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); + search("comp_", FIELD, REFERENCES); + assertSearchResults("src/X.java Point(int) [comp_] EXACT_MATCH"); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + + //1 reference of the component in compact constructor - clashing method parameter + public void testBug558812_008() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point {\n" + + " comp_=11;\n" + + " }\n" + + "public void method ( int comp_) { \n"+ + "} \n"+ + "}\n" + ); + IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies + String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); + try { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); + search("comp_", FIELD, REFERENCES); + assertSearchResults("src/X.java Point(int) [comp_] EXACT_MATCH"); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + + + //1 reference of the component in compact constructor - clashing method's local variable + public void testBug558812_009() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point {\n" + + " comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int comp_=11;\n" + + "} \n"+ + "}\n" + ); + IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies + String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); + try { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); + search("comp_", FIELD, REFERENCES); + assertSearchResults("src/X.java Point(int) [comp_] EXACT_MATCH"); + } finally { + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); + } + } + + // more test case + // test case of comp_ selection in compact constructor + // selection - select record type + public void testBug558812_010() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record /* here*/Point(int comp_) { \n" + + " public Point {\n" + + " comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=11;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here*/Point"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + //TODO: check if record + } + + //selection - select local field in a method in record + public void testBug558812_011() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record /* here*/Point(int comp_) { \n" + + " public Point {\n" + + " comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int /* here*/compp_=11;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here*/compp_"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof LocalVariable); + + } + + //selection - select local field in a compact constructor in record + public void testBug558812_012() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record /* here*/Point(int comp_) { \n" + + " public Point {\n" + + " /* here*/comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=11;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here*/comp_"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof LocalVariable); + + } + + //selection - select local field in a compact constructor in record + public void testBug558812_013() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record /* here*/Point(int comp_) { \n" + + " public Point {\n" + + " /* here*/this.comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=11;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here*/this.comp_"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof ResolvedSourceField); + } + + //selection - select field in a method in record ( using this) + public void testBug558812_014() throws CoreException { + + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record /* here*/Point(int comp_) { \n" + + " public Point {\n" + + " /* here*/comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=/* here*/this.comp_;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here*/this.comp_"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof ResolvedSourceField); + } + + //selection - select field in a method in record ( without this) + public void testBug558812_015() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record /* here*/Point(int comp_) { \n" + + " public Point {\n" + + " comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=/* here2*/comp_;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here2*/comp_"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof ResolvedSourceField); + ResolvedSourceField sm = (ResolvedSourceField)elements[0]; + IJavaElement parent = sm.getParent(); + SourceType st = (SourceType)parent; + assertTrue(st.isRecord()); + } + + public void testBug558812_016() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point {\n" + + " /*here2*/comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=comp_;\n" + + " int compp2_=comp_;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/*here2*/comp_"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof LocalVariable); + search(elements[0], REFERENCES, EXACT_RULE); + assertSearchResults( + "src/X.java Point(int) [comp_] EXACT_MATCH\n" + + "src/X.java void Point.method() [comp_] EXACT_MATCH\n" + + "src/X.java void Point.method() [comp_] EXACT_MATCH"); + } + public void testBug558812_016a() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point {\n" + + " comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=comp_;\n" + + " int compp2_=comp_;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "comp_"; + int start = str.lastIndexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof IField); + search(elements[0], REFERENCES, EXACT_RULE); + assertSearchResults( + "src/X.java Point(int) [comp_] EXACT_MATCH\n" + + "src/X.java void Point.method() [comp_] EXACT_MATCH\n" + + "src/X.java void Point.method() [comp_] EXACT_MATCH"); + } + + public void testBug558812_017() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point {\n" + + " /*here2*/comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=comp_;\n" + + " int compp2_=comp_;\n" + + " int compp3_=this.comp_;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/*here2*/comp_"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof LocalVariable); + search(elements[0], REFERENCES, EXACT_RULE); + assertSearchResults( + "src/X.java Point(int) [comp_] EXACT_MATCH\n" + + "src/X.java void Point.method() [comp_] EXACT_MATCH\n" + + "src/X.java void Point.method() [comp_] EXACT_MATCH\n" + + "src/X.java void Point.method() [comp_] EXACT_MATCH"); + } + public void testBug558812_017a() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point {\n" + + " comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=comp_;\n" + + " int compp2_=comp_;\n" + + " int compp3_=this.comp_;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "comp_"; + int start = str.lastIndexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof IField); + search(elements[0], REFERENCES, EXACT_RULE); + assertSearchResults( + "src/X.java Point(int) [comp_] EXACT_MATCH\n" + + "src/X.java void Point.method() [comp_] EXACT_MATCH\n" + + "src/X.java void Point.method() [comp_] EXACT_MATCH\n" + + "src/X.java void Point.method() [comp_] EXACT_MATCH"); + } + + //selection - select CC type + public void testBug558812_018() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public /* here*/Point {\n" + + " comp_=11;\n" + + " }\n" + + " public Point (int a, int b){\n" + + " comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=11;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here*/Point"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof SourceMethod); + SourceMethod sm = (SourceMethod)elements[0]; + IJavaElement parent = sm.getParent(); + SourceType st = (SourceType)parent; + assertTrue(st.isRecord()); + } + + //selection - select CC type and search + public void testBug558812_019() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public /* here*/Point {\n" + + " comp_=11;\n" + + " }\n" + + " public Point (int a, int b){\n" + + " comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=11;\n" + + " Point p = new Point(1) ;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here*/Point"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof SourceMethod); + search(elements[0], REFERENCES, EXACT_RULE); + assertSearchResults( + "src/X.java void Point.method() [new Point(1)] EXACT_MATCH"); + } + + //selection - select non-CC type and search + public void testBug558812_020() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point {\n" + + " comp_=11;\n" + + " }\n" + + " public /* here*/Point (int a, int b){\n" + + " comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=11;\n" + + " Point p = new Point(1) ;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here*/Point"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof SourceMethod); + search(elements[0], REFERENCES, EXACT_RULE); + assertSearchResults(""); + } + + //selection - select non-CC type and search- 2 + public void testBug558812_021() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public Point {\n" + + " comp_=11;\n" + + " }\n" + + " public /* here*/Point (int a, int b){\n" + + " comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=11;\n" + + " Point p = new Point(1) ;\n" + + " Point p = new Point(1,2) ;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here*/Point"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof SourceMethod); + search(elements[0], REFERENCES, EXACT_RULE); + assertSearchResults( + "src/X.java void Point.method() [new Point(1,2)] EXACT_MATCH"); + } + //selection - select CC type and search- 2 + public void testBug558812_022() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp_) { \n" + + " public /* here*/Point {\n" + + " comp_=11;\n" + + " }\n" + + " public Point (int a, int b){\n" + + " comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=11;\n" + + " Point p = new Point(1) ;\n" + + " Point p = new Point(1,2) ;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here*/Point"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof SourceMethod); + search(elements[0], REFERENCES, EXACT_RULE); + assertSearchResults( + "src/X.java void Point.method() [new Point(1)] EXACT_MATCH"); + } + + //selection - select local field in a compact constructor in record and search1 + public void testBug558812_23() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record /* here*/Point(int comp_) { \n" + + " public Point {\n" + + " /* here*/comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " int compp_=11;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here*/comp_"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof LocalVariable); + search(elements[0], REFERENCES, EXACT_RULE); + assertSearchResults( + "src/X.java Point(int) [comp_] EXACT_MATCH"); + } + + //selection - select local field in a compact constructor in record and search2 + public void testBug558812_24() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record /* here*/Point(int comp_) { \n" + + " public Point {\n" + + " /* here*/comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " comp_=11;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here*/comp_"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof LocalVariable); + search(elements[0], REFERENCES, EXACT_RULE); + assertSearchResults( + "src/X.java Point(int) [comp_] EXACT_MATCH\n" + + "src/X.java void Point.method() [comp_] EXACT_MATCH"); + } + + //selection - select local field in a compact constructor in record and search3 + public void testBug558812_25() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record /* here*/Point(int comp_) { \n" + + " public Point {\n" + + " /* here*/comp_=11;\n" + + " }\n" + + "public void method ( ) { \n"+ + " comp_=11;\n" + + " int a=this.comp_;\n" + + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/* here*/comp_"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof LocalVariable); + search(elements[0], REFERENCES, EXACT_RULE); + assertSearchResults( + "src/X.java Point(int) [comp_] EXACT_MATCH\n" + + "src/X.java void Point.method() [comp_] EXACT_MATCH\n"+ + "src/X.java void Point.method() [comp_] EXACT_MATCH"); + } + + //selection - select record in another file + public void testBug558812_26() throws CoreException { + IJavaProject project1 = createJavaProject("JavaSearchBugs14", new String[] {"src"}, new String[] {"JCL14_LIB"}, "bin", "14"); + try { + Map<String, String> options = project1.getOptions(false); + options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_14); + project1.setOptions(options); + project1.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); + project1.open(null); + createFolder("/JavaSearchBugs14/src/pack11"); + String fileContent = "package pack11;\n" + + "public record X11() {\n" + + "}\n"; + String fileContent2 = "package pack11;\n" + + "public class X12 {\n" + + "/*here*/X11 p =null;\n"+ + "}\n"; + + createFile("/JavaSearchBugs14/src/pack11/X11.java", fileContent); + createFile("/JavaSearchBugs14/src/pack11/X12.java",fileContent2); + ICompilationUnit unit = getCompilationUnit("/JavaSearchBugs14/src/pack11/X12.java"); + String x11 = "/*here*/X11"; + int start = fileContent2.indexOf(x11); + IJavaElement[] elements = unit.codeSelect(start, x11.length()); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof ResolvedSourceType); + boolean record = ((ResolvedSourceType)elements[0]).isRecord(); + assertTrue(record); + } finally { + deleteProject(project1); + } + } + + //selection - select parameter in normal constructor matching component name + public void testBug558812_27() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record X(int a) {\n" + + "public X { \n"+ + " this.a = a; \n"+ + " }\n"+ + "public X(int/*here*/a, int b) { // select the a here\n"+ + "this.a = a;\n"+ + "}\n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/*here*/a"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(elements[0] instanceof LocalVariable); + search(elements[0], REFERENCES, EXACT_RULE); + assertSearchResults( + "src/X.java X(int, int) [a] EXACT_MATCH"); + } + + public void testBug560486_028() throws CoreException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", + "public record Point(int comp) { \n" + + "public void method ( ) { \n"+ + "/*here*/comp(); \n"+ + "} \n"+ + "}\n" + ); + + String str = this.workingCopies[0].getSource(); + String selection = "/*here*/comp"; + int start = str.indexOf(selection); + int length = selection.length(); + + IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); + assertTrue(elements.length ==1); + assertTrue(!(elements[0] instanceof SourceType)); + assertTrue((elements[0] instanceof SourceField)); + } //Bug 561048 code selection public void testBug561048_029() throws CoreException { diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugs15Tests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugs15Tests.java index 8f56122ed8..d58c02074b 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugs15Tests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchBugs15Tests.java @@ -21,7 +21,6 @@ import java.util.Map; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.IField; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; @@ -32,11 +31,6 @@ import org.eclipse.jdt.core.search.ReferenceMatch; import org.eclipse.jdt.core.search.SearchEngine; import org.eclipse.jdt.core.search.SearchMatch; import org.eclipse.jdt.core.search.TypeReferenceMatch; -import org.eclipse.jdt.internal.core.LocalVariable; -import org.eclipse.jdt.internal.core.ResolvedSourceField; -import org.eclipse.jdt.internal.core.ResolvedSourceType; -import org.eclipse.jdt.internal.core.SourceField; -import org.eclipse.jdt.internal.core.SourceMethod; import org.eclipse.jdt.internal.core.SourceType; import junit.framework.Test; @@ -400,797 +394,5 @@ public class JavaSearchBugs15Tests extends AbstractJavaSearchTests { } } - // Record related tests for Java 15 - // 0 reference of the component in compact constructor - public void testBug558812_001() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point {\n" + - " }\n" + - "}\n" - ); - IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies - String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); - try { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); - search("comp_", FIELD, REFERENCES); - assertSearchResults(""); - } finally { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); - } - } - - //1 reference of the component in compact constructor - // Java 15 - should give 0 reference - public void testBug558812_002() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point {\n" + - " comp_=11;\n" + - " }\n" + - "}\n" - ); - IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies - String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); - try { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); - search("comp_", FIELD, REFERENCES); - assertSearchResults(""); - } finally { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); - } - } - - //2 reference of the component in compact constructor - // Java 15 - only 1 - although code compiles no more but it should give 1 result - public void testBug558812_003() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point {\n" + - " comp_=11;\n" + - " this.comp_=11;\n" + - " }\n" + - "}\n" - ); - IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies - String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); - try { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); - search("comp_", FIELD, REFERENCES); - assertSearchResults("src/X.java Point(int) [comp_] EXACT_MATCH"); - } finally { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); - } - } - - //3 reference of the component in compact constructor - //Java 15 only 1 this.comp_ reference should be given - public void testBug558812_004() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point {\n" + - " comp_=11;\n" + - " this.comp_=comp_;\n" + - " }\n" + - "}\n" - ); - IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies - String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); - try { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); - search("comp_", FIELD, REFERENCES); - assertSearchResults("src/X.java Point(int) [comp_] EXACT_MATCH"); - } finally { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); - } - } - - //0 reference of the component in canonical constructor - public void testBug558812_005() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point (int a) {\n" + - " }\n" + - "}\n" - ); - IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies - String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); - try { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); - search("comp_", FIELD, REFERENCES); - assertSearchResults(""); - } finally { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); - } - } - - //1 reference of the component in canonical constructor - public void testBug558812_006() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point (int a) {\n" + - " comp_=11;\n" + - " }\n" + - "}\n" - ); - IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies - String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); - try { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); - search("comp_", FIELD, REFERENCES); - assertSearchResults("src/X.java Point(int) [comp_] EXACT_MATCH"); - } finally { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); - } - } - - //1 reference of the component in canonical constructor - part2 - public void testBug558812_007() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point (int a) {\n" + - " // comp_=11;\n" + - " this.comp_=a;\n" + - " }\n" + - "}\n" - ); - IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies - String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); - try { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); - search("comp_", FIELD, REFERENCES); - assertSearchResults("src/X.java Point(int) [comp_] EXACT_MATCH"); - } finally { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); - } - } - - //1 reference of the component in compact constructor - clashing method parameter - // Java 15 - 0 reference - public void testBug558812_008() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point {\n" + - " comp_=11;\n" + - " }\n" + - "public void method ( int comp_) { \n"+ - "} \n"+ - "}\n" - ); - IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies - String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); - try { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); - search("comp_", FIELD, REFERENCES); - assertSearchResults(""); - } finally { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); - } - } - - - //1 reference of the component in compact constructor - clashing method's local variable - // Java 15 - 0 reference - public void testBug558812_009() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point {\n" + - " comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int comp_=11;\n" + - "} \n"+ - "}\n" - ); - IJavaProject javaProject = this.workingCopies[0].getJavaProject(); //assuming single project for all working copies - String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); - try { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); - search("comp_", FIELD, REFERENCES); - assertSearchResults(""); - } finally { - javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); - } - } - - // more test case - // test case of comp_ selection in compact constructor - // selection - select record type - public void testBug558812_010() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record /* here*/Point(int comp_) { \n" + - " public Point {\n" + - " comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=11;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here*/Point"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - //TODO: check if record - } - - //selection - select local field in a method in record - public void testBug558812_011() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record /* here*/Point(int comp_) { \n" + - " public Point {\n" + - " comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int /* here*/compp_=11;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here*/compp_"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof LocalVariable); - - } - - //selection - select local field in a compact constructor in record - public void testBug558812_012() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record /* here*/Point(int comp_) { \n" + - " public Point {\n" + - " /* here*/comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=11;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here*/comp_"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof LocalVariable); - - } - - //selection - select local field in a compact constructor in record - public void testBug558812_013() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record /* here*/Point(int comp_) { \n" + - " public Point {\n" + - " /* here*/this.comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=11;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here*/this.comp_"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof ResolvedSourceField); - } - - //selection - select field in a method in record ( using this) - public void testBug558812_014() throws CoreException { - - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record /* here*/Point(int comp_) { \n" + - " public Point {\n" + - " /* here*/comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=/* here*/this.comp_;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here*/this.comp_"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof ResolvedSourceField); - } - - //selection - select field in a method in record ( without this) - public void testBug558812_015() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record /* here*/Point(int comp_) { \n" + - " public Point {\n" + - " comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=/* here2*/comp_;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here2*/comp_"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof ResolvedSourceField); - ResolvedSourceField sm = (ResolvedSourceField)elements[0]; - IJavaElement parent = sm.getParent(); - SourceType st = (SourceType)parent; - assertTrue(st.isRecord()); - } - - //changed for Java 15 - public void testBug558812_016() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point {\n" + - " /*here2*/comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=comp_;\n" + - " int compp2_=comp_;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/*here2*/comp_"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof LocalVariable); - search(elements[0], REFERENCES, EXACT_RULE); - assertSearchResults( - "src/X.java Point(int) [comp_] EXACT_MATCH"); - } - //java 15 change - public void testBug558812_016a() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point {\n" + - " comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=comp_;\n" + - " int compp2_=comp_;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "comp_"; - int start = str.lastIndexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof IField); - search(elements[0], REFERENCES, EXACT_RULE); - assertSearchResults( - "src/X.java void Point.method() [comp_] EXACT_MATCH\n" + - "src/X.java void Point.method() [comp_] EXACT_MATCH"); - } - - public void testBug558812_017() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point {\n" + - " /*here2*/comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=comp_;\n" + - " int compp2_=comp_;\n" + - " int compp3_=this.comp_;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/*here2*/comp_"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof LocalVariable); - search(elements[0], REFERENCES, EXACT_RULE); - assertSearchResults( - "src/X.java Point(int) [comp_] EXACT_MATCH"); - - } - //java 15 change - public void testBug558812_017a() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point {\n" + - " comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=comp_;\n" + - " int compp2_=comp_;\n" + - " int compp3_=this.comp_;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "comp_"; - int start = str.lastIndexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof IField); - search(elements[0], REFERENCES, EXACT_RULE); - assertSearchResults( - "src/X.java void Point.method() [comp_] EXACT_MATCH\n" + - "src/X.java void Point.method() [comp_] EXACT_MATCH\n" + - "src/X.java void Point.method() [comp_] EXACT_MATCH"); - } - - //selection - select CC type - public void testBug558812_018() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public /* here*/Point {\n" + - " comp_=11;\n" + - " }\n" + - " public Point (int a, int b){\n" + - " comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=11;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here*/Point"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof SourceMethod); - SourceMethod sm = (SourceMethod)elements[0]; - IJavaElement parent = sm.getParent(); - SourceType st = (SourceType)parent; - assertTrue(st.isRecord()); - } - - //selection - select CC type and search - public void testBug558812_019() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public /* here*/Point {\n" + - " comp_=11;\n" + - " }\n" + - " public Point (int a, int b){\n" + - " comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=11;\n" + - " Point p = new Point(1) ;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here*/Point"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof SourceMethod); - search(elements[0], REFERENCES, EXACT_RULE); - assertSearchResults( - "src/X.java void Point.method() [new Point(1)] EXACT_MATCH"); - } - - //selection - select non-CC type and search - public void testBug558812_020() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point {\n" + - " comp_=11;\n" + - " }\n" + - " public /* here*/Point (int a, int b){\n" + - " comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=11;\n" + - " Point p = new Point(1) ;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here*/Point"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof SourceMethod); - search(elements[0], REFERENCES, EXACT_RULE); - assertSearchResults(""); - } - - //selection - select non-CC type and search- 2 - public void testBug558812_021() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public Point {\n" + - " comp_=11;\n" + - " }\n" + - " public /* here*/Point (int a, int b){\n" + - " comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=11;\n" + - " Point p = new Point(1) ;\n" + - " Point p = new Point(1,2) ;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here*/Point"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof SourceMethod); - search(elements[0], REFERENCES, EXACT_RULE); - assertSearchResults( - "src/X.java void Point.method() [new Point(1,2)] EXACT_MATCH"); - } - - //selection - select CC type and search- 2 - public void testBug558812_022() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp_) { \n" + - " public /* here*/Point {\n" + - " comp_=11;\n" + - " }\n" + - " public Point (int a, int b){\n" + - " comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=11;\n" + - " Point p = new Point(1) ;\n" + - " Point p = new Point(1,2) ;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here*/Point"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof SourceMethod); - search(elements[0], REFERENCES, EXACT_RULE); - assertSearchResults( - "src/X.java void Point.method() [new Point(1)] EXACT_MATCH"); - } - - //selection - select local field in a compact constructor in record and search1 - public void testBug558812_23() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record /* here*/Point(int comp_) { \n" + - " public Point {\n" + - " /* here*/comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " int compp_=11;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here*/comp_"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof LocalVariable); - search(elements[0], REFERENCES, EXACT_RULE); - assertSearchResults( - "src/X.java Point(int) [comp_] EXACT_MATCH"); - } - - //selection - select local field in a compact constructor in record and search2 - public void testBug558812_24() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record /* here*/Point(int comp_) { \n" + - " public Point {\n" + - " /* here*/comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " comp_=11;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here*/comp_"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof LocalVariable); - search(elements[0], REFERENCES, EXACT_RULE); - assertSearchResults( - "src/X.java Point(int) [comp_] EXACT_MATCH"); - - } - - //selection - select local field in a compact constructor in record and search3 - // java 15 change - public void testBug558812_25() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record /* here*/Point(int comp_) { \n" + - " public Point {\n" + - " /* here*/comp_=11;\n" + - " }\n" + - "public void method ( ) { \n"+ - " comp_=11;\n" + - " int a=this.comp_;\n" + - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/* here*/comp_"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof LocalVariable); - search(elements[0], REFERENCES, EXACT_RULE); - assertSearchResults( - "src/X.java Point(int) [comp_] EXACT_MATCH"); - } - - //selection - select record in another file - public void testBug558812_26() throws CoreException { - IJavaProject project1 = createJavaProject("JavaSearchBugs14", new String[] {"src"}, new String[] {"JCL14_LIB"}, "bin", "14"); - try { - Map<String, String> options = project1.getOptions(false); - options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_14); - project1.setOptions(options); - project1.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); - project1.open(null); - createFolder("/JavaSearchBugs14/src/pack11"); - String fileContent = "package pack11;\n" + - "public record X11() {\n" + - "}\n"; - String fileContent2 = "package pack11;\n" + - "public class X12 {\n" + - "/*here*/X11 p =null;\n"+ - "}\n"; - - createFile("/JavaSearchBugs14/src/pack11/X11.java", fileContent); - createFile("/JavaSearchBugs14/src/pack11/X12.java",fileContent2); - ICompilationUnit unit = getCompilationUnit("/JavaSearchBugs14/src/pack11/X12.java"); - String x11 = "/*here*/X11"; - int start = fileContent2.indexOf(x11); - IJavaElement[] elements = unit.codeSelect(start, x11.length()); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof ResolvedSourceType); - boolean record = ((ResolvedSourceType)elements[0]).isRecord(); - assertTrue(record); - } finally { - deleteProject(project1); - } - } - - //selection - select parameter in normal constructor matching component name - public void testBug558812_27() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record X(int a) {\n" + - "public X { \n"+ - " this.a = a; \n"+ - " }\n"+ - "public X(int/*here*/a, int b) { // select the a here\n"+ - "this.a = a;\n"+ - "}\n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/*here*/a"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(elements[0] instanceof LocalVariable); - search(elements[0], REFERENCES, EXACT_RULE); - assertSearchResults( - "src/X.java X(int, int) [a] EXACT_MATCH"); - } - - public void testBug560486_028() throws CoreException { - this.workingCopies = new ICompilationUnit[1]; - this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", - "public record Point(int comp) { \n" + - "public void method ( ) { \n"+ - "/*here*/comp(); \n"+ - "} \n"+ - "}\n" - ); - - String str = this.workingCopies[0].getSource(); - String selection = "/*here*/comp"; - int start = str.indexOf(selection); - int length = selection.length(); - - IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length); - assertTrue(elements.length ==1); - assertTrue(!(elements[0] instanceof SourceType)); - assertTrue((elements[0] instanceof SourceField)); - } - - } diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/FieldLocator.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/FieldLocator.java index 32338cd107..5ef2789129 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/FieldLocator.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/FieldLocator.java @@ -332,6 +332,12 @@ public int resolveLevel(ASTNode possiblelMatchingNode) { @Override public int resolveLevel(Binding binding) { if (binding == null) return INACCURATE_MATCH; + if( binding instanceof LocalVariableBinding) { + // for matching the component in constructor of a record + if ( ((LocalVariableBinding)binding).declaringScope.referenceContext() instanceof CompactConstructorDeclaration) { + return matchLocal((LocalVariableBinding) binding, true); + } + } if (!(binding instanceof FieldBinding)) return IMPOSSIBLE_MATCH; return matchField((FieldBinding) binding, true); diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/LocalVariableLocator.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/LocalVariableLocator.java index 86d6f4ee3a..72212d7b74 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/LocalVariableLocator.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/LocalVariableLocator.java @@ -17,8 +17,10 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.internal.compiler.ast.*; import org.eclipse.jdt.internal.compiler.lookup.Binding; +import org.eclipse.jdt.internal.compiler.lookup.FieldBinding; import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding; import org.eclipse.jdt.internal.core.LocalVariable; +import org.eclipse.jdt.internal.core.SourceField; public class LocalVariableLocator extends VariableLocator { @@ -65,6 +67,14 @@ protected void matchReportReference(ASTNode reference, IJavaElement element, Bin this.match = locator.newDeclarationMatch(element, null, accuracy, offset, length); locator.report(this.match); return; + } else if (reference instanceof FieldReference) { // for record's component in constructor + FieldReference fieldReference = (FieldReference) reference; + long position = fieldReference.nameSourcePosition; + int start = (int) (position >>> 32); + int end = (int) position; + this.match = locator.newFieldReferenceMatch(element, null, elementBinding, accuracy, start, end-start+1, fieldReference); + locator.report(this.match); + return; } if (offset >= 0) { this.match = locator.newLocalVariableReferenceMatch(element, accuracy, offset, length, reference); @@ -95,15 +105,51 @@ public int resolveLevel(ASTNode possiblelMatchingNode) { return resolveLevel((NameReference) possiblelMatchingNode); if (possiblelMatchingNode instanceof LocalDeclaration) return matchLocalVariable(((LocalDeclaration) possiblelMatchingNode).binding, true); + if(possiblelMatchingNode instanceof FieldReference ) { + //for the local variable in the constructor of record matching component's name + FieldBinding binding = ((FieldReference)possiblelMatchingNode).binding; + if (binding.isRecordComponent()) + return matchField(binding, true); + } return IMPOSSIBLE_MATCH; } @Override public int resolveLevel(Binding binding) { if (binding == null) return INACCURATE_MATCH; + // for record's component local variable matching component name + if(binding instanceof FieldBinding && ((FieldBinding) binding).isRecordComponent()) { + return matchField(binding, true); + } + if(binding instanceof LocalVariableBinding) { + if ( ((LocalVariableBinding)binding).declaringScope.referenceContext() instanceof CompactConstructorDeclaration) { + //update with binding + if( this.pattern instanceof FieldPattern) { + return matchField(binding, true); + } + } + } if (!(binding instanceof LocalVariableBinding)) return IMPOSSIBLE_MATCH; return matchLocalVariable((LocalVariableBinding) binding, true); } +private int matchField(Binding binding, boolean matchName) { + if (binding == null) return INACCURATE_MATCH; + if(binding instanceof FieldBinding) { + if (! ((FieldBinding)binding).declaringClass.isRecord()) + return IMPOSSIBLE_MATCH; + } + if(this.pattern instanceof LocalVariablePattern) { + LocalVariablePattern lvp = (LocalVariablePattern)this.pattern; + LocalVariable localVariable = lvp.localVariable; + IJavaElement parent = localVariable.getParent() ; + // if the parent is not sourceField, skip + if(!(parent instanceof SourceField)) + return IMPOSSIBLE_MATCH; + } + if (matchName && matchesName(this.pattern.name, binding.readableName())) + return ACCURATE_MATCH; + return IMPOSSIBLE_MATCH; +} protected int resolveLevel(NameReference nameRef) { return resolveLevel(nameRef.binding); } |