Skip to content

Commit

Permalink
[WIP] fix SelectionParser
Browse files Browse the repository at this point in the history
Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Jan 17, 2024
1 parent fb523f3 commit 02a0ce7
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
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.ILocalVariable;
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 JavaSearchBugs21Tests extends AbstractJavaSearchTests {

public JavaSearchBugs21Tests(String name) {
super(name);
this.endChar = "";
}

public static Test suite() {
return buildModelTestSuite(JavaSearchBugs21Tests.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 {
super.setUpSuite();
JAVA_PROJECT = setUpJavaProject("JavaSearchBugs", "21");
}

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 testListOfPatterns() throws CoreException {
this.workingCopies = new ICompilationUnit[1];
this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java",
"""
public class X {
public void myMethod() {
MyTokenSearchRecord value = new MyTokenSearchRecord(1, new ClazzB());
switch (value) {
case MyTokenSearchRecord(int num1, ClazzA aaa), MyTokenSearchRecord(int num2, ClazzB bbb) when 0 == 0 && /*here*/num1 < num2:
System.out.println("Hello, World");
break;
default:
break;
}
}
}
record MyTokenSearchRecord(int i, Clazz j) {}
abstract sealed class Clazz permits ClazzA, ClazzB, ClazzC {}
final class ClazzA extends Clazz {}
final class ClazzB extends Clazz {}
final class ClazzC extends Clazz {}
"""
);
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.DISABLED);
String str = this.workingCopies[0].getSource();
String selection = "/*here*/num1";
int start = str.indexOf(selection);
int length = selection.length();
IJavaElement[] elements = this.workingCopies[0].codeSelect(start, length);
assertEquals("incorrect no of elements", 1, elements.length);
assertTrue(elements[0] instanceof ILocalVariable);
search(elements[0], REFERENCES, EXACT_RULE);
assertSearchResults("src/X.java void X.myMethod() [num1] EXACT_MATCH");
} finally {
javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
*******************************************************************************/
package org.eclipse.jdt.core.tests.model;

import java.lang.reflect.*;
import java.util.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.tests.junit.extension.TestCase;

Expand Down Expand Up @@ -71,6 +72,7 @@ public static Test suite() {
allClasses.add(JavaSearchBugs16Tests.class);
allClasses.add(JavaSearchBugs17Tests.class);
allClasses.add(JavaSearchBugs19Tests.class);
allClasses.add(JavaSearchBugs21Tests.class);
allClasses.add(JavaSearchMultipleProjectsTests.class);
allClasses.add(SearchTests.class);
allClasses.add(JavaSearchScopeTests.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ private void buildMoreCompletionContext(Expression expression) {

Statement whileBody = null;

boolean hasGuard = false;
int guardPosition = -1;

int kind;

for (int i = this.elementPtr; i > -1; i--) {
Expand All @@ -206,22 +209,24 @@ private void buildMoreCompletionContext(Expression expression) {
thenStat = elseStat = null;
break;
case K_BETWEEN_CASE_AND_COLONORARROW:
if (hasGuard) {
int patternListLength = 0;
this.expressionPtr = guardPosition;
while (this.expressionStack[this.expressionPtr] instanceof Pattern) {
patternListLength++;
this.expressionPtr--;
}
Pattern[] patterns = new Pattern[patternListLength];
System.arraycopy(this.expressionStack, this.expressionPtr + 1, patterns, 0, patternListLength);
parentNode = orphan = new GuardedPattern(patterns, (Expression) orphan);
}
parentNode = orphan = new CaseStatement((Expression) orphan, orphan.sourceStart, orphan.sourceEnd);
break;
case K_INSIDE_WHEN:
if (this.expressionPtr >= 0 && orphan instanceof Expression) {
this.expressionPtr--;
this.expressionLengthPtr--;
int patternLength = this.expressionLengthStack[this.expressionLengthPtr--];
Pattern[] patterns = new Pattern[patternLength];
System.arraycopy(
this.expressionStack,
this.expressionPtr - patternLength + 1,
patterns,
0,
patternLength);
this.expressionPtr -= patternLength;
parentNode = orphan = new GuardedPattern(patterns, (Expression) orphan);
hasGuard = true;
guardPosition = this.expressionPtr;
while (guardPosition > 0 && !(this.expressionStack[guardPosition] instanceof Pattern)) {
guardPosition--;
}
break;
case K_POST_WHILE_EXPRESSION:
Expand Down

0 comments on commit 02a0ce7

Please sign in to comment.