Skip to content

Commit

Permalink
Project cleanups
Browse files Browse the repository at this point in the history
 - use spotless for java9 sources
 - fix warnings reported by IDE
 - fix used dependencies
  • Loading branch information
slawekjaranowski committed Sep 27, 2024
1 parent 83c78a7 commit 71b2147
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 177 deletions.
19 changes: 6 additions & 13 deletions plexus-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -45,12 +50,6 @@
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>6.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand All @@ -69,12 +68,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>**/*Test.java</include>
<include>**/*IT.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public JavaModuleDescriptor getModuleDescriptor(Path modulePath, JavaVersion jdk
.equalsIgnoreCase(
manifest.getMainAttributes().getValue("Multi-Release"))) {
int javaVersion =
Integer.valueOf(jdkVersion.asMajor().getValue(1));
Integer.parseInt(jdkVersion.asMajor().getValue(1));

for (int version = javaVersion; version >= 9; version--) {
String resource = "META-INF/versions/" + version + "/module-info.class";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,13 @@ public <T> ResolvePathsResult<T> resolvePaths(final ResolvePathsRequest<T> reque
JavaModuleDescriptor moduleDescriptor;
ModuleNameSource source;

ModuleNameExtractor nameExtractor = new ModuleNameExtractor() {
@Override
public String extract(Path path) throws IOException {
if (request.getJdkHome() != null) {
filenameAutoModules.put(t, path);
} else {
return CmdModuleNameExtractor.getModuleName(path);
}
return null;
ModuleNameExtractor nameExtractor = path -> {
if (request.getJdkHome() != null) {
filenameAutoModules.put(t, path);
} else {
return CmdModuleNameExtractor.getModuleName(path);
}
return null;
};

try {
Expand Down Expand Up @@ -323,7 +320,7 @@ private ResolvePathResult resolvePath(
// either jar or outputDirectory
if (Files.isRegularFile(path) && !path.getFileName().toString().endsWith(".jar")) {
throw new IllegalArgumentException(
"'" + path.toString() + "' not allowed on the path, only outputDirectories and jars are accepted");
"'" + path + "' not allowed on the path, only outputDirectories and jars are accepted");
}

if (Files.isRegularFile(path) || Files.exists(path.resolve("module-info.class"))) {
Expand Down Expand Up @@ -428,13 +425,8 @@ private void collectProviders(JavaModuleDescriptor moduleDescriptor, Map<String,
// module-info.class uses FQN, i.e. $-separator for subclasses
final String serviceClassName = provides.service().replace('$', '.');

Set<String> providingModules = availableProviders.get(serviceClassName);
Set<String> providingModules = availableProviders.computeIfAbsent(serviceClassName, k -> new HashSet<>());

if (providingModules == null) {
providingModules = new HashSet<>();

availableProviders.put(serviceClassName, providingModules);
}
providingModules.add(moduleDescriptor.name());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
* under the License.
*/

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
Expand All @@ -36,7 +35,7 @@
class ManifestModuleNameExtractor implements ModuleNameExtractor {
@Override
public String extract(Path file) throws IOException {
Manifest manifest = extractManifest(file.toFile());
Manifest manifest = extractManifest(file);

String automaticModuleName;
if (manifest != null) {
Expand All @@ -48,14 +47,14 @@ public String extract(Path file) throws IOException {
return automaticModuleName;
}

private Manifest extractManifest(File file) throws IOException {
private Manifest extractManifest(Path file) throws IOException {
Manifest manifest;
if (file.isFile()) {
try (JarFile jarFile = new JarFile(file)) {
if (Files.isRegularFile(file)) {
try (JarFile jarFile = new JarFile(file.toFile())) {
manifest = jarFile.getManifest();
}
} else if (new File(file, "META-INF/MANIFEST.MF").exists()) {
try (InputStream is = new FileInputStream(new File(file, "META-INF/MANIFEST.MF"))) {
} else if (Files.exists(file.resolve("META-INF/MANIFEST.MF"))) {
try (InputStream is = Files.newInputStream(file.resolve("META-INF/MANIFEST.MF"))) {
manifest = new Manifest(is);
}
} else {
Expand Down
12 changes: 6 additions & 6 deletions plexus-java/src/main/java9/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
* specific language governing permissions and limitations
* under the License.
*/
module org.codehaus.plexus.languages.java
{
module org.codehaus.plexus.languages.java {
requires com.thoughtworks.qdox;
requires org.objectweb.asm;

exports org.codehaus.plexus.languages.java.jpms;
exports org.codehaus.plexus.languages.java.version;

provides org.codehaus.plexus.languages.java.jpms.LocationManager with org.codehaus.plexus.languages.java.jpms.LocationManager;
}

provides org.codehaus.plexus.languages.java.jpms.LocationManager with
org.codehaus.plexus.languages.java.jpms.LocationManager;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,68 +21,55 @@

import java.io.IOException;
import java.io.InputStream;

import java.lang.module.ModuleDescriptor;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.Builder;

class BinaryModuleInfoParser extends AbstractBinaryModuleInfoParser
{
class BinaryModuleInfoParser extends AbstractBinaryModuleInfoParser {
@Override
JavaModuleDescriptor parse( InputStream in ) throws IOException
{
ModuleDescriptor descriptor = ModuleDescriptor.read( in );

Builder builder = JavaModuleDescriptor.newModule( descriptor.name() );

for ( ModuleDescriptor.Requires requires : descriptor.requires() )
{
if ( requires.modifiers().contains( ModuleDescriptor.Requires.Modifier.STATIC )
|| requires.modifiers().contains( ModuleDescriptor.Requires.Modifier.TRANSITIVE ) )
{
JavaModuleDescriptor parse(InputStream in) throws IOException {
ModuleDescriptor descriptor = ModuleDescriptor.read(in);

Builder builder = JavaModuleDescriptor.newModule(descriptor.name());

for (ModuleDescriptor.Requires requires : descriptor.requires()) {
if (requires.modifiers().contains(ModuleDescriptor.Requires.Modifier.STATIC)
|| requires.modifiers().contains(ModuleDescriptor.Requires.Modifier.TRANSITIVE)) {
Set<JavaModuleDescriptor.JavaRequires.JavaModifier> modifiers = new LinkedHashSet<>();
if ( requires.modifiers().contains( ModuleDescriptor.Requires.Modifier.STATIC ) )
{
modifiers.add( org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaRequires.JavaModifier.STATIC );
if (requires.modifiers().contains(ModuleDescriptor.Requires.Modifier.STATIC)) {
modifiers.add(
org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaRequires.JavaModifier
.STATIC);
}
if ( requires.modifiers().contains( ModuleDescriptor.Requires.Modifier.TRANSITIVE ) )
{
modifiers.add( org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaRequires.JavaModifier.TRANSITIVE );
if (requires.modifiers().contains(ModuleDescriptor.Requires.Modifier.TRANSITIVE)) {
modifiers.add(
org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaRequires.JavaModifier
.TRANSITIVE);
}
builder.requires( modifiers, requires.name() );
}
else
{
builder.requires( requires.name() );
builder.requires(modifiers, requires.name());
} else {
builder.requires(requires.name());
}
}

for ( ModuleDescriptor.Exports exports : descriptor.exports() )
{
if ( exports.targets().isEmpty() )
{
builder.exports( exports.source() );
}
else
{
builder.exports( exports.source(), exports.targets() );

for (ModuleDescriptor.Exports exports : descriptor.exports()) {
if (exports.targets().isEmpty()) {
builder.exports(exports.source());
} else {
builder.exports(exports.source(), exports.targets());
}
}

for ( String uses : descriptor.uses() )
{
builder.uses( uses );

for (String uses : descriptor.uses()) {
builder.uses(uses);
}

for ( ModuleDescriptor.Provides provides : descriptor.provides() )
{
builder.provides( provides.service(), provides.providers() );

for (ModuleDescriptor.Provides provides : descriptor.provides()) {
builder.provides(provides.service(), provides.providers());
}



return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,54 +41,43 @@
* <p>
* The result is a properties-file written ot the StdOut, having the jar path as key and the module name as value.<br>
* Any exception is written to the StdErr.
* </p>
*
* </p>
*
* @author Robert Scholte
* @since 1.0.0
*/
public class CmdModuleNameExtractor
{
public static void main( String[] args )
{
public class CmdModuleNameExtractor {
public static void main(String[] args) {
Properties properties = new Properties();

for ( String path : args )
{
try
{
String moduleName = getModuleName( Paths.get( path ) );
if ( moduleName != null )
{
properties.setProperty( path, moduleName );
for (String path : args) {
try {
String moduleName = getModuleName(Paths.get(path));
if (moduleName != null) {
properties.setProperty(path, moduleName);
}
}
catch ( Exception e )
{
System.err.append( e.getMessage() );
} catch (Exception e) {
System.err.append(e.getMessage());
}
}

try
{
properties.store( System.out, "" );
}
catch ( IOException e )
{
System.exit( 1 );
try {
properties.store(System.out, "");
} catch (IOException e) {
System.exit(1);
}
}

/**
* Get the name of the module, using Java 9 code without reflection
*
*
* @param modulePath the module path
* @return the module name
* @throws FindException If an error occurs finding the module
*/
public static String getModuleName( Path modulePath ) throws FindException
{
Set<ModuleReference> moduleReferences = ModuleFinder.of( modulePath ).findAll();

public static String getModuleName(Path modulePath) throws FindException {
Set<ModuleReference> moduleReferences = ModuleFinder.of(modulePath).findAll();

Optional<ModuleReference> modRef = moduleReferences.stream().findFirst();

return modRef.isPresent() ? modRef.get().descriptor().name() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CmdModuleNameExtractorTest {
@Test
void testMethodCount() throws Exception {
// ensure that both implementations are in sync
assertThat(CmdModuleNameExtractor.class.getDeclaredMethods().length).isEqualTo(2);
assertThat(CmdModuleNameExtractor.class.getDeclaredMethods()).hasSize(2);

// if these don't exist, a NoSuchMethodException is thrown
CmdModuleNameExtractor.class.getDeclaredMethod("main", String[].class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;

import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -77,10 +76,10 @@ void testManifestWithoutReflectRequires() throws Exception {

ResolvePathsResult<Path> result = locationManager.resolvePaths(request);

assertThat(result.getPathExceptions()).hasSize(0);
assertThat(result.getPathExceptions()).isEmpty();
assertThat(result.getMainModuleDescriptor()).isEqualTo(descriptor);
assertThat(result.getPathElements()).hasSize(1);
assertThat(result.getModulepathElements()).hasSize(0);
assertThat(result.getModulepathElements()).isEmpty();
assertThat(result.getClasspathElements()).hasSize(1);
}

Expand Down Expand Up @@ -116,7 +115,7 @@ void testResolvePathWithException() {
void testClassicJarNameStartsWithNumber() throws Exception {
Path p = Paths.get("src/test/test-data/jar.empty.invalid.name/101-1.0.0-SNAPSHOT.jar");
ResolvePathsRequest<Path> request =
ResolvePathsRequest.ofPaths(Arrays.asList(p)).setMainModuleDescriptor(mockModuleInfoJava);
ResolvePathsRequest.ofPaths(Collections.singletonList(p)).setMainModuleDescriptor(mockModuleInfoJava);

ResolvePathsResult<Path> result = locationManager.resolvePaths(request);

Expand Down
Loading

0 comments on commit 71b2147

Please sign in to comment.