Search in sources :

Example 1 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class TypeWriterDefaultTest method testTypeInLegacyConstantPoolRemapped.

@Test
public void testTypeInLegacyConstantPoolRemapped() throws Exception {
    Class<?> dynamicType = new ByteBuddy(ClassFileVersion.JAVA_V4).with(TypeValidation.DISABLED).subclass(Object.class).defineMethod(FOO, Object.class, Visibility.PUBLIC).intercept(FixedValue.value(Object.class)).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(dynamicType.getDeclaredMethod(FOO).invoke(dynamicType.getDeclaredConstructor().newInstance()), is((Object) Object.class));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 2 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class TypeWriterDefaultTest method testLegacyTypeRedefinitionIsDiscovered.

@Test
public void testLegacyTypeRedefinitionIsDiscovered() throws Exception {
    Class<?> dynamicType = new ByteBuddy().with(TypeValidation.DISABLED).redefine(Class.forName("net.bytebuddy.test.precompiled.TypeConstantSample")).method(named(BAR)).intercept(FixedValue.value(int.class)).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(dynamicType.getDeclaredMethod(BAR).invoke(null), is((Object) int.class));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 3 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class TypeWriterDefaultTest method testPrimitiveTypeInLegacyConstantPoolRemapped.

@Test
public void testPrimitiveTypeInLegacyConstantPoolRemapped() throws Exception {
    Class<?> dynamicType = new ByteBuddy(ClassFileVersion.JAVA_V4).with(TypeValidation.DISABLED).subclass(Object.class).defineMethod(FOO, Object.class, Visibility.PUBLIC).intercept(FixedValue.value(int.class)).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(dynamicType.getDeclaredMethod(FOO).invoke(dynamicType.getDeclaredConstructor().newInstance()), is((Object) int.class));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 4 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class SubclassDynamicTypeBuilderTest method testDoesNotOverrideMethodWithPackagePrivateArgumentType.

@Test
public void testDoesNotOverrideMethodWithPackagePrivateArgumentType() throws Exception {
    Class<?> type = new ByteBuddy().subclass(PackagePrivateArgumentType.class).name("net.bytebuddy.test.generated." + FOO).method(isDeclaredBy(PackagePrivateArgumentType.class)).intercept(StubMethod.INSTANCE).make().load(new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(PackagePrivateArgumentType.class, PackagePrivateArgumentType.Argument.class)), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethods().length, is(0));
}
Also used : ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) ByteBuddy(net.bytebuddy.ByteBuddy) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Example 5 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class SubclassDynamicTypeBuilderTest method testParameterMetaDataSubclassForLoaded.

@Test
@JavaVersionRule.Enforce(8)
public void testParameterMetaDataSubclassForLoaded() throws Exception {
    Class<?> dynamicType = new ByteBuddy().subclass(Class.forName(PARAMETER_NAME_CLASS)).method(named(FOO)).intercept(StubMethod.INSTANCE).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(dynamicType.getDeclaredMethods().length, is(1));
    Class<?> executable = Class.forName("java.lang.reflect.Executable");
    Method getParameters = executable.getDeclaredMethod("getParameters");
    Class<?> parameter = Class.forName("java.lang.reflect.Parameter");
    Method getName = parameter.getDeclaredMethod("getName");
    Method getModifiers = parameter.getDeclaredMethod("getModifiers");
    Method first = dynamicType.getDeclaredMethod("foo", String.class, long.class, int.class);
    Object[] methodParameter = (Object[]) getParameters.invoke(first);
    assertThat(getName.invoke(methodParameter[0]), is((Object) "first"));
    assertThat(getName.invoke(methodParameter[1]), is((Object) "second"));
    assertThat(getName.invoke(methodParameter[2]), is((Object) "third"));
    assertThat(getModifiers.invoke(methodParameter[0]), is((Object) Opcodes.ACC_FINAL));
    assertThat(getModifiers.invoke(methodParameter[1]), is((Object) 0));
    assertThat(getModifiers.invoke(methodParameter[2]), is((Object) 0));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) StubMethod(net.bytebuddy.implementation.StubMethod) Method(java.lang.reflect.Method) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Aggregations

ByteBuddy (net.bytebuddy.ByteBuddy)384 Test (org.junit.Test)361 DynamicType (net.bytebuddy.dynamic.DynamicType)190 Method (java.lang.reflect.Method)36 TypeDescription (net.bytebuddy.description.type.TypeDescription)25 InvocationTargetException (java.lang.reflect.InvocationTargetException)21 Field (java.lang.reflect.Field)18 AbstractDynamicTypeBuilderTest (net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest)17 URLClassLoader (java.net.URLClassLoader)15 Instrumentation (java.lang.instrument.Instrumentation)10 StubMethod (net.bytebuddy.implementation.StubMethod)8 TextConstant (net.bytebuddy.implementation.bytecode.constant.TextConstant)7 ClassFileLocator (net.bytebuddy.dynamic.ClassFileLocator)6 File (java.io.File)5 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)5 FieldDescription (net.bytebuddy.description.field.FieldDescription)5 ByteArrayClassLoader (net.bytebuddy.dynamic.loading.ByteArrayClassLoader)5 RandomString (net.bytebuddy.utility.RandomString)5 Constructor (java.lang.reflect.Constructor)4 Callable (java.util.concurrent.Callable)4