Skip to content

Commit

Permalink
Merge branch 'master' into android-extension
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Aug 20, 2024
2 parents 28c108d + c274e42 commit d11652d
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 75 deletions.
27 changes: 25 additions & 2 deletions byte-buddy-dep/src/main/java/net/bytebuddy/ClassFileVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ public static ClassFileVersion ofMinorMajor(int versionNumber) {
* @return The appropriate class file version.
*/
public static ClassFileVersion ofJavaVersionString(String javaVersionString) {
return ofJavaVersionString(javaVersionString, OpenedClassReader.EXPERIMENTAL);
}

/**
* Returns the Java class file by its representation by a version string in accordance to the formats known to <i>javac</i>.
*
* @param javaVersionString The Java version string.
* @param experimental {@code true} if unknown version strings should be parsed as if they were known.
* @return The appropriate class file version.
*/
public static ClassFileVersion ofJavaVersionString(String javaVersionString, boolean experimental) {
if (javaVersionString.equals("1.1")) {
return JAVA_V1;
} else if (javaVersionString.equals("1.2")) {
Expand Down Expand Up @@ -259,7 +270,7 @@ public static ClassFileVersion ofJavaVersionString(String javaVersionString) {
} else if (javaVersionString.equals("1.23") || javaVersionString.equals("23")) {
return JAVA_V23;
} else {
if (OpenedClassReader.EXPERIMENTAL) {
if (experimental) {
try {
int version = Integer.parseInt(javaVersionString.startsWith("1.")
? javaVersionString.substring(2)
Expand All @@ -282,6 +293,18 @@ public static ClassFileVersion ofJavaVersionString(String javaVersionString) {
* @return A wrapper for the given Java class file version.
*/
public static ClassFileVersion ofJavaVersion(int javaVersion) {
return ofJavaVersion(javaVersion, OpenedClassReader.EXPERIMENTAL);
}

/**
* Creates a class file version for a given major release of Java. Currently, all versions reaching from
* Java 1 to Java 9 are supported.
*
* @param javaVersion The Java version.
* @param experimental {@code true} if unknown Java versions should also be considered.
* @return A wrapper for the given Java class file version.
*/
public static ClassFileVersion ofJavaVersion(int javaVersion, boolean experimental) {
switch (javaVersion) {
case 1:
return JAVA_V1;
Expand Down Expand Up @@ -330,7 +353,7 @@ public static ClassFileVersion ofJavaVersion(int javaVersion) {
case 23:
return JAVA_V23;
default:
if (OpenedClassReader.EXPERIMENTAL && javaVersion > 0) {
if (experimental && javaVersion > 0) {
return new ClassFileVersion(BASE_VERSION + javaVersion);
} else {
throw new IllegalArgumentException("Unknown Java version: " + javaVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ interface Factory {
*/
AsmClassReader make(byte[] binaryRepresentation);

/**
* Creates a class reader for a given class file.
*
* @param binaryRepresentation The class file's binary representation.
* @param experimental {@code true} if unknown Java class files versions should also be considered.
* @return A class reader representation for the supplied class file.
*/
AsmClassReader make(byte[] binaryRepresentation, boolean experimental);

/**
* A default implementation that creates a pure ASM {@link ClassReader}.
*/
Expand All @@ -72,6 +81,13 @@ enum Default implements Factory {
public AsmClassReader make(byte[] binaryRepresentation) {
return new AsmClassReader.Default(OpenedClassReader.of(binaryRepresentation));
}

/**
* {@inheritDoc}
*/
public AsmClassReader make(byte[] binaryRepresentation, boolean experimental) {
return new AsmClassReader.Default(OpenedClassReader.of(binaryRepresentation, experimental));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public AsmClassReader make(byte[] binaryRepresentation) {
return new AsmClassReader.Default(of(binaryRepresentation));
}

/**
* {@inheritDoc}
*/
public AsmClassReader make(byte[] binaryRepresentation, boolean experimental) {
return new AsmClassReader.Default(of(binaryRepresentation, experimental));
}

/**
* A proxy for {@code java.security.AccessController#doPrivileged} that is activated if available.
*
Expand All @@ -77,16 +84,28 @@ private static <T> T doPrivileged(PrivilegedAction<T> action) {
return action.run();
}


/**
* Creates a class reader for the given binary representation of a class file.
*
* @param binaryRepresentation The binary representation of a class file to read.
* @return An appropriate class reader.
*/
public static ClassReader of(byte[] binaryRepresentation) {
return of(binaryRepresentation, EXPERIMENTAL);
}

/**
* Creates a class reader for the given binary representation of a class file.
*
* @param binaryRepresentation The binary representation of a class file to read.
* @param experimental {@code true} if unknown class file versions should also be processed.
* @return An appropriate class reader.
*/
public static ClassReader of(byte[] binaryRepresentation, boolean experimental) {
ClassFileVersion classFileVersion = ClassFileVersion.ofClassFile(binaryRepresentation), latest = ClassFileVersion.latest();
if (classFileVersion.isGreaterThan(latest)) {
if (EXPERIMENTAL) {
if (experimental) {
binaryRepresentation[4] = (byte) (latest.getMinorVersion() >>> 8);
binaryRepresentation[5] = (byte) latest.getMinorVersion();
binaryRepresentation[6] = (byte) (latest.getMajorVersion() >>> 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.bytebuddy.ClassFileVersion;
import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.build.AccessControllerPlugin;
import net.bytebuddy.build.HashCodeAndEqualsPlugin;
import net.bytebuddy.description.method.MethodDescription;
Expand Down Expand Up @@ -1234,7 +1235,7 @@ protected DynamicClassLoader(Class<?> target) {
*/
@SuppressFBWarnings(value = {"REC_CATCH_EXCEPTION", "DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED"}, justification = "Expected internal invocation.")
protected static Object proxy(Class<?> proxy, Map<Method, Dispatcher> dispatchers) {
ClassWriter classWriter = new ClassWriter(0);
ClassWriter classWriter = new ClassWriter(AsmVisitorWrapper.NO_FLAGS);
classWriter.visit(ClassFileVersion.JAVA_V5.getMinorMajorVersion(),
Opcodes.ACC_PUBLIC,
Type.getInternalName(proxy) + "$Proxy",
Expand Down Expand Up @@ -1310,7 +1311,7 @@ protected static Object proxy(Class<?> proxy, Map<Method, Dispatcher> dispatcher
*/
@SuppressFBWarnings(value = {"REC_CATCH_EXCEPTION", "DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED"}, justification = "Expected internal invocation.")
protected static Invoker invoker() {
ClassWriter classWriter = new ClassWriter(0);
ClassWriter classWriter = new ClassWriter(AsmVisitorWrapper.NO_FLAGS);
classWriter.visit(ClassFileVersion.JAVA_V5.getMinorMajorVersion(),
Opcodes.ACC_PUBLIC,
Type.getInternalName(Invoker.class) + "$Dispatcher",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package net.bytebuddy.asm;

import net.bytebuddy.utility.OpenedClassReader;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Opcodes;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package net.bytebuddy.description.type;

import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.description.TypeVariableSource;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.ParameterDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.loading.ByteArrayClassLoader;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.bytecode.StackSize;
import net.bytebuddy.test.utility.JavaVersionRule;
import net.bytebuddy.utility.AsmClassReader;
import net.bytebuddy.utility.AsmClassWriter;
import net.bytebuddy.utility.OpenedClassReader;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
Expand Down Expand Up @@ -1991,11 +1995,11 @@ public abstract static class InconsistentGenerics<T extends Exception> implement
public static class GenericDisintegrator extends ClassVisitor {

public static Field make() throws IOException, ClassNotFoundException, NoSuchFieldException {
ClassReader classReader = new ClassReader(InconsistentGenerics.class.getName());
ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS);
classReader.accept(new GenericDisintegrator(classWriter), 0);
AsmClassReader classReader = AsmClassReader.Factory.Default.INSTANCE.make(ClassFileLocator.ForClassLoader.read(InconsistentGenerics.class));
AsmClassWriter classWriter = AsmClassWriter.Factory.Default.INSTANCE.make(ClassWriter.COMPUTE_MAXS, classReader);
classReader.accept(new GenericDisintegrator(classWriter.getVisitor()), AsmVisitorWrapper.NO_FLAGS);
return new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER,
Collections.singletonMap(InconsistentGenerics.class.getName(), classWriter.toByteArray()),
Collections.singletonMap(InconsistentGenerics.class.getName(), classWriter.getBinaryRepresentation()),
ByteArrayClassLoader.PersistenceHandler.MANIFEST)
.loadClass(InconsistentGenerics.class.getName()).getDeclaredField(FOO);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.bytebuddy.description.type;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.description.TypeVariableSource;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.annotation.AnnotationList;
Expand All @@ -16,6 +17,8 @@
import net.bytebuddy.test.scope.EnclosingType;
import net.bytebuddy.test.utility.JavaVersionRule;
import net.bytebuddy.test.visibility.Sample;
import net.bytebuddy.utility.AsmClassReader;
import net.bytebuddy.utility.AsmClassWriter;
import net.bytebuddy.utility.OpenedClassReader;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matcher;
Expand Down Expand Up @@ -777,13 +780,13 @@ public void testNestMatesSupported() throws Exception {

@Test
public void testNonEnclosedAnonymousType() throws Exception {
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, "foo/Bar", null, "java/lang/Object", null);
classWriter.visitInnerClass("foo/Bar", null, null, Opcodes.ACC_PUBLIC);
classWriter.visitEnd();
AsmClassWriter classWriter = AsmClassWriter.Factory.Default.INSTANCE.make(AsmVisitorWrapper.NO_FLAGS);
classWriter.getVisitor().visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, "foo/Bar", null, "java/lang/Object", null);
classWriter.getVisitor().visitInnerClass("foo/Bar", null, null, Opcodes.ACC_PUBLIC);
classWriter.getVisitor().visitEnd();

ClassLoader classLoader = new ByteArrayClassLoader(null,
Collections.singletonMap("foo.Bar", classWriter.toByteArray()),
Collections.singletonMap("foo.Bar", classWriter.getBinaryRepresentation()),
ByteArrayClassLoader.PersistenceHandler.MANIFEST);
Class<?> type = classLoader.loadClass("foo.Bar");

Expand Down Expand Up @@ -946,11 +949,11 @@ public SignatureMalformer(ClassVisitor classVisitor) {
}

public static Class<?> malform(Class<?> type) throws Exception {
ClassReader classReader = new ClassReader(type.getName());
ClassWriter classWriter = new ClassWriter(classReader, 0);
classReader.accept(new SignatureMalformer(classWriter), 0);
AsmClassReader classReader = AsmClassReader.Factory.Default.INSTANCE.make(ClassFileLocator.ForClassLoader.read(type));
AsmClassWriter classWriter = AsmClassWriter.Factory.Default.INSTANCE.make(AsmVisitorWrapper.NO_FLAGS, classReader);
classReader.accept(new SignatureMalformer(classWriter.getVisitor()), AsmVisitorWrapper.NO_FLAGS);
ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER,
Collections.singletonMap(type.getName(), classWriter.toByteArray()),
Collections.singletonMap(type.getName(), classWriter.getBinaryRepresentation()),
ByteArrayClassLoader.PersistenceHandler.MANIFEST);
return classLoader.loadClass(type.getName());
}
Expand Down Expand Up @@ -978,11 +981,11 @@ public TypeVariableMalformer(ClassVisitor classVisitor) {
}

public static Class<?> malform(Class<?> type) throws Exception {
ClassReader classReader = new ClassReader(type.getName());
ClassWriter classWriter = new ClassWriter(classReader, 0);
classReader.accept(new TypeVariableMalformer(classWriter), 0);
AsmClassReader classReader = AsmClassReader.Factory.Default.INSTANCE.make(ClassFileLocator.ForClassLoader.read(type));
AsmClassWriter classWriter = AsmClassWriter.Factory.Default.INSTANCE.make(AsmVisitorWrapper.NO_FLAGS, classReader);
classReader.accept(new TypeVariableMalformer(classWriter.getVisitor()), AsmVisitorWrapper.NO_FLAGS);
ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER,
Collections.singletonMap(type.getName(), classWriter.toByteArray()),
Collections.singletonMap(type.getName(), classWriter.getBinaryRepresentation()),
ByteArrayClassLoader.PersistenceHandler.MANIFEST);
return classLoader.loadClass(type.getName());
}
Expand Down Expand Up @@ -1015,11 +1018,11 @@ public ParameterizedTypeLengthMalformer(ClassVisitor classVisitor) {
}

public static Class<?> malform(Class<?> type) throws Exception {
ClassReader classReader = new ClassReader(type.getName());
ClassWriter classWriter = new ClassWriter(classReader, 0);
classReader.accept(new ParameterizedTypeLengthMalformer(classWriter), 0);
AsmClassReader classReader = AsmClassReader.Factory.Default.INSTANCE.make(ClassFileLocator.ForClassLoader.read(type));
AsmClassWriter classWriter = AsmClassWriter.Factory.Default.INSTANCE.make(AsmVisitorWrapper.NO_FLAGS, classReader);
classReader.accept(new ParameterizedTypeLengthMalformer(classWriter.getVisitor()), AsmVisitorWrapper.NO_FLAGS);
ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER,
Collections.singletonMap(type.getName(), classWriter.toByteArray()),
Collections.singletonMap(type.getName(), classWriter.getBinaryRepresentation()),
ByteArrayClassLoader.PersistenceHandler.MANIFEST);
return classLoader.loadClass(type.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.test.utility.CallTraceable;
import net.bytebuddy.test.utility.JavaVersionRule;
import net.bytebuddy.utility.AsmClassWriter;
import net.bytebuddy.utility.OpenedClassReader;
import net.bytebuddy.utility.visitor.ContextClassVisitor;
import org.hamcrest.CoreMatchers;
Expand Down Expand Up @@ -1520,12 +1521,12 @@ public void testWrapClassVisitor() throws Exception {
TypeDescription typeDescription = createPlain()
.make()
.getTypeDescription();
ClassWriter classWriter = new ClassWriter(AsmVisitorWrapper.NO_FLAGS);
AsmClassWriter classWriter = AsmClassWriter.Factory.Default.INSTANCE.make(AsmVisitorWrapper.NO_FLAGS);
ContextClassVisitor classVisitor = createPlain()
.defineMethod(FOO, Object.class, Visibility.PUBLIC, Ownership.STATIC)
.throwing(Exception.class)
.intercept(new Implementation.Simple(new TextConstant(FOO), MethodReturn.REFERENCE))
.wrap(classWriter);
.wrap(classWriter.getVisitor());
classVisitor.visit(ClassFileVersion.ofThisVm().getMinorMajorVersion(),
typeDescription.getActualModifiers(true),
typeDescription.getInternalName(),
Expand All @@ -1536,7 +1537,7 @@ public void testWrapClassVisitor() throws Exception {
assertThat(classVisitor.getAuxiliaryTypes().size(), is(0));
assertThat(classVisitor.getLoadedTypeInitializer().isAlive(), is(false));
Class<?> type = new DynamicType.Default.Unloaded<Object>(typeDescription,
classWriter.toByteArray(),
classWriter.getBinaryRepresentation(),
LoadedTypeInitializer.NoOp.INSTANCE,
Collections.<DynamicType>emptyList(),
TypeResolutionStrategy.Passive.INSTANCE).load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
Expand Down
Loading

0 comments on commit d11652d

Please sign in to comment.