forked from eclipse-jdt/eclipse.jdt.core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Search support for the implicitly declared class
- Loading branch information
1 parent
81ab185
commit 845eafa
Showing
2 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
...e.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchImplicitTypeDeclaration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package org.eclipse.jdt.core.tests.model; | ||
|
||
import java.io.IOException; | ||
|
||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.jdt.core.ICompilationUnit; | ||
import org.eclipse.jdt.core.IJavaElement; | ||
import org.eclipse.jdt.core.IJavaProject; | ||
import org.eclipse.jdt.core.JavaCore; | ||
import org.eclipse.jdt.core.JavaModelException; | ||
import org.eclipse.jdt.core.WorkingCopyOwner; | ||
import org.eclipse.jdt.core.search.IJavaSearchScope; | ||
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 junit.framework.Test; | ||
|
||
public class JavaSearchImplicitTypeDeclaration extends JavaSearchTests { | ||
public JavaSearchImplicitTypeDeclaration(String name) { | ||
super(name); | ||
this.endChar = ""; | ||
} | ||
public static Test suite() { | ||
return buildModelTestSuite(JavaSearchImplicitTypeDeclaration.class, BYTECODE_DECLARATION_ORDER); | ||
} | ||
class TestCollector extends JavaSearchResultCollector { | ||
public void acceptSearchMatch(SearchMatch searchMatch) throws CoreException { | ||
super.acceptSearchMatch(searchMatch); | ||
} | ||
} | ||
|
||
class ReferenceCollector extends JavaSearchResultCollector { | ||
protected void writeLine() throws CoreException { | ||
super.writeLine(); | ||
ReferenceMatch refMatch = (ReferenceMatch) this.match; | ||
IJavaElement localElement = refMatch.getLocalElement(); | ||
if (localElement != null) { | ||
this.line.append("+["); | ||
if (localElement.getElementType() == IJavaElement.ANNOTATION) { | ||
this.line.append('@'); | ||
this.line.append(localElement.getElementName()); | ||
this.line.append(" on "); | ||
this.line.append(localElement.getParent().getElementName()); | ||
} else { | ||
this.line.append(localElement.getElementName()); | ||
} | ||
this.line.append(']'); | ||
} | ||
} | ||
} | ||
|
||
class TypeReferenceCollector extends ReferenceCollector { | ||
protected void writeLine() throws CoreException { | ||
super.writeLine(); | ||
TypeReferenceMatch typeRefMatch = (TypeReferenceMatch) this.match; | ||
IJavaElement[] others = typeRefMatch.getOtherElements(); | ||
int length = others==null ? 0 : others.length; | ||
if (length > 0) { | ||
this.line.append("+["); | ||
for (int i=0; i<length; i++) { | ||
IJavaElement other = others[i]; | ||
if (i>0) this.line.append(','); | ||
if (other.getElementType() == IJavaElement.ANNOTATION) { | ||
this.line.append('@'); | ||
this.line.append(other.getElementName()); | ||
this.line.append(" on "); | ||
this.line.append(other.getParent().getElementName()); | ||
} else { | ||
this.line.append(other.getElementName()); | ||
} | ||
} | ||
this.line.append(']'); | ||
} | ||
} | ||
} | ||
|
||
protected IJavaProject setUpJavaProject(final String projectName, String compliance, boolean useFullJCL) throws CoreException, IOException { | ||
// copy files in project from source workspace to target workspace | ||
IJavaProject setUpJavaProject = super.setUpJavaProject(projectName, compliance, useFullJCL); | ||
return setUpJavaProject; | ||
} | ||
|
||
IJavaSearchScope getJavaSearchScope() { | ||
return SearchEngine.createJavaSearchScope(new IJavaProject[] {getJavaProject("JavaSearchBugs")}); | ||
} | ||
|
||
IJavaSearchScope getJavaSearchScopeBugs(String packageName, boolean addSubpackages) throws JavaModelException { | ||
if (packageName == null) return getJavaSearchScope(); | ||
return getJavaSearchPackageScope("JavaSearchBugs", packageName, addSubpackages); | ||
} | ||
|
||
public ICompilationUnit getWorkingCopy(String path, String source) throws JavaModelException { | ||
if (this.wcOwner == null) { | ||
this.wcOwner = new WorkingCopyOwner() {}; | ||
} | ||
return getWorkingCopy(path, source, this.wcOwner); | ||
} | ||
|
||
@Override | ||
public void setUpSuite() throws Exception { | ||
JAVA_PROJECT = setUpJavaProject("JavaSearchBugs", "23"); | ||
JAVA_PROJECT.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); | ||
super.setUpSuite(); | ||
} | ||
|
||
public void tearDownSuite() throws Exception { | ||
deleteProject("JavaSearchBugs"); | ||
super.tearDownSuite(); | ||
} | ||
|
||
protected void setUp () throws Exception { | ||
super.setUp(); | ||
this.resultCollector = new TestCollector(); | ||
this.resultCollector.showAccuracy(true); | ||
} | ||
|
||
public void test_001() throws CoreException { | ||
this.workingCopies = new ICompilationUnit[1]; | ||
String code = """ | ||
/** | ||
* Hello | ||
*/ | ||
void main() { | ||
System.out.println("sasi"); | ||
abc(); | ||
} | ||
void abc() { | ||
System.out.println("abc"); | ||
} | ||
"""; | ||
this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", code); | ||
IJavaProject javaProject = this.workingCopies[0].getJavaProject(); | ||
String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true); | ||
try { | ||
search("abc", METHOD, ALL_OCCURRENCES, EXACT_RULE); | ||
assertSearchResults("src/X.java void X.main() [abc()] EXACT_MATCH\n" + | ||
"src/X.java void X.abc() [abc] EXACT_MATCH"); | ||
} finally { | ||
javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters