Skip to content

Commit

Permalink
Merge pull request #1180 from coehlrich/project-dependency-module
Browse files Browse the repository at this point in the history
Put module classpath entries that are projects on the modulepath for launch configurations
  • Loading branch information
donat authored Aug 30, 2023
2 parents 0449686 + 0575e38 commit 7e26d68
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -115,12 +116,12 @@ private void collectContainerRuntimeClasspathWithGradleScopes(IClasspathContaine
for (final IClasspathEntry cpe : container.getClasspathEntries()) {
if (!includeExportedEntriesOnly || cpe.isExported()) {
if (cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY && configurationScopes.isEntryIncluded(cpe)) {
addLibraryClasspathEntry(result, cpe, modular);
result.add(getRuntimeClasspathEntry(cpe.getPath(), modular, "Archive", JavaRuntime::newArchiveRuntimeClasspathEntry, IPath.class, cpe));
} else if (cpe.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
Optional<IProject> candidate = findAccessibleJavaProject(cpe.getPath().segment(0));
if (candidate.isPresent()) {
IJavaProject dependencyProject = JavaCore.create(candidate.get());
IRuntimeClasspathEntry projectRuntimeEntry = JavaRuntime.newProjectRuntimeClasspathEntry(dependencyProject);
IRuntimeClasspathEntry projectRuntimeEntry = getRuntimeClasspathEntry(dependencyProject, modular, "Project", JavaRuntime::newProjectRuntimeClasspathEntry, IJavaProject.class, cpe);
// add the project entry itself so that the source lookup can find the
// classes. See https://github.com/eclipse/buildship/issues/383
result.add(projectRuntimeEntry);
Expand All @@ -142,12 +143,12 @@ private IRuntimeClasspathEntry[] runtimeClasspathWithTestSources(IJavaProject pr

for (final IClasspathEntry cpe : container.getClasspathEntries()) {
if (cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY && !(excludeTestCode && hasTestAttribute(cpe))) {
addLibraryClasspathEntry(result, cpe, moduleSuppport);
result.add(getRuntimeClasspathEntry(cpe.getPath(), moduleSuppport, "Archive", JavaRuntime::newArchiveRuntimeClasspathEntry, IPath.class, cpe));
} else if (cpe.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
Optional<IProject> candidate = findAccessibleJavaProject(cpe.getPath().segment(0));
if (candidate.isPresent()) {
IJavaProject dependencyProject = JavaCore.create(candidate.get());
IRuntimeClasspathEntry projectRuntimeEntry = JavaRuntime.newProjectRuntimeClasspathEntry(dependencyProject);
IRuntimeClasspathEntry projectRuntimeEntry = getRuntimeClasspathEntry(dependencyProject, moduleSuppport, "Project", JavaRuntime::newProjectRuntimeClasspathEntry, IJavaProject.class, cpe);
// add the project entry itself so that the source lookup can find the classes
// see https://github.com/eclipse/buildship/issues/383
result.add(projectRuntimeEntry);
Expand All @@ -158,20 +159,20 @@ private IRuntimeClasspathEntry[] runtimeClasspathWithTestSources(IJavaProject pr
return result.toArray(new IRuntimeClasspathEntry[result.size()]);
}

private void addLibraryClasspathEntry(List<IRuntimeClasspathEntry> result, final IClasspathEntry cpe,boolean moduleSupport) {
private <T> IRuntimeClasspathEntry getRuntimeClasspathEntry(final T parameter, boolean moduleSupport, String type, Function<T, IRuntimeClasspathEntry> old, Class<T> parameterType, IClasspathEntry cpe) {
if (moduleSupport) {
try {
Method m = JavaRuntime.class.getMethod("newArchiveRuntimeClasspathEntry", IPath.class, int.class);
Method m = JavaRuntime.class.getMethod(String.format("new%sRuntimeClasspathEntry", type), parameterType, int.class);
int modulePathAttribute = IRuntimeClasspathEntry.class.getField("MODULE_PATH").getInt(null);
int classPathAttribute = IRuntimeClasspathEntry.class.getField("CLASS_PATH").getInt(null);
result.add( (IRuntimeClasspathEntry) m.invoke(null, cpe.getPath(), hasModuleAttribute(cpe) ? modulePathAttribute : classPathAttribute));
return (IRuntimeClasspathEntry) m.invoke(null, parameter, hasModuleAttribute(cpe) ? modulePathAttribute : classPathAttribute);
} catch (Exception e) {
// Method does not exist yet in this version of eclipse.
// Revert to old behavior
result.add(JavaRuntime.newArchiveRuntimeClasspathEntry(cpe.getPath()));
return old.apply(parameter);
}
} else {
result.add(JavaRuntime.newArchiveRuntimeClasspathEntry(cpe.getPath()));
return old.apply(parameter);
}
}

Expand Down

0 comments on commit 7e26d68

Please sign in to comment.