Skip to content

Commit 0edde57

Browse files
committed
Avoid detecting JUnit version based on restricted annotations
CoreTestSearchEngine.hasJUnit6TestAnnotation() will detect JUnit 6 if a classpath entry for junit-platform-commons 6 exists, but has forbidden access rule restriction. This change adjusts the search for the JUnit 5 and 6 annotation types to also consider classpath access rules. Fixes: #2830
1 parent 679f20c commit 0edde57

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/util/CoreTestSearchEngine.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
import org.eclipse.jdt.core.search.SearchPattern;
5353
import org.eclipse.jdt.core.search.SearchRequestor;
5454

55+
import org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner;
56+
import org.eclipse.jdt.internal.core.JavaProject;
57+
import org.eclipse.jdt.internal.core.NameLookup;
5558
import org.eclipse.jdt.internal.core.PackageFragmentRoot;
5659
import org.eclipse.jdt.internal.junit.JUnitCorePlugin;
5760
import org.eclipse.jdt.internal.junit.buildpath.BuildPathSupport;
@@ -169,10 +172,10 @@ private static boolean hasJUnitJupiterTestAnnotation(IJavaProject project, int j
169172
try {
170173
if (project != null) {
171174
String junitBundlePrefix = JUNIT_PLATFORM_COMMONS_PREFIX;
172-
IType type= project.findType(JUnitCorePlugin.JUNIT5_TESTABLE_ANNOTATION_NAME);
175+
IType type= findAnnotation(project, JUnitCorePlugin.JUNIT5_TESTABLE_ANNOTATION_NAME);
173176
if (type == null) {
174177
junitBundlePrefix = JUNIT_PLATFORM_SUITE_API_PREFIX;
175-
type= project.findType(JUnitCorePlugin.JUNIT5_SUITE_ANNOTATION_NAME);
178+
type= findAnnotation(project, JUnitCorePlugin.JUNIT5_SUITE_ANNOTATION_NAME);
176179
}
177180
if (type != null) {
178181
// check if we have the right JUnit JUpiter version
@@ -331,4 +334,22 @@ public static void findSuiteMethods(IJavaElement element, Set<IType> result, IPr
331334
SearchParticipant[] participants= new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
332335
new SearchEngine().search(suitePattern, participants, scope, requestor, pm);
333336
}
337+
338+
private static IType findAnnotation(IJavaProject project, String fullyQualifiedName) throws JavaModelException {
339+
if (project instanceof JavaProject p) {
340+
NameLookup lookup= p.newNameLookup(DefaultWorkingCopyOwner.PRIMARY);
341+
NameLookup.Answer answer = lookup.findType(
342+
fullyQualifiedName,
343+
false /* no partial matches */,
344+
NameLookup.ACCEPT_ANNOTATIONS,
345+
false /* no secondary types */,
346+
true /* wait for indexer */,
347+
true /* check restrictions */,
348+
null);
349+
if (answer != null) {
350+
return answer.type;
351+
}
352+
}
353+
return null;
354+
}
334355
}

0 commit comments

Comments
 (0)