Skip to content

Commit

Permalink
Allow overriding the Java search logic with a custom implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Stryker <[email protected]>

Wrong since tag

Signed-off-by: Rob Stryker <[email protected]>
  • Loading branch information
robstryker authored and Rob Stryker committed Feb 12, 2025
1 parent 668fa97 commit 616c374
Show file tree
Hide file tree
Showing 14 changed files with 455 additions and 8 deletions.
8 changes: 8 additions & 0 deletions org.eclipse.jdt.core.tests.model/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,13 @@
class="org.eclipse.jdt.core.tests.model.TestCompletionEngineProvider"
id="org.eclipse.jdt.core.tests.model.TestCompletionEngineProvider" />
</extension>
<extension
point="org.eclipse.jdt.core.javaSearchDelegate">
<searchDelegate
class="org.eclipse.jdt.core.tests.model.TestJavaSearchDelegate"
id="org.eclipse.jdt.core.tests.model.TestJavaSearchDelegate">
</searchDelegate>
</extension>


</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*******************************************************************************
* Copyright (c) 2025 Red Hat Inc and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.core.tests.model;

import junit.framework.Test;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchDelegate;
import org.eclipse.jdt.internal.core.search.matching.JavaSearchDelegateDiscovery;

public class JavaSearchExtensionTest extends AbstractJavaSearchTests {
private static final String DELEGATE_SYSPROP = "IJavaSearchDelegate";
public JavaSearchExtensionTest(String name) {
super(name);
}
public static Test suite() {
return buildModelTestSuite(JavaSearchExtensionTest.class, ALPHABETICAL_SORT);
}
@Override
public void setUpSuite() throws Exception {
super.setUpSuite();
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testDelegateFound() throws JavaModelException {
String oldSystemProperty = System.getProperty(DELEGATE_SYSPROP);

try {
System.setProperty(DELEGATE_SYSPROP, "org.eclipse.jdt.core.tests.model.TestJavaSearchDelegate");
IJavaSearchDelegate del = JavaSearchDelegateDiscovery.getInstance();
assertNotNull(del);
assertTrue(del instanceof TestJavaSearchDelegate);

System.clearProperty(DELEGATE_SYSPROP);
del = JavaSearchDelegateDiscovery.getInstance();
assertNull(del);

System.setProperty(DELEGATE_SYSPROP, "unknownVal");
del = JavaSearchDelegateDiscovery.getInstance();
assertNull(del);
} finally {
if( oldSystemProperty != null )
System.setProperty(DELEGATE_SYSPROP, oldSystemProperty);
else
System.clearProperty(DELEGATE_SYSPROP);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class RunJavaSearchTests extends junit.framework.TestCase {
static {
// All test suites put in this list should use the same tests projects
// (eg. JavaSearch and JavaSearch15)
TEST_CLASSES.add(JavaSearchExtensionTest.class);
TEST_CLASSES.add(JavaSearchTests.class);
TEST_CLASSES.add(JavaSearchGenericTypeTests.class);
TEST_CLASSES.add(JavaSearchGenericTypeEquivalentTests.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright (c) 2025 Red Hat Inc and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.core.tests.model;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.search.IJavaSearchDelegate;
import org.eclipse.jdt.internal.core.search.matching.MatchLocator;
import org.eclipse.jdt.internal.core.search.matching.PossibleMatch;

public class TestJavaSearchDelegate implements IJavaSearchDelegate {

public TestJavaSearchDelegate() {
// TODO Auto-generated constructor stub
}

@Override
public void locateMatches(MatchLocator locator, IJavaProject javaProject, PossibleMatch[] possibleMatches,
int start, int length) throws CoreException {
// do nothing, stub
}

}
16 changes: 16 additions & 0 deletions org.eclipse.jdt.core/.settings/.api_filters
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,20 @@
</message_arguments>
</filter>
</resource>
<resource path="search/org/eclipse/jdt/core/search/IJavaSearchDelegate.java" type="org.eclipse.jdt.core.search.IJavaSearchDelegate">
<filter id="643846161">
<message_arguments>
<message_argument value="MatchLocator"/>
<message_argument value="IJavaSearchDelegate"/>
<message_argument value="locateMatches(MatchLocator, IJavaProject, PossibleMatch[], int, int)"/>
</message_arguments>
</filter>
<filter id="643846161">
<message_arguments>
<message_argument value="PossibleMatch"/>
<message_argument value="IJavaSearchDelegate"/>
<message_argument value="locateMatches(MatchLocator, IJavaProject, PossibleMatch[], int, int)"/>
</message_arguments>
</filter>
</resource>
</component>
1 change: 1 addition & 0 deletions org.eclipse.jdt.core/plugin.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ codeFormattersName=Source Code Formatters
compilationParticipantsName=Compilation Participants
compilationUnitResolverName=Compilation Unit Resolver
completionEngineProviderName=Completion Engine Provider
javaSearchDelegateName=Java Search Delegate
annotationProcessorManagerName=Java 6 Annotation Processor Manager
javaTaskName=Java Task
javaPropertiesName=Java Properties File
Expand Down
7 changes: 7 additions & 0 deletions org.eclipse.jdt.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@
<extension-point name="%completionEngineProviderName"
id="completionEngineProvider"
schema="schema/completionEngineProvider.exsd"/>

<!-- =================================================================================== -->
<!-- Extension Point: Java Search Delegate -->
<!-- =================================================================================== -->
<extension-point name="%javaSearchDelegateName"
id="javaSearchDelegate"
schema="schema/javaSearchDelegate.exsd"/>

<!-- =================================================================================== -->
<!-- Extension Point: Java 6 Annotation Processor Manager -->
Expand Down
121 changes: 121 additions & 0 deletions org.eclipse.jdt.core/schema/javaSearchDelegate.exsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?xml version='1.0' encoding='UTF-8'?>
<!-- Schema file written by PDE -->
<schema targetNamespace="org.eclipse.jdt.core" xmlns="http://www.w3.org/2001/XMLSchema">
<annotation>
<appInfo>
<meta.schema plugin="org.eclipse.jdt.core" id="javaSearchDelegate" name="Java Search Delegate"/>
</appInfo>
<documentation>
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.
</documentation>
</annotation>

<element name="extension">
<annotation>
<appInfo>
<meta.element />
</appInfo>
</annotation>
<complexType>
<sequence>
<element ref="searchDelegate" minOccurs="0" maxOccurs="1"/>
</sequence>
<attribute name="point" type="string" use="required">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
<attribute name="id" type="string">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>

</documentation>
<appInfo>
<meta.attribute translatable="true"/>
</appInfo>
</annotation>
</attribute>
</complexType>
</element>

<element name="searchDelegate">
<annotation>
<documentation>
Definition of a Java Search Delegate.
</documentation>
</annotation>
<complexType>
<attribute name="class" type="string" use="required">
<annotation>
<documentation>
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.
</documentation>
<appInfo>
<meta.attribute kind="java" basedOn=":org.eclipse.jdt.core.search.IJavaSearchDelegate"/>
</appInfo>
</annotation>
</attribute>
<attribute name="id" type="string" use="required">
<annotation>
<documentation>
A unique identifier for this search delegate
</documentation>
</annotation>
</attribute>
</complexType>
</element>

<annotation>
<appInfo>
<meta.section type="since"/>
</appInfo>
<documentation>
3.38
</documentation>
</annotation>

<annotation>
<appInfo>
<meta.section type="examples"/>
</appInfo>
<documentation>
Example of a declaration of a &lt;code&gt;IJavaSearchDelegate&lt;/code&gt;: &lt;pre&gt;
&lt;extension
point=&quot;org.eclipse.jdt.core.javaSearchDelegate&quot;&gt;
&lt;resolver
class=&quot;org.eclipse.jdt.core.MySearchDelegate&quot;
id=&quot;org.eclipse.jdt.core.MySearchDelegate&quot;&gt;
&lt;/resolver&gt;
&lt;/extension&gt;
&lt;/pre&gt;
</documentation>
</annotation>



<annotation>
<appInfo>
<meta.section type="copyright"/>
</appInfo>
<documentation>
Copyright (c) 2025 Red Hat, Inc. and others.&lt;br&gt;

This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
which accompanies this distribution, and is available at
&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;/

SPDX-License-Identifier: EPL-2.0
</documentation>
</annotation>

</schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2025 Red Hat Inc and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.core.search;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.internal.core.search.matching.MatchLocator;
import org.eclipse.jdt.internal.core.search.matching.PossibleMatch;

/**
* This interface represents a delegate that can take over the discovery of search results
* for java-based searches.
*
* This interface makes use of internal classes and is not considered stable or API.
*
* @since 3.41
*/
public interface IJavaSearchDelegate {

/**
* Fill the PossibleMatch objects' state with confirmed and possible results using the
* search strategy that this delegate employs.
*
* @param locator The MatchLocator initiating the request
* @param javaProject The context in which the search is being performed
* @param possibleMatches An array of possible matches
* @param start The start index with which to begin searching
* @param length The length of matches with which to search
* @throws CoreException
*/
void locateMatches(MatchLocator locator, IJavaProject javaProject, PossibleMatch[] possibleMatches, int start, int length) throws CoreException;

}
Loading

0 comments on commit 616c374

Please sign in to comment.