Skip to content

Commit e9b7bbd

Browse files
committed
Allow overriding the Java search logic with a custom implementation
Signed-off-by: Rob Stryker <[email protected]>
1 parent d568f49 commit e9b7bbd

File tree

14 files changed

+448
-8
lines changed

14 files changed

+448
-8
lines changed

org.eclipse.jdt.core.tests.model/plugin.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,13 @@
105105
class="org.eclipse.jdt.core.tests.model.TestCompletionEngineProvider"
106106
id="org.eclipse.jdt.core.tests.model.TestCompletionEngineProvider" />
107107
</extension>
108+
<extension
109+
point="org.eclipse.jdt.core.javaSearchDelegate">
110+
<searchDelegate
111+
class="org.eclipse.jdt.core.tests.model.TestJavaSearchDelegate"
112+
id="org.eclipse.jdt.core.tests.model.TestJavaSearchDelegate">
113+
</searchDelegate>
114+
</extension>
115+
108116

109117
</plugin>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Red Hat Inc and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Red Hat Inc - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.jdt.core.tests.model;
15+
16+
import junit.framework.Test;
17+
import org.eclipse.jdt.core.JavaModelException;
18+
import org.eclipse.jdt.core.search.IJavaSearchDelegate;
19+
import org.eclipse.jdt.internal.core.search.matching.JavaSearchDelegateDiscovery;
20+
21+
public class JavaSearchExtensionTest extends AbstractJavaSearchTests {
22+
private static final String DELEGATE_SYSPROP = "IJavaSearchDelegate";
23+
public JavaSearchExtensionTest(String name) {
24+
super(name);
25+
}
26+
public static Test suite() {
27+
return buildModelTestSuite(JavaSearchExtensionTest.class, ALPHABETICAL_SORT);
28+
}
29+
@Override
30+
public void setUpSuite() throws Exception {
31+
super.setUpSuite();
32+
}
33+
@Override
34+
protected void setUp() throws Exception {
35+
super.setUp();
36+
}
37+
@Override
38+
protected void tearDown() throws Exception {
39+
super.tearDown();
40+
}
41+
public void testDelegateFound() throws JavaModelException {
42+
String oldSystemProperty = System.getProperty(DELEGATE_SYSPROP);
43+
44+
try {
45+
System.setProperty(DELEGATE_SYSPROP, "org.eclipse.jdt.core.tests.model.TestJavaSearchDelegate");
46+
IJavaSearchDelegate del = JavaSearchDelegateDiscovery.getInstance();
47+
assertNotNull(del);
48+
assertTrue(del instanceof TestJavaSearchDelegate);
49+
50+
System.clearProperty(DELEGATE_SYSPROP);
51+
del = JavaSearchDelegateDiscovery.getInstance();
52+
assertNull(del);
53+
54+
System.setProperty(DELEGATE_SYSPROP, "unknownVal");
55+
del = JavaSearchDelegateDiscovery.getInstance();
56+
assertNull(del);
57+
} finally {
58+
if( oldSystemProperty != null )
59+
System.setProperty(DELEGATE_SYSPROP, oldSystemProperty);
60+
else
61+
System.clearProperty(DELEGATE_SYSPROP);
62+
}
63+
}
64+
}

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/RunJavaSearchTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class RunJavaSearchTests extends junit.framework.TestCase {
2828
static {
2929
// All test suites put in this list should use the same tests projects
3030
// (eg. JavaSearch and JavaSearch15)
31+
TEST_CLASSES.add(JavaSearchExtensionTest.class);
3132
TEST_CLASSES.add(JavaSearchTests.class);
3233
TEST_CLASSES.add(JavaSearchGenericTypeTests.class);
3334
TEST_CLASSES.add(JavaSearchGenericTypeEquivalentTests.class);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Red Hat Inc and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Red Hat Inc - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.jdt.core.tests.model;
15+
16+
import org.eclipse.core.runtime.CoreException;
17+
import org.eclipse.jdt.core.IJavaProject;
18+
import org.eclipse.jdt.core.search.IJavaSearchDelegate;
19+
import org.eclipse.jdt.internal.core.search.matching.MatchLocator;
20+
import org.eclipse.jdt.internal.core.search.matching.PossibleMatch;
21+
22+
public class TestJavaSearchDelegate implements IJavaSearchDelegate {
23+
24+
public TestJavaSearchDelegate() {
25+
// TODO Auto-generated constructor stub
26+
}
27+
28+
@Override
29+
public void locateMatches(MatchLocator locator, IJavaProject javaProject, PossibleMatch[] possibleMatches,
30+
int start, int length) throws CoreException {
31+
// do nothing, stub
32+
}
33+
34+
}

org.eclipse.jdt.core/.settings/.api_filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,13 @@
278278
</message_arguments>
279279
</filter>
280280
</resource>
281+
<resource path="search/org/eclipse/jdt/core/search/IJavaSearchDelegate.java" type="org.eclipse.jdt.core.search.IJavaSearchDelegate">
282+
<filter id="643846161">
283+
<message_arguments>
284+
<message_argument value="PossibleMatch"/>
285+
<message_argument value="IJavaSearchDelegate"/>
286+
<message_argument value="locateMatches(MatchLocator, IJavaProject, PossibleMatch[], int, int)"/>
287+
</message_arguments>
288+
</filter>
289+
</resource>
281290
</component>

org.eclipse.jdt.core/plugin.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ codeFormattersName=Source Code Formatters
2525
compilationParticipantsName=Compilation Participants
2626
compilationUnitResolverName=Compilation Unit Resolver
2727
completionEngineProviderName=Completion Engine Provider
28+
javaSearchDelegateName=Java Search Delegate
2829
annotationProcessorManagerName=Java 6 Annotation Processor Manager
2930
javaTaskName=Java Task
3031
javaPropertiesName=Java Properties File

org.eclipse.jdt.core/plugin.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@
7878
<extension-point name="%completionEngineProviderName"
7979
id="completionEngineProvider"
8080
schema="schema/completionEngineProvider.exsd"/>
81+
82+
<!-- =================================================================================== -->
83+
<!-- Extension Point: CompletionEngine Provider -->
84+
<!-- =================================================================================== -->
85+
<extension-point name="%completionEngineProviderName"
86+
id="javaSearchDelegate"
87+
schema="schema/javaSearchDelegate.exsd"/>
8188

8289
<!-- =================================================================================== -->
8390
<!-- Extension Point: Java 6 Annotation Processor Manager -->
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!-- Schema file written by PDE -->
3+
<schema targetNamespace="org.eclipse.jdt.core" xmlns="http://www.w3.org/2001/XMLSchema">
4+
<annotation>
5+
<appInfo>
6+
<meta.schema plugin="org.eclipse.jdt.core" id="javaSearchDelegate" name="Java Search Delegate"/>
7+
</appInfo>
8+
<documentation>
9+
This extension point provides the ability to replace the Java Search functionality. The resolver will be instantiated on-demand based on the value of the system property `IJavaSearchDelegate`, which must be set to the id of an implementing extension. This extension point is not intended to be implemented by clients. This extension point is not considered API. This extension point may be modified or removed at any moment.
10+
</documentation>
11+
</annotation>
12+
13+
<element name="extension">
14+
<annotation>
15+
<appInfo>
16+
<meta.element />
17+
</appInfo>
18+
</annotation>
19+
<complexType>
20+
<sequence>
21+
<element ref="searchDelegate" minOccurs="0" maxOccurs="1"/>
22+
</sequence>
23+
<attribute name="point" type="string" use="required">
24+
<annotation>
25+
<documentation>
26+
27+
</documentation>
28+
</annotation>
29+
</attribute>
30+
<attribute name="id" type="string">
31+
<annotation>
32+
<documentation>
33+
34+
</documentation>
35+
</annotation>
36+
</attribute>
37+
<attribute name="name" type="string">
38+
<annotation>
39+
<documentation>
40+
41+
</documentation>
42+
<appInfo>
43+
<meta.attribute translatable="true"/>
44+
</appInfo>
45+
</annotation>
46+
</attribute>
47+
</complexType>
48+
</element>
49+
50+
<element name="searchDelegate">
51+
<annotation>
52+
<documentation>
53+
Definition of a Java Search Delegate.
54+
</documentation>
55+
</annotation>
56+
<complexType>
57+
<attribute name="class" type="string" use="required">
58+
<annotation>
59+
<documentation>
60+
The class that implements this Java Search Delegate. This class must implement the &lt;code&gt;org.eclipse.jdt.core.search.IJavaSearchDelegate&lt;/code&gt; interface with a public 0-arg constructor.
61+
</documentation>
62+
<appInfo>
63+
<meta.attribute kind="java" basedOn=":org.eclipse.jdt.core.search.IJavaSearchDelegate"/>
64+
</appInfo>
65+
</annotation>
66+
</attribute>
67+
<attribute name="id" type="string" use="required">
68+
<annotation>
69+
<documentation>
70+
A unique identifier for this search delegate
71+
</documentation>
72+
</annotation>
73+
</attribute>
74+
</complexType>
75+
</element>
76+
77+
<annotation>
78+
<appInfo>
79+
<meta.section type="since"/>
80+
</appInfo>
81+
<documentation>
82+
3.38
83+
</documentation>
84+
</annotation>
85+
86+
<annotation>
87+
<appInfo>
88+
<meta.section type="examples"/>
89+
</appInfo>
90+
<documentation>
91+
Example of a declaration of a &lt;code&gt;IJavaSearchDelegate&lt;/code&gt;: &lt;pre&gt;
92+
&lt;extension
93+
point=&quot;org.eclipse.jdt.core.javaSearchDelegate&quot;&gt;
94+
&lt;resolver
95+
class=&quot;org.eclipse.jdt.core.MySearchDelegate&quot;
96+
id=&quot;org.eclipse.jdt.core.MySearchDelegate&quot;&gt;
97+
&lt;/resolver&gt;
98+
&lt;/extension&gt;
99+
&lt;/pre&gt;
100+
</documentation>
101+
</annotation>
102+
103+
104+
105+
<annotation>
106+
<appInfo>
107+
<meta.section type="copyright"/>
108+
</appInfo>
109+
<documentation>
110+
Copyright (c) 2025 Red Hat, Inc. and others.&lt;br&gt;
111+
112+
This program and the accompanying materials
113+
are made available under the terms of the Eclipse Public License 2.0
114+
which accompanies this distribution, and is available at
115+
&lt;a href=&quot;https://www.eclipse.org/legal/epl-2.0&quot;&gt;https://www.eclipse.org/legal/epl-v20.html&lt;/a&gt;/
116+
117+
SPDX-License-Identifier: EPL-2.0
118+
</documentation>
119+
</annotation>
120+
121+
</schema>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Red Hat Inc and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Red Hat Inc - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.jdt.core.search;
15+
16+
import org.eclipse.core.runtime.CoreException;
17+
import org.eclipse.jdt.core.IJavaProject;
18+
import org.eclipse.jdt.internal.core.search.matching.MatchLocator;
19+
import org.eclipse.jdt.internal.core.search.matching.PossibleMatch;
20+
21+
/**
22+
* This interface represents a delegate that can take over the discovery of search results
23+
* for java-based searches.
24+
*
25+
* This interface makes use of internal classes and is not considered stable or API.
26+
*
27+
* @since 3.41
28+
*/
29+
public interface IJavaSearchDelegate {
30+
31+
/**
32+
* Fill the PossibleMatch objects' state with confirmed and possible results using the
33+
* search strategy that this delegate employs.
34+
*
35+
* @param locator The MatchLocator initiating the request
36+
* @param javaProject The context in which the search is being performed
37+
* @param possibleMatches An array of possible matches
38+
* @param start The start index with which to begin searching
39+
* @param length The length of matches with which to search
40+
* @throws CoreException
41+
*/
42+
void locateMatches(MatchLocator locator, IJavaProject javaProject, PossibleMatch[] possibleMatches, int start, int length) throws CoreException;
43+
44+
}

0 commit comments

Comments
 (0)