javax.persistence.Embedded#net.bytebuddy.dynamic.DynamicType源码实例Demo

下面列出了javax.persistence.Embedded#net.bytebuddy.dynamic.DynamicType 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: byte-buddy   文件: MethodCallTest.java
@Test
public void testImplementationAppendingMethod() throws Exception {
    DynamicType.Loaded<MethodCallAppending> loaded = new ByteBuddy()
            .subclass(MethodCallAppending.class)
            .method(isDeclaredBy(MethodCallAppending.class))
            .intercept(MethodCall.invokeSuper().andThen(new Implementation.Simple(new TextConstant(FOO), MethodReturn.REFERENCE)))
            .make()
            .load(MethodCallAppending.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredMethod(FOO), not(nullValue(Method.class)));
    assertThat(loaded.getLoaded().getDeclaredConstructors().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(0));
    MethodCallAppending instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(MethodCallAppending.class)));
    assertThat(instance, instanceOf(MethodCallAppending.class));
    assertThat(instance.foo(), is((Object) FOO));
    instance.assertOnlyCall(FOO);
}
 
源代码2 项目: byte-buddy   文件: MethodDelegationMorphTest.java
@Test
public void testMorphVoid() throws Exception {
    SimpleMorph simpleMorph = new SimpleMorph(QUX);
    DynamicType.Loaded<Bar> loaded = new ByteBuddy()
            .subclass(Bar.class)
            .method(isDeclaredBy(Bar.class))
            .intercept(MethodDelegation.withDefaultConfiguration()
                    .withBinders(Morph.Binder.install(Morphing.class))
                    .to(simpleMorph))
            .make()
            .load(Bar.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Bar instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    instance.foo();
    instance.assertOnlyCall(FOO);
    simpleMorph.assertOnlyCall(BAR);
}
 
源代码3 项目: byte-buddy   文件: MethodCallTest.java
@Test
public void testStaticMethodInvocationWithoutArguments() throws Exception {
    DynamicType.Loaded<SimpleMethod> loaded = new ByteBuddy()
            .subclass(SimpleMethod.class)
            .method(named(FOO))
            .intercept(MethodCall.invoke(SimpleMethod.class.getDeclaredMethod(BAR)))
            .make()
            .load(SimpleMethod.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));
    SimpleMethod instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.foo(), is(BAR));
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(SimpleMethod.class)));
    assertThat(instance, instanceOf(SimpleMethod.class));
}
 
源代码4 项目: byte-buddy   文件: EqualsMethodOtherTest.java
@Test
public void testSuperMethod() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy()
            .subclass(EqualsBase.class)
            .defineField(FOO, Object.class, Visibility.PUBLIC)
            .method(isEquals())
            .intercept(EqualsMethod.requiringSuperClassEquality())
            .make()
            .load(EqualsBase.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(1));
    Object left = loaded.getLoaded().getDeclaredConstructor().newInstance(), right = loaded.getLoaded().getDeclaredConstructor().newInstance();
    left.getClass().getDeclaredField(FOO).set(left, FOO);
    right.getClass().getDeclaredField(FOO).set(right, FOO);
    assertThat(left, is(right));
}
 
源代码5 项目: byte-buddy   文件: AgentBuilderListenerTest.java
@Test
public void testReadEdgeAddingListenerDuplexCanRead() throws Exception {
    Instrumentation instrumentation = mock(Instrumentation.class);
    JavaModule source = mock(JavaModule.class), target = mock(JavaModule.class);
    TypeDescription typeDescription = mock(TypeDescription.class);
    PackageDescription packageDescription = mock(PackageDescription.class);
    when(typeDescription.getPackage()).thenReturn(packageDescription);
    when(source.isNamed()).thenReturn(true);
    when(source.canRead(target)).thenReturn(true);
    when(source.isOpened(packageDescription, target)).thenReturn(true);
    when(target.canRead(source)).thenReturn(true);
    AgentBuilder.Listener listener = new AgentBuilder.Listener.ModuleReadEdgeCompleting(instrumentation, true, Collections.singleton(target));
    listener.onTransformation(typeDescription, mock(ClassLoader.class), source, LOADED, mock(DynamicType.class));
    verify(source).isNamed();
    verify(source).canRead(target);
    verify(source).isOpened(packageDescription, target);
    verifyNoMoreInteractions(source);
    verify(target).canRead(source);
    verifyNoMoreInteractions(target);
}
 
源代码6 项目: byte-buddy   文件: MethodCallTest.java
@Test
public void testInstanceMethodInvocationWithoutArguments() throws Exception {
    DynamicType.Loaded<InstanceMethod> loaded = new ByteBuddy()
            .subclass(InstanceMethod.class)
            .method(named(FOO))
            .intercept(MethodCall.invoke(InstanceMethod.class.getDeclaredMethod(BAR)))
            .make()
            .load(SimpleMethod.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));
    InstanceMethod instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.foo(), is(BAR));
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(InstanceMethod.class)));
    assertThat(instance, instanceOf(InstanceMethod.class));
}
 
源代码7 项目: byte-buddy   文件: MethodCallTest.java
@Test
public void testWithExplicitArgumentStackManipulation() throws Exception {
    DynamicType.Loaded<MethodCallWithExplicitArgument> loaded = new ByteBuddy()
            .subclass(MethodCallWithExplicitArgument.class)
            .method(isDeclaredBy(MethodCallWithExplicitArgument.class))
            .intercept(MethodCall.invokeSuper().with(new TextConstant(FOO), String.class))
            .make()
            .load(MethodCallWithExplicitArgument.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredMethod(FOO, String.class), not(nullValue(Method.class)));
    assertThat(loaded.getLoaded().getDeclaredConstructors().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(0));
    MethodCallWithExplicitArgument instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(MethodCallWithExplicitArgument.class)));
    assertThat(instance, instanceOf(MethodCallWithExplicitArgument.class));
    assertThat(instance.foo(BAR), is(FOO));
}
 
源代码8 项目: byte-buddy   文件: MethodCallTest.java
@Test
public void testMethodInvocationUsingStackManipulation() throws Exception {
    DynamicType.Loaded<SimpleMethod> loaded = new ByteBuddy()
        .subclass(SimpleMethod.class)
        .method(named(FOO))
        .intercept(MethodCall.invoke(String.class.getMethod("toUpperCase")).on(new TextConstant(FOO), String.class))
        .make()
        .load(SimpleMethod.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));
    SimpleMethod instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.foo(), is(FOO.toUpperCase()));
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(SimpleMethod.class)));
    assertThat(instance, instanceOf(SimpleMethod.class));
}
 
源代码9 项目: kanela   文件: ClassDumperListener.java
private void addClassToDump(DynamicType dynamicType) {
    if(!dumpDir.exists()){
        runSafe(dumpDir::mkdirs, "Error creating directory...");
    }

    if(config.getCreateJar()) {
        if(!jarFile.exists()) {
            runSafe(jarFile::createNewFile, "Error creating a new file...");
            runSafe(() -> dynamicType.toJar(jarFile), "Error trying to add transformed class to a new jar...");
        } else {
            runSafe( () -> dynamicType.inject(jarFile), "Error trying to add transformed class to existing jar...");
        }
    } else {
        runSafe(() -> dynamicType.saveIn(dumpDir), "Error trying to save transformed class into directory...");
    }
}
 
源代码10 项目: byte-buddy   文件: MethodCallTest.java
@Test
public void testSelfInvocation() throws Exception {
    SuperMethodInvocation delegate = mock(SuperMethodInvocation.class);
    when(delegate.foo()).thenReturn(FOO);
    DynamicType.Loaded<SuperMethodInvocation> loaded = new ByteBuddy()
            .subclass(SuperMethodInvocation.class)
            .method(takesArguments(0).and(named(FOO)))
            .intercept(MethodCall.invokeSelf().on(delegate))
            .make()
            .load(SuperMethodInvocation.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredMethod(FOO), not(nullValue(Method.class)));
    assertThat(loaded.getLoaded().getDeclaredConstructors().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(1));
    SuperMethodInvocation instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(SuperMethodInvocation.class)));
    assertThat(instance, instanceOf(SuperMethodInvocation.class));
    assertThat(instance.foo(), is(FOO));
    verify(delegate).foo();
    verifyNoMoreInteractions(delegate);
}
 
源代码11 项目: byte-buddy   文件: MethodDelegationOriginTest.java
@Test
@SuppressWarnings("unchecked")
@JavaVersionRule.Enforce(8)
public void testOriginExecutableConstructorWithoutCache() throws Exception {
    Object originConstructor = Class.forName(ORIGIN_EXECUTABLE).getDeclaredConstructor().newInstance();
    Field constructor = Class.forName(ORIGIN_EXECUTABLE).getDeclaredField("executable");
    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(constructor.get(originConstructor), instanceOf(Constructor.class));
    assertThat(constructor.get(originConstructor), is((Object) loaded.getLoaded().getDeclaredConstructor()));
    Object previous = constructor.get(originConstructor);
    loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(constructor.get(originConstructor), instanceOf(Constructor.class));
    assertThat(constructor.get(originConstructor), is((Object) loaded.getLoaded().getDeclaredConstructor()));
    assertThat(constructor.get(originConstructor), not(sameInstance(previous)));
}
 
源代码12 项目: byte-buddy   文件: InvocationHandlerAdapterTest.java
@Test
public void testStaticAdapterWithMethodCache() throws Exception {
    Foo foo = new Foo();
    DynamicType.Loaded<Bar> loaded = new ByteBuddy()
            .subclass(Bar.class)
            .method(isDeclaredBy(Bar.class))
            .intercept(InvocationHandlerAdapter.of(foo))
            .make()
            .load(Bar.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(2));
    Bar instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.bar(FOO), is((Object) instance));
    assertThat(foo.methods.size(), is(1));
    assertThat(instance.bar(FOO), is((Object) instance));
    assertThat(foo.methods.size(), is(2));
    assertThat(foo.methods.get(0), sameInstance(foo.methods.get(1)));
    instance.assertZeroCalls();
}
 
源代码13 项目: byte-buddy   文件: ToStringMethodTest.java
@Test
public void testEqualsTrue() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy()
            .subclass(Object.class)
            .defineField(FOO, type, Visibility.PUBLIC)
            .method(isToString())
            .intercept(ToStringMethod.prefixedBy(FOO))
            .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();
    instance.getClass().getDeclaredField(FOO).set(instance, value);
    assertThat(instance.toString(), is(FOO + "{" + FOO + "=" + string + "}"));
}
 
源代码14 项目: byte-buddy   文件: MethodCallTest.java
@Test
public void testInvokeOnArgumentUsingMatcher() throws Exception {
    DynamicType.Loaded<ArgumentCall> loaded = new ByteBuddy()
            .subclass(ArgumentCall.class)
            .method(named("foo"))
            .intercept(MethodCall.invoke(named("toUpperCase").and(takesArguments(0))).onMethodCall(MethodCall.invoke(named("foo")).onArgument(0)))
            .make()
            .load(ArgumentCall.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));
    ArgumentCall instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.foo(new ArgumentCall.Target("foo")), is("FOO"));
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(InstanceMethod.class)));
    assertThat(instance, instanceOf(ArgumentCall.class));
}
 
源代码15 项目: windup   文件: SetInPropertiesHandler.java
/**
 * The handling method.
 */
@Override
public <E> DynamicType.Builder<E> processMethod(final DynamicType.Builder<E> builder, final Method method, final Annotation annotation)
{
    String methodName = method.getName();
    if (ReflectionUtility.isGetMethod(method))
        return createInterceptor(builder, method);
    else if (ReflectionUtility.isSetMethod(method))
        return createInterceptor(builder, method);
    else if (methodName.startsWith("addAll"))
        return createInterceptor(builder, method);
    else if (methodName.startsWith("add"))
        return createInterceptor(builder, method);
    else
        throw new WindupException("Only get*, set*, add*, and addAll* method names are supported for @"
                    + SetInProperties.class.getSimpleName() + ", found at: " + method.getName());
}
 
源代码16 项目: byte-buddy   文件: MethodCallTest.java
@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));
}
 
源代码17 项目: byte-buddy   文件: ExceptionMethodTest.java
@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();
}
 
源代码18 项目: byte-buddy   文件: InvokeDynamicTest.java
@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapWithFieldExplicitType() throws Exception {
    TypeDescription typeDescription = TypeDescription.ForLoadedType.of(Class.forName(BOOTSTRAP_CLASS));
    DynamicType.Loaded<Simple> dynamicType = new ByteBuddy()
            .subclass(Simple.class)
            .defineField(FOO, Object.class)
            .method(isDeclaredBy(Simple.class))
            .intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named("bootstrapSimple")).getOnly())
                    .invoke(QUX, String.class)
                    .withField(FOO).as(String.class)
                    .withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC))
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(1));
    Simple instance = dynamicType.getLoaded().getDeclaredConstructor().newInstance();
    Field field = dynamicType.getLoaded().getDeclaredField(FOO);
    field.setAccessible(true);
    field.set(instance, FOO);
    assertThat(instance.foo(), is(FOO));
}
 
源代码19 项目: byte-buddy   文件: EqualsMethodOtherTest.java
@Test
public void testTypeOrderForEnumerationTypedFields() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy()
            .subclass(Object.class)
            .defineField(FOO, Object.class, Visibility.PUBLIC)
            .defineField(BAR, RetentionPolicy.class, Visibility.PUBLIC)
            .method(isEquals())
            .intercept(EqualsMethod.isolated().withNonNullableFields(any()).withEnumerationTypedFieldsFirst())
            .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).set(left, RetentionPolicy.RUNTIME);
    right.getClass().getDeclaredField(BAR).set(right, RetentionPolicy.CLASS);
    assertThat(left, not(right));
}
 
源代码20 项目: byte-buddy   文件: MethodDelegationOriginTest.java
@Test
@SuppressWarnings("unchecked")
public void testOriginConstructorWithoutCache() throws Exception {
    OriginConstructor originConstructor = new OriginConstructor();
    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, is((Constructor) loaded.getLoaded().getDeclaredConstructor()));
    assertThat(originConstructor.constructor, not(sameInstance((Constructor) previous)));
}
 
源代码21 项目: byte-buddy   文件: TypeWriterDefaultTest.java
@Test
@JavaVersionRule.Enforce(8)
public void testTypeInitializerOnRebasedModernInterface() throws Exception {
    assertThat(new ByteBuddy()
            .rebase(Class.forName(JAVA_8_INTERFACE))
            .invokable(isTypeInitializer())
            .intercept(StubMethod.INSTANCE)
            .make(), notNullValue(DynamicType.class));
}
 
源代码22 项目: byte-buddy   文件: MethodDelegationSuperTest.java
@Test
public void testSuperInstance() throws Exception {
    DynamicType.Loaded<Foo> loaded = new ByteBuddy()
            .subclass(Foo.class)
            .method(isDeclaredBy(Foo.class))
            .intercept(MethodDelegation.to(Baz.class))
            .make()
            .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Foo instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.qux(), is((Object) (FOO + QUX)));
}
 
@Test
public void testExplicitFieldAccessExplicitTarget() throws Exception {
    DynamicType.Loaded<ExplicitInherited> loaded = new ByteBuddy()
            .subclass(ExplicitInherited.class)
            .method(isDeclaredBy(ExplicitInherited.class))
            .intercept(MethodDelegation.withDefaultConfiguration().withBinders(FieldProxy.Binder.install(Get.class, Set.class)).to(SwapInherited.class))
            .make()
            .load(ExplicitInherited.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    ExplicitInherited explicitInherited = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(((Explicit) explicitInherited).foo, is(FOO));
    assertThat(explicitInherited.foo, is(QUX));
    explicitInherited.swap();
    assertThat(((Explicit) explicitInherited).foo, is(FOO + BAR));
    assertThat(explicitInherited.foo, is(QUX));
}
 
@Test
public void testRunnableSuperCallNoCache() throws Exception {
    DynamicType.Loaded<Foo> loaded = new ByteBuddy()
            .subclass(Foo.class)
            .method(isDeclaredBy(Foo.class))
            .intercept(MethodDelegation.to(SampleClassNoCache.class))
            .make()
            .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(0));
    Foo instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.value, is(BAR));
    instance.foo();
    assertThat(instance.value, is(FOO));
}
 
@Test(expected = ClassCastException.class)
public void testIncompatibleGetterTypeThrowsException() throws Exception {
    DynamicType.Loaded<Explicit> loaded = new ByteBuddy()
            .subclass(Explicit.class)
            .method(isDeclaredBy(Explicit.class))
            .intercept(MethodDelegation.withDefaultConfiguration().withBinders(FieldProxy.Binder.install(Get.class, Set.class)).to(GetterIncompatible.class))
            .make()
            .load(Explicit.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Explicit explicit = loaded.getLoaded().getDeclaredConstructor().newInstance();
    explicit.swap();
}
 
源代码26 项目: byte-buddy   文件: ByteBuddyTutorialExamplesTest.java
@Test
public void testTutorialGettingStartedNamingStrategy() throws Exception {
    DynamicType.Unloaded<?> dynamicType = new ByteBuddy()
            .with(new GettingStartedNamingStrategy())
            .subclass(Object.class)
            .make();
    assertThat(dynamicType, notNullValue());
    Class<?> type = dynamicType.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getName(), is("i.love.ByteBuddy.Object"));
}
 
源代码27 项目: byte-buddy   文件: FieldAccessorOtherTest.java
@Test
public void testClassConstant() throws Exception {
    DynamicType.Loaded<SampleNoArgumentSetter> loaded = new ByteBuddy()
            .subclass(SampleNoArgumentSetter.class)
            .method(named(FOO))
            .intercept(FieldAccessor.ofField(FOO).setsValue(TypeDescription.OBJECT))
            .make()
            .load(SampleNoArgumentSetter.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(0));
    SampleNoArgumentSetter instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    instance.foo();
    assertThat(instance.foo, is((Object) Object.class));
}
 
@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));
}
 
@Test
@JavaVersionRule.Enforce(8)
public void testDefaultMethodFallback() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy()
            .subclass(Object.class)
            .implement(Class.forName(SINGLE_DEFAULT_METHOD))
            .intercept(MethodDelegation.to(NonVoidTarget.class))
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    Method method = loaded.getLoaded().getMethod(FOO);
    assertThat(method.invoke(instance), is((Object) FOO));
}
 
/**
 * Creates a redefinition dynamic type builder.
 *
 * @param instrumentedType             An instrumented type representing the subclass.
 * @param classFileVersion             The class file version to use for types that are not based on an existing class file.
 * @param auxiliaryTypeNamingStrategy  The naming strategy to use for naming auxiliary types.
 * @param annotationValueFilterFactory The annotation value filter factory to use.
 * @param annotationRetention          The annotation retention strategy to use.
 * @param implementationContextFactory The implementation context factory to use.
 * @param methodGraphCompiler          The method graph compiler to use.
 * @param typeValidation               Determines if a type should be explicitly validated.
 * @param visibilityBridgeStrategy     The visibility bridge strategy to apply.
 * @param classWriterStrategy          The class writer strategy to use.
 * @param ignoredMethods               A matcher for identifying methods that should be excluded from instrumentation.
 * @param originalType                 The original type that is being redefined or rebased.
 * @param classFileLocator             The class file locator for locating the original type's class file.
 */
public RedefinitionDynamicTypeBuilder(InstrumentedType.WithFlexibleName instrumentedType,
                                      ClassFileVersion classFileVersion,
                                      AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
                                      AnnotationValueFilter.Factory annotationValueFilterFactory,
                                      AnnotationRetention annotationRetention,
                                      Implementation.Context.Factory implementationContextFactory,
                                      MethodGraph.Compiler methodGraphCompiler,
                                      TypeValidation typeValidation,
                                      VisibilityBridgeStrategy visibilityBridgeStrategy,
                                      ClassWriterStrategy classWriterStrategy,
                                      LatentMatcher<? super MethodDescription> ignoredMethods,
                                      TypeDescription originalType,
                                      ClassFileLocator classFileLocator) {
    this(instrumentedType,
            new FieldRegistry.Default(),
            new MethodRegistry.Default(),
            new RecordComponentRegistry.Default(),
            annotationRetention.isEnabled()
                    ? new TypeAttributeAppender.ForInstrumentedType.Differentiating(originalType)
                    : TypeAttributeAppender.ForInstrumentedType.INSTANCE,
            AsmVisitorWrapper.NoOp.INSTANCE,
            classFileVersion,
            auxiliaryTypeNamingStrategy,
            annotationValueFilterFactory,
            annotationRetention,
            implementationContextFactory,
            methodGraphCompiler,
            typeValidation,
            visibilityBridgeStrategy,
            classWriterStrategy,
            ignoredMethods,
            Collections.<DynamicType>emptyList(),
            originalType,
            classFileLocator);
}