net.bytebuddy.dynamic.DynamicType Java Examples
The following examples show how to use
net.bytebuddy.dynamic.DynamicType.
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: MethodDelegationOriginTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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))); }
Example #2
Source File: MethodDelegationOriginTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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))); }
Example #3
Source File: MethodCallTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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)); }
Example #4
Source File: MethodCallTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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)); }
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: InvocationHandlerAdapterTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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(); }
Example #7
Source File: ClassDumperListener.java From kanela with Apache License 2.0 | 6 votes |
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..."); } }
Example #8
Source File: MethodCallTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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)); }
Example #9
Source File: ToStringMethodTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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 + "}")); }
Example #10
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 #11
Source File: SetInPropertiesHandler.java From windup with Eclipse Public License 1.0 | 6 votes |
/** * 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()); }
Example #12
Source File: AgentBuilderListenerTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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); }
Example #13
Source File: EqualsMethodOtherTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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)); }
Example #14
Source File: MethodCallTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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 #15
Source File: MethodCallTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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)); }
Example #16
Source File: MethodDelegationMorphTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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); }
Example #17
Source File: EqualsMethodOtherTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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)); }
Example #18
Source File: ExceptionMethodTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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 #19
Source File: InvokeDynamicTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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)); }
Example #20
Source File: MethodCallTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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); }
Example #21
Source File: FieldAccessorOtherTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test @JavaVersionRule.Enforce(7) public void testJavaConstant() throws Exception { DynamicType.Loaded<SampleNoArgumentSetter> loaded = new ByteBuddy() .subclass(SampleNoArgumentSetter.class) .method(named(FOO)) .intercept(FieldAccessor.ofField(FOO).setsValue(JavaConstant.MethodType.ofConstant(Object.class))) .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, instanceOf(JavaType.METHOD_TYPE.load())); }
Example #22
Source File: PropertyMutatorCollector.java From jackson-modules-base with Apache License 2.0 | 5 votes |
private <T extends OptimizedSettableBeanProperty<T>> DynamicType.Builder<?> _addSetters( DynamicType.Builder<?> builder, List<T> props, String methodName, MethodVariableAccess beanValueAccess) { return builder.method(named(methodName)) .intercept( new Implementation.Simple( new MethodAppender<T>(beanClassDefinition, props, beanValueAccess) ) ); }
Example #23
Source File: RedefinitionDynamicTypeBuilder.java From byte-buddy with Apache License 2.0 | 5 votes |
@Override protected DynamicType.Builder<T> materialize(InstrumentedType.WithFlexibleName instrumentedType, FieldRegistry fieldRegistry, MethodRegistry methodRegistry, RecordComponentRegistry recordComponentRegistry, TypeAttributeAppender typeAttributeAppender, AsmVisitorWrapper asmVisitorWrapper, 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, List<? extends DynamicType> auxiliaryTypes) { return new RedefinitionDynamicTypeBuilder<T>(instrumentedType, fieldRegistry, methodRegistry, recordComponentRegistry, typeAttributeAppender, asmVisitorWrapper, classFileVersion, auxiliaryTypeNamingStrategy, annotationValueFilterFactory, annotationRetention, implementationContextFactory, methodGraphCompiler, typeValidation, visibilityBridgeStrategy, classWriterStrategy, ignoredMethods, auxiliaryTypes, originalType, classFileLocator); }
Example #24
Source File: ClassEnhancePluginDefine.java From skywalking with Apache License 2.0 | 5 votes |
/** * Begin to define how to enhance class. After invoke this method, only means definition is finished. * * @param typeDescription target class description * @param newClassBuilder byte-buddy's builder to manipulate class bytecode. * @return new byte-buddy's builder for further manipulation. */ @Override protected DynamicType.Builder<?> enhance(TypeDescription typeDescription, DynamicType.Builder<?> newClassBuilder, ClassLoader classLoader, EnhanceContext context) throws PluginException { newClassBuilder = this.enhanceClass(typeDescription, newClassBuilder, classLoader); newClassBuilder = this.enhanceInstance(typeDescription, newClassBuilder, classLoader, context); return newClassBuilder; }
Example #25
Source File: MethodDelegationThisTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testThis() throws Exception { DynamicType.Loaded<Foo> loaded = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(MethodDelegation.to(Bar.class)) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER); Foo instance = loaded.getLoaded().getDeclaredConstructor().newInstance(); assertThat(instance.foo(), is((Object) instance)); }
Example #26
Source File: DefaultMethodCallTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test @JavaVersionRule.Enforce(8) public void testUnambiguousDefaultMethod() throws Exception { DynamicType.Loaded<?> loaded = new ByteBuddy() .subclass(Object.class) .implement(Class.forName(SINGLE_DEFAULT_METHOD)) .intercept(DefaultMethodCall.unambiguousOnly()) .make() .load(Class.forName(SINGLE_DEFAULT_METHOD).getClassLoader(), ClassLoadingStrategy.Default.WRAPPER); assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1)); Method method = loaded.getLoaded().getDeclaredMethod(FOO); Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance(); assertThat(method.invoke(instance), is((Object) FOO)); }
Example #27
Source File: FieldAccessorTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@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)); }
Example #28
Source File: BeanBuilder.java From jackson-modules-base with Apache License 2.0 | 5 votes |
private DynamicType.Builder<?> createGetter(DynamicType.Builder<?> builder, POJOProperty property, TypeDefinition typeDefinition) { final String methodName = property.getGetter() != null ? property.getGetter().getName() //if the getter exists, use it's name because it could be like 'isXXX' : buildGetterName(property.getName()); return builder .defineMethod(methodName, typeDefinition) .intercept(FieldAccessor.ofBeanProperty()); }
Example #29
Source File: BeanBuilder.java From jackson-modules-base with Apache License 2.0 | 5 votes |
/** * Method that generates byte code for class that implements abstract * types requested so far. * * @param className Fully-qualified name of the class to generate * @return Byte code Class instance built by this builder */ public byte[] build(String className) { DynamicType.Builder<?> builder = new ByteBuddy() //needed because className can contain Java keywords .with(TypeValidation.DISABLED) .subclass(_type.getRawClass()) .name(className); for (POJOProperty prop : _beanProperties.values()) { final TypeDefinition typeDefinition = getFieldType(prop); builder = createField(builder, prop, typeDefinition); if (!prop.hasConcreteGetter()) { builder = createGetter(builder, prop, typeDefinition); } if (!prop.hasConcreteSetter()) { builder = createSetter(builder, prop, typeDefinition); } } for (Method m : _unsupportedMethods.values()) { builder = builder .defineMethod(m.getName(), m.getReturnType(), Visibility.PUBLIC) .intercept( ExceptionMethod.throwing( UnsupportedOperationException.class, "Unimplemented method '"+m.getName()+"' (not a setter/getter, could not materialize)") ); } if (_type.isInterface()) { builder = createEqualsAndHashCode(builder); } return builder.make().getBytes(); }
Example #30
Source File: TypeWriterDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@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)); }