Skip to content

Commit

Permalink
Merge pull request #5429 from timothyjward/fix/m2e-version
Browse files Browse the repository at this point in the history
[m2e] Avoid runtime errors due to API changes between 1.x and 2.x
  • Loading branch information
bjhargrave authored Nov 14, 2022
2 parents b793ce4 + c8ea019 commit 754f87f
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 14 deletions.
21 changes: 19 additions & 2 deletions bndtools.m2e/src/bndtools/m2e/AbstractMavenRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import java.io.File;
import java.io.InputStream;
import java.util.List;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.eclipse.m2e.core.project.IMavenProjectChangedListener;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.IMavenProjectRegistry;
import org.eclipse.m2e.core.project.MavenProjectChangedEvent;
import org.eclipse.osgi.service.datalocation.Location;
import org.osgi.service.repository.Repository;

Expand Down Expand Up @@ -81,8 +84,8 @@ File guessBundleFile(IMavenProjectFacade projectFacade) {

ArtifactKey artifactKey = projectFacade.getArtifactKey();

String finalNameGuess = buildDirectoryGuess + "/" + artifactKey.getArtifactId() + "-" + artifactKey.getVersion()
+ ".jar";
String finalNameGuess = buildDirectoryGuess + "/" + ArtifactKeyHelper.getArtifactId(artifactKey) + "-"
+ ArtifactKeyHelper.getVersion(artifactKey) + ".jar";

return new File(finalNameGuess);
}
Expand All @@ -91,4 +94,18 @@ void cleanup() {
mavenProjectRegistry.removeMavenProjectChangedListener(this);
}

/**
* Needed in M2E version 2.0
*/
public void mavenProjectChanged(List<MavenProjectChangedEvent> events, IProgressMonitor monitor) {
mavenProjectChanged(events.toArray(new MavenProjectChangedEvent[0]), monitor);
}

/**
* Needed in M2E version 2.0 to link to when the interface doesn't include
* it
*/
@Override
public abstract void mavenProjectChanged(MavenProjectChangedEvent[] events, IProgressMonitor monitor);

}
44 changes: 44 additions & 0 deletions bndtools.m2e/src/bndtools/m2e/ArtifactKeyHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package bndtools.m2e;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.eclipse.m2e.core.embedder.ArtifactKey;

import aQute.bnd.exceptions.Exceptions;

public class ArtifactKeyHelper {

public static String getGroupId(ArtifactKey key) {
return doExtract(key, "getGroupId", "groupId");
}

public static String getArtifactId(ArtifactKey key) {
return doExtract(key, "getArtifactId", "artifactId");
}

public static String getVersion(ArtifactKey key) {
return doExtract(key, "getVersion", "version");
}

public static String getClassifier(ArtifactKey key) {
return doExtract(key, "getClassifier", "classifier");
}

private static String doExtract(ArtifactKey key, String getter, String property) {
Method m;
try {
try {
m = ArtifactKey.class.getMethod(getter);
} catch (NoSuchMethodException nsme) {
m = ArtifactKey.class.getMethod(property);
}
return (String) m.invoke(key);
} catch (InvocationTargetException ite) {
Exceptions.duck(ite.getCause());
} catch (Exception e) {
Exceptions.duck(e);
}
return null;
}
}
15 changes: 8 additions & 7 deletions bndtools.m2e/src/bndtools/m2e/BndConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,20 +254,21 @@ private boolean needsBuilding(IResourceDelta delta, IMavenProjectFacade projectF
private boolean isOurArtifact(String lastSegment, IMavenProjectFacade projectFacade) {
ArtifactKey artifactKey = projectFacade.getArtifactKey();
String artifact = null;
if (artifactKey.getClassifier() == null) {
artifact = String.format(ARTIFACT_PATTERN, artifactKey.getArtifactId(), artifactKey.getVersion(),
projectFacade.getMavenProject()
if (ArtifactKeyHelper.getClassifier(artifactKey) == null) {
artifact = String.format(ARTIFACT_PATTERN, ArtifactKeyHelper.getArtifactId(artifactKey),
ArtifactKeyHelper.getVersion(artifactKey), projectFacade.getMavenProject()
.getPackaging());
} else {
artifact = String.format(CLASSIFIER_ARTIFACT_PATTERN, artifactKey.getArtifactId(), artifactKey.getVersion(),
artifactKey.getClassifier(), projectFacade.getMavenProject()
artifact = String.format(CLASSIFIER_ARTIFACT_PATTERN, ArtifactKeyHelper.getArtifactId(artifactKey),
ArtifactKeyHelper.getVersion(artifactKey), ArtifactKeyHelper.getClassifier(artifactKey),
projectFacade.getMavenProject()
.getPackaging());
}
if (artifact.equals(lastSegment)) {
return true;
}
artifact = String.format(CLASSIFIER_ARTIFACT_PATTERN, artifactKey.getArtifactId(), artifactKey.getVersion(),
"tests", projectFacade.getMavenProject()
artifact = String.format(CLASSIFIER_ARTIFACT_PATTERN, ArtifactKeyHelper.getArtifactId(artifactKey),
ArtifactKeyHelper.getVersion(artifactKey), "tests", projectFacade.getMavenProject()
.getPackaging());
return artifact.equals(lastSegment);
}
Expand Down
33 changes: 29 additions & 4 deletions bndtools.m2e/src/bndtools/m2e/IndexConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.eclipse.core.resources.IResourceChangeEvent.POST_BUILD;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -111,11 +112,11 @@ public List<String> findVersions(org.eclipse.aether.artifact.Artifact artifact)
List<String> versions = new ArrayList<>();
for (IMavenProjectFacade facade : registry.getProjects()) {
ArtifactKey key = facade.getArtifactKey();
if (key.getArtifactId()
if (ArtifactKeyHelper.getArtifactId(key)
.equals(artifact.getArtifactId())
&& key.getGroupId()
&& ArtifactKeyHelper.getGroupId(key)
.equals(artifact.getGroupId())) {
versions.add(key.getVersion());
versions.add(ArtifactKeyHelper.getVersion(key));
}
}
return versions;
Expand Down Expand Up @@ -367,7 +368,7 @@ private static MavenProject getMavenProject(final IMavenProjectFacade projectFac
*/
@Override
public void resourceChanged(final IResourceChangeEvent event) {
projects: for (IMavenProjectFacade facade : getRegistry().getProjects()) {
projects: for (IMavenProjectFacade facade : safeGetProjects()) {
IProject currentProject = facade.getProject();
synchronized (pendingJobs) {
RebuildIndexCheck existing = pendingJobs.get(currentProject);
Expand Down Expand Up @@ -432,4 +433,28 @@ public void resourceChanged(final IResourceChangeEvent event) {
}
}
}

/**
* Needed to work safely with M2E 1.x and 2.x simultaneously
*
* @return
*/
@SuppressWarnings("unchecked")
private IMavenProjectFacade[] safeGetProjects() {
Object projects;
try {
projects = IMavenProjectRegistry.class.getMethod("getProjects")
.invoke(getRegistry());
} catch (InvocationTargetException e) {
throw Exceptions.duck(e.getTargetException());
} catch (Exception e) {
throw Exceptions.duck(e);
}

if (projects instanceof List) {
return ((List<?>) projects).toArray(new IMavenProjectFacade[0]);
} else {
return (IMavenProjectFacade[]) projects;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package bndtools.m2e;

import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.m2e.core.project.IMavenProjectChangedListener;
import org.eclipse.m2e.core.project.MavenProjectChangedEvent;
Expand All @@ -19,6 +21,23 @@ public MavenProjectChangedListenersTracker() {
open();
}

/**
* Needed in M2E version 2.0
*/
public void mavenProjectChanged(List<MavenProjectChangedEvent> events, IProgressMonitor monitor) {
getTracked().values()
.stream()
.forEach(listener -> {
try {
IMavenProjectChangedListener.class
.getMethod("mavenProjectChanged", List.class, IProgressMonitor.class)
.invoke(listener, events, monitor);
} catch (Throwable t) {
logger.error(t.getMessage(), t);
}
});
}

@Override
public void mavenProjectChanged(MavenProjectChangedEvent[] events, IProgressMonitor monitor) {
getTracked().values()
Expand Down
34 changes: 33 additions & 1 deletion bndtools.m2e/src/bndtools/m2e/MavenWorkspaceRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.File;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -249,6 +250,13 @@ public List<String> list(String pattern) throws Exception {
return bsns;
}

/**
* Needed in M2E version 2.0
*/
public void mavenProjectChanged(List<MavenProjectChangedEvent> events, IProgressMonitor monitor) {
mavenProjectChanged(events.toArray(new MavenProjectChangedEvent[0]), monitor);
}

@Override
public void mavenProjectChanged(MavenProjectChangedEvent[] events, IProgressMonitor monitor) {
boolean changed = false;
Expand Down Expand Up @@ -367,14 +375,38 @@ protected boolean isValid(IProject project) {
private boolean process() {
boolean changed = false;
IProgressMonitor monitor = new NullProgressMonitor();
for (IMavenProjectFacade projectFacade : mavenProjectRegistry.getProjects()) {
for (IMavenProjectFacade projectFacade : safeGetProjects()) {
if (process(projectFacade, monitor)) {
changed = true;
}
}
return changed;
}

/**
* Needed to work safely with M2E 1.x and 2.x simultaneously
*
* @return
*/
@SuppressWarnings("unchecked")
private IMavenProjectFacade[] safeGetProjects() {
Object projects;
try {
projects = IMavenProjectRegistry.class.getMethod("getProjects")
.invoke(mavenProjectRegistry);
} catch (InvocationTargetException e) {
throw Exceptions.duck(e.getTargetException());
} catch (Exception e) {
throw Exceptions.duck(e);
}

if (projects instanceof List) {
return ((List<?>) projects).toArray(new IMavenProjectFacade[0]);
} else {
return (IMavenProjectFacade[]) projects;
}
}

private boolean process(IMavenProjectFacade projectFacade, IProgressMonitor monitor) {
Set<Artifact> collected = collect(projectFacade, monitor);
if (collected.isEmpty()) {
Expand Down

0 comments on commit 754f87f

Please sign in to comment.