Skip to content

Commit e86a858

Browse files
committed
Merge branch 'pr-97'
2 parents f0ddade + e9cb37e commit e86a858

File tree

4 files changed

+74
-12
lines changed

4 files changed

+74
-12
lines changed

ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This is a bugfix release.
1313

1414
### Fixed Issues
1515

16+
* [#96](https://github.com/pmd/pmd-eclipse-plugin/issues/96): Wrong auxclasspath if project is stored outside of workspace
1617

1718
## 31-March-2019: 4.2.0.v20190331-1136
1819

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/EclipseUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public static boolean removePMDNature(final IProject project) throws CoreExcepti
213213
* @param project
214214
* @throws CoreException
215215
*/
216-
private static void addJavaNature(final IProject project) throws CoreException {
216+
public static void addJavaNature(final IProject project) throws CoreException {
217217
if (!project.hasNature(JavaCore.NATURE_ID)) {
218218
final IProjectDescription description = project.getDescription();
219219
final String[] prevNatures = description.getNatureIds();

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/ReviewCmdTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package net.sourceforge.pmd.eclipse.runtime.cmd;
66

77
import java.io.InputStream;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
810
import java.util.ArrayList;
911
import java.util.Arrays;
1012
import java.util.List;
@@ -14,8 +16,11 @@
1416
import org.eclipse.core.resources.IFile;
1517
import org.eclipse.core.resources.IMarker;
1618
import org.eclipse.core.resources.IProject;
19+
import org.eclipse.core.resources.IProjectDescription;
1720
import org.eclipse.core.resources.IResource;
21+
import org.eclipse.core.resources.IWorkspaceRoot;
1822
import org.eclipse.core.resources.IncrementalProjectBuilder;
23+
import org.eclipse.core.resources.ResourcesPlugin;
1924
import org.eclipse.core.runtime.CoreException;
2025
import org.junit.After;
2126
import org.junit.Assert;
@@ -155,6 +160,70 @@ public void testProjectBuildPath() throws Exception {
155160
}
156161
}
157162

163+
/**
164+
* https://github.com/pmd/pmd-eclipse-plugin/issues/96
165+
*/
166+
@Test
167+
public void testProjectBuildPathOutsideWorkspace() throws Exception {
168+
String projectName = "PMDTestProject2";
169+
Path tempDir = Files.createTempDirectory(projectName);
170+
boolean oldSetting = PMDPlugin.getDefault().getPreferencesManager().loadPreferences()
171+
.isProjectBuildPathEnabled();
172+
final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
173+
final IProject newProject = root.getProject(projectName);
174+
175+
try {
176+
final IProjectDescription description = newProject.getWorkspace().newProjectDescription(projectName);
177+
if (!newProject.exists()) {
178+
description.setLocationURI(tempDir.toUri());
179+
newProject.create(description, null);
180+
}
181+
182+
if (!newProject.isOpen()) {
183+
newProject.open(null);
184+
}
185+
EclipseUtils.addJavaNature(newProject);
186+
187+
IProjectProperties properties = PMDPlugin.getDefault().getPropertiesManager()
188+
.loadProjectProperties(newProject);
189+
properties.setPmdEnabled(true);
190+
Rule compareObjectsWithEquals = properties.getProjectRuleSet().getRuleByName("CompareObjectsWithEquals");
191+
RuleSet projectRuleSet = RuleSetUtil.newSingle(compareObjectsWithEquals);
192+
properties.setProjectRuleSet(projectRuleSet);
193+
194+
PMDPlugin.getDefault().getPreferencesManager().loadPreferences().setProjectBuildPathEnabled(true);
195+
EclipseUtils.createTestSourceFile(newProject, "/src/MyEnum.java", "public enum MyEnum { A, B }");
196+
IFile sourceFile = EclipseUtils.createTestSourceFile(newProject, "/src/Foo.java",
197+
"class Foo {\n" + " boolean bar(MyEnum a, MyEnum b) {\n" + " return a == b;\n" + // line 3
198+
" }\n" + "}");
199+
newProject.build(IncrementalProjectBuilder.FULL_BUILD, null);
200+
newProject.refreshLocal(IResource.DEPTH_INFINITE, null);
201+
202+
ReviewCodeCmd cmd = new ReviewCodeCmd();
203+
cmd.addResource(newProject);
204+
cmd.performExecute();
205+
cmd.join();
206+
Map<IFile, Set<MarkerInfo2>> markers = cmd.getMarkers();
207+
// with type resolution, this comparison is ok, as MyEnum is a enum
208+
Assert.assertTrue("Type Resolution didn't work", markers.get(sourceFile).isEmpty());
209+
210+
// without type resolution, there is a violation
211+
PMDPlugin.getDefault().getPreferencesManager().loadPreferences().setProjectBuildPathEnabled(false);
212+
cmd = new ReviewCodeCmd();
213+
cmd.addResource(newProject);
214+
cmd.performExecute();
215+
cmd.join();
216+
markers = cmd.getMarkers();
217+
// there is a violation expected without type resolution
218+
Assert.assertFalse(markers.get(sourceFile).isEmpty());
219+
220+
} finally {
221+
PMDPlugin.getDefault().getPreferencesManager().loadPreferences().setProjectBuildPathEnabled(oldSetting);
222+
223+
newProject.delete(true, true, null);
224+
}
225+
}
226+
158227
/**
159228
* The ReviewCodeCmd must also work on a ResourceDelta
160229
*/

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/JavaProjectClassLoader.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import org.apache.log4j.Logger;
1414
import org.eclipse.core.resources.IProject;
15-
import org.eclipse.core.resources.IWorkspaceRoot;
1615
import org.eclipse.core.runtime.CoreException;
1716
import org.eclipse.core.runtime.IPath;
1817
import org.eclipse.jdt.core.IClasspathEntry;
@@ -27,7 +26,6 @@ public class JavaProjectClassLoader extends URLClassLoader {
2726
private static final Logger LOG = Logger.getLogger(JavaProjectClassLoader.class);
2827

2928
private Set<IJavaProject> javaProjects = new HashSet<IJavaProject>();
30-
private IWorkspaceRoot workspaceRoot;
3129

3230
public JavaProjectClassLoader(ClassLoader parent, IProject project) {
3331
super(new URL[0], parent);
@@ -38,14 +36,12 @@ public JavaProjectClassLoader(ClassLoader parent, IProject project) {
3836
} catch (CoreException e) {
3937
throw new IllegalArgumentException("The project " + project + " is not a java project", e);
4038
}
41-
workspaceRoot = project.getProject().getWorkspace().getRoot();
4239

4340
IJavaProject javaProject = JavaCore.create(project);
4441
addURLs(javaProject, false);
4542

4643
// No longer need these things, drop references
4744
javaProjects = null;
48-
workspaceRoot = null;
4945
}
5046

5147
private static IProject projectFor(IJavaProject javaProject, IClasspathEntry classpathEntry) {
@@ -62,7 +58,8 @@ private void addURLs(IJavaProject javaProject, boolean exportsOnly) {
6258

6359
try {
6460
// Add default output location
65-
addURL(javaProject.getOutputLocation());
61+
IPath projectLocation = javaProject.getProject().getLocation();
62+
addURL(projectLocation.append(javaProject.getOutputLocation().removeFirstSegments(1)));
6663

6764
// Add each classpath entry
6865
IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
@@ -89,7 +86,7 @@ private void addURLs(IJavaProject javaProject, boolean exportsOnly) {
8986
case IClasspathEntry.CPE_SOURCE:
9087
IPath outputLocation = classpathEntry.getOutputLocation();
9188
if (outputLocation != null) {
92-
addURL(outputLocation);
89+
addURL(projectLocation.append(outputLocation.removeFirstSegments(1)));
9390
}
9491
break;
9592

@@ -113,11 +110,6 @@ private void addURL(IClasspathEntry classpathEntry) {
113110

114111
private void addURL(IPath path) {
115112
try {
116-
if (workspaceRoot.exists(path)) {
117-
// path = workspaceRoot.getFileForLocation(path).getFullPath();
118-
// path = workspaceRoot.getFile(path).getFullPath();
119-
path = workspaceRoot.getLocation().append(path);
120-
}
121113
URL url = path.toFile().getAbsoluteFile().toURI().toURL();
122114
addURL(url);
123115
} catch (MalformedURLException e) {

0 commit comments

Comments
 (0)