Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Commit

Permalink
Merge pull request #261 from WonderCsabo/182_doNotDropClassPathAttrib…
Browse files Browse the repository at this point in the history
…utes

Modifying classpath takes previous attributes into account
  • Loading branch information
rgladwell committed Mar 22, 2015
2 parents c8176a4 + fd1b7c8 commit 5822e79
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 17 deletions.
Expand Up @@ -81,6 +81,9 @@
<configuration>
<source>1.6</source>
<target>1.6</target>
<includes>
<include>**/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
Expand Down
Expand Up @@ -24,7 +24,6 @@
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
Expand Down
Expand Up @@ -9,11 +9,13 @@
package me.gladwell.eclipse.m2e.android.test;

import static java.io.File.separator;
import static me.gladwell.eclipse.m2e.android.configuration.Classpaths.findSourceEntry;
import static me.gladwell.eclipse.m2e.android.test.ClasspathMatchers.containsEntry;
import static me.gladwell.eclipse.m2e.android.test.ClasspathMatchers.containsIncludePattern;
import static me.gladwell.eclipse.m2e.android.test.ClasspathMatchers.hasAttribute;
import static org.eclipse.jdt.core.IClasspathAttribute.IGNORE_OPTIONAL_PROBLEMS;
import static org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants.ATTR_CLASSPATH_PROVIDER;
import static org.junit.Assert.assertThat;
import static me.gladwell.eclipse.m2e.android.configuration.Classpaths.findSourceEntry;
import static me.gladwell.eclipse.m2e.android.test.ClasspathMatchers.containsEntry;

import java.io.File;

Expand All @@ -31,9 +33,11 @@
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.junit.JUnitCore;
import org.eclipse.jdt.junit.TestRunListener;
import org.eclipse.jdt.junit.model.ITestElement;
Expand Down Expand Up @@ -286,4 +290,24 @@ public void testConfigureDoesNotSetIgnoreWarnings() throws Exception {
IClasspathEntry gen = findSourceEntry(javaProject.getRawClasspath(), "gen");
assertFalse("external assets folder isn't linked", booleanAttribute(IGNORE_OPTIONAL_PROBLEMS, gen));
}

public void testConfigureDoesNotRemoveIncludesFromEntry() throws Exception {
IClasspathEntry mainJava = findSourceEntry(javaProject.getRawClasspath(), "src" + separator + "main" + separator + "java");
assertThat(mainJava, containsIncludePattern("**" + separator + "*.java"));
}

public void testConfigureDoesNotRemovePomDerivedAttributeFromEntry() throws JavaModelException {
IClasspathEntry mainJava = findSourceEntry(javaProject.getRawClasspath(), "src" + separator + "main" + separator + "java");
assertThat(mainJava, hasAttribute(IClasspathManager.POMDERIVED_ATTRIBUTE, "true"));
}

public void testConfigureDoesNotRemoveOptionalAttributeFromEntry() throws Exception {
IClasspathEntry mainJava = findSourceEntry(javaProject.getRawClasspath(), "src" + separator + "main" + separator + "java");
assertThat(mainJava, hasAttribute(IClasspathAttribute.OPTIONAL, "true"));
}

public void testConfigureDoesNotRemovePomDerivedAttributeFromMavenContainer() throws Exception {
IClasspathEntry mavenContainer = getClasspathContainer(javaProject, IClasspathManager.CONTAINER_ID);
assertThat(mavenContainer, hasAttribute(IClasspathManager.POMDERIVED_ATTRIBUTE, "true"));
}
}
Expand Up @@ -8,6 +8,9 @@

package me.gladwell.eclipse.m2e.android.test;

import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
Expand Down Expand Up @@ -42,4 +45,58 @@ private static boolean containsEntry(String path, IRuntimeClasspathEntry[] class
}
return false;
}

public static Matcher<IClasspathEntry> containsIncludePattern(final String pattern) {
return new BaseMatcher<IClasspathEntry>() {

@Override
public boolean matches(Object target) {
IClasspathEntry entry = (IClasspathEntry) target;
return containsIncludePattern(pattern, entry);
}

@Override
public void describeTo(Description description) {
description.appendText("classpath entry containing include pattern") //
.appendText(pattern);
}
};
}

private static boolean containsIncludePattern(String pattern, IClasspathEntry entry) {
for (IPath inclusionPattern : entry.getInclusionPatterns()) {
if (inclusionPattern.toOSString().equals(pattern)) {
return true;
}
}
return false;
}

public static Matcher<IClasspathEntry> hasAttribute(final String attributeName, final String attributeValue) {
return new BaseMatcher<IClasspathEntry>() {

@Override
public boolean matches(Object target) {
IClasspathEntry entry = (IClasspathEntry) target;
return hasAttribute(entry, attributeName, attributeValue);
}

@Override
public void describeTo(Description description) {
description.appendText("classpath entry having attribute ") //
.appendText(attributeName) //
.appendText(" with value ") //
.appendText(attributeValue);
}
};
}

public static boolean hasAttribute(IClasspathEntry entry, String attributeName, String attributeValue) {
for (IClasspathAttribute attribute : entry.getExtraAttributes()) {
if (attribute.getName().equals(attributeName) && attribute.getValue().equals(attributeValue)) {
return true;
}
}
return false;
}
}
Expand Up @@ -5,25 +5,21 @@
import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.m2e.jdt.IClasspathDescriptor;
import org.eclipse.m2e.jdt.IClasspathEntryDescriptor;

public class EclipseSourceEntry implements SourceEntry {

private final IProject project;
private final IClasspathDescriptor classpath;
private final IClasspathEntryDescriptor entry;

public EclipseSourceEntry(IProject project, IClasspathDescriptor classpath, IClasspathEntryDescriptor entry) {
public EclipseSourceEntry(IProject project, IClasspathEntryDescriptor entry) {
super();
this.project = project;
this.classpath = classpath;
this.entry = entry;
}

public void setOutputLocation(String path) {
classpath.removeEntry(entry.getPath());
classpath.addSourceEntry(entry.getPath(), project.getFullPath().append(path), true);
entry.setOutputLocation(project.getFullPath().append(path));
}

public String getOutputLocation() {
Expand Down
Expand Up @@ -46,7 +46,7 @@ public Iterable<SourceEntry> getSourceEntries() {
List<SourceEntry> entries = new ArrayList<SourceEntry>();
for(IClasspathEntryDescriptor entry : classpath.getEntryDescriptors()) {
if(entry.getOutputLocation() != null && entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
entries.add(new EclipseSourceEntry(project.getProject(), classpath, entry));
entries.add(new EclipseSourceEntry(project.getProject(), entry));
}
}
return entries;
Expand Down Expand Up @@ -84,16 +84,18 @@ public void addSourceEntry(String path) {
}

public void markExported(String path) {
IClasspathEntry oldEntry = findContainerMatching(classpath, path);
IClasspathEntry newEntry = newContainerEntry(oldEntry.getPath(), true);
classpath.removeEntry(oldEntry.getPath());
classpath.addEntry(newEntry);
setClassPathEntryExported(path, true);
}

public void markNotExported(String path) {
setClassPathEntryExported(path, false);
}

private void setClassPathEntryExported(String path, boolean exported) {
IClasspathEntry oldEntry = findContainerMatching(classpath, path);
if(oldEntry != null) {
IClasspathEntry newEntry = newContainerEntry(oldEntry.getPath(), false);
if (oldEntry != null) {
IClasspathEntry newEntry = newContainerEntry(oldEntry.getPath(), oldEntry.getAccessRules(),
oldEntry.getExtraAttributes(), exported);
classpath.removeEntry(oldEntry.getPath());
classpath.addEntry(newEntry);
} else {
Expand All @@ -102,7 +104,7 @@ public void markNotExported(String path) {
}

public SourceEntry getSourceEntry(String path) {
return new EclipseSourceEntry(project.getProject(), classpath, findSourceEntryDescriptor(classpath, path));
return new EclipseSourceEntry(project.getProject(), findSourceEntryDescriptor(classpath, path));
}

}

0 comments on commit 5822e79

Please sign in to comment.