Skip to content

Commit

Permalink
Clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Sep 25, 2024
1 parent 94ee0ce commit 642ea4f
Show file tree
Hide file tree
Showing 21 changed files with 91 additions and 100 deletions.
56 changes: 23 additions & 33 deletions byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -757,31 +757,21 @@ public Resolution resolve(int index, Class<?> type) {
*/
interface Engine {

/**
* The class file extension.
*/
String CLASS_FILE_EXTENSION = ".class";

/**
* The module info class file.
*/
String MODULE_INFO = "module-info" + CLASS_FILE_EXTENSION;
String MODULE_INFO = "module-info" + ClassFileLocator.CLASS_FILE_EXTENSION;

/**
* The package info class file.
*/
String PACKAGE_INFO = "package-info" + CLASS_FILE_EXTENSION;
String PACKAGE_INFO = "package-info" + ClassFileLocator.CLASS_FILE_EXTENSION;

/**
* The name of the file that contains declares Byte Buddy plugins for discovery.
*/
String PLUGIN_FILE = "META-INF/net.bytebuddy/build.plugins";

/**
* The prefix folder for {@code META-INF/versions/} which contains multi-release files.
*/
String META_INF_VERSIONS = "META-INF/versions/";

/**
* Defines a new Byte Buddy instance for usage for type creation.
*
Expand Down Expand Up @@ -3012,11 +3002,11 @@ public static Source ofTypes(
) {
Map<String, byte[]> storage = new HashMap<String, byte[]>();
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
storage.put(entry.getKey().getInternalName() + CLASS_FILE_EXTENSION, entry.getValue());
storage.put(entry.getKey().getInternalName() + ClassFileLocator.CLASS_FILE_EXTENSION, entry.getValue());
}
for (Map.Entry<ClassFileVersion, Map<TypeDescription, byte[]>> versioned : versionedBinaryRepresentations.entrySet()) {
for (Map.Entry<TypeDescription, byte[]> entry : versioned.getValue().entrySet()) {
storage.put(META_INF_VERSIONS + versioned.getKey().getJavaVersion() + "/" + entry.getKey().getInternalName() + CLASS_FILE_EXTENSION, entry.getValue());
storage.put(ClassFileLocator.META_INF_VERSIONS + versioned.getKey().getJavaVersion() + "/" + entry.getKey().getInternalName() + ClassFileLocator.CLASS_FILE_EXTENSION, entry.getValue());
}
}
return new InMemory(storage);
Expand Down Expand Up @@ -3386,7 +3376,7 @@ public ForJarOutputStream(JarOutputStream outputStream) {
*/
public void store(Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
outputStream.putNextEntry(new JarEntry(entry.getKey().getInternalName() + CLASS_FILE_EXTENSION));
outputStream.putNextEntry(new JarEntry(entry.getKey().getInternalName() + ClassFileLocator.CLASS_FILE_EXTENSION));
outputStream.write(entry.getValue());
outputStream.closeEntry();
}
Expand All @@ -3397,7 +3387,7 @@ public void store(Map<TypeDescription, byte[]> binaryRepresentations) throws IOE
*/
public void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
outputStream.putNextEntry(new JarEntry(META_INF_VERSIONS + version + "/" + entry.getKey().getInternalName() + CLASS_FILE_EXTENSION));
outputStream.putNextEntry(new JarEntry(ClassFileLocator.META_INF_VERSIONS + version + "/" + entry.getKey().getInternalName() + ClassFileLocator.CLASS_FILE_EXTENSION));
outputStream.write(entry.getValue());
outputStream.closeEntry();
}
Expand Down Expand Up @@ -3526,7 +3516,7 @@ public Sink write(@MaybeNull Manifest manifest) throws IOException {
*/
public void store(Map<TypeDescription, byte[]> binaryRepresentations) {
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
storage.put(entry.getKey().getInternalName() + CLASS_FILE_EXTENSION, entry.getValue());
storage.put(entry.getKey().getInternalName() + ClassFileLocator.CLASS_FILE_EXTENSION, entry.getValue());
}
}

Expand All @@ -3535,7 +3525,7 @@ public void store(Map<TypeDescription, byte[]> binaryRepresentations) {
*/
public void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
storage.put(META_INF_VERSIONS + version + "/" + entry.getKey().getInternalName() + CLASS_FILE_EXTENSION, entry.getValue());
storage.put(ClassFileLocator.META_INF_VERSIONS + version + "/" + entry.getKey().getInternalName() + ClassFileLocator.CLASS_FILE_EXTENSION, entry.getValue());
}
}

Expand Down Expand Up @@ -3588,9 +3578,9 @@ public Map<String, byte[]> getStorage() {
public Map<String, byte[]> toTypeMap() {
Map<String, byte[]> binaryRepresentations = new HashMap<String, byte[]>();
for (Map.Entry<String, byte[]> entry : storage.entrySet()) {
if (entry.getKey().endsWith(CLASS_FILE_EXTENSION) && !entry.getKey().startsWith(META_INF_VERSIONS)) {
if (entry.getKey().endsWith(ClassFileLocator.CLASS_FILE_EXTENSION) && !entry.getKey().startsWith(ClassFileLocator.META_INF_VERSIONS)) {
binaryRepresentations.put(entry.getKey()
.substring(0, entry.getKey().length() - CLASS_FILE_EXTENSION.length())
.substring(0, entry.getKey().length() - ClassFileLocator.CLASS_FILE_EXTENSION.length())
.replace('/', '.'), entry.getValue());
}
}
Expand All @@ -3607,13 +3597,13 @@ public Map<String, byte[]> toTypeMap(ClassFileVersion classFileVersion) {
Map<String, byte[]> binaryRepresentations = new HashMap<String, byte[]>();
Map<String, Integer> versions = new HashMap<String, Integer>();
for (Map.Entry<String, byte[]> entry : storage.entrySet()) {
if (entry.getKey().endsWith(CLASS_FILE_EXTENSION)) {
if (entry.getKey().endsWith(ClassFileLocator.CLASS_FILE_EXTENSION)) {
String suffix;
int version;
if (entry.getKey().startsWith(META_INF_VERSIONS)) {
suffix = entry.getKey().substring(entry.getKey().indexOf('/', META_INF_VERSIONS.length()) + 1);
if (entry.getKey().startsWith(ClassFileLocator.META_INF_VERSIONS)) {
suffix = entry.getKey().substring(entry.getKey().indexOf('/', ClassFileLocator.META_INF_VERSIONS.length()) + 1);
try {
int candidate = Integer.parseInt(entry.getKey().substring(META_INF_VERSIONS.length(), entry.getKey().indexOf('/', META_INF_VERSIONS.length())));
int candidate = Integer.parseInt(entry.getKey().substring(ClassFileLocator.META_INF_VERSIONS.length(), entry.getKey().indexOf('/', ClassFileLocator.META_INF_VERSIONS.length())));
if (candidate < 7 || candidate > classFileVersion.getJavaVersion()) {
continue;
}
Expand All @@ -3629,7 +3619,7 @@ public Map<String, byte[]> toTypeMap(ClassFileVersion classFileVersion) {
if (current == null || current < version) {
versions.put(suffix, version);
binaryRepresentations.put(suffix
.substring(0, suffix.length() - CLASS_FILE_EXTENSION.length())
.substring(0, suffix.length() - ClassFileLocator.CLASS_FILE_EXTENSION.length())
.replace('/', '.'), entry.getValue());
}
}
Expand Down Expand Up @@ -3667,7 +3657,7 @@ public ForFolder(File folder) {
*/
private static void doStore(File folder, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
File target = new File(folder, entry.getKey().getInternalName() + CLASS_FILE_EXTENSION);
File target = new File(folder, entry.getKey().getInternalName() + ClassFileLocator.CLASS_FILE_EXTENSION);
if (!target.getParentFile().isDirectory() && !target.getParentFile().mkdirs()) {
throw new IOException("Could not create directory: " + target.getParent());
}
Expand Down Expand Up @@ -3710,7 +3700,7 @@ public void store(Map<TypeDescription, byte[]> binaryRepresentations) throws IOE
* {@inheritDoc}
*/
public void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
doStore(new File(folder, META_INF_VERSIONS + version), binaryRepresentations);
doStore(new File(folder, ClassFileLocator.META_INF_VERSIONS + version), binaryRepresentations);
}

/**
Expand Down Expand Up @@ -4894,14 +4884,14 @@ public Summary apply(Source source, Target target, List<? extends Plugin.Factory
while (name.startsWith("/")) {
name = name.substring(1);
}
if (name.endsWith(CLASS_FILE_EXTENSION) && !name.endsWith(PACKAGE_INFO) && !name.equals(MODULE_INFO)) {
if (name.endsWith(ClassFileLocator.CLASS_FILE_EXTENSION) && !name.endsWith(PACKAGE_INFO) && !name.equals(MODULE_INFO)) {
try {
dispatcher.accept(new Preprocessor(element,
name.substring(name.startsWith(META_INF_VERSIONS)
? name.indexOf('/', META_INF_VERSIONS.length()) + 1
: 0, name.length() - CLASS_FILE_EXTENSION.length()).replace('/', '.'),
name.startsWith(META_INF_VERSIONS)
? Integer.parseInt(name.substring(META_INF_VERSIONS.length(), name.indexOf('/', META_INF_VERSIONS.length())))
name.substring(name.startsWith(ClassFileLocator.META_INF_VERSIONS)
? name.indexOf('/', ClassFileLocator.META_INF_VERSIONS.length()) + 1
: 0, name.length() - ClassFileLocator.CLASS_FILE_EXTENSION.length()).replace('/', '.'),
name.startsWith(ClassFileLocator.META_INF_VERSIONS)
? Integer.parseInt(name.substring(ClassFileLocator.META_INF_VERSIONS.length(), name.indexOf('/', ClassFileLocator.META_INF_VERSIONS.length())))
: 0,
classFileLocator,
typePool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public interface ClassFileLocator extends Closeable {
*/
String CLASS_FILE_EXTENSION = ".class";

/**
* The prefix folder for {@code META-INF/versions/} which contains multi-release files.
*/
String META_INF_VERSIONS = "META-INF/versions/";

/**
* Locates the class file for a given type and returns the binary data of the class file.
*
Expand Down Expand Up @@ -195,11 +200,6 @@ public void close() {
@HashCodeAndEqualsPlugin.Enhance
abstract class MultiReleaseAware implements ClassFileLocator {

/**
* The path prefix of a multi-release folder.
*/
private static final String MULTI_RELEASE_PREFIX = "META-INF/versions/";

/**
* The property name of a multi-release JAR file.
*/
Expand Down Expand Up @@ -232,7 +232,7 @@ protected MultiReleaseAware(int[] version) {
public Resolution locate(String name) throws IOException {
String path = name.replace('.', File.separatorChar) + CLASS_FILE_EXTENSION;
for (int index = 0; index < version.length + 1; index++) {
byte[] binaryRepresentation = doLocate(index == version.length ? path : MULTI_RELEASE_PREFIX + version[index] + "/" + path);
byte[] binaryRepresentation = doLocate(index == version.length ? path : META_INF_VERSIONS + version[index] + "/" + path);
if (binaryRepresentation != null) {
return new Resolution.Explicit(binaryRepresentation);
}
Expand Down Expand Up @@ -901,9 +901,9 @@ private static ClassFileLocator of(JarFile jarFile, ClassFileVersion classFileVe
Enumeration<JarEntry> enumeration = jarFile.entries();
while (enumeration.hasMoreElements()) {
String name = enumeration.nextElement().getName();
if (name.endsWith(CLASS_FILE_EXTENSION) && name.startsWith(MultiReleaseAware.MULTI_RELEASE_PREFIX)) {
if (name.endsWith(CLASS_FILE_EXTENSION) && name.startsWith(META_INF_VERSIONS)) {
try {
int candidate = Integer.parseInt(name.substring(MultiReleaseAware.MULTI_RELEASE_PREFIX.length(), name.indexOf('/', MultiReleaseAware.MULTI_RELEASE_PREFIX.length())));
int candidate = Integer.parseInt(name.substring(META_INF_VERSIONS.length(), name.indexOf('/', META_INF_VERSIONS.length())));
if (candidate > 7 && candidate <= classFileVersion.getJavaVersion()) {
versions.add(candidate);
}
Expand Down Expand Up @@ -1255,7 +1255,7 @@ public static ClassFileLocator of(File folder, ClassFileVersion classFileVersion
}
int[] version;
if (multiRelease) {
File[] file = new File(folder, MultiReleaseAware.MULTI_RELEASE_PREFIX).listFiles();
File[] file = new File(folder, META_INF_VERSIONS).listFiles();
if (file != null) {
SortedSet<Integer> versions = new TreeSet<Integer>();
for (int index = 0; index < file.length; index++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testMissingDependency() throws IOException {
PluginEngineDefaultOtherTest.class,
TypeWithDependency.class,
TypeWithoutDependency.class}) {
outputStream.putNextEntry(new JarEntry(type.getName().replace(".", "/") + ".class"));
outputStream.putNextEntry(new JarEntry(type.getName().replace(".", "/") + ClassFileLocator.CLASS_FILE_EXTENSION));
outputStream.write(ClassFileLocator.ForClassLoader.read(type));
outputStream.closeEntry();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public void testUnresolved() throws Exception {
? new SimplePlugin()
: new PreprocessingPlugin(new SimplePlugin());
Plugin.Engine.Source source = new Plugin.Engine.Source.InMemory(Collections.singletonMap(
Sample.class.getName().replace('.', '/') + ".class",
Sample.class.getName().replace('.', '/') + ClassFileLocator.CLASS_FILE_EXTENSION,
ClassFileLocator.ForClassLoader.read(Sample.class))) {
@Override
public ClassFileLocator toClassFileLocator(ClassFileVersion classFileVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void testOfTypes() throws Exception {
Iterator<Plugin.Engine.Source.Element> iterator = origin.iterator();
assertThat(iterator.hasNext(), is(true));
Plugin.Engine.Source.Element element = iterator.next();
assertThat(element.getName(), is(Foo.class.getName().replace('.', '/') + ".class"));
assertThat(element.getName(), is(Foo.class.getName().replace('.', '/') + ClassFileLocator.CLASS_FILE_EXTENSION));
assertThat(element.resolveAs(Object.class), nullValue(Object.class));
assertThat(StreamDrainer.DEFAULT.drain(element.getInputStream()), is(ClassFileLocator.ForClassLoader.read(Foo.class)));
assertThat(iterator.hasNext(), is(false));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.bytebuddy.build;

import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.test.utility.JavaVersionRule;
import net.bytebuddy.utility.StreamDrainer;
import org.junit.Before;
Expand Down Expand Up @@ -45,7 +46,7 @@ public void testWriteType() throws Exception {
} finally {
sink.close();
}
File file = new File(folder, TypeDescription.ForLoadedType.of(Object.class).getInternalName() + ".class");
File file = new File(folder, TypeDescription.ForLoadedType.of(Object.class).getInternalName() + ClassFileLocator.CLASS_FILE_EXTENSION);
assertThat(file.isFile(), is(true));
InputStream inputStream = new FileInputStream(file);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.bytebuddy.build;

import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.utility.StreamDrainer;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -50,7 +51,7 @@ public void testWriteType() throws Exception {
try {
assertThat(inputStream.getManifest(), nullValue(Manifest.class));
JarEntry entry = inputStream.getNextJarEntry();
assertThat(entry.getName(), is(TypeDescription.ForLoadedType.of(Object.class).getInternalName() + ".class"));
assertThat(entry.getName(), is(TypeDescription.ForLoadedType.of(Object.class).getInternalName() + ClassFileLocator.CLASS_FILE_EXTENSION));
assertThat(StreamDrainer.DEFAULT.drain(inputStream), is(new byte[]{1, 2, 3}));
assertThat(inputStream.getNextJarEntry(), nullValue(JarEntry.class));
} finally {
Expand Down
Loading

0 comments on commit 642ea4f

Please sign in to comment.