net.bytebuddy.NamingStrategy Java Examples

The following examples show how to use net.bytebuddy.NamingStrategy. 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: ByteBuddyProxyHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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() )
	);
}
 
Example #2
Source File: BasicProxyFactoryImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public BasicProxyFactoryImpl(Class superClass, Class[] interfaces, ByteBuddyState byteBuddyState) {
	if ( superClass == null && ( interfaces == null || interfaces.length < 1 ) ) {
		throw new AssertionFailure( "attempting to build proxy without any superclass or interfaces" );
	}

	final Class<?> superClassOrMainInterface = superClass != null ? superClass : interfaces[0];
	final TypeCache.SimpleKey cacheKey = getCacheKey( superClass, interfaces );

	this.proxyClass = byteBuddyState.loadBasicProxy( superClassOrMainInterface, cacheKey, byteBuddy -> byteBuddy
			.with( new NamingStrategy.SuffixingRandom( PROXY_NAMING_SUFFIX, new NamingStrategy.SuffixingRandom.BaseNameResolver.ForFixedValue( superClassOrMainInterface.getName() ) ) )
			.subclass( superClass == null ? Object.class : superClass, ConstructorStrategy.Default.DEFAULT_CONSTRUCTOR )
			.implement( interfaces == null ? NO_INTERFACES : interfaces )
			.defineField( ProxyConfiguration.INTERCEPTOR_FIELD_NAME, ProxyConfiguration.Interceptor.class, Visibility.PRIVATE )
			.method( byteBuddyState.getProxyDefinitionHelpers().getVirtualNotFinalizerFilter() )
					.intercept( byteBuddyState.getProxyDefinitionHelpers().getDelegateToInterceptorDispatcherMethodDelegation() )
			.implement( ProxyConfiguration.class )
					.intercept( byteBuddyState.getProxyDefinitionHelpers().getInterceptorFieldAccessor() )
	);
	this.interceptor = new PassThroughInterceptor( proxyClass.getName() );
}
 
Example #3
Source File: BytecodeProviderImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public ReflectionOptimizer getReflectionOptimizer(
		final Class clazz,
		final String[] getterNames,
		final String[] setterNames,
		final Class[] types) {
	final Class fastClass;
	if ( !clazz.isInterface() && !Modifier.isAbstract( clazz.getModifiers() ) ) {
		// we only provide a fast class instantiator if the class can be instantiated
		final Constructor<?> constructor = findConstructor( clazz );

		fastClass = byteBuddyState.load( clazz, byteBuddy -> byteBuddy
				.with( new NamingStrategy.SuffixingRandom( INSTANTIATOR_PROXY_NAMING_SUFFIX,
						new NamingStrategy.SuffixingRandom.BaseNameResolver.ForFixedValue( clazz.getName() ) ) )
				.subclass( ReflectionOptimizer.InstantiationOptimizer.class )
				.method( newInstanceMethodName )
						.intercept( MethodCall.construct( constructor ) )
		);
	}
	else {
		fastClass = null;
	}

	final Method[] getters = new Method[getterNames.length];
	final Method[] setters = new Method[setterNames.length];
	findAccessors( clazz, getterNames, setterNames, types, getters, setters );

	final Class bulkAccessor = byteBuddyState.load( clazz, byteBuddy -> byteBuddy
			.with( new NamingStrategy.SuffixingRandom( OPTIMIZER_PROXY_NAMING_SUFFIX,
					new NamingStrategy.SuffixingRandom.BaseNameResolver.ForFixedValue( clazz.getName() ) ) )
			.subclass( ReflectionOptimizer.AccessOptimizer.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 ) ) )
	);

	try {
		return new ReflectionOptimizerImpl(
				fastClass != null ? (ReflectionOptimizer.InstantiationOptimizer) fastClass.newInstance() : null,
				(ReflectionOptimizer.AccessOptimizer) bulkAccessor.newInstance()
		);
	}
	catch (Exception exception) {
		throw new HibernateException( exception );
	}
}