Skip to content

Commit

Permalink
Merge pull request #67
Browse files Browse the repository at this point in the history
* Removed deprecated methods
  • Loading branch information
shartte authored Apr 25, 2024
1 parent 5c4f4f8 commit 72f8d15
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 137 deletions.
32 changes: 0 additions & 32 deletions src/main/java/cpw/mods/jarhandling/JarMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.lang.module.ModuleDescriptor;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

public interface JarMetadata {
Expand Down Expand Up @@ -157,35 +156,4 @@ private static String cleanModuleName(String mn) {

return mn;
}

/**
* @deprecated Build from jar contents directly using {@link #from(JarContents)}.
*/
@Deprecated(forRemoval = true, since = "2.1.23")
static SimpleJarMetadata fromFileName(final Path path, final Set<String> pkgs, final List<SecureJar.Provider> providers) {
var nav = computeNameAndVersion(path);
return new SimpleJarMetadata(nav.name(), nav.version(), () -> pkgs, providers);
}

/**
* @deprecated Use {@link #from(JarContents)} instead.
*/
@Deprecated(forRemoval = true, since = "2.1.16")
static JarMetadata from(final SecureJar jar, final Path... path) {
if (path.length==0) throw new IllegalArgumentException("Need at least one path");
final var pkgs = jar.getPackages();
var mi = jar.moduleDataProvider().findFile("module-info.class");
if (mi.isPresent()) {
return new ModuleJarMetadata(mi.get(), jar::getPackages);
} else {
var providers = jar.getProviders();
var fileCandidate = fromFileName(path[0], pkgs, providers);
var aname = jar.moduleDataProvider().getManifest().getMainAttributes().getValue("Automatic-Module-Name");
if (aname != null) {
return new SimpleJarMetadata(aname, fileCandidate.version(), () -> pkgs, providers);
} else {
return fileCandidate;
}
}
}
}
60 changes: 1 addition & 59 deletions src/main/java/cpw/mods/jarhandling/SecureJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static Provider fromPath(final Path path, final BiPredicate<String, Strin
try {
var entries = Files.readAllLines(path).stream()
.map(String::trim)
.filter(l->l.length() > 0 && !l.startsWith("#")) // We support comments :)
.filter(l-> !l.isEmpty() && !l.startsWith("#")) // We support comments :)
.filter(p-> pkgFilter == null || pkgFilter.test(p.replace('.','/'), ""))
.toList();
return new Provider(sname, entries);
Expand All @@ -153,62 +153,4 @@ public static Provider fromPath(final Path path, final BiPredicate<String, Strin
enum Status {
NONE, INVALID, UNVERIFIED, VERIFIED
}

/**
* @deprecated Obtain via the {@link ModuleDescriptor} of the jar if you really need this.
*/
@Deprecated(forRemoval = true, since = "2.1.16")
default Set<String> getPackages() {
return moduleDataProvider().descriptor().packages();
}

/**
* @deprecated Obtain via the {@link ModuleDescriptor} of the jar if you really need this.
*/
@Deprecated(forRemoval = true, since = "2.1.16")
default List<Provider> getProviders() {
return moduleDataProvider().descriptor().provides().stream()
.map(p -> new Provider(p.service(), p.providers()))
.toList();
}

/**
* @deprecated Use {@link JarContentsBuilder} and {@link #from(JarContents)} instead.
*/
@Deprecated(forRemoval = true, since = "2.1.16")
static SecureJar from(BiPredicate<String, String> filter, final Path... paths) {
return from(jar->JarMetadata.from(jar, paths), filter, paths);
}

/**
* @deprecated Use {@link JarContentsBuilder} and {@link #from(JarContents)} instead.
*/
@Deprecated(forRemoval = true, since = "2.1.16")
static SecureJar from(Function<SecureJar, JarMetadata> metadataSupplier, final Path... paths) {
return from(Manifest::new, metadataSupplier, paths);
}

/**
* @deprecated Use {@link JarContentsBuilder} and {@link #from(JarContents)} instead.
*/
@Deprecated(forRemoval = true, since = "2.1.16")
static SecureJar from(Function<SecureJar, JarMetadata> metadataSupplier, BiPredicate<String, String> filter, final Path... paths) {
return from(Manifest::new, metadataSupplier, filter, paths);
}

/**
* @deprecated Use {@link JarContentsBuilder} and {@link #from(JarContents)} instead.
*/
@Deprecated(forRemoval = true, since = "2.1.16")
static SecureJar from(Supplier<Manifest> defaultManifest, Function<SecureJar, JarMetadata> metadataSupplier, final Path... paths) {
return from(defaultManifest, metadataSupplier, null, paths);
}

/**
* @deprecated Use {@link JarContentsBuilder} and {@link #from(JarContents)} instead.
*/
@Deprecated(forRemoval = true, since = "2.1.16")
static SecureJar from(Supplier<Manifest> defaultManifest, Function<SecureJar, JarMetadata> metadataSupplier, BiPredicate<String, String> filter, final Path... paths) {
return new Jar(defaultManifest, metadataSupplier, filter, paths);
}
}
26 changes: 1 addition & 25 deletions src/main/java/cpw/mods/jarhandling/impl/Jar.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.CodeSigner;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.Optional;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

Expand All @@ -30,17 +27,6 @@ public class Jar implements SecureJar {

private final JarMetadata metadata;

@Deprecated(forRemoval = true, since = "2.1.16")
public Jar(final Supplier<Manifest> defaultManifest, final Function<SecureJar, JarMetadata> metadataFunction, final BiPredicate<String, String> pathfilter, final Path... paths) {
this.contents = new JarContentsImpl(paths, defaultManifest, pathfilter);
this.manifest = contents.getManifest();
this.signingData = contents.signingData;
this.filesystem = contents.filesystem;

this.moduleDataProvider = new JarModuleDataProvider(this);
this.metadata = metadataFunction.apply(this);
}

public Jar(JarContentsImpl contents, JarMetadata metadata) {
this.contents = contents;
this.manifest = contents.getManifest();
Expand Down Expand Up @@ -107,16 +93,6 @@ public String name() {
return metadata.name();
}

@Override
public Set<String> getPackages() {
return contents.getPackages();
}

@Override
public List<Provider> getProviders() {
return contents.getMetaInfServices();
}

@Override
public Path getPath(String first, String... rest) {
return filesystem.getPath(first, rest);
Expand Down
10 changes: 0 additions & 10 deletions src/test/java/cpw/mods/jarhandling/impl/TestDummyJarProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@ public boolean hasSecurityData() {
return false;
}

@Override
public Set<String> getPackages() {
return null;
}

@Override
public List<Provider> getProviders() {
return null;
}

@Override
public String name() {
return null;
Expand Down
57 changes: 46 additions & 11 deletions src/test/java/cpw/mods/jarhandling/impl/TestMetadata.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,74 @@
package cpw.mods.jarhandling.impl;

import cpw.mods.jarhandling.JarContents;
import cpw.mods.jarhandling.JarMetadata;
import cpw.mods.jarhandling.SecureJar;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.jar.Manifest;

public class TestMetadata
{
@Test
void testMavenJar()
{
void testMavenJar() {
var path = Paths.get("startofthepathchain/new-protected-class-1.16.5/1.1_mapped_official_1.17.1/new-protected-class-1.16.5-1.1_mapped_official_1.17.1-api.jar");
var meta = JarMetadata.fromFileName(path, new HashSet<>(), new ArrayList<>());
var meta = JarMetadata.from(new FakeJarContent(path));
Assertions.assertEquals("_new._protected._class._1._16._5", meta.name());
Assertions.assertEquals("1.1_mapped_official_1.17.1", meta.version());
}

@Test
void testRootStart()
{
void testRootStart() {
var path = Paths.get("/instance/mods/1life-1.5.jar");
var meta = JarMetadata.fromFileName(path, new HashSet<>(), new ArrayList<>());
var meta = JarMetadata.from(new FakeJarContent(path));
Assertions.assertEquals("_1life", meta.name());
Assertions.assertEquals("1.5", meta.version());
}

@Test
void testNumberStart()
{
void testNumberStart() {
var path = Paths.get("mods/1life-1.5.jar");
var meta = JarMetadata.fromFileName(path, new HashSet<>(), new ArrayList<>());
var meta = JarMetadata.from(new FakeJarContent(path));
Assertions.assertEquals("_1life", meta.name());
Assertions.assertEquals("1.5", meta.version());
}

record FakeJarContent(Path primaryPath) implements JarContents {
@Override
public Path getPrimaryPath() {
return primaryPath;
}

@Override
public Optional<URI> findFile(String name) {
return Optional.empty();
}

@Override
public Manifest getManifest() {
return new Manifest();
}

@Override
public Set<String> getPackages() {
return Set.of();
}

@Override
public Set<String> getPackagesExcluding(String... excludedRootPackages) {
return Set.of();
}

@Override
public List<SecureJar.Provider> getMetaInfServices() {
return List.of();
}
}
}

0 comments on commit 72f8d15

Please sign in to comment.