Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Get correct Java project in multi-module case #1865

Merged
merged 5 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import java.util.Optional;

import org.eclipse.core.internal.utils.FileUtil;
jdneo marked this conversation as resolved.
Show resolved Hide resolved
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
Expand Down Expand Up @@ -271,13 +272,13 @@ public static IJavaProject getJavaProjectFromUri(String uri) throws CoreExceptio

// For multi-module scenario
Arrays.sort(containers, (Comparator<IContainer>) (IContainer a, IContainer b) -> {
return a.getFullPath().toPortableString().length() - b.getFullPath().toPortableString().length();
return a.getFullPath().segmentCount() - b.getFullPath().segmentCount();
});

IJavaElement targetElement = null;
for (IContainer container : containers) {
targetElement = JavaCore.create(container.getProject());
if (targetElement != null) {
if (targetElement != null && targetElement.exists()) {
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse</groupId>
<artifactId>multimodule3</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>org.eclipse</groupId>
<artifactId>this_is_a_very_long_module_name</artifactId>
<version>1.0-SNAPSHOT</version>
<name>this_is_a_very_long_module_name</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.eclipse;

public class App {
public static final String GREETING = "Hello World!";

public static void main(String[] args) {
System.out.println(GREETING);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.eclipse;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class AppTest {
@Test
public void testApp() {
assertTrue(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ public void testGetMavenProjectFromUri() throws Exception {
assertNotNull("Can get project from project uri", javaProject);
}

@Test
public void testGetMultiModuleMavenProjectFromUri() throws Exception {
importProjects("maven/multimodule3");
IProject project = WorkspaceHelper.getProject("this_is_a_very_long_module_name");
String javaSource = project.getFile("src/main/org/eclipse/App.java").getLocationURI().toString();
IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(javaSource);
assertEquals("this_is_a_very_long_module_name", javaProject.getElementName());

String projectUri = project.getLocationURI().toString();
javaProject = ProjectCommand.getJavaProjectFromUri(projectUri);
assertEquals("this_is_a_very_long_module_name", javaProject.getElementName());
}

@Test
public void testGetInvisibleProjectFromUri() throws Exception {
IProject project = copyAndImportFolder("singlefile/simple", "src/App.java");
Expand Down