Skip to content

Commit

Permalink
fix(core): Handle invalid modules in module descriptor command
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
aalmiray committed Feb 24, 2023
1 parent 78b05f2 commit 8083ba6
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2022-2023 The Jarviz authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kordamp.jarviz.core.model;

import java.lang.module.ModuleDescriptor;
import java.util.Optional;

/**
* @author Andres Almiray
* @since 0.3.0
*/
public class ModuleMetadata {
private final ModuleName moduleName;
private final ModuleDescriptor moduleDescriptor;

public static ModuleMetadata of(ModuleName moduleName) {
return new ModuleMetadata(moduleName, null);
}

public static ModuleMetadata of(ModuleName moduleName, ModuleDescriptor moduleDescriptor) {
return new ModuleMetadata(moduleName, moduleDescriptor);
}

private ModuleMetadata(ModuleName moduleName, ModuleDescriptor moduleDescriptor) {
this.moduleName = moduleName;
this.moduleDescriptor = moduleDescriptor;
}

public ModuleName getModuleName() {
return moduleName;
}

public Optional<ModuleDescriptor> getModuleDescriptor() {
return Optional.ofNullable(moduleDescriptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
* @author Andres Almiray
* @since 0.1.0
*/
public class ShowBytecodeJarProcessor implements JarProcessor<BytecodeVersions> {
public class BytecodeShowJarProcessor implements JarProcessor<BytecodeVersions> {
private static final Pattern MULTIRELEASE = Pattern.compile("META-INF/versions/(\\d+)/(.*\\.class)");

private final JarFileResolver jarFileResolver;

public ShowBytecodeJarProcessor(JarFileResolver jarFileResolver) {
public BytecodeShowJarProcessor(JarFileResolver jarFileResolver) {
this.jarFileResolver = jarFileResolver;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
* @author Andres Almiray
* @since 0.1.0
*/
public class QueryManifestJarProcessor implements JarProcessor<Optional<String>> {
public class ManifestQueryJarProcessor implements JarProcessor<Optional<String>> {
private final JarFileResolver jarFileResolver;
private String attributeName;
private String sectionName;

public QueryManifestJarProcessor(JarFileResolver jarFileResolver) {
public ManifestQueryJarProcessor(JarFileResolver jarFileResolver) {
this.jarFileResolver = jarFileResolver;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
* @author Andres Almiray
* @since 0.1.0
*/
public class ShowManifestJarProcessor implements JarProcessor<Optional<Manifest>> {
public class ManifestShowJarProcessor implements JarProcessor<Optional<Manifest>> {
private final JarFileResolver jarFileResolver;

public ShowManifestJarProcessor(JarFileResolver jarFileResolver) {
public ManifestShowJarProcessor(JarFileResolver jarFileResolver) {
this.jarFileResolver = jarFileResolver;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import org.kordamp.jarviz.core.JarProcessor;
import org.kordamp.jarviz.core.JarvizException;
import org.kordamp.jarviz.core.analyzers.ModuleDescriptorJarPathAnalyzer;
import org.kordamp.jarviz.core.model.ModuleMetadata;
import org.kordamp.jarviz.core.model.ModuleName;

import java.lang.module.ModuleDescriptor;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
Expand All @@ -33,16 +34,18 @@
* @author Andres Almiray
* @since 0.2.0
*/
public class DescriptorModuleJarProcessor implements JarProcessor<ModuleDescriptor> {
public class ModuleDescriptorJarProcessor implements JarProcessor<ModuleMetadata> {
private final JarFileResolver jarFileResolver;
private final ModuleNameJarProcessor moduleNameJarProcessor;

public DescriptorModuleJarProcessor(JarFileResolver jarFileResolver) {
public ModuleDescriptorJarProcessor(JarFileResolver jarFileResolver) {
this.jarFileResolver = jarFileResolver;
this.moduleNameJarProcessor = new ModuleNameJarProcessor(jarFileResolver);
}

@Override
public Set<JarFileResult<ModuleDescriptor>> getResult() throws JarvizException {
Set<JarFileResult<ModuleDescriptor>> set = new TreeSet<>();
public Set<JarFileResult<ModuleMetadata>> getResult() throws JarvizException {
Set<JarFileResult<ModuleMetadata>> set = new TreeSet<>();

for (JarFile jarFile : jarFileResolver.resolveJarFiles()) {
set.add(processJarFile(jarFile));
Expand All @@ -51,11 +54,16 @@ public Set<JarFileResult<ModuleDescriptor>> getResult() throws JarvizException {
return set;
}

private JarFileResult<ModuleDescriptor> processJarFile(JarFile jarFile) {
private JarFileResult<ModuleMetadata> processJarFile(JarFile jarFile) {
JarFileResult<ModuleName> moduleName = moduleNameJarProcessor.processJarFile(jarFile);
if (!moduleName.getResult().isValid()) {
return JarFileResult.of(jarFile, ModuleMetadata.of(moduleName.getResult()));
}

Path jarPath = Paths.get(jarFile.getName());

ModuleDescriptorJarPathAnalyzer analyzer = new ModuleDescriptorJarPathAnalyzer();
analyzer.handle(jarPath);
return JarFileResult.of(jarFile, analyzer.getResult());
return JarFileResult.of(jarFile, ModuleMetadata.of(moduleName.getResult(), analyzer.getResult()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
* @author Andres Almiray
* @since 0.2.0
*/
public class NameModuleJarProcessor implements JarProcessor<ModuleName> {
public class ModuleNameJarProcessor implements JarProcessor<ModuleName> {
// This should be the correct pattern
// private static final Pattern VERSION_PATTERN = Pattern.compile("-(\\d+(\\.|_|-|\\+|$))");
private static final Pattern VERSION_PATTERN = Pattern.compile("-(\\d+(\\.|$))");
private final JarFileResolver jarFileResolver;

public NameModuleJarProcessor(JarFileResolver jarFileResolver) {
public ModuleNameJarProcessor(JarFileResolver jarFileResolver) {
this.jarFileResolver = jarFileResolver;
}

Expand All @@ -62,7 +62,7 @@ public Set<JarFileResult<ModuleName>> getResult() throws JarvizException {
return set;
}

private JarFileResult<ModuleName> processJarFile(JarFile jarFile) {
JarFileResult<ModuleName> processJarFile(JarFile jarFile) {
Path jarPath = Paths.get(jarFile.getName());
String automaticModuleNameByFilename = deriveModuleNameFromFilename(jarPath.getFileName().toString());
String automaticModuleNameByManifest = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
* @author Andres Almiray
* @since 0.3.0
*/
public class SplitPackageJarProcessor implements JarProcessor<Set<String>> {
public class PackageSplitJarProcessor implements JarProcessor<Set<String>> {
private static final String MODULE_INFO = "module-info.class";
private static final String VERSIONED = "META-INF/versions/";

private final JarFileResolver jarFileResolver;

public SplitPackageJarProcessor(JarFileResolver jarFileResolver) {
public PackageSplitJarProcessor(JarFileResolver jarFileResolver) {
this.jarFileResolver = jarFileResolver;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @author Andres Almiray
* @since 0.3.0
*/
public class ValidatePackageJarProcessor implements JarProcessor<Set<String>> {
public class PackageValidateJarProcessor implements JarProcessor<Set<String>> {
private static final String VERSIONED = "META-INF/versions/";

private static final Set<String> RESERVED_KEYWORDS = Set.of(
Expand Down Expand Up @@ -99,7 +99,7 @@ public class ValidatePackageJarProcessor implements JarProcessor<Set<String>> {

private final JarFileResolver jarFileResolver;

public ValidatePackageJarProcessor(JarFileResolver jarFileResolver) {
public PackageValidateJarProcessor(JarFileResolver jarFileResolver) {
this.jarFileResolver = jarFileResolver;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
* @author Andres Almiray
* @since 0.1.0
*/
public class ListServicesJarProcessor implements JarProcessor<Optional<Set<String>>> {
public class ServicesListJarProcessor implements JarProcessor<Optional<Set<String>>> {
private static final String META_INF_SERVICES = "META-INF/services/";
private final JarFileResolver jarFileResolver;
private Integer release;

public ListServicesJarProcessor(JarFileResolver jarFileResolver) {
public ServicesListJarProcessor(JarFileResolver jarFileResolver) {
this.jarFileResolver = jarFileResolver;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
* @author Andres Almiray
* @since 0.1.0
*/
public class ShowServicesJarProcessor implements JarProcessor<Optional<Set<String>>> {
public class ServicesShowJarProcessor implements JarProcessor<Optional<Set<String>>> {
private static final String META_INF_SERVICES = "META-INF/services/";
private final JarFileResolver jarFileResolver;
private Integer release;
private String serviceName;

public ShowServicesJarProcessor(JarFileResolver jarFileResolver) {
public ServicesShowJarProcessor(JarFileResolver jarFileResolver) {
this.jarFileResolver = jarFileResolver;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.kordamp.jarviz.core.JarProcessor;
import org.kordamp.jarviz.core.model.BytecodeVersion;
import org.kordamp.jarviz.core.model.BytecodeVersions;
import org.kordamp.jarviz.core.processors.ShowBytecodeJarProcessor;
import org.kordamp.jarviz.core.processors.BytecodeShowJarProcessor;
import org.kordamp.jarviz.reporting.Format;
import org.kordamp.jarviz.reporting.Node;
import picocli.CommandLine;
Expand Down Expand Up @@ -52,7 +52,7 @@ public class BytecodeShow extends AbstractJarvizSubcommand<Bytecode> {
@Override
protected int execute() {
JarFileResolver jarFileResolver = createJarFileResolver();
ShowBytecodeJarProcessor processor = new ShowBytecodeJarProcessor(jarFileResolver);
BytecodeShowJarProcessor processor = new BytecodeShowJarProcessor(jarFileResolver);

Set<JarProcessor.JarFileResult<BytecodeVersions>> results = processor.getResult();
if (results.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.kordamp.jarviz.cli.internal.AbstractJarvizSubcommand;
import org.kordamp.jarviz.core.JarFileResolver;
import org.kordamp.jarviz.core.JarProcessor;
import org.kordamp.jarviz.core.processors.QueryManifestJarProcessor;
import org.kordamp.jarviz.core.processors.ManifestQueryJarProcessor;
import org.kordamp.jarviz.reporting.Format;
import org.kordamp.jarviz.reporting.Node;
import picocli.CommandLine;
Expand All @@ -45,7 +45,7 @@ public class ManifestQuery extends AbstractJarvizSubcommand<Manifest> {
@Override
protected int execute() {
JarFileResolver jarFileResolver = createJarFileResolver();
QueryManifestJarProcessor processor = new QueryManifestJarProcessor(jarFileResolver);
ManifestQueryJarProcessor processor = new ManifestQueryJarProcessor(jarFileResolver);
processor.setAttributeName(attributeName);
processor.setSectionName(sectionName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.kordamp.jarviz.core.JarFileResolver;
import org.kordamp.jarviz.core.JarProcessor;
import org.kordamp.jarviz.core.JarvizException;
import org.kordamp.jarviz.core.processors.ShowManifestJarProcessor;
import org.kordamp.jarviz.core.processors.ManifestShowJarProcessor;
import org.kordamp.jarviz.reporting.Format;
import org.kordamp.jarviz.reporting.Node;
import picocli.CommandLine;
Expand All @@ -41,7 +41,7 @@ public class ManifestShow extends AbstractJarvizSubcommand<Manifest> {
@Override
protected int execute() {
JarFileResolver jarFileResolver = createJarFileResolver();
ShowManifestJarProcessor processor = new ShowManifestJarProcessor(jarFileResolver);
ManifestShowJarProcessor processor = new ManifestShowJarProcessor(jarFileResolver);

Set<JarProcessor.JarFileResult<Optional<java.util.jar.Manifest>>> results = processor.getResult();
if (results.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import org.kordamp.jarviz.cli.internal.AbstractJarvizSubcommand;
import org.kordamp.jarviz.core.JarFileResolver;
import org.kordamp.jarviz.core.JarProcessor;
import org.kordamp.jarviz.core.processors.DescriptorModuleJarProcessor;
import org.kordamp.jarviz.core.model.ModuleMetadata;
import org.kordamp.jarviz.core.processors.ModuleDescriptorJarProcessor;
import org.kordamp.jarviz.reporting.Format;
import org.kordamp.jarviz.reporting.Node;
import picocli.CommandLine;
Expand All @@ -35,6 +36,8 @@
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.kordamp.jarviz.cli.internal.Colorizer.magenta;
import static org.kordamp.jarviz.cli.modules.ModuleName.EXPLICIT;
import static org.kordamp.jarviz.cli.modules.ModuleName.resolveSource;

/**
* @author Andres Almiray
Expand All @@ -45,9 +48,9 @@ public class ModuleDescriptor extends AbstractJarvizSubcommand<Module> {
@Override
protected int execute() {
JarFileResolver jarFileResolver = createJarFileResolver();
DescriptorModuleJarProcessor processor = new DescriptorModuleJarProcessor(jarFileResolver);
ModuleDescriptorJarProcessor processor = new ModuleDescriptorJarProcessor(jarFileResolver);

Set<JarProcessor.JarFileResult<java.lang.module.ModuleDescriptor>> results = processor.getResult();
Set<JarProcessor.JarFileResult<ModuleMetadata>> results = processor.getResult();
if (results.isEmpty()) {
return 1;
}
Expand All @@ -58,9 +61,9 @@ protected int execute() {
return 0;
}

private void output(Set<JarProcessor.JarFileResult<java.lang.module.ModuleDescriptor>> results) {
private void output(Set<JarProcessor.JarFileResult<ModuleMetadata>> results) {
Node root = createRootNode();
for (JarProcessor.JarFileResult<java.lang.module.ModuleDescriptor> result : results) {
for (JarProcessor.JarFileResult<ModuleMetadata> result : results) {
if (null == outputFormat) {
output(result);
} else {
Expand All @@ -70,9 +73,20 @@ private void output(Set<JarProcessor.JarFileResult<java.lang.module.ModuleDescri
if (null != outputFormat) writeOutput(resolveFormatter(outputFormat).write(root));
}

private void output(JarProcessor.JarFileResult<java.lang.module.ModuleDescriptor> result) {
private void output(JarProcessor.JarFileResult<ModuleMetadata> result) {
parent().getOut().println($$("output.subject", result.getJarFileName()));
java.lang.module.ModuleDescriptor md = result.getResult();

org.kordamp.jarviz.core.model.ModuleName moduleName = result.getResult().getModuleName();
if (!moduleName.isValid()) {
parent().getOut().println($$("module.name", moduleName.getModuleName()));
parent().getOut().println($$("module.source", resolveSource(moduleName)));
parent().getOut().println($$("module.automatic", $b(!EXPLICIT.equals(resolveSource(moduleName)))));
parent().getOut().println($$("module.valid", $b(moduleName.isValid())));
parent().getOut().println($$("module.reason", moduleName.getReason()));
return;
}

java.lang.module.ModuleDescriptor md = result.getResult().getModuleDescriptor().get();

List<java.lang.module.ModuleDescriptor.Exports> unqualifiedExports = md.exports().stream()
.sorted(comparing(java.lang.module.ModuleDescriptor.Exports::source))
Expand Down Expand Up @@ -159,22 +173,33 @@ private void output(JarProcessor.JarFileResult<java.lang.module.ModuleDescriptor
}
}

private void report(Set<JarProcessor.JarFileResult<java.lang.module.ModuleDescriptor>> results) {
private void report(Set<JarProcessor.JarFileResult<ModuleMetadata>> results) {
if (null == reportPath) return;

for (Format format : validateReportFormats()) {
Node root = createRootNode();
for (JarProcessor.JarFileResult<java.lang.module.ModuleDescriptor> result : results) {
for (JarProcessor.JarFileResult<ModuleMetadata> result : results) {
buildReport(format, root, result);
}
writeReport(resolveFormatter(format).write(root), format);
}
}

private void buildReport(Format format, Node root, JarProcessor.JarFileResult<java.lang.module.ModuleDescriptor> result) {
java.lang.module.ModuleDescriptor md = result.getResult();
private void buildReport(Format format, Node root, JarProcessor.JarFileResult<ModuleMetadata> result) {

appendSubject(root, result.getJarPath(), "module descriptor", resultNode -> {
org.kordamp.jarviz.core.model.ModuleName moduleName = result.getResult().getModuleName();
if (!moduleName.isValid()) {
resultNode.node($("report.key.name")).value(moduleName.getModuleName()).end()
.node($("report.key.source")).value(resolveSource(moduleName)).end()
.node($("report.key.automatic")).value(!EXPLICIT.equals(resolveSource(moduleName))).end()
.node($("report.key.valid")).value(moduleName.isValid()).end()
.node($("report.key.reason")).value(moduleName.getReason()).end();
return;
}

java.lang.module.ModuleDescriptor md = result.getResult().getModuleDescriptor().get();

Node module = resultNode.node($("report.key.module"));
module.node($("report.key.name")).value(md.name()).end();
md.version().ifPresent(v -> module.node($("report.key.version")).value(v).end());
Expand Down
Loading

0 comments on commit 8083ba6

Please sign in to comment.