Skip to content

Commit

Permalink
Merge branch 'release-0.4.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
jamierocks committed Feb 9, 2021
2 parents 8f1f261 + 66d436c commit f722060
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

package org.cadixdev.bombe.jar;

import java.util.Collections;
import java.util.List;

/**
* A visitor for {@link AbstractJarEntry}, allowing them be be
* transformed.
Expand All @@ -41,42 +44,64 @@ public interface JarEntryTransformer {

/**
* Transforms the given class entry.
* <p>
* It is possible to remove entries by returning {@code null}, when this
* occurs no further transformers will process the entry.
*
* @param entry The class entry
* @return The transformed entry
* @return The transformed entry, or {@code null} if the entry should be removed
*/
default JarClassEntry transform(final JarClassEntry entry) {
return entry;
}

/**
* Transforms the given resource entry.
* <p>
* It is possible to remove entries by returning {@code null}, when this
* occurs no further transformers will process the entry.
*
* @param entry The resource entry
* @return The transformed entry
* @return The transformed entry, or {@code null} if the entry should be removed
*/
default JarResourceEntry transform(final JarResourceEntry entry) {
return entry;
}

/**
* Transforms the given manifest entry.
* <p>
* It is possible to remove entries by returning {@code null}, when this
* occurs no further transformers will process the entry.
*
* @param entry The manifest entry
* @return The transformed entry
* @return The transformed entry, or {@code null} if the entry should be removed
*/
default JarManifestEntry transform(final JarManifestEntry entry) {
return entry;
}

/**
* Transforms the given service provider configuration entry.
* <p>
* It is possible to remove entries by returning {@code null}, when this
* occurs no further transformers will process the entry.
*
* @param entry The service provider configuration entry
* @return The transformed entry
* @return The transformed entry, or {@code null} if the entry should be removed
*/
default JarServiceProviderConfigurationEntry transform(final JarServiceProviderConfigurationEntry entry) {
return entry;
}

/**
* Provides a list of {@link AbstractJarEntry jar entries} to add into the
* processed jar file.
*
* @return Entries to add into the final jar
*/
default List<AbstractJarEntry> additions() {
return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@

package org.cadixdev.bombe.jar.asm;

import static java.util.jar.Attributes.Name.MAIN_CLASS;

import org.cadixdev.bombe.jar.JarClassEntry;
import org.cadixdev.bombe.jar.JarEntryTransformer;
import org.cadixdev.bombe.jar.JarManifestEntry;
import org.cadixdev.bombe.jar.JarResourceEntry;
import org.cadixdev.bombe.jar.JarServiceProviderConfigurationEntry;
import org.cadixdev.bombe.jar.ServiceProviderConfiguration;
import org.objectweb.asm.ClassReader;
Expand All @@ -41,7 +44,9 @@
import org.objectweb.asm.commons.ClassRemapper;
import org.objectweb.asm.commons.Remapper;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.jar.Attributes;
import java.util.stream.Collectors;
Expand All @@ -55,6 +60,8 @@
*/
public class JarEntryRemappingTransformer implements JarEntryTransformer {

private static final Attributes.Name SHA_256_DIGEST = new Attributes.Name("SHA-256-Digest");

private final Remapper remapper;
private final BiFunction<ClassVisitor, Remapper, ClassRemapper> clsRemapper;

Expand Down Expand Up @@ -86,14 +93,24 @@ public JarClassEntry transform(final JarClassEntry entry) {
@Override
public JarManifestEntry transform(final JarManifestEntry entry) {
// Remap the Main-Class attribute, if present
if (entry.getManifest().getMainAttributes().containsKey(new Attributes.Name("Main-Class"))) {
final String mainClassObf = entry.getManifest().getMainAttributes().getValue("Main-Class")
if (entry.getManifest().getMainAttributes().containsKey(MAIN_CLASS)) {
final String mainClassObf = entry.getManifest().getMainAttributes().getValue(MAIN_CLASS)
.replace('.', '/');
final String mainClassDeobf = this.remapper.map(mainClassObf)
.replace('/', '.');

// Since Manifest is mutable, we need'nt create a new entry \o/
entry.getManifest().getMainAttributes().putValue("Main-Class", mainClassDeobf);
// Since Manifest is mutable, we needn't create a new entry \o/
entry.getManifest().getMainAttributes().put(MAIN_CLASS, mainClassDeobf);
}

// Remove all signature entries
for (final Iterator<Map.Entry<String, Attributes>> it = entry.getManifest().getEntries().entrySet().iterator(); it.hasNext();) {
final Map.Entry<String, Attributes> section = it.next();
if (section.getValue().remove(SHA_256_DIGEST) != null) {
if (section.getValue().isEmpty()) {
it.remove();
}
}
}

return entry;
Expand All @@ -119,4 +136,16 @@ public JarServiceProviderConfigurationEntry transform(final JarServiceProviderCo
return new JarServiceProviderConfigurationEntry(entry.getTime(), config);
}

@Override
public JarResourceEntry transform(final JarResourceEntry entry) {
// Strip signature files from metadata
if (entry.getName().startsWith("META-INF")) {
if (entry.getExtension().equals("RSA")
|| entry.getExtension().equals("SF")) {
return null;
}
}
return entry;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public Map<MethodSignature, InheritanceType> getMethods() {
@Override
public Set<ClassInfo> provideParents(final InheritanceProvider provider) {
if (this.parents == null) {
ClassInfo.super.provideParents(provider, this.parents = new HashSet<>());
super.provideParents(provider, this.parents = new HashSet<>());
}
return this.parents;
}
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ subprojects {

group = 'org.cadixdev'
archivesBaseName = project.name.toLowerCase()
version = '0.4.3'
version = '0.4.4'

repositories {
mavenCentral()
Expand Down Expand Up @@ -126,7 +126,7 @@ subprojects {
developer {
id = 'jamierocks'
name = 'Jamie Mansfield'
email = 'dev@jamierocks.uk'
email = 'jmansfield@cadixdev.org'
url = 'https://www.jamiemansfield.me/'
timezone = 'Europe/London'
}
Expand Down
15 changes: 15 additions & 0 deletions changelogs/0.3.5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Bombe 0.3.5
===

Bombe 0.3.5 is a small release introducing some new APIs to bolster the
capabilities of the jar transformation framework, namely allowing entries to be
introduced. To accomplish this, a `JarEntryTransformer#additions()` method has
been introduced. The `Jars` utility has been updated to support this, and a
release of Atlas will be made shortly to implement this feature.

The `Jars` utility has been deprecated in this version, advising consumers to
switch to Atlas. Jars was removed in 0.4.0, so this just serves as a final
notice to any lingering applications using the utility.

The remapping transformer will additionally strip signature files and entries
in the manifest. This transformer may in future be available standalone.
11 changes: 11 additions & 0 deletions changelogs/0.4.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Bombe 0.4.4
===

Bombe 0.4.4 is a small release introducing some new APIs to bolster the
capabilities of the jar transformation framework, namely allowing entries to be
introduced. To accomplish this, a `JarEntryTransformer#additions()` method has
been introduced. A release of Atlas will be made shortly to implement this
feature.

The remapping transformer will additionally strip signature files and entries
in the manifest. This transformer may in future be available standalone.

0 comments on commit f722060

Please sign in to comment.