Skip to content

Commit 27c93aa

Browse files
committed
Fix 'Cannot nest <source folder>'
1 parent 5beb82e commit 27c93aa

File tree

10 files changed

+244
-12
lines changed

10 files changed

+244
-12
lines changed

org.eclipse.m2e.apt.core/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %Bundle-Name
44
Bundle-SymbolicName: org.eclipse.m2e.apt.core;singleton:=true
5-
Bundle-Version: 2.3.100.qualifier
5+
Bundle-Version: 2.3.200.qualifier
66
Bundle-Localization: plugin
77
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.27.0,4.0.0)",
88
org.eclipse.core.resources,

org.eclipse.m2e.apt.core/src/org/eclipse/m2e/apt/internal/AbstractAptConfiguratorDelegate.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ private List<File> getJars(List<File> files) {
238238
@Override
239239
public void configureClasspath(IClasspathDescriptor classpath, IProgressMonitor monitor) throws CoreException {
240240

241+
removeStaled(classpath);
241242
AnnotationProcessorConfiguration configuration = getAnnotationProcessorConfiguration(monitor);
242243

243244
if((configuration == null) || !configuration.isAnnotationProcessingEnabled()) {
@@ -259,6 +260,10 @@ public void configureClasspath(IClasspathDescriptor classpath, IProgressMonitor
259260
File outputFolder = new File(mavenProject.getBuild().getTestOutputDirectory());
260261
addToClassPath(eclipseProject, generatedTestSourcesDirectory, outputFolder, classpath, true);
261262
}
263+
if (generatedSourcesDirectory != null || generatedTestSourcesDirectory != null) {
264+
IJavaProject javaProject = JavaCore.create(eclipseProject);
265+
javaProject.setRawClasspath(classpath.getEntries(), monitor);
266+
}
262267
}
263268

264269
protected abstract AnnotationProcessorConfiguration getAnnotationProcessorConfiguration(IProgressMonitor monitor)
@@ -283,11 +288,9 @@ private void addToClassPath(IProject project, File sourceDirectory, File targetD
283288
IPath[] includes = new IPath[] {};
284289
IPath[] excludes = new IPath[] {};
285290

286-
// If the source folder is non-nested, add it
291+
// If the source folder doesn't exist, add it
287292
if((generatedSourcesFolder != null) && generatedSourcesFolder.getProject().equals(project)) {
288-
IClasspathEntryDescriptor enclosing = getEnclosingEntryDescriptor(classpath,
289-
generatedSourcesFolder.getFullPath());
290-
if((enclosing == null) || (getEntryDescriptor(classpath, generatedSourcesFolder.getFullPath()) != null)) {
293+
if(getEntryDescriptor(classpath, generatedSourcesFolder.getFullPath()) == null) {
291294
IClasspathEntryDescriptor entry = classpath.addSourceEntry(generatedSourcesFolder.getFullPath(), outputPath,
292295
includes, excludes, true);
293296
entry.setClasspathAttribute(IClasspathAttribute.IGNORE_OPTIONAL_PROBLEMS, "true"); //$NON-NLS-1$
@@ -331,20 +334,25 @@ private static IClasspathEntryDescriptor getEnclosingEntryDescriptor(IClasspathD
331334
return null;
332335
}
333336

334-
private IClasspathEntryDescriptor getEntryDescriptor(IClasspathDescriptor classpath, IPath fullPath) {
337+
private void removeStaled(IClasspathDescriptor classpath) {
335338
List<IPath> stalePaths = new ArrayList<>();
336-
IClasspathEntryDescriptor matchingDescriptor = null;
337339
for(IClasspathEntryDescriptor cped : classpath.getEntryDescriptors()) {
338-
if(cped.getPath().equals(fullPath)) {
339-
matchingDescriptor = cped;
340-
} else if(Boolean.parseBoolean(cped.getClasspathAttributes().get(M2E_APT_KEY))) {
340+
if(Boolean.parseBoolean(cped.getClasspathAttributes().get(M2E_APT_KEY))) {
341341
stalePaths.add(cped.getPath());
342342
}
343343
}
344344
for(IPath stalePath : stalePaths) {
345345
classpath.removeEntry(stalePath);
346346
}
347-
return matchingDescriptor;
347+
}
348+
349+
private IClasspathEntryDescriptor getEntryDescriptor(IClasspathDescriptor classpath, IPath fullPath) {
350+
for(IClasspathEntryDescriptor cped : classpath.getEntryDescriptors()) {
351+
if(cped.getPath().equals(fullPath)) {
352+
return cped;
353+
}
354+
}
355+
return null;
348356
}
349357

350358
protected <T> T getParameterValue(String parameter, Class<T> asType, MojoExecution mojoExecution)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>org.sample</groupId>
7+
<artifactId>nested-test</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
</properties>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.mapstruct</groupId>
17+
<artifactId>mapstruct</artifactId>
18+
<version>1.6.3</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.mapstruct</groupId>
22+
<artifactId>mapstruct-processor</artifactId>
23+
<version>1.6.3</version>
24+
<scope>provided</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.slf4j</groupId>
28+
<artifactId>slf4j-api</artifactId>
29+
<version>1.7.36</version>
30+
<exclusions>
31+
<exclusion>
32+
<groupId>org.mapstruct</groupId>
33+
<artifactId>mapstruct-processor</artifactId>
34+
</exclusion>
35+
</exclusions>
36+
</dependency>
37+
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.codehaus.mojo</groupId>
44+
<artifactId>build-helper-maven-plugin</artifactId>
45+
<version>3.2.0</version>
46+
<executions>
47+
<execution>
48+
<?m2e execute onConfiguration,onIncremental?>
49+
<id>add-source</id>
50+
<phase>generate-sources</phase>
51+
<goals>
52+
<goal>add-source</goal>
53+
</goals>
54+
<configuration>
55+
<sources>
56+
<source>${basedir}/target/generated-sources/</source>
57+
</sources>
58+
</configuration>
59+
</execution>
60+
</executions>
61+
</plugin>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-compiler-plugin</artifactId>
65+
<version>3.8.1</version>
66+
<configuration>
67+
<release>21</release>
68+
<compilerArgs>
69+
<arg>-parameters</arg>
70+
<arg>-Xlint:all</arg>
71+
</compilerArgs>
72+
73+
</configuration>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.mapper;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Person person = new Person(1l, "test");
6+
PersonDto personDto = PersonMapper.INSTANCE.personToPersonDto(person);
7+
System.out.println(personDto);
8+
}
9+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.mapper;
2+
3+
public class Person {
4+
5+
private Long id;
6+
private String name;
7+
8+
public Person(Long id, String name) {
9+
this.id = id;
10+
this.name = name;
11+
}
12+
13+
public Long getId() {
14+
return id;
15+
}
16+
17+
public void setId(Long id) {
18+
this.id = id;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return "Person [id=" + id + ", name=" + name + "]";
32+
}
33+
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.mapper;
2+
3+
public class PersonDto {
4+
5+
private Long id;
6+
private String name;
7+
8+
public Long getId() {
9+
return id;
10+
}
11+
12+
public void setId(Long id) {
13+
this.id = id;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "PersonDto [id=" + id + ", name=" + name + "]";
27+
}
28+
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.mapper;
2+
3+
import org.mapstruct.Mapper;
4+
import org.mapstruct.factory.Mappers;
5+
6+
@Mapper
7+
public interface PersonMapper {
8+
PersonMapper INSTANCE = Mappers.getMapper(PersonMapper.class);
9+
PersonDto personToPersonDto(Person person);
10+
}

org.eclipse.m2e.apt.tests/src/org/eclipse/m2e/apt/tests/M2eAptProjectconfiguratorTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,13 @@ public void testDependencyManagement() throws Exception {
444444
assertNotNull(javaProject);
445445
assertNoErrors(p);
446446
}
447+
448+
@Test
449+
public void testNestedSourceFolders() throws Exception {
450+
IProject project = importProject("projects/nested/pom.xml");
451+
waitForJobsToComplete();
452+
IJavaProject javaProject = JavaCore.create(project);
453+
assertNotNull(javaProject);
454+
assertNoErrors(project);
455+
}
447456
}

org.eclipse.m2e.jdt/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %Bundle-Name
44
Bundle-SymbolicName: org.eclipse.m2e.jdt;singleton:=true
5-
Bundle-Version: 2.5.100.qualifier
5+
Bundle-Version: 2.5.101.qualifier
66
Bundle-Localization: plugin
77
Export-Package: org.eclipse.m2e.jdt,
88
org.eclipse.m2e.jdt.internal;x-friends:="org.eclipse.m2e.jdt.ui",

org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/ClasspathDescriptor.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
import org.eclipse.jdt.core.IClasspathAttribute;
2626
import org.eclipse.jdt.core.IClasspathEntry;
2727
import org.eclipse.jdt.core.IJavaProject;
28+
import org.eclipse.jdt.core.JavaCore;
2829
import org.eclipse.jdt.core.JavaModelException;
30+
import org.eclipse.jdt.internal.core.ClasspathEntry;
31+
import org.eclipse.jdt.internal.core.util.Util;
2932

3033
import org.eclipse.m2e.jdt.IClasspathDescriptor;
3134
import org.eclipse.m2e.jdt.IClasspathEntryDescriptor;
@@ -148,6 +151,59 @@ public IClasspathEntry[] getEntries() {
148151
}
149152
}
150153

154+
// check nested source entries
155+
List<IClasspathEntry> toRemove = null;
156+
List<IClasspathEntry> toAdd = null;
157+
for(IClasspathEntry entry : result) {
158+
int kind = entry.getEntryKind();
159+
IPath entryPath = entry.getPath();
160+
if(kind == IClasspathEntry.CPE_SOURCE) {
161+
for(IClasspathEntry otherEntry : result) {
162+
int otherKind = otherEntry.getEntryKind();
163+
if(otherKind != IClasspathEntry.CPE_SOURCE)
164+
continue;
165+
if(entry != otherEntry) {
166+
IPath otherPath = otherEntry.getPath();
167+
char[][] inclusionPatterns, exclusionPatterns;
168+
if(otherPath.isPrefixOf(entryPath) && !otherPath.equals(entryPath)
169+
&& !Util.isExcluded(entryPath.append("*"), //$NON-NLS-1$
170+
inclusionPatterns = ((ClasspathEntry) otherEntry).fullInclusionPatternChars(),
171+
exclusionPatterns = ((ClasspathEntry) otherEntry).fullExclusionPatternChars(), false)) {
172+
String exclusionPattern = entryPath.removeFirstSegments(otherPath.segmentCount()).segment(0);
173+
if(!Util.isExcluded(entryPath, inclusionPatterns, exclusionPatterns, false)) {
174+
IPath[] newExclusions;
175+
IPath[] oldExclusions = otherEntry.getExclusionPatterns();
176+
if(otherEntry.getExclusionPatterns() == null)
177+
newExclusions = new IPath[2];
178+
else {
179+
newExclusions = new IPath[oldExclusions.length + 2];
180+
System.arraycopy(oldExclusions, 0, newExclusions, 0, oldExclusions.length);
181+
}
182+
IPath path = entryPath.removeFirstSegments(otherPath.segmentCount()).addTrailingSeparator();
183+
IPath projectRelativePath = entryPath.removeFirstSegments(1).addTrailingSeparator();
184+
newExclusions[newExclusions.length - 1] = projectRelativePath;
185+
newExclusions[newExclusions.length - 2] = path;
186+
if(toRemove == null || toAdd == null) {
187+
toRemove = new ArrayList<>();
188+
toAdd = new ArrayList<>();
189+
}
190+
IClasspathEntry newOtherEntry = JavaCore.newSourceEntry(otherEntry.getPath(),
191+
otherEntry.getInclusionPatterns(), newExclusions, otherEntry.getOutputLocation(),
192+
otherEntry.getExtraAttributes());
193+
toRemove.add(otherEntry);
194+
toAdd.add(newOtherEntry);
195+
}
196+
}
197+
}
198+
}
199+
}
200+
}
201+
if(toRemove != null) {
202+
toRemove.stream().forEach(e -> result.remove(e));
203+
}
204+
if(toAdd != null) {
205+
toAdd.stream().forEach(e -> result.add(e));
206+
}
151207
return result.toArray(new IClasspathEntry[result.size()]);
152208
}
153209

0 commit comments

Comments
 (0)