org.springframework.cglib.proxy.Factory Java Examples
The following examples show how to use
org.springframework.cglib.proxy.Factory.
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: CglibAopProxy.java From spring-analysis-note with MIT License | 6 votes |
@Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) { Object other = args[0]; if (proxy == other) { return true; } if (other instanceof Factory) { Callback callback = ((Factory) other).getCallback(INVOKE_EQUALS); if (!(callback instanceof EqualsInterceptor)) { return false; } AdvisedSupport otherAdvised = ((EqualsInterceptor) callback).advised; return AopProxyUtils.equalsInProxy(this.advised, otherAdvised); } else { return false; } }
Example #2
Source File: CglibSubclassingInstantiationStrategy.java From spring-analysis-note with MIT License | 6 votes |
/** * Create a new instance of a dynamically generated subclass implementing the * required lookups. * @param ctor constructor to use. If this is {@code null}, use the * no-arg constructor (no parameterization, or Setter Injection) * @param args arguments to use for the constructor. * Ignored if the {@code ctor} parameter is {@code null}. * @return new instance of the dynamically generated subclass */ public Object instantiate(@Nullable Constructor<?> ctor, Object... args) { Class<?> subclass = createEnhancedSubclass(this.beanDefinition); Object instance; if (ctor == null) { instance = BeanUtils.instantiateClass(subclass); } else { try { Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes()); instance = enhancedSubclassConstructor.newInstance(args); } catch (Exception ex) { throw new BeanInstantiationException(this.beanDefinition.getBeanClass(), "Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex); } } // SPR-10785: set callbacks directly on the instance instead of in the // enhanced class (via the Enhancer) in order to avoid memory leaks. Factory factory = (Factory) instance; factory.setCallbacks(new Callback[] {NoOp.INSTANCE, new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner), new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)}); return instance; }
Example #3
Source File: CglibAopProxy.java From java-technology-stack with MIT License | 6 votes |
@Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) { Object other = args[0]; if (proxy == other) { return true; } if (other instanceof Factory) { Callback callback = ((Factory) other).getCallback(INVOKE_EQUALS); if (!(callback instanceof EqualsInterceptor)) { return false; } AdvisedSupport otherAdvised = ((EqualsInterceptor) callback).advised; return AopProxyUtils.equalsInProxy(this.advised, otherAdvised); } else { return false; } }
Example #4
Source File: CglibSubclassingInstantiationStrategy.java From java-technology-stack with MIT License | 6 votes |
/** * Create a new instance of a dynamically generated subclass implementing the * required lookups. * @param ctor constructor to use. If this is {@code null}, use the * no-arg constructor (no parameterization, or Setter Injection) * @param args arguments to use for the constructor. * Ignored if the {@code ctor} parameter is {@code null}. * @return new instance of the dynamically generated subclass */ public Object instantiate(@Nullable Constructor<?> ctor, Object... args) { Class<?> subclass = createEnhancedSubclass(this.beanDefinition); Object instance; if (ctor == null) { instance = BeanUtils.instantiateClass(subclass); } else { try { Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes()); instance = enhancedSubclassConstructor.newInstance(args); } catch (Exception ex) { throw new BeanInstantiationException(this.beanDefinition.getBeanClass(), "Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex); } } // SPR-10785: set callbacks directly on the instance instead of in the // enhanced class (via the Enhancer) in order to avoid memory leaks. Factory factory = (Factory) instance; factory.setCallbacks(new Callback[] {NoOp.INSTANCE, new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner), new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)}); return instance; }
Example #5
Source File: EagleTraceCglibProxy.java From eagle with Apache License 2.0 | 6 votes |
@Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) { Object other = args[0]; if (proxy == other) { return true; } if (other instanceof Factory) { Callback callback = ((Factory) other).getCallback(INVOKE_EQUALS); if (!(callback instanceof EagleTraceCglibProxy.EqualsInterceptor)) { return false; } AdvisedSupport otherAdvised = ((EagleTraceCglibProxy.EqualsInterceptor) callback).advised; return AopProxyUtils.equalsInProxy(this.advised, otherAdvised); } else { return false; } }
Example #6
Source File: AbstractResolver.java From spring-data with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) protected Object proxy( final String id, final TypeInformation<?> type, final A annotation, final ResolverCallback<A> callback) { final ProxyInterceptor interceptor = new ProxyInterceptor(id, type, annotation, callback, conversionService); if (type.getType().isInterface()) { final ProxyFactory proxyFactory = new ProxyFactory(new Class<?>[] { type.getType() }); for (final Class<?> interf : type.getType().getInterfaces()) { proxyFactory.addInterface(interf); } proxyFactory.addInterface(LazyLoadingProxy.class); proxyFactory.addAdvice(interceptor); return proxyFactory.getProxy(); } else { final Factory factory = (Factory) objenesis.newInstance(enhancedTypeFor(type.getType())); factory.setCallbacks(new Callback[] { interceptor }); return factory; } }
Example #7
Source File: CglibSubclassingInstantiationStrategy.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Create a new instance of a dynamically generated subclass implementing the * required lookups. * @param ctor constructor to use. If this is {@code null}, use the * no-arg constructor (no parameterization, or Setter Injection) * @param args arguments to use for the constructor. * Ignored if the {@code ctor} parameter is {@code null}. * @return new instance of the dynamically generated subclass */ public Object instantiate(Constructor<?> ctor, Object... args) { Class<?> subclass = createEnhancedSubclass(this.beanDefinition); Object instance; if (ctor == null) { instance = BeanUtils.instantiateClass(subclass); } else { try { Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes()); instance = enhancedSubclassConstructor.newInstance(args); } catch (Exception ex) { throw new BeanInstantiationException(this.beanDefinition.getBeanClass(), "Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex); } } // SPR-10785: set callbacks directly on the instance instead of in the // enhanced class (via the Enhancer) in order to avoid memory leaks. Factory factory = (Factory) instance; factory.setCallbacks(new Callback[] {NoOp.INSTANCE, new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner), new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)}); return instance; }
Example #8
Source File: CglibAopProxy.java From lams with GNU General Public License v2.0 | 6 votes |
@Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) { Object other = args[0]; if (proxy == other) { return true; } if (other instanceof Factory) { Callback callback = ((Factory) other).getCallback(INVOKE_EQUALS); if (!(callback instanceof EqualsInterceptor)) { return false; } AdvisedSupport otherAdvised = ((EqualsInterceptor) callback).advised; return AopProxyUtils.equalsInProxy(this.advised, otherAdvised); } else { return false; } }
Example #9
Source File: CglibAopProxy.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) { Object other = args[0]; if (proxy == other) { return true; } if (other instanceof Factory) { Callback callback = ((Factory) other).getCallback(INVOKE_EQUALS); if (!(callback instanceof EqualsInterceptor)) { return false; } AdvisedSupport otherAdvised = ((EqualsInterceptor) callback).advised; return AopProxyUtils.equalsInProxy(this.advised, otherAdvised); } else { return false; } }
Example #10
Source File: CglibSubclassingInstantiationStrategy.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Create a new instance of a dynamically generated subclass implementing the * required lookups. * @param ctor constructor to use. If this is {@code null}, use the * no-arg constructor (no parameterization, or Setter Injection) * @param args arguments to use for the constructor. * Ignored if the {@code ctor} parameter is {@code null}. * @return new instance of the dynamically generated subclass */ public Object instantiate(Constructor<?> ctor, Object... args) { Class<?> subclass = createEnhancedSubclass(this.beanDefinition); Object instance; if (ctor == null) { instance = BeanUtils.instantiate(subclass); } else { try { Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes()); instance = enhancedSubclassConstructor.newInstance(args); } catch (Exception ex) { throw new BeanInstantiationException(this.beanDefinition.getBeanClass(), "Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex); } } // SPR-10785: set callbacks directly on the instance instead of in the // enhanced class (via the Enhancer) in order to avoid memory leaks. Factory factory = (Factory) instance; factory.setCallbacks(new Callback[] {NoOp.INSTANCE, new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner), new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)}); return instance; }
Example #11
Source File: LazyUtil.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
private static SimpleLazyDynamicInvocationHandler getProxy(Object object) { if (Proxy.isProxyClass(object.getClass()) && (Proxy.getInvocationHandler(object) instanceof SimpleLazyDynamicInvocationHandler)) { return (SimpleLazyDynamicInvocationHandler) Proxy .getInvocationHandler(object); } else if (object instanceof Factory) { Callback[] callbacks = ((Factory) object).getCallbacks(); if (callbacks != null && callbacks.length == 1 && callbacks[0] instanceof SimpleLazyDynamicInvocationHandler) { return (SimpleLazyDynamicInvocationHandler) callbacks[0]; } } return null; }
Example #12
Source File: CglibSubclassingInstantiationStrategy.java From blog_demos with Apache License 2.0 | 5 votes |
/** * Create a new instance of a dynamically generated subclass implementing the * required lookups. * @param ctor constructor to use. If this is {@code null}, use the * no-arg constructor (no parameterization, or Setter Injection) * @param args arguments to use for the constructor. * Ignored if the {@code ctor} parameter is {@code null}. * @return new instance of the dynamically generated subclass */ Object instantiate(Constructor<?> ctor, Object[] args) { Class<?> subclass = createEnhancedSubclass(this.beanDefinition); Object instance; if (ctor == null) { instance = BeanUtils.instantiate(subclass); } else { try { Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes()); instance = enhancedSubclassConstructor.newInstance(args); } catch (Exception e) { throw new BeanInstantiationException(this.beanDefinition.getBeanClass(), String.format( "Failed to invoke construcor for CGLIB enhanced subclass [%s]", subclass.getName()), e); } } // SPR-10785: set callbacks directly on the instance instead of in the // enhanced class (via the Enhancer) in order to avoid memory leaks. Factory factory = (Factory) instance; factory.setCallbacks(new Callback[] { NoOp.INSTANCE,// new LookupOverrideMethodInterceptor(beanDefinition, owner),// new ReplaceOverrideMethodInterceptor(beanDefinition, owner) }); return instance; }
Example #13
Source File: MockHelper.java From COLA with GNU Lesser General Public License v2.1 | 4 votes |
public static Object createMockFor(Class clazz, MethodInterceptor interceptor){ Class proxyCls = createMockClass(clazz); Factory proxy = (Factory)newInstance(proxyCls); proxy.setCallbacks(new Callback[] {interceptor}); return proxy; }
Example #14
Source File: CglibStaticConfigurationInitializer.java From conf4j with MIT License | 4 votes |
private CglibStaticConfigurationMethodInterceptor getMethodInterceptor() { return (CglibStaticConfigurationMethodInterceptor) ((Factory) configuration).getCallback(ProxyCallbackFilter.CONFIGURATION); }
Example #15
Source File: CglibDynamicConfigurationInitializer.java From conf4j with MIT License | 4 votes |
private CglibDynamicConfigurationMethodInterceptor getMethodInterceptor() { return (CglibDynamicConfigurationMethodInterceptor) ((Factory) configuration).getCallback(ProxyCallbackFilter.CONFIGURATION); }
Example #16
Source File: LazyUtil.java From spring-cloud-gcp with Apache License 2.0 | 3 votes |
/** * Returns a proxy that lazily loads the value provided by a supplier. The proxy also * stores the key(s) that can be used in case the value was not loaded. If the type of the * value is interface, {@link java.lang.reflect.Proxy} is used, otherwise cglib proxy is * used (creates a sub-class of the original type; the original class can't be final and * can't have final methods). * @param supplierFunc a function that provides the value * @param type the type of the value * @param keys Datastore key(s) that can be used when the parent entity is saved * @return true if the object is a proxy that was not evaluated */ static <T> T wrapSimpleLazyProxy(Supplier<T> supplierFunc, Class<T> type, Value keys) { if (type.isInterface()) { return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class[] {type}, new SimpleLazyDynamicInvocationHandler<T>(supplierFunc, keys)); } Factory factory = (Factory) objenesis.newInstance(getEnhancedTypeFor(type)); factory.setCallbacks(new Callback[] { new SimpleLazyDynamicInvocationHandler<T>(supplierFunc, keys) }); return (T) factory; }