/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { Implementation.Composable constructor = MethodCall.invoke(DEFAULT_CONSTRUCTOR).andThen(FieldAccessor.ofField(TYPE_FIELD).setsArgumentAt(0)); int index = 1; for (String field : fields.keySet()) { constructor = constructor.andThen(FieldAccessor.ofField(field).setsArgumentAt(index++)); } DynamicType.Builder<?> builder = new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .subclass(PrivilegedExceptionAction.class, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .defineConstructor(Visibility.PUBLIC) .withParameters(CompoundList.of(Class.class, new ArrayList<Class<?>>(fields.values()))) .intercept(constructor) .method(named("run")) .intercept(MethodCall.invoke(methodDescription) .onField(TYPE_FIELD) .withField(fields.keySet().toArray(new String[fields.size()]))) .defineField(TYPE_FIELD, Class.class, Visibility.PRIVATE); for (Map.Entry<String, Class<?>> entry : fields.entrySet()) { builder = builder.defineField(entry.getKey(), entry.getValue(), Visibility.PRIVATE); } return builder.make(); } }
.method(isHashCode()) .intercept(hashCode) .method(isEquals()) .intercept(equals) .serialVersionUid(42L)
@SuppressWarnings({ "unchecked", "rawtypes" }) public Class buildProxy( final Class persistentClass, final Class[] interfaces) { Set<Class<?>> key = new HashSet<Class<?>>(); if ( interfaces.length == 1 ) { key.add( persistentClass ); } key.addAll( Arrays.<Class<?>>asList( interfaces ) ); return byteBuddyState.loadProxy( persistentClass, new TypeCache.SimpleKey(key), byteBuddy -> byteBuddy .ignore( byteBuddyState.getProxyDefinitionHelpers().getGroovyGetMetaClassFilter() ) .with( new NamingStrategy.SuffixingRandom( PROXY_NAMING_SUFFIX, new NamingStrategy.SuffixingRandom.BaseNameResolver.ForFixedValue( persistentClass.getName() ) ) ) .subclass( interfaces.length == 1 ? persistentClass : Object.class, ConstructorStrategy.Default.IMITATE_SUPER_CLASS_OPENING ) .implement( (Type[]) interfaces ) .method( byteBuddyState.getProxyDefinitionHelpers().getVirtualNotFinalizerFilter() ) .intercept( byteBuddyState.getProxyDefinitionHelpers().getDelegateToInterceptorDispatcherMethodDelegation() ) .method( byteBuddyState.getProxyDefinitionHelpers().getHibernateGeneratedMethodFilter() ) .intercept( SuperMethodCall.INSTANCE ) .defineField( ProxyConfiguration.INTERCEPTOR_FIELD_NAME, ProxyConfiguration.Interceptor.class, Visibility.PRIVATE ) .implement( ProxyConfiguration.class ) .intercept( byteBuddyState.getProxyDefinitionHelpers().getInterceptorFieldAccessor() ) ); }
.defineField(JMOCK_KEY, Invokable.class, Visibility.PRIVATE) .implement(InjectInvokable.class).intercept(FieldAccessor.ofField(JMOCK_KEY)) .method(ElementMatchers.not(ElementMatchers.isDeclaredBy(InjectInvokable.class))) .intercept(MethodDelegation.to(Interceptor.class));
.method( getPropertyValuesMethodName ) .intercept( new Implementation.Simple( new GetPropertyValues( clazz, getters ) ) ) .method( setPropertyValuesMethodName ) .intercept( new Implementation.Simple( new SetPropertyValues( clazz, setters ) ) ) .method( getPropertyNamesMethodName ) .intercept( MethodCall.call( new CloningPropertyCall( getterNames ) ) ) );
.method(ElementMatchers.named("invoke")) .intercept(MethodCall.invoke(processMethodType) .onField("delegate")
public <T> Class<? extends T> createStageClass( Class<T> stageClass ) { return new ByteBuddy() .subclass( stageClass, ConstructorStrategy.Default.IMITATE_SUPER_CLASS_OPENING ) .implement( StageInterceptorInternal.class ) .defineField( INTERCEPTOR_FIELD_NAME, StepInterceptor.class ) .method( named(SETTER_NAME) ) .intercept( MethodDelegation.withDefaultConfiguration() .withBinders( FieldProxy.Binder.install( StepInterceptorGetterSetter.class )) .to(new StepInterceptorSetter() )) .method( not( named( SETTER_NAME ) .or(ElementMatchers.isDeclaredBy(Object.class)))) .intercept( MethodDelegation.withDefaultConfiguration() .withBinders(FieldProxy.Binder.install( StepInterceptorGetterSetter.class )) .to( new ByteBuddyMethodInterceptor() )) .make() .load( getClassLoader( stageClass ), getClassLoadingStrategy( stageClass ) ) .getLoaded(); }
static Object create(final Object bean, DecoratorExecutor executor) { try { Object proxy = new ByteBuddy() .subclass(bean.getClass()) .implement(EnhancedProxy.class) .defineField("bean", bean.getClass(), Modifier.PRIVATE) .defineField("executor", DecoratorExecutor.class, Modifier.PRIVATE) .method(isEquals()) .intercept(to(EqualsInterceptor.class)) .method(isAnnotatedWith(nameStartsWith("cucumber.api"))) .intercept(to(CucumberInterceptor.class)) .method(not(isEquals().or(isClone()).or(isAnnotatedWith(nameStartsWith("cucumber.api"))))) .intercept(toField("bean")) .make() .load(bean.getClass().getClassLoader(), ClassLoadingStrategy.Default.INJECTION) .getLoaded() .newInstance(); ReflectionUtils.injectValue(proxy, "bean", bean); ReflectionUtils.injectValue(proxy, "executor", executor); return proxy; } catch (Exception e) { throw new JpaUnitException("Failed to create proxy for " + bean, e); } } }
@SuppressWarnings({ "unchecked", "rawtypes" }) public Class buildProxy( final Class persistentClass, final Class[] interfaces) { Set<Class<?>> key = new HashSet<Class<?>>(); if ( interfaces.length == 1 ) { key.add( persistentClass ); } key.addAll( Arrays.<Class<?>>asList( interfaces ) ); return byteBuddyState.loadProxy( persistentClass, new TypeCache.SimpleKey(key), byteBuddy -> byteBuddy .ignore( byteBuddyState.getProxyDefinitionHelpers().getGroovyGetMetaClassFilter() ) .with( new NamingStrategy.SuffixingRandom( PROXY_NAMING_SUFFIX, new NamingStrategy.SuffixingRandom.BaseNameResolver.ForFixedValue( persistentClass.getName() ) ) ) .subclass( interfaces.length == 1 ? persistentClass : Object.class, ConstructorStrategy.Default.IMITATE_SUPER_CLASS_OPENING ) .implement( (Type[]) interfaces ) .method( byteBuddyState.getProxyDefinitionHelpers().getVirtualNotFinalizerFilter() ) .intercept( byteBuddyState.getProxyDefinitionHelpers().getDelegateToInterceptorDispatcherMethodDelegation() ) .method( byteBuddyState.getProxyDefinitionHelpers().getHibernateGeneratedMethodFilter() ) .intercept( SuperMethodCall.INSTANCE ) .defineField( ProxyConfiguration.INTERCEPTOR_FIELD_NAME, ProxyConfiguration.Interceptor.class, Visibility.PRIVATE ) .implement( ProxyConfiguration.class ) .intercept( byteBuddyState.getProxyDefinitionHelpers().getInterceptorFieldAccessor() ) ); }
public MetamodelUtil getMetamodelUtil() { MetamodelUtil metamodel = null; try { Constructor<?> c = new ByteBuddy() .subclass(MetamodelUtil.class) .method(named("isFieldGetterMethod")).intercept(FixedValue.value(false)) .method(named("isSafeMethod")).intercept(FixedValue.value(true)) .make() .load(getClass().getClassLoader()) .getLoaded() .getDeclaredConstructor(Schema.class); c.setAccessible(true); metamodel = (MetamodelUtil) c.newInstance(new SchemaImpl("")); c.setAccessible(false); } catch (ReflectiveOperationException ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex); } return metamodel; }
private <T extends ViewContext> T generateViewContextProxy(Class<T> clazz, Map<String, ContextProperty> properties) throws InstantiationException, IllegalAccessException, NoSuchMethodException { final Constructor<ViewContextBase> targetCtor = ViewContextBase.class.getConstructor(Map.class); return clazz.cast(new ByteBuddy() .subclass(ViewContextBase.class, ConstructorStrategy.Default.NO_CONSTRUCTORS) .implement(clazz, Serializable.class) .method(named("getViewContextType")).intercept(value(clazz)) .method(returns(ContextProperty.class)) .intercept(to(new StatePropertyGetterDelegate())) .defineConstructor(Modifier.PUBLIC) .intercept(invoke(targetCtor) .with(properties) .andThen(to(new ContextInitializer()))) .make() .load(getClass().getClassLoader(), WRAPPER) .getLoaded() .newInstance()); }
@Override public Class<?> generateProxyClass(ClassLoader classLoader, Class<?>... proxyClasses) { Class/*<? extends Echo>*/ dynamicUserType = new ByteBuddy() .subclass(proxyClasses[0]) .implement(ObjectInvokerAccessor.class).intercept(FieldAccessor.ofBeanProperty()) .method(not(isDeclaredBy(Object.class))) .intercept(MethodDelegation.to(DynamicDelegator.class)) .make() .load(getClass().getClassLoader()) .getLoaded(); return dynamicUserType; } }
static DynamicType.Builder<?> createEqualsAndHashCode(DynamicType.Builder<?> builder) { return builder .method(named("equals")).intercept(MethodDelegation.to(EqualsTarget.class)) .method(named("hashCode")).intercept(MethodDelegation.to(HashCodeTarget.class)); }
.method( getPropertyValuesMethodName ) .intercept( new Implementation.Simple( new GetPropertyValues( clazz, getters ) ) ) .method( setPropertyValuesMethodName ) .intercept( new Implementation.Simple( new SetPropertyValues( clazz, setters ) ) ) .method( getPropertyNamesMethodName ) .intercept( MethodCall.call( new CloningPropertyCall( getterNames ) ) ) );
.withParameters(factoryMethod.getParameterTypes()) .intercept(ConstructorImplementation.INSTANCE) .method(named(lambdaMethodName) .and(takesArguments(lambdaMethod.getParameterTypes())) .and(returns(lambdaMethod.getReturnType())))
.intercept(MethodDelegation.to(FieldAccessorInterceptor.class)) .method(ElementMatchers.isDeclaredBy(RObject.class) .or(ElementMatchers.isDeclaredBy(RObjectAsync.class))) .intercept(MethodDelegation.to(RObjectInterceptor.class))
.intercept(MethodDelegation.to(FieldAccessorInterceptor.class)) .method(ElementMatchers.isDeclaredBy(RObject.class) .or(ElementMatchers.isDeclaredBy(RObjectAsync.class))) .intercept(MethodDelegation.to(RObjectInterceptor.class))
/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { return new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .subclass(morphingType, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .implement(serializableProxy ? new Class<?>[]{Serializable.class} : new Class<?>[0]) .defineConstructor().withParameters(specialMethodInvocation.getMethodDescription().isStatic() ? Collections.<TypeDescription>emptyList() : Collections.singletonList(instrumentedType)) .intercept(specialMethodInvocation.getMethodDescription().isStatic() ? StaticFieldConstructor.INSTANCE : new InstanceFieldConstructor(instrumentedType)) .method(ElementMatchers.<MethodDescription>isAbstract().and(isDeclaredBy(morphingType))) .intercept(new MethodCall(methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT), assigner)) .make(); }
/** * {@inheritDoc} */ public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, FieldDescription fieldDescription, Assigner assigner, MethodAccessorFactory methodAccessorFactory) { return builder .method(is(getterMethod)).intercept(new FieldGetter(fieldDescription, assigner, methodAccessorFactory)) .method(is(setterMethod)).intercept(fieldDescription.isFinal() ? ExceptionMethod.throwing(UnsupportedOperationException.class, "Cannot set final field " + fieldDescription) : new FieldSetter(fieldDescription, assigner, methodAccessorFactory)); } }
/** * {@inheritDoc} */ public Builder<S> withHashCodeEquals() { return method(isHashCode()) .intercept(HashCodeMethod.usingDefaultOffset().withIgnoredFields(isSynthetic())) .method(isEquals()) .intercept(EqualsMethod.isolated().withIgnoredFields(isSynthetic())); }