Skip to content

Commit

Permalink
Move org.eclipse.m2e.core
Browse files Browse the repository at this point in the history
+ Adopt named instanceof
+ adopt switch-expressions
+ Use Objects.equals/hash
  • Loading branch information
mickaelistria committed Jun 1, 2022
1 parent 53a40c8 commit f9c009d
Show file tree
Hide file tree
Showing 32 changed files with 134 additions and 226 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.m2e.core/.classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
Expand Down
6 changes: 3 additions & 3 deletions org.eclipse.m2e.core/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ org.eclipse.jdt.core.compiler.annotation.nullable.secondary=
org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
Expand Down Expand Up @@ -146,7 +146,7 @@ org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=warning
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=11
org.eclipse.jdt.core.compiler.source=17
org.eclipse.jdt.core.compiler.taskCaseSensitive=enabled
org.eclipse.jdt.core.compiler.taskPriorities=NORMAL,HIGH,HIGH
org.eclipse.jdt.core.compiler.taskTags=TODO,FIXME,XXX
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.m2e.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Require-Bundle: org.eclipse.osgi;bundle-version="3.10.0",
org.eclipse.core.filesystem;bundle-version="1.7.700",
org.apache.commons.codec;bundle-version="1.14.0"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-11
Bundle-RequiredExecutionEnvironment: JavaSE-17
Export-Package: org.eclipse.m2e.core,
org.eclipse.m2e.core.archetype;x-friends:="org.eclipse.m2e.core.ui",
org.eclipse.m2e.core.embedder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.m2e.core.embedder;

import java.io.Serializable;
import java.util.Objects;

import org.eclipse.osgi.util.NLS;

Expand Down Expand Up @@ -53,26 +54,15 @@ public ArtifactKey(String groupId, String artifactId, String version, String cla
public boolean equals(Object o) {
if(this == o)
return true;
if(o instanceof ArtifactKey) {
ArtifactKey other = (ArtifactKey) o;
return equals(groupId, other.groupId) && equals(artifactId, other.artifactId) && equals(version, other.version)
&& equals(classifier, other.classifier);
}
return false;
return o instanceof ArtifactKey other && //

Objects.equals(groupId, other.groupId) && Objects.equals(artifactId, other.artifactId)
&& Objects.equals(version, other.version) && Objects.equals(classifier, other.classifier);
}

@Override
public int hashCode() {
int hash = 17;
hash = hash * 31 + (groupId != null ? groupId.hashCode() : 0);
hash = hash * 31 + (artifactId != null ? artifactId.hashCode() : 0);
hash = hash * 31 + (version != null ? version.hashCode() : 0);
hash = hash * 31 + (classifier != null ? classifier.hashCode() : 0);
return hash;
}

private static boolean equals(Object o1, Object o2) {
return o1 == null ? o2 == null : o1.equals(o2);
return Objects.hash(groupId, artifactId, version, classifier);
}

// XXX this method does not belong here, it compares versions, while ArtifactKey uses baseVersions in many cases
Expand All @@ -83,8 +73,8 @@ public static boolean equals(Artifact a1, Artifact a2) {
if(a2 == null) {
return false;
}
return equals(a1.getGroupId(), a2.getGroupId()) && equals(a1.getArtifactId(), a2.getArtifactId())
&& equals(a1.getVersion(), a2.getVersion()) && equals(a1.getClassifier(), a2.getClassifier());
return Objects.equals(a1.getGroupId(), a2.getGroupId()) && Objects.equals(a1.getArtifactId(), a2.getArtifactId())
&& Objects.equals(a1.getVersion(), a2.getVersion()) && Objects.equals(a1.getClassifier(), a2.getClassifier());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.m2e.core.embedder;

import java.io.Serializable;
import java.util.Objects;

import org.apache.maven.artifact.repository.ArtifactRepository;

Expand Down Expand Up @@ -48,26 +49,15 @@ public String getUsername() {

@Override
public int hashCode() {
int hash = 17;
hash = hash * 31 + (id != null ? id.hashCode() : 0);
hash = hash * 31 + (url != null ? url.hashCode() : 0);
hash = hash * 31 + (username != null ? username.hashCode() : 0);
return hash;
return Objects.hash(id, url, username);
}

@Override
public boolean equals(Object o) {
if(o == this) {
return true;
}
if(!(o instanceof ArtifactRepositoryRef)) {
return false;
}
ArtifactRepositoryRef other = (ArtifactRepositoryRef) o;
return eq(id, other.id) && eq(url, other.url) && eq(username, other.username);
}

private static <T> boolean eq(T a, T b) {
return a != null ? a.equals(b) : b == null;
return o instanceof ArtifactRepositoryRef other && //
Objects.equals(id, other.id) && Objects.equals(url, other.url) && Objects.equals(username, other.username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public static IFile getPomFile(MavenProject project) {
}
while(path.segmentCount() > 1) {
IResource ires = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
if(ires instanceof IFile) {
stack.push((IFile) ires);
if(ires instanceof IFile f) {
stack.push(f);
}
path = path.removeFirstSegments(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public Set<IProject> build(MavenSession session, IMavenProjectFacade projectFaca
participant.setGetDeltaCallback(getDeltaProvider());
participant.setSession(session);
participant.setBuildContext((AbstractEclipseBuildContext) incrementalContexts.get(0));
if(participant instanceof InternalBuildParticipant2) {
((InternalBuildParticipant2) participant).setArgs(args);
if(participant instanceof InternalBuildParticipant2 participant2) {
participant2.setArgs(args);
}
long executionStartTime = System.currentTimeMillis();
try {
Expand All @@ -146,8 +146,8 @@ public Set<IProject> build(MavenSession session, IMavenProjectFacade projectFaca
participant.setGetDeltaCallback(null);
participant.setSession(null);
participant.setBuildContext(null);
if(participant instanceof InternalBuildParticipant2) {
((InternalBuildParticipant2) participant).setArgs(Collections.<String, String> emptyMap());
if(participant instanceof InternalBuildParticipant2 participant2) {
participant2.setArgs(Collections.<String, String> emptyMap());
}

processMavenSessionErrors(session, mojoExecutionKey, buildErrors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ protected IResource getResource(File file) {
return null;
}
IResource baseResource = getBaseResource();
if(baseResource instanceof IContainer) {
return ((IContainer) baseResource).findMember(relpath);
if(baseResource instanceof IContainer container) {
return container.findMember(relpath);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ protected void collectExtensions(IMavenLauncherConfiguration collector, IProgres
if(extensions != null) {
IMavenProjectRegistry registry = MavenPlugin.getMavenProjectRegistry();
for(ClasspathEntry entry : extensions) {
if(entry instanceof ProjectClasspathEntry) {
collectProject(collector, (ProjectClasspathEntry) entry, registry, monitor);
if(entry instanceof ProjectClasspathEntry projectClasspathEntry) {
collectProject(collector, projectClasspathEntry, registry, monitor);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/
public abstract class ClasspathEntry {
public String toExternalForm() {
if(this instanceof ProjectClasspathEntry) {
return "P/" + ((ProjectClasspathEntry) this).getProject();
if(this instanceof ProjectClasspathEntry projectClasspathEntry) {
return "P/" + projectClasspathEntry.getProject();
}
throw new IllegalArgumentException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public void setAppMain(String mainClassName, String mainRealmName) {
try (FileInputStream is = new FileInputStream(getLauncherConfigurationFile())) {
parser.parse(is);
} catch(Exception e) {
if(e instanceof ExceptionWrapper && e.getCause() instanceof CoreException) {
throw (CoreException) e.getCause();
if(e instanceof ExceptionWrapper && e.getCause() instanceof CoreException coreException) {
throw coreException;
}
throw new CoreException(Status.error(Messages.MavenExternalRuntime_error_cannot_parse, e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,21 +247,19 @@ private static Xpp3Dom parse(String pi) {
if(a == null) {
return null;
}
switch(a) {
case ignore:
return new Xpp3Dom("ignore"); //$NON-NLS-1$

case configurator:
return switch(a) {
case ignore -> new Xpp3Dom("ignore"); //$NON-NLS-1$
case configurator -> {
if(split.size() != 2) {
return null;
yield null;
}
Xpp3Dom conf = new Xpp3Dom("configurator"); //$NON-NLS-1$
Xpp3Dom id = new Xpp3Dom("id"); //$NON-NLS-1$
id.setValue(split.get(1));
conf.addChild(id);
return conf;

case execute:
yield conf;
}
case execute -> {
Xpp3Dom exec = new Xpp3Dom("execute"); //$NON-NLS-1$
if(split.size() > 1) {
EXECUTE_SPLITTER.splitAsStream(split.get(1)).map(String::strip).map(EXECUTE_OPTIONS::get)
Expand All @@ -271,11 +269,10 @@ private static Xpp3Dom parse(String pi) {
exec.addChild(opt);
});
}
return exec;

default:
return null;
}
yield exec;
}
default -> null;
};
}

private static PluginExecutionAction getAction(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,15 +758,11 @@ private static List<PluginExecutionMetadata> applyParametersFilter(List<PluginEx
}

private static boolean isValidPluginExecutionMetadata(PluginExecutionMetadata metadata) {
switch(metadata.getAction()) {
case error:
case execute:
case ignore:
return true;
case configurator:
return isConfigurator(metadata);
}
return false;
return switch(metadata.getAction()) {
case error, execute, ignore -> true;
case configurator -> isConfigurator(metadata);
default -> false;
};
}

static boolean isConfigurator(PluginExecutionMetadata metadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package org.eclipse.m2e.core.internal.lifecyclemapping.discovery;

import java.util.Objects;

import org.eclipse.core.runtime.Assert;

import org.eclipse.m2e.core.internal.lifecyclemapping.LifecycleMappingFactory;
Expand Down Expand Up @@ -67,14 +69,7 @@ public boolean equals(Object obj) {
return true;
}

if(!(obj instanceof MojoExecutionMappingRequirement)) {
return false;
}

MojoExecutionMappingRequirement other = (MojoExecutionMappingRequirement) obj;

return execution.equals(other.execution);

return obj instanceof MojoExecutionMappingRequirement other && execution.equals(other.execution);
}

/**
Expand Down Expand Up @@ -118,13 +113,8 @@ public boolean equals(Object obj) {
return true;
}

if(!(obj instanceof ProjectConfiguratorMappingRequirement)) {
return false;
}

ProjectConfiguratorMappingRequirement other = (ProjectConfiguratorMappingRequirement) obj;

return configuratorId.equals(other.configuratorId) && execution.equals(other.execution);
return obj instanceof ProjectConfiguratorMappingRequirement other && configuratorId.equals(other.configuratorId)
&& execution.equals(other.execution);
}

public MojoExecutionKey getExecution() {
Expand Down Expand Up @@ -212,39 +202,31 @@ public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(!(obj instanceof MojoExecutionMappingConfiguration)) {
return false;
}
MojoExecutionMappingConfiguration other = (MojoExecutionMappingConfiguration) obj;

if(!execution.equals(other.execution)) {
return false;
}

if(mapping == null) {
return other.mapping == null;
}

if(other.mapping == null) {
return false;
}
if(obj instanceof MojoExecutionMappingConfiguration other) {
if(!execution.equals(other.execution)) {
return false;
}

if(mapping.getAction() != other.mapping.getAction()) {
return false;
}
if(mapping == null) {
return other.mapping == null;
}

if(mapping.getAction() == PluginExecutionAction.configurator) {
String configuratorId = LifecycleMappingFactory.getProjectConfiguratorId(mapping);
String otherConfiguratorId = LifecycleMappingFactory.getProjectConfiguratorId(other.mapping);
if(!eq(configuratorId, otherConfiguratorId)) {
if(other.mapping == null) {
return false;
}
}

return true;
}
if(mapping.getAction() != other.mapping.getAction()) {
return false;
}

private static <T> boolean eq(T a, T b) {
return a != null ? a.equals(b) : b == null;
if(mapping.getAction() == PluginExecutionAction.configurator) {
String configuratorId = LifecycleMappingFactory.getProjectConfiguratorId(mapping);
String otherConfiguratorId = LifecycleMappingFactory.getProjectConfiguratorId(other.mapping);
if(!Objects.equals(configuratorId, otherConfiguratorId)) {
return false;
}
}
}
return false;
}
}
Loading

0 comments on commit f9c009d

Please sign in to comment.