Java Code Examples for net.bytebuddy.dynamic.DynamicType#Loaded

The following examples show how to use net.bytebuddy.dynamic.DynamicType#Loaded . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MethodCallTypeTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testFieldReference() throws Exception {
    DynamicType.Loaded<Foo> loaded = new ByteBuddy()
            .subclass(Foo.class)
            .method(isDeclaredBy(Foo.class))
            .intercept(MethodCall.invokeSuper().withReference(value))
            .make()
            .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredMethod(FOO, Object.class), not(nullValue(Method.class)));
    assertThat(loaded.getLoaded().getDeclaredConstructors().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(definesFieldReference ? 1 : 0));
    Foo instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(Foo.class)));
    assertThat(instance, instanceOf(Foo.class));
    assertThat(instance.foo(new Object()), sameInstance(value));
}
 
Example 2
Source File: FieldAccessorOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testExplicitNameSetter() throws Exception {
    DynamicType.Loaded<SampleSetter> loaded = new ByteBuddy()
            .subclass(SampleSetter.class)
            .method(isDeclaredBy(SampleSetter.class))
            .intercept(FieldAccessor.ofField(FOO))
            .make()
            .load(SampleSetter.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    SampleSetter sampleSetter = loaded.getLoaded().getDeclaredConstructor().newInstance();
    Field field = SampleSetter.class.getDeclaredField(FOO);
    field.setAccessible(true);
    assertThat(field.get(sampleSetter), is((Object) QUX));
    sampleSetter.bar(BAZ);
    assertThat(field.get(sampleSetter), is((Object) BAZ));
    sampleSetter.assertZeroCalls();
}
 
Example 3
Source File: MethodDelegationOriginTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testOriginConstructorWithPrivilege() throws Exception {
    OriginConstructorWithPrivilege originConstructor = new OriginConstructorWithPrivilege();
    DynamicType.Loaded<Foo> loaded = new ByteBuddy()
            .subclass(Foo.class)
            .constructor(ElementMatchers.any())
            .intercept(SuperMethodCall.INSTANCE.andThen(MethodDelegation.to(originConstructor)))
            .make()
            .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(1));
    loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(originConstructor.constructor, instanceOf(Constructor.class));
    assertThat(originConstructor.constructor, is((Constructor) loaded.getLoaded().getDeclaredConstructor()));
    Constructor<?> previous = originConstructor.constructor;
    loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(originConstructor.constructor, instanceOf(Constructor.class));
    assertThat(originConstructor.constructor, sameInstance((Constructor) previous));
}
 
Example 4
Source File: MethodDelegationOriginTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testOriginConstructorWithCache() throws Exception {
    OriginConstructorWithCache originConstructor = new OriginConstructorWithCache();
    DynamicType.Loaded<Foo> loaded = new ByteBuddy()
            .subclass(Foo.class)
            .constructor(ElementMatchers.any())
            .intercept(SuperMethodCall.INSTANCE.andThen(MethodDelegation.to(originConstructor)))
            .make()
            .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(originConstructor.constructor, instanceOf(Constructor.class));
    assertThat(originConstructor.constructor, is((Constructor) loaded.getLoaded().getDeclaredConstructor()));
    Constructor<?> previous = originConstructor.constructor;
    loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(originConstructor.constructor, instanceOf(Constructor.class));
    assertThat(originConstructor.constructor, sameInstance((Constructor) previous));
}
 
Example 5
Source File: ExceptionMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithMessage() throws Exception {
    DynamicType.Loaded<Foo> loaded = new ByteBuddy()
            .subclass(Foo.class)
            .method(isDeclaredBy(Foo.class))
            .intercept(ExceptionMethod.throwing(RuntimeException.class, BAR))
            .make()
            .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(0));
    Foo instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(Foo.class)));
    assertThat(instance, instanceOf(Foo.class));
    try {
        instance.foo();
        fail();
    } catch (RuntimeException exception) {
        assertThat(exception.getClass(), CoreMatchers.<Class<?>>is(RuntimeException.class));
        assertThat(exception.getMessage(), is(BAR));
    }
    instance.assertZeroCalls();
}
 
Example 6
Source File: EqualsMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testTypeOrderForPrimitiveTypedFields() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy()
            .subclass(Object.class)
            .defineField(FOO, Object.class, Visibility.PUBLIC)
            .defineField(BAR, int.class, Visibility.PUBLIC)
            .method(isEquals())
            .intercept(EqualsMethod.isolated().withNonNullableFields(any()).withPrimitiveTypedFieldsFirst())
            .make()
            .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(2));
    Object left = loaded.getLoaded().getDeclaredConstructor().newInstance(), right = loaded.getLoaded().getDeclaredConstructor().newInstance();
    left.getClass().getDeclaredField(BAR).setInt(left, 42);
    right.getClass().getDeclaredField(BAR).setInt(right, 84);
    assertThat(left, not(right));
}
 
Example 7
Source File: EqualsMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testEqualToSelfIdentity() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy()
            .subclass(Object.class)
            .defineField(FOO, Object.class, Visibility.PUBLIC)
            .method(isEquals())
            .intercept(EqualsMethod.isolated())
            .make()
            .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(1));
    Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    loaded.getLoaded().getDeclaredField(FOO).set(instance, new NonEqualsBase());
    assertThat(instance, is(instance));
}
 
Example 8
Source File: MethodCallTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testObjectConstruction() throws Exception {
    DynamicType.Loaded<SelfReference> loaded = new ByteBuddy()
            .subclass(SelfReference.class)
            .method(isDeclaredBy(SelfReference.class))
            .intercept(MethodCall.construct(SelfReference.class.getDeclaredConstructor()))
            .make()
            .load(SelfReference.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredConstructors().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(0));
    SelfReference instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    SelfReference created = instance.foo();
    assertThat(created.getClass(), CoreMatchers.<Class<?>>is(SelfReference.class));
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(SelfReference.class)));
    assertThat(instance, instanceOf(SelfReference.class));
    assertThat(created, not(instance));
}
 
Example 9
Source File: MethodCallTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithThis() throws Exception {
    DynamicType.Loaded<MethodCallWithThis> loaded = new ByteBuddy()
            .subclass(MethodCallWithThis.class)
            .method(isDeclaredBy(MethodCallWithThis.class))
            .intercept(MethodCall.invokeSuper().withThis())
            .make()
            .load(MethodCallWithThis.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredMethod(FOO, MethodCallWithThis.class), not(nullValue(Method.class)));
    assertThat(loaded.getLoaded().getDeclaredConstructors().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(0));
    MethodCallWithThis instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(MethodCallWithThis.class)));
    assertThat(instance, instanceOf(MethodCallWithThis.class));
    assertThat(instance.foo(null), is(instance));
}
 
Example 10
Source File: FieldAccessorTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testStaticDefault() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy()
            .subclass(staticSwap)
            .method(isDeclaredBy(staticSwap))
            .intercept(FieldAccessor.ofField(FOO).setsDefaultValue())
            .make()
            .load(staticSwap.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoaded().getMethod(FOO).invoke(loaded.getLoaded().getConstructor().newInstance()), nullValue(Object.class));
    assertThat(loaded.getLoaded().getField(FOO).get(null), not(value));
}
 
Example 11
Source File: InvokeDynamicTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = LinkageError.class)
@JavaVersionRule.Enforce(8)
public void testLambdaMetaFactoryIllegalBinding() throws Exception {
    DynamicType.Loaded<FunctionalFactory> dynamicType = new ByteBuddy()
            .subclass(FunctionalFactory.class)
            .method(isDeclaredBy(FunctionalFactory.class))
            .intercept(InvokeDynamic.lambda(InvokeDynamicTest.class.getMethod("value"), Callable.class).withValue(FOO))
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(0));
    assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().make().call(), is(FOO));
}
 
Example 12
Source File: MethodDelegationFieldValueTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtendedFieldSkipsNonVisible() throws Exception {
    DynamicType.Loaded<ExtendedPrivateField> loaded = new ByteBuddy()
            .subclass(ExtendedPrivateField.class)
            .method(isDeclaredBy(ExtendedPrivateField.class))
            .intercept(MethodDelegation.to(SimpleInterceptor.class))
            .make()
            .load(ExtendedPrivateField.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    SimpleField instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    instance.foo = FOO;
    assertThat(instance.foo(), is((Object) FOO));
    instance.foo = BAR;
    assertThat(instance.foo(), is((Object) BAR));
}
 
Example 13
Source File: MethodDelegationMorphTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testMorphSerializable() throws Exception {
    DynamicType.Loaded<Foo> loaded = new ByteBuddy()
            .subclass(Foo.class)
            .method(isDeclaredBy(Foo.class))
            .intercept(MethodDelegation.withDefaultConfiguration()
                    .withBinders(Morph.Binder.install(Morphing.class))
                    .to(SimpleMorphSerializable.class))
            .make()
            .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Foo instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.foo(FOO), is(QUX + BAR));
}
 
Example 14
Source File: MethodDelegationMorphTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = ClassCastException.class)
public void testMorphToIncompatibleTypeThrowsException() throws Exception {
    DynamicType.Loaded<Foo> loaded = new ByteBuddy()
            .subclass(Foo.class)
            .method(isDeclaredBy(Foo.class))
            .intercept(MethodDelegation.withDefaultConfiguration()
                    .withBinders(Morph.Binder.install(Morphing.class))
                    .to(new SimpleMorph(new Object())))
            .make()
            .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Foo instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    instance.foo(QUX);
}
 
Example 15
Source File: MethodDelegationDefaultMethodTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@JavaVersionRule.Enforce(8)
public void testCallableDefaultCallNoCache() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy()
            .subclass(Object.class)
            .implement(Class.forName(SINGLE_DEFAULT_METHOD))
            .intercept(MethodDelegation.to(SampleClassNoCache.class))
            .make()
            .load(Class.forName(SINGLE_DEFAULT_METHOD).getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(0));
    Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    Method method = loaded.getLoaded().getMethod(FOO);
    assertThat(method.invoke(instance), is((Object) FOO));
}
 
Example 16
Source File: MethodDelegationDefaultCallTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@JavaVersionRule.Enforce(8)
public void testRunnableDefaultCall() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy()
            .subclass(Object.class)
            .implement(Class.forName(SINGLE_DEFAULT_METHOD))
            .intercept(MethodDelegation.to(RunnableClass.class))
            .make()
            .load(Class.forName(SINGLE_DEFAULT_METHOD).getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    Method method = loaded.getLoaded().getMethod(FOO);
    assertThat(method.invoke(instance), is((Object) QUX));
}
 
Example 17
Source File: MethodDelegationSuperMethodTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testRunnableSuperCall() throws Exception {
    DynamicType.Loaded<Foo> loaded = new ByteBuddy()
            .subclass(Foo.class)
            .method(isDeclaredBy(Foo.class))
            .intercept(MethodDelegation.to(SampleClass.class))
            .make()
            .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(1));
    Foo instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.value, is(BAR));
    instance.foo();
    assertThat(instance.value, is(FOO));
}
 
Example 18
Source File: MethodDelegationMorphTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@JavaVersionRule.Enforce(8)
public void testDefaultMethodExplicit() throws Exception {
    DynamicType.Loaded<Object> loaded = new ByteBuddy()
            .subclass(Object.class)
            .implement(Class.forName(DEFAULT_INTERFACE))
            .intercept(MethodDelegation.withDefaultConfiguration()
                    .withBinders(Morph.Binder.install(Morphing.class))
                    .to(Class.forName(DEFAULT_INTERFACE_TARGET_EXPLICIT)))
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.getClass().getDeclaredMethod(FOO, String.class)
            .invoke(instance, QUX), is((Object) (FOO + BAR)));
}
 
Example 19
Source File: MethodDelegationDefaultMethodTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@JavaVersionRule.Enforce(8)
public void testCallableDefaultCallPrivileged() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy()
            .subclass(Object.class)
            .implement(Class.forName(SINGLE_DEFAULT_METHOD))
            .intercept(MethodDelegation.to(SampleClassPrivileged.class))
            .make()
            .load(Class.forName(SINGLE_DEFAULT_METHOD).getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getAuxiliaryTypes().size(), is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(1));
    Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    Method method = loaded.getLoaded().getMethod(FOO);
    assertThat(method.invoke(instance), is((Object) FOO));
}
 
Example 20
Source File: FieldAccessorTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testStaticReference() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy()
            .subclass(staticSwap)
            .method(isDeclaredBy(staticSwap))
            .intercept(FieldAccessor.ofField(BAR).setsReference(value))
            .make()
            .load(staticSwap.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoaded().getMethod(FOO).invoke(loaded.getLoaded().getConstructor().newInstance()), nullValue(Object.class));
    assertThat(loaded.getLoaded().getField(BAR).get(null), is(value));
}