Skip to content

Commit

Permalink
java 17: exploit new language features
Browse files Browse the repository at this point in the history
Also, clean up code based upon IDE suggestions.

Signed-off-by: BJ Hargrave <[email protected]>
  • Loading branch information
bjhargrave committed Aug 22, 2024
1 parent 4db5947 commit caaea43
Show file tree
Hide file tree
Showing 38 changed files with 225 additions and 443 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
@ExternalPlugin(name = "Transformer", objectClass = AnalyzerPlugin.class)
public class TransformerAnalyzerPlugin extends BaseTransformerPlugin implements AnalyzerPlugin {
public TransformerAnalyzerPlugin() {
/**
* We want to run before other AnalyzerPlugins so they will operate on
* the transformed classes and resources.
/*
We want to run before other AnalyzerPlugins so they will operate on
the transformed classes and resources.
*/
super(-10_000);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
@ExternalPlugin(name = "Transformer", objectClass = VerifierPlugin.class)
public class TransformerVerifierPlugin extends BaseTransformerPlugin implements VerifierPlugin {
public TransformerVerifierPlugin() {
/**
* We want to run after other VerifierPlugins so we will operate on the
* final classes and resources.
/*
We want to run after other VerifierPlugins so we will operate on the
final classes and resources.
*/
super(10_000);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public boolean skip() {
// Exit without generating anything if this project is not a known
// packaging type. Probably it's just a parent project.
if (!getPackagingTypes().contains(getProject().getPackaging())) {
getLogger().debug("skip project with packaging=" + getProject().getPackaging());
getLogger().debug("skip project with packaging={}", getProject().getPackaging());
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,16 @@ private TransformerArtifact prepareArtifactDescription(TransformerArtifact artif
List<Dependency> firstPass = dependencies.stream()
.filter(dependency -> Objects.equals(artifactDescription.getGroupId(), dependency.getGroupId())
&& Objects.equals(artifactDescription.getArtifactId(), dependency.getArtifactId()))
.collect(toList());
.toList();

Optional<Dependency> matchingDependency = firstPass.stream()
.filter(dependency -> Objects.equals(artifactDescription.getClassifier(), dependency.getClassifier())
&& Objects.equals(artifactDescription.getType(), dependency.getType()))
.findFirst();
if (!matchingDependency.isPresent()) {
if (matchingDependency.isEmpty()) {
matchingDependency = firstPass.stream()
.findFirst();
if (!matchingDependency.isPresent()) {
if (matchingDependency.isEmpty()) {
throw new MojoExecutionException(String.format(
"No version found for artifact %s:%s in project, dependencies, or dependency management",
artifactDescription.getGroupId(), artifactDescription.getArtifactId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ public static String extractClassifier(PluginExecution execution) {
Optional<Xpp3Dom> rootConfiguration = Optional.ofNullable((Xpp3Dom) execution.getConfiguration());
Optional<Xpp3Dom> classifierConfiguration = rootConfiguration
.map(configuration -> configuration.getChild("classifier"));
if (!classifierConfiguration.isPresent()) {
if (classifierConfiguration.isEmpty()) {
classifierConfiguration = rootConfiguration.map(configuration -> configuration.getChild("artifact"))
.map(configuration -> configuration.getChild("classifier"));
}
if (!classifierConfiguration.isPresent()) {
if (classifierConfiguration.isEmpty()) {
return defaultClassifier(execution);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,59 +63,29 @@ private static List<String> condition(List<String> values) {

@Override
public List<String> getOptionValues(AppOption option) {
List<String> values;
switch (option) {
case RULES_BUNDLES :
values = rules.getBundles();
break;
case RULES_DIRECT :
values = rules.getDirects();
break;
case RULES_IMMEDIATE_DATA :
values = rules.getImmediates();
break;
case RULES_MASTER_TEXT :
values = rules.getTexts();
break;
case RULES_PER_CLASS_CONSTANT :
values = rules.getPerClassConstants();
break;
case RULES_RENAMES :
values = rules.getRenames();
break;
case RULES_SELECTIONS :
values = rules.getSelections();
break;
case RULES_VERSIONS :
values = rules.getVersions();
break;
default :
values = null;
break;
}
List<String> values = switch (option) {
case RULES_BUNDLES -> rules.getBundles();
case RULES_DIRECT -> rules.getDirects();
case RULES_IMMEDIATE_DATA -> rules.getImmediates();
case RULES_MASTER_TEXT -> rules.getTexts();
case RULES_PER_CLASS_CONSTANT -> rules.getPerClassConstants();
case RULES_RENAMES -> rules.getRenames();
case RULES_SELECTIONS -> rules.getSelections();
case RULES_VERSIONS -> rules.getVersions();
default -> null;
};
return condition(values);
}

@Override
public boolean hasOption(AppOption option) {
boolean has;
switch (option) {
case OVERWRITE :
has = rules.isOverwrite();
break;
case INVERT :
has = rules.isInvert();
break;
case WIDEN_ARCHIVE_NESTING :
has = rules.isWiden();
break;
case STRIP_SIGNATURES :
has = rules.isStripSignatures();
break;
default :
has = TransformOptions.super.hasOption(option);
break;
}
boolean has = switch (option) {
case OVERWRITE -> rules.isOverwrite();
case INVERT -> rules.isInvert();
case WIDEN_ARCHIVE_NESTING -> rules.isWiden();
case STRIP_SIGNATURES -> rules.isStripSignatures();
default -> TransformOptions.super.hasOption(option);
};
return has;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void execute() throws MojoFailureException, MojoExecutionException {
*/
public void transform(final Artifact sourceArtifact) throws MojoFailureException, MojoExecutionException {
final String sourceClassifier = sourceArtifact.getClassifier();
final String targetClassifier = (sourceClassifier == null || sourceClassifier.length() == 0) ? this.classifier
final String targetClassifier = (sourceClassifier == null || sourceClassifier.isEmpty()) ? this.classifier
: sourceClassifier + "-" + this.classifier;

final File targetFile = new File(outputDirectory, sourceArtifact.getArtifactId() + "-" + targetClassifier + "-"
Expand All @@ -129,36 +129,25 @@ public void transform(final Artifact sourceArtifact) throws MojoFailureException
final Function<String, URL> ruleLoader = JakartaTransform.getRuleLoader();
@Override
public boolean hasOption(AppOption option) {
switch (option) {
case OVERWRITE :
return overwrite;
case INVERT :
return invert;
case STRIP_SIGNATURES:
return stripSignatures;
default :
return TransformOptions.super.hasOption(option);
}
return switch (option) {
case OVERWRITE -> overwrite;
case INVERT -> invert;
case STRIP_SIGNATURES -> stripSignatures;
default -> TransformOptions.super.hasOption(option);
};
}

@Override
public String getOptionValue(AppOption option) {
switch (option) {
case RULES_RENAMES :
return emptyAsNull(rulesRenamesUri);
case RULES_VERSIONS :
return emptyAsNull(rulesVersionUri);
case RULES_BUNDLES :
return emptyAsNull(rulesBundlesUri);
case RULES_DIRECT :
return emptyAsNull(rulesDirectUri);
case RULES_MASTER_TEXT :
return emptyAsNull(rulesXmlsUri);
case RULES_PER_CLASS_CONSTANT :
return emptyAsNull(rulesPerClassConstantUri);
default :
return null;
}
return switch (option) {
case RULES_RENAMES -> emptyAsNull(rulesRenamesUri);
case RULES_VERSIONS -> emptyAsNull(rulesVersionUri);
case RULES_BUNDLES -> emptyAsNull(rulesBundlesUri);
case RULES_DIRECT -> emptyAsNull(rulesDirectUri);
case RULES_MASTER_TEXT -> emptyAsNull(rulesXmlsUri);
case RULES_PER_CLASS_CONSTANT -> emptyAsNull(rulesPerClassConstantUri);
default -> null;
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void testMultipleArtifactTransformerPlugin() throws Exception {
.equals("zip")
&& a.getArtifactId()
.equals("simple-service")))
.map(a -> a.getClassifier())
.map(Artifact::getClassifier)
.collect(Collectors.toSet());

assertEquals(6, mavenProject.getAttachedArtifacts()
Expand Down Expand Up @@ -260,7 +260,7 @@ private static File createService(String packaging, File targetDirectory) throws
final File tempFile = File.createTempFile("service", "." + packaging);
tempFile.delete();

final Archive archive;
final Archive<?> archive;
if (packaging.equals("jar")) {
archive = ShrinkWrap.create(JavaArchive.class, "service." + packaging)
.addClass(EchoService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ void zip_non_UTF_8_encoding() throws Exception {
transformer.transform();

Changes lastActiveChanges = transformer.getLastActiveChanges();
if (lastActiveChanges instanceof ContainerChanges) {
ContainerChanges containerChanges = (ContainerChanges) lastActiveChanges;
if (lastActiveChanges instanceof ContainerChanges containerChanges) {
int numDuplicated = containerChanges.getAllDuplicated();
int numFailed = containerChanges.getAllFailed();

Expand Down Expand Up @@ -391,8 +390,7 @@ private void verifyAction(String actionClassName, String inputFileName, String o
transformer.transform();

Changes lastActiveChanges = transformer.getLastActiveChanges();
if (lastActiveChanges instanceof ContainerChanges) {
ContainerChanges containerChanges = (ContainerChanges) lastActiveChanges;
if (lastActiveChanges instanceof ContainerChanges containerChanges) {
int numDuplicated = containerChanges.getAllDuplicated();
int numFailed = containerChanges.getAllFailed();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,5 @@

package org.eclipse.transformer;

public class ImmediateRuleData {
public final AppOption target;
public final String key;
public final String value;

public ImmediateRuleData(AppOption target, String key, String value) {
this.target = target;
this.key = key;
this.value = value;
}
public record ImmediateRuleData(AppOption target, String key, String value) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public static void setPackageVersions(

} else if ( nextChar == ';' ) {
// The length test guards against 'p=v;a1=v1;;a2=v2'
if ( versionBuilder.length() != 0 ) {
if (!versionBuilder.isEmpty()) {
setVersion(
newPackageName, newVersion,
nameBuilder, versionBuilder,
Expand All @@ -165,7 +165,7 @@ public static void setPackageVersions(
//
// See issue #300.

if ( nameBuilder.length() != 0 ) {
if (!nameBuilder.isEmpty()) {
throw new IllegalArgumentException("Package version syntax error: Version missing for package [ " + newPackageName + " ] and attribute [ " + nameBuilder.toString() + " ]");
}
}
Expand All @@ -184,7 +184,7 @@ public static void setPackageVersions(
// end with a ';', in which case the builders will be empty,
// and there is no update to be done.

if ( versionBuilder.length() != 0 ) {
if (!versionBuilder.isEmpty()) {
setVersion(
newPackageName, newVersion,
nameBuilder, versionBuilder,
Expand All @@ -206,15 +206,15 @@ public static void setVersion(
Map<String, Map<String, String>> specificPackageVersions) {

String versionText;
if ( versionBuilder.length() == 0 ) {
if (versionBuilder.isEmpty()) {
throw new IllegalArgumentException("Package version syntax error: No version in [ " + newVersion + " ]");
} else {
versionText = versionBuilder.toString();
versionBuilder.setLength(0);
}

String propertyName;
if ( nameBuilder.length() == 0 ) {
if (nameBuilder.isEmpty()) {
propertyName = null;
} else {
propertyName = nameBuilder.toString();
Expand All @@ -224,10 +224,7 @@ public static void setVersion(
if ( propertyName == null ) {
packageVersions.put(newPackageName, versionText);
} else {
Map<String, String> versionsForProperty = specificPackageVersions.get(propertyName);
if ( versionsForProperty == null ) {
specificPackageVersions.put( propertyName, (versionsForProperty = new HashMap<>()) );
}
Map<String, String> versionsForProperty = specificPackageVersions.computeIfAbsent(propertyName, k -> new HashMap<>());
versionsForProperty.put(newPackageName, versionText);
}
}
Expand Down Expand Up @@ -266,7 +263,6 @@ public static <MAP extends Map<String, String>> MAP copyPropertiesToMap(Properti
* file.
* @throws TransformException Thrown if an error occurs processing the file.
*/
@SuppressWarnings("resource")
public static boolean isFeatureManifest(String manifestPath, File manifestFile) throws TransformException {
FileReader manifestReader;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.eclipse.transformer.action.ContainerChanges;
import org.eclipse.transformer.action.SelectionRule;
import org.eclipse.transformer.action.SignatureRule;
import org.eclipse.transformer.action.impl.ActionContextImpl;
import org.eclipse.transformer.action.impl.ActionSelectorImpl;
import org.eclipse.transformer.action.impl.BundleDataImpl;
import org.eclipse.transformer.action.impl.ClassActionImpl;
Expand Down Expand Up @@ -207,8 +206,7 @@ protected ResultCode basicRun() {
}

Changes lastActiveChanges = getLastActiveChanges();
if (lastActiveChanges instanceof ContainerChanges) {
ContainerChanges containerChanges = (ContainerChanges) lastActiveChanges;
if (lastActiveChanges instanceof ContainerChanges containerChanges) {
int numDuplicated = containerChanges.getAllDuplicated();
int numFailed = containerChanges.getAllFailed();

Expand Down Expand Up @@ -458,28 +456,28 @@ protected void processImmediateData(
throws IOException, URISyntaxException {

for ( ImmediateRuleData nextData : immediateData ) {
switch ( nextData.target ) {
switch ( nextData.target() ) {
case RULES_SELECTIONS:
addImmediateSelection(nextData.key, nextData.value);
addImmediateSelection(nextData.key(), nextData.value());
break;
case RULES_RENAMES:
addImmediateRename(nextData.key, nextData.value, orphanedFinalVersions);
addImmediateRename(nextData.key(), nextData.value(), orphanedFinalVersions);
break;
case RULES_VERSIONS:
addImmediateVersion(nextData.key, nextData.value);
addImmediateVersion(nextData.key(), nextData.value());
break;
case RULES_BUNDLES:
addImmediateBundleData(nextData.key, nextData.value);
addImmediateBundleData(nextData.key(), nextData.value());
break;
case RULES_DIRECT:
addImmediateDirect(nextData.key, nextData.value);
addImmediateDirect(nextData.key(), nextData.value());
break;
case RULES_MASTER_TEXT:
addImmediateMasterText(masterTextRef, nextData.key, nextData.value);
addImmediateMasterText(masterTextRef, nextData.key(), nextData.value());
break;

default:
getLogger().error(consoleMarker, "Unrecognized immediate data target [ {} ]", nextData.target);
getLogger().error(consoleMarker, "Unrecognized immediate data target [ {} ]", nextData.target());
}
}
}
Expand Down Expand Up @@ -1103,7 +1101,7 @@ private boolean outputExists(File outputFile) {
// As a separate method to allow re-use.

public ActionContext getActionContext() {
return new ActionContextImpl(getLogger(), getSelectionRule(), getSignatureRule());
return new ActionContext(getLogger(), getSelectionRule(), getSignatureRule());
}

public ActionSelector getActionSelector() {
Expand Down
Loading

0 comments on commit caaea43

Please sign in to comment.