net.bytebuddy.ByteBuddy Java Examples
The following examples show how to use
net.bytebuddy.ByteBuddy.
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: AdviceInconsistentFrameTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(7) public void testFrameInconsistentParameterTrivialCopying() throws Exception { Class<?> type = new ByteBuddy() .subclass(Object.class) .defineMethod(FOO, String.class, Visibility.PUBLIC, Ownership.STATIC) .withParameters(Void.class) .intercept(new InconsistentParameterReferenceMethod()) .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT) .getLoaded(); assertThat(type.getDeclaredMethod(FOO, Void.class).invoke(null, (Object) null), is((Object) BAR)); new ByteBuddy() .redefine(type) .visit(Advice.to(copying).on(named(FOO))) .make(); }
Example #2
Source File: MethodCallTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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 #3
Source File: UnrolledUnsafeCopierBuilder.java From unsafe with BSD 2-Clause "Simplified" License | 6 votes |
/** * Constructs a new Copier using the passed in Unsafe instance * * @param unsafe The sun.misc.Unsafe instance this copier uses * @return The new UnsageCopier built with the specific parameters * @throws IllegalAccessException * @throws InstantiationException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalArgumentException if any argument is invalid */ public UnsafeCopier build(Unsafe unsafe) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { checkArgument(offset >= 0, "Offset must be set"); checkArgument(length >= 0, "Length must be set"); checkNotNull(unsafe); Class<?> dynamicType = new ByteBuddy() .subclass(UnsafeCopier.class) .method(named("copy")) .intercept(new CopierImplementation(offset, length)).make() .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); return (UnsafeCopier) dynamicType.getDeclaredConstructor(Unsafe.class).newInstance(unsafe); }
Example #4
Source File: MethodCallTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(value = 7, atMost = 7, j9 = false) public void testJava7TypesExplicit() throws Exception { DynamicType.Loaded<SimpleMethod> loaded = new ByteBuddy() .subclass(SimpleMethod.class) .method(named(FOO)) .intercept(MethodCall.invoke(Foo.class.getDeclaredMethod(BAR, Object.class, Object.class)) .with(JavaConstant.MethodHandle.ofLoaded(makeMethodHandle()), JavaConstant.MethodType.ofLoaded(makeMethodType(void.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("" + makeMethodHandle() + makeMethodType(void.class))); assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(SimpleMethod.class))); assertThat(instance, instanceOf(SimpleMethod.class)); }
Example #5
Source File: MethodCallTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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); }
Example #6
Source File: MemberSubstitutionTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testStaticFieldReadWithMethodSubstitution() throws Exception { Class<?> type = new ByteBuddy() .redefine(StaticFieldAccessSample.class) .visit(MemberSubstitution.strict().field(named(FOO)).replaceWith(StaticFieldAccessSample.class.getDeclaredMethod(BAZ)).on(named(RUN))) .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); Object instance = type.getDeclaredConstructor().newInstance(); assertThat(type.getDeclaredField(FOO).get(null), is((Object) FOO)); assertThat(type.getDeclaredField(BAR).get(null), is((Object) BAR)); assertThat(type.getDeclaredField(BAZ).get(null), is((Object) BAZ)); assertThat(type.getDeclaredMethod(RUN).invoke(instance), nullValue(Object.class)); assertThat(type.getDeclaredField(FOO).get(null), is((Object) FOO)); assertThat(type.getDeclaredField(BAR).get(null), is((Object) BAZ)); assertThat(type.getDeclaredField(BAZ).get(null), is((Object) BAZ)); }
Example #7
Source File: FixedValueConstantPoolTypesTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testConstantPool() throws Exception { DynamicType.Loaded<T> loaded = new ByteBuddy() .subclass(helperClass) .method(isDeclaredBy(helperClass)) .intercept(FixedValue.value(fixedValue)) .make() .load(helperClass.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER); assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0)); assertThat(loaded.getLoaded().getDeclaredMethods().length, is(2)); assertThat(loaded.getLoaded().getDeclaredFields().length, is(0)); T instance = loaded.getLoaded().getDeclaredConstructor().newInstance(); assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(StringTarget.class))); assertThat(instance, instanceOf(helperClass)); assertThat(loaded.getLoaded().getDeclaredMethod(FOO).invoke(instance), is(fixedValue)); assertThat(loaded.getLoaded().getDeclaredMethod(BAR).invoke(instance), is(fixedValue)); instance.assertZeroCalls(); }
Example #8
Source File: AdviceInconsistentFrameTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(7) public void testFrameDropImplicitTrivialCopying() throws Exception { Class<?> type = new ByteBuddy() .subclass(Object.class) .defineMethod(FOO, String.class, Visibility.PUBLIC) .intercept(new DropImplicitMethod()) .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT) .getLoaded(); assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR)); new ByteBuddy() .redefine(type) .visit(Advice.to(copying).on(named(FOO))) .make(); }
Example #9
Source File: ClassInjectorUsingLookupTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Before public void setUp() throws Exception { type = new ByteBuddy() .subclass(Object.class) .name("net.bytebuddy.test.Foo") .defineMethod("lookup", Object.class, Ownership.STATIC, Visibility.PUBLIC) .intercept(MethodCall.invoke(Class.forName("java.lang.invoke.MethodHandles").getMethod("lookup"))) .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); }
Example #10
Source File: SubclassDynamicTypeBuilderTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(8) @SuppressWarnings("unchecked") public void testAnnotationTypeOnSuperClass() throws Exception { Class<? extends Annotation> typeAnnotationType = (Class<? extends Annotation>) Class.forName(TYPE_VARIABLE_NAME); MethodDescription.InDefinedShape value = TypeDescription.ForLoadedType.of(typeAnnotationType).getDeclaredMethods().filter(named(VALUE)).getOnly(); Class<?> type = new ByteBuddy() .subclass(TypeDescription.Generic.Builder.rawType(Object.class) .build(AnnotationDescription.Builder.ofType(typeAnnotationType).define(VALUE, BAZ).build())) .make() .load(typeAnnotationType.getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST) .getLoaded(); assertThat(type.getSuperclass(), is((Object) Object.class)); assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveSuperClassType(type).asList().size(), is(1)); assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveSuperClassType(type).asList().ofType(typeAnnotationType) .getValue(value).resolve(Integer.class), is(BAZ)); }
Example #11
Source File: AdviceTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testExceptionNotCatchedAdvice() throws Exception { Class<?> type = new ByteBuddy() .redefine(ExceptionNotCatchedAdvice.class) .visit(Advice.to(ExceptionNotCatchedAdvice.class).on(named(FOO))) .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); try { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { assertThat(exception.getCause(), instanceOf(Exception.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 0)); }
Example #12
Source File: MethodCallTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testOnFieldUsingMatcher() throws Exception { DynamicType.Loaded<MethodCallOnField> loaded = new ByteBuddy() .subclass(MethodCallOnField.class) .method(named("foo")) .intercept(MethodCall.invoke(named("trim")).onField("foo")) .make() .load(MethodCallOnField.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)); MethodCallOnField instance = loaded.getLoaded().getDeclaredConstructor().newInstance(); instance.foo = " abc "; assertThat(instance, instanceOf(MethodCallOnField.class)); assertThat(instance.foo(), is("abc")); }
Example #13
Source File: MethodCallProxy.java From byte-buddy with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { MethodDescription accessorMethod = methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT); LinkedHashMap<String, TypeDescription> parameterFields = extractFields(accessorMethod); DynamicType.Builder<?> builder = new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .with(PrecomputedMethodGraph.INSTANCE) .subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .implement(Runnable.class, Callable.class).intercept(new MethodCall(accessorMethod, assigner)) .implement(serializableProxy ? new Class<?>[]{Serializable.class} : new Class<?>[0]) .defineConstructor().withParameters(parameterFields.values()) .intercept(ConstructorCall.INSTANCE); for (Map.Entry<String, TypeDescription> field : parameterFields.entrySet()) { builder = builder.defineField(field.getKey(), field.getValue(), Visibility.PRIVATE); } return builder.make(); }
Example #14
Source File: MemberSubstitutionTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testMethodReadWithMethodSubstitution() throws Exception { Class<?> type = new ByteBuddy() .redefine(MethodInvokeSample.class) .visit(MemberSubstitution.strict().method(named(FOO)).replaceWith(MethodInvokeSample.class.getDeclaredMethod(BAZ)).on(named(RUN))) .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); Object instance = type.getDeclaredConstructor().newInstance(); assertThat(type.getDeclaredField(FOO).get(instance), is((Object) FOO)); assertThat(type.getDeclaredField(BAR).get(instance), is((Object) BAR)); assertThat(type.getDeclaredField(BAZ).get(instance), is((Object) BAZ)); assertThat(type.getDeclaredMethod(RUN).invoke(instance), nullValue(Object.class)); assertThat(type.getDeclaredField(FOO).get(instance), is((Object) FOO)); assertThat(type.getDeclaredField(BAR).get(instance), is((Object) BAZ)); assertThat(type.getDeclaredField(BAZ).get(instance), is((Object) BAZ)); }
Example #15
Source File: AdviceTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testAdviceNotSkipExceptionImplicit() throws Exception { Class<?> type = new ByteBuddy() .redefine(Sample.class) .visit(Advice.to(TrivialAdvice.class).on(named(FOO + BAR))) .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); try { type.getDeclaredMethod(FOO + BAR).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { assertThat(exception.getCause(), instanceOf(RuntimeException.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1)); }
Example #16
Source File: MethodDelegationTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("unchecked") public void testStaticMethodBinding() throws Exception { DynamicType.Loaded<T> loaded = new ByteBuddy() .subclass(sourceType) .method(isDeclaredBy(sourceType)) .intercept(MethodDelegation.to(targetType)) .make() .load(sourceType.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER); assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0)); assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1)); assertThat(loaded.getLoaded().getDeclaredFields().length, is(0)); T instance = loaded.getLoaded().getDeclaredConstructor().newInstance(); assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(sourceType))); assertThat(instance, instanceOf(sourceType)); assertThat(loaded.getLoaded().getDeclaredMethod(FOO, parameterTypes).invoke(instance, arguments), (Matcher) matcher); instance.assertZeroCalls(); }
Example #17
Source File: MethodCallTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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)); }
Example #18
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test @JavaVersionRule.Enforce(11) public void testNonStaticFieldVarHandle() throws Exception { Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.ofVarHandle(SampleClass.class.getDeclaredField("qux")))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(Class.forName("java.lang.invoke.VarHandle"))); }
Example #19
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test @JavaVersionRule.Enforce(11) public void testArrayVarHandle() throws Exception { Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.ofArrayVarHandle(Object[].class))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(Class.forName("java.lang.invoke.VarHandle"))); }
Example #20
Source File: MemberRemovalTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testFieldRemoval() throws Exception { Class<?> type = new ByteBuddy() .redefine(Sample.class) .visit(new MemberRemoval().stripFields(named(FOO))) .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); try { type.getDeclaredField(FOO); fail(); } catch (NoSuchFieldException ignored) { } assertThat(type.getDeclaredField(BAR), notNullValue(Field.class)); }
Example #21
Source File: TypeWriterDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test(expected = IllegalStateException.class) public void testAnnotationOnFieldPreJava5TypeAssertion() throws Exception { new ByteBuddy(ClassFileVersion.JAVA_V4) .subclass(Object.class) .defineField(FOO, Void.class) .annotateField(AnnotationDescription.Builder.ofType(Foo.class).build()) .make(); }
Example #22
Source File: MemberSubstitutionTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test(expected = IllegalStateException.class) public void testMethodMatchedNoTarget() throws Exception { new ByteBuddy() .redefine(MatcherSample.class) .visit(MemberSubstitution.strict().field(named(FOO)).replaceWithMethod(none()).on(named(FOO))) .make(); }
Example #23
Source File: Plugin.java From byte-buddy with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ public Engine with(ByteBuddy byteBuddy) { return new Default(byteBuddy, typeStrategy, poolStrategy, classFileLocator, listener, errorHandler, dispatcherFactory, ignoredTypeMatcher); }
Example #24
Source File: ClassByExtensionBenchmark.java From byte-buddy with Apache License 2.0 | 5 votes |
/** * Performs a benchmark of a class extension using Byte Buddy. This benchmark uses delegation but completes with a * hard-coded super method call. This benchmark uses a type pool to compare against usage of the reflection API. * * @return The created instance, in order to avoid JIT removal. * @throws Exception If the invocation causes an exception. */ @Benchmark public ExampleClass benchmarkByteBuddyWithPrefixWithTypePool() throws Exception { return (ExampleClass) new ByteBuddy() .with(TypeValidation.DISABLED) .ignore(none()) .subclass(baseClassDescription) .method(isDeclaredBy(baseClassDescription)).intercept(MethodDelegation.to(prefixClassDescription).andThen(SuperMethodCall.INSTANCE)) .make() .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION) .getLoaded() .getDeclaredConstructor() .newInstance(); }
Example #25
Source File: TypeWriterDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test @JavaVersionRule.Enforce(8) public void testStaticMethodOnInterfaceAssertionJava8() throws Exception { new ByteBuddy() .makeAnnotation() .defineMethod(FOO, String.class, Visibility.PUBLIC, Ownership.STATIC) .intercept(StubMethod.INSTANCE) .make(); }
Example #26
Source File: MethodDelegationDefaultMethodTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test @JavaVersionRule.Enforce(8) public void testExplicitDefaultCall() throws Exception { DynamicType.Loaded<?> loaded = new ByteBuddy() .subclass(Object.class) .implement(Class.forName(SINGLE_DEFAULT_METHOD), Class.forName(CONFLICTING_INTERFACE)) .intercept(MethodDelegation.to(Class.forName(PREFERRING_INTERCEPTOR))) .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)); }
Example #27
Source File: MethodDelegationMorphTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testMorphPrimitive() throws Exception { DynamicType.Loaded<Qux> loaded = new ByteBuddy() .subclass(Qux.class) .method(isDeclaredBy(Qux.class)) .intercept(MethodDelegation.withDefaultConfiguration() .withBinders(Morph.Binder.install(Morphing.class)) .to(new PrimitiveMorph(BAZ))) .make() .load(Qux.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER); Qux instance = loaded.getLoaded().getDeclaredConstructor().newInstance(); assertThat(instance.foo(0), is(BAZ * 2L)); }
Example #28
Source File: TypePoolDefaultWithLazyResolutionTypeDescriptionTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testNonGenericResolutionIsLazyForSimpleCreationNonFrozen() throws Exception { ClassFileLocator classFileLocator = spy(ClassFileLocator.ForClassLoader.ofSystemLoader()); new ByteBuddy() .with(TypeValidation.DISABLED) .with(MethodGraph.Empty.INSTANCE) .with(InstrumentedType.Factory.Default.MODIFIABLE) .redefine(describe(NonGenericType.class, classFileLocator, new TypePool.CacheProvider.Simple()), classFileLocator) .make(); verify(classFileLocator, times(2)).locate(NonGenericType.class.getName()); verifyNoMoreInteractions(classFileLocator); }
Example #29
Source File: MethodCallTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test(expected = IllegalStateException.class) public void testWithArgumentsFromArrayIllegalTypeCompleteArray() throws Exception { new ByteBuddy() .subclass(MethodCallWithExplicitArgument.class) .implement(IllegalMethodCallDelegator.class) .intercept(MethodCall.invoke(MethodCallWithExplicitArgument.class.getDeclaredMethod("foo", String.class)).withArgumentArrayElements(0)) .make(); }
Example #30
Source File: ClassInjectorUsingLookupTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test @JavaVersionRule.Enforce(9) public void testLookupInjectionPropagate() throws Exception { ClassInjector injector = ClassInjector.UsingLookup.of(Class.forName("java.lang.invoke.MethodHandles").getMethod("lookup").invoke(null)).in(type); DynamicType dynamicType = new ByteBuddy() .subclass(Object.class) .name("net.bytebuddy.test.Bar") .make(); assertThat(injector.inject(Collections.singletonMap(dynamicType.getTypeDescription(), dynamicType.getBytes())) .get(dynamicType.getTypeDescription()).getName(), is("net.bytebuddy.test.Bar")); }