Skip to content

Commit

Permalink
Consistent use of factories.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Aug 20, 2024
1 parent 03e92da commit 2aec967
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 41 deletions.
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,5 +1,6 @@
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;
Expand Down Expand Up @@ -1996,7 +1997,7 @@ public static class GenericDisintegrator extends ClassVisitor {
public static Field make() throws IOException, ClassNotFoundException, NoSuchFieldException {
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()), 0);
classReader.accept(new GenericDisintegrator(classWriter.getVisitor()), AsmVisitorWrapper.NO_FLAGS);
return new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER,
Collections.singletonMap(InconsistentGenerics.class.getName(), classWriter.getBinaryRepresentation()),
ByteArrayClassLoader.PersistenceHandler.MANIFEST)
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 Down Expand Up @@ -779,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 @@ -949,8 +950,8 @@ public SignatureMalformer(ClassVisitor classVisitor) {

public static Class<?> malform(Class<?> type) throws Exception {
AsmClassReader classReader = AsmClassReader.Factory.Default.INSTANCE.make(ClassFileLocator.ForClassLoader.read(type));
AsmClassWriter classWriter = AsmClassWriter.Factory.Default.INSTANCE.make(0, classReader);
classReader.accept(new SignatureMalformer(classWriter.getVisitor()), 0);
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.getBinaryRepresentation()),
ByteArrayClassLoader.PersistenceHandler.MANIFEST);
Expand Down Expand Up @@ -981,8 +982,8 @@ public TypeVariableMalformer(ClassVisitor classVisitor) {

public static Class<?> malform(Class<?> type) throws Exception {
AsmClassReader classReader = AsmClassReader.Factory.Default.INSTANCE.make(ClassFileLocator.ForClassLoader.read(type));
AsmClassWriter classWriter = AsmClassWriter.Factory.Default.INSTANCE.make(0, classReader);
classReader.accept(new TypeVariableMalformer(classWriter.getVisitor()), 0);
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.getBinaryRepresentation()),
ByteArrayClassLoader.PersistenceHandler.MANIFEST);
Expand Down Expand Up @@ -1018,8 +1019,8 @@ public ParameterizedTypeLengthMalformer(ClassVisitor classVisitor) {

public static Class<?> malform(Class<?> type) throws Exception {
AsmClassReader classReader = AsmClassReader.Factory.Default.INSTANCE.make(ClassFileLocator.ForClassLoader.read(type));
AsmClassWriter classWriter = AsmClassWriter.Factory.Default.INSTANCE.make(0, classReader);
classReader.accept(new ParameterizedTypeLengthMalformer(classWriter.getVisitor()), 0);
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.getBinaryRepresentation()),
ByteArrayClassLoader.PersistenceHandler.MANIFEST);
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.test.scope.EnclosingType;
import net.bytebuddy.utility.AsmClassReader;
import net.bytebuddy.utility.OpenedClassReader;
import org.junit.Test;
Expand Down Expand Up @@ -62,7 +61,7 @@ public TypeWriterDeclarationPreservationTest(Class<?> type) {
@Test
public void testRedefinition() throws Exception {
TypeModifierExtractor typeModifierExtractor = new TypeModifierExtractor();
AsmClassReader.Factory.Default.INSTANCE.make(ClassFileLocator.ForClassLoader.read(type)).accept(typeModifierExtractor, 0);
AsmClassReader.Factory.Default.INSTANCE.make(ClassFileLocator.ForClassLoader.read(type)).accept(typeModifierExtractor, AsmVisitorWrapper.NO_FLAGS);
new ByteBuddy()
.redefine(type)
.visit(new TypeValidator.Wrapper(typeModifierExtractor))
Expand All @@ -72,7 +71,7 @@ public void testRedefinition() throws Exception {
@Test
public void testRebasing() throws Exception {
TypeModifierExtractor typeModifierExtractor = new TypeModifierExtractor();
AsmClassReader.Factory.Default.INSTANCE.make(ClassFileLocator.ForClassLoader.read(type)).accept(typeModifierExtractor, 0);
AsmClassReader.Factory.Default.INSTANCE.make(ClassFileLocator.ForClassLoader.read(type)).accept(typeModifierExtractor, AsmVisitorWrapper.NO_FLAGS);
new ByteBuddy()
.rebase(type)
.visit(new TypeValidator.Wrapper(typeModifierExtractor))
Expand All @@ -82,7 +81,7 @@ public void testRebasing() throws Exception {
@Test
public void testDecoration() throws Exception {
TypeModifierExtractor typeModifierExtractor = new TypeModifierExtractor();
AsmClassReader.Factory.Default.INSTANCE.make(ClassFileLocator.ForClassLoader.read(type)).accept(typeModifierExtractor, 0);
AsmClassReader.Factory.Default.INSTANCE.make(ClassFileLocator.ForClassLoader.read(type)).accept(typeModifierExtractor, AsmVisitorWrapper.NO_FLAGS);
new ByteBuddy()
.decorate(type)
.visit(new TypeValidator.Wrapper(typeModifierExtractor))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
import net.bytebuddy.test.utility.JavaVersionRule;
import net.bytebuddy.utility.AsmClassReader;
import net.bytebuddy.utility.AsmClassWriter;
import net.bytebuddy.utility.JavaConstant;
import net.bytebuddy.utility.OpenedClassReader;
import org.junit.Rule;
Expand Down Expand Up @@ -678,15 +680,15 @@ public void testPropertyDefinitionEmptyName() throws Exception {

@Test
public void testOldJavaClassFileDeprecation() {
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(Opcodes.V1_4, Opcodes.ACC_DEPRECATED | Opcodes.ACC_ABSTRACT, "foo/Bar", null, "java/lang/Object", null);
classWriter.visitField(Opcodes.ACC_DEPRECATED, "qux", "Ljava/lang/Object;", null, null).visitEnd();
classWriter.visitMethod(Opcodes.ACC_DEPRECATED | Opcodes.ACC_ABSTRACT, "baz", "()V", null, null).visitEnd();
classWriter.visitEnd();
AsmClassWriter classWriter = AsmClassWriter.Factory.Default.INSTANCE.make(AsmVisitorWrapper.NO_FLAGS);
classWriter.getVisitor().visit(Opcodes.V1_4, Opcodes.ACC_DEPRECATED | Opcodes.ACC_ABSTRACT, "foo/Bar", null, "java/lang/Object", null);
classWriter.getVisitor().visitField(Opcodes.ACC_DEPRECATED, "qux", "Ljava/lang/Object;", null, null).visitEnd();
classWriter.getVisitor().visitMethod(Opcodes.ACC_DEPRECATED | Opcodes.ACC_ABSTRACT, "baz", "()V", null, null).visitEnd();
classWriter.getVisitor().visitEnd();

TypeDescription typeDescription = new TypeDescription.Latent("foo.Bar", 0, TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(Object.class));
Class<?> type = ByteArrayClassLoader.load(ClassLoadingStrategy.BOOTSTRAP_LOADER,
Collections.singletonMap(typeDescription, classWriter.toByteArray()),
Collections.singletonMap(typeDescription, classWriter.getBinaryRepresentation()),
ClassLoadingStrategy.NO_PROTECTION_DOMAIN,
ByteArrayClassLoader.PersistenceHandler.MANIFEST,
PackageDefinitionStrategy.Trivial.INSTANCE,
Expand All @@ -700,7 +702,7 @@ public void testOldJavaClassFileDeprecation() {
.make()
.getBytes();

ClassReader classReader = new ClassReader(binaryRepresentation);
AsmClassReader classReader = AsmClassReader.Factory.Default.INSTANCE.make(binaryRepresentation);
classReader.accept(new ClassVisitor(OpenedClassReader.ASM_API) {
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
Expand All @@ -725,7 +727,7 @@ public MethodVisitor visitMethod(int access, String name, String descriptor, Str
}
return super.visitMethod(access, name, descriptor, signature, exceptions);
}
}, 0);
}, AsmVisitorWrapper.NO_FLAGS);
}

@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.loading.ByteArrayClassLoader;
import net.bytebuddy.test.utility.JavaVersionRule;
import net.bytebuddy.utility.AsmClassWriter;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -156,42 +157,42 @@ public void testMultipleTypeArgumentAnnotation() throws Exception {

private Class<?> makeTypeWithAnnotation(Annotation annotation) throws Exception {
when(valueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
ClassWriter classWriter = new ClassWriter(AsmVisitorWrapper.NO_FLAGS);
classWriter.visit(ClassFileVersion.ofThisVm().getMinorMajorVersion(),
AsmClassWriter classWriter = AsmClassWriter.Factory.Default.INSTANCE.make(AsmVisitorWrapper.NO_FLAGS);
classWriter.getVisitor().visit(ClassFileVersion.ofThisVm().getMinorMajorVersion(),
Opcodes.ACC_PUBLIC,
BAR.replace('.', '/'),
null,
Type.getInternalName(Object.class),
null);
AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true);
AnnotationVisitor annotationVisitor = classWriter.getVisitor().visitAnnotation(Type.getDescriptor(annotation.annotationType()), true);
when(target.visit(any(String.class), anyBoolean())).thenReturn(annotationVisitor);
AnnotationDescription annotationDescription = AnnotationDescription.ForLoadedAnnotation.of(annotation);
annotationAppender.append(annotationDescription, valueFilter);
classWriter.visitEnd();
Class<?> bar = new ByteArrayClassLoader(getClass().getClassLoader(), Collections.singletonMap(BAR, classWriter.toByteArray())).loadClass(BAR);
classWriter.getVisitor().visitEnd();
Class<?> bar = new ByteArrayClassLoader(getClass().getClassLoader(), Collections.singletonMap(BAR, classWriter.getBinaryRepresentation())).loadClass(BAR);
assertThat(bar.getName(), is(BAR));
assertThat(bar.getSuperclass(), CoreMatchers.<Class<?>>is(Object.class));
return bar;
}

private Class<?> makeTypeWithSuperClassAnnotation(Annotation annotation) throws Exception {
when(valueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
ClassWriter classWriter = new ClassWriter(AsmVisitorWrapper.NO_FLAGS);
classWriter.visit(ClassFileVersion.ofThisVm().getMinorMajorVersion(),
AsmClassWriter classWriter = AsmClassWriter.Factory.Default.INSTANCE.make(AsmVisitorWrapper.NO_FLAGS);
classWriter.getVisitor().visit(ClassFileVersion.ofThisVm().getMinorMajorVersion(),
Opcodes.ACC_PUBLIC,
BAR.replace('.', '/'),
null,
Type.getInternalName(Object.class),
null);
AnnotationVisitor annotationVisitor = classWriter.visitTypeAnnotation(TypeReference.newSuperTypeReference(-1).getValue(),
AnnotationVisitor annotationVisitor = classWriter.getVisitor().visitTypeAnnotation(TypeReference.newSuperTypeReference(-1).getValue(),
null,
Type.getDescriptor(annotation.annotationType()),
true);
when(target.visit(any(String.class), anyBoolean())).thenReturn(annotationVisitor);
AnnotationDescription annotationDescription = AnnotationDescription.ForLoadedAnnotation.of(annotation);
annotationAppender.append(annotationDescription, valueFilter);
classWriter.visitEnd();
Class<?> bar = new ByteArrayClassLoader(getClass().getClassLoader(), Collections.singletonMap(BAR, classWriter.toByteArray())).loadClass(BAR);
classWriter.getVisitor().visitEnd();
Class<?> bar = new ByteArrayClassLoader(getClass().getClassLoader(), Collections.singletonMap(BAR, classWriter.getBinaryRepresentation())).loadClass(BAR);
assertThat(bar.getName(), is(BAR));
assertThat(bar.getSuperclass(), CoreMatchers.<Class<?>>is(Object.class));
return bar;
Expand Down

0 comments on commit 2aec967

Please sign in to comment.