net.sf.cglib.proxy.MethodProxy Java Examples
The following examples show how to use
net.sf.cglib.proxy.MethodProxy.
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: LibCGDecoratorGenerator.java From ProjectAres with GNU Affero General Public License v3.0 | 6 votes |
@Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if(isDecorated(method)) { // Decorated method return proxy.invokeSuper(obj, args); } else { final T t = ((Decorator<T>) obj).delegate(); if(method.getDeclaringClass().isInstance(t)) { // Forwarded method return proxy.invoke(t, args); } else { // Forwarded method shadowed by an interface method in the decorator. // // This can happen if the decorator implements an interface that the // base class doesn't, and that interface contains a method that shadows // one on the base class. Java would allow the method to be called on the // base anyway, but MethodProxy refuses to invoke it on something that // is not assignable to the method's declaring type. So, unfortunately, // we have to fall back to the JDK to handle this case. return methodHandles.get(method, () -> resolver.virtualHandle(t.getClass(), method).bindTo(t) ).invokeWithArguments(args); } } }
Example #2
Source File: ReflectiveParserManifest.java From ProjectAres with GNU Affero General Public License v3.0 | 6 votes |
@Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { // Give our delegate a chance to intercept, and cache the decision if(delegatedMethods.get(method, () -> method.getDeclaringClass() != Object.class && Methods.hasOverrideIn(Delegate.class, method))) { return method.invoke(delegate, args); } // If we have a value for the property, return that final Object value = values.get(method); if(value != null) return value; // If there's no value, then the method MUST be callable (or the code is broken). // This can only fail for an abstract non-property method (which we should probably be checking for). if(method.isDefault()) { // invokeSuper doesn't understand default methods return defaultMethodHandles.get(method) .bindTo(obj) .invokeWithArguments(args); } else { return proxy.invokeSuper(obj, args); } }
Example #3
Source File: InterceptorOfASingleElement.java From java-client with Apache License 2.0 | 6 votes |
/** * Look at {@link MethodInterceptor#intercept(Object, Method, Object[], MethodProxy)}. */ public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if (method.getName().equals("toString") && args.length == 0) { return locator.toString(); } if (Object.class.equals(method.getDeclaringClass())) { return proxy.invokeSuper(obj, args); } if (WrapsDriver.class.isAssignableFrom(method.getDeclaringClass()) && method.getName() .equals("getWrappedDriver")) { return driver; } WebElement realElement = locator.findElement(); return getObject(realElement, method, args); }
Example #4
Source File: DefaultMQProducerMethodInterceptor.java From pepper-metrics with Apache License 2.0 | 6 votes |
@Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { final String[] tags = {"method", method.getName(), "namespace", namespace}; long begin = System.currentTimeMillis(); Object result; stats.incConc(tags); try { result = proxy.invokeSuper(obj, args); } catch (Throwable t) { stats.error(tags); throw t; } finally { stats.observe(System.currentTimeMillis() - begin, tags); stats.decConc(tags); } return result; }
Example #5
Source File: ContinuesInterceptor.java From ghidra with Apache License 2.0 | 6 votes |
@Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { Object retValFromSuper = null; if (method.getAnnotation(DoNotContinue.class) != null) { retValFromSuper = proxy.invokeSuper(obj, args); } else { try { retValFromSuper = proxy.invokeSuper(obj, args); } catch (Exception e) { handler.handle(e); } } return retValFromSuper; }
Example #6
Source File: VariableInterpolator.java From onedev with MIT License | 6 votes |
@SuppressWarnings("unchecked") public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { if (BeanUtils.isGetter(method) && method.getAnnotation(Editable.class) != null && method.getAnnotation(io.onedev.server.web.editable.annotation.Interpolative.class) != null) { Serializable propertyValue = (Serializable) methodProxy.invokeSuper(proxy, args); Build build = Build.get(); if (build == null) { throw new RuntimeException("No build context"); } else if (propertyValue instanceof String) { return build.interpolate((String) propertyValue); } else if (propertyValue instanceof List) { List<String> interpolatedList = new ArrayList<>(); for (String element: (List<String>) propertyValue) interpolatedList.add(build.interpolate(element)); return interpolatedList; } } return methodProxy.invokeSuper(proxy, args); }
Example #7
Source File: PropertyStateSupport.java From ymate-platform-v2 with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") public T bind() { if (__bound == null) { __bound = (T) ClassUtils.wrapper(__source).duplicate(Enhancer.create(__targetClass, new MethodInterceptor() { @Override public Object intercept(Object targetObject, Method targetMethod, Object[] methodParams, MethodProxy methodProxy) throws Throwable { PropertyStateMeta _meta = __propertyStates.get(targetMethod.getName()); if (_meta != null && ArrayUtils.isNotEmpty(methodParams) && !ObjectUtils.equals(_meta.getOriginalValue(), methodParams[0])) { if (__ignoreNull && methodParams[0] == null) { methodParams[0] = _meta.getOriginalValue(); } _meta.setNewValue(methodParams[0]); } return methodProxy.invokeSuper(targetObject, methodParams); } })); } return __bound; }
Example #8
Source File: MjProxy.java From Mars-Java with MIT License | 6 votes |
/** * 绑定代理 * * @param o * @param method * @param args * @param methodProxy * @return obj * @throws Throwable */ @Override public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { MarsGet marsGet = method.getAnnotation(MarsGet.class); MarsSelect marsSelect = method.getAnnotation(MarsSelect.class); MarsUpdate marsUpdate = method.getAnnotation(MarsUpdate.class); int count = checkAnnot(marsGet, marsSelect, marsUpdate); if (count == 0) { return methodProxy.invokeSuper(o, args); } else if (count == 1) { return executeMethod(args,method,marsGet,marsSelect,marsUpdate); } else { throw new Exception(method.getName() + "方法上不允许有多个sql注解"); } }
Example #9
Source File: ModularServiceMethodInterceptor.java From nuls with MIT License | 6 votes |
/** * 拦截方法 * Intercept method * * @param obj 方法所属对象/Method owner * @param method 方法定义/Method definition * @param params 方法参数列表/Method parameter list * @param methodProxy 方法代理器 * @return 返回拦截的方法的返回值,可以对该值进行处理和替换/Returns the return value of the intercepting method, which can be processed and replaced. * @throws Throwable 该方法可能抛出异常,请谨慎处理/This method may throw an exception, handle with care. */ @Override public Object intercept(Object obj, Method method, Object[] params, MethodProxy methodProxy) throws Throwable { // Log.debug(method.toString()); threadLocal.set(0); Throwable throwable = null; while (threadLocal.get() < 100) { try { return this.doIntercept(obj, method, params, methodProxy); } catch (BeanStatusException e) { threadLocal.set(threadLocal.get() + 1); throwable = e; Thread.sleep(200L); } } throw throwable; }
Example #10
Source File: CglibProxy.java From retry with Apache License 2.0 | 6 votes |
@Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { int times = 0; while (times < RetryConstant.MAX_TIMES) { try { //通过代理子类调用父类的方法 return methodProxy.invokeSuper(o, objects); } catch (Exception e) { times++; if (times >= RetryConstant.MAX_TIMES) { throw new RuntimeException(e); } } } return null; }
Example #11
Source File: NodeDelegate.java From tddl5 with Apache License 2.0 | 5 votes |
@Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { ASTNode node = (ASTNode) obj; String name = method.getName(); if (name.equals("getDataNode")) { return node.getDataNode(shareIndex); } else if (name.equals("executeOn")) { return node.executeOn((String) args[0], shareIndex); } else if (name.equals("getExtra")) { return node.getExtra(shareIndex); } else if (name.equals("setExtra")) { return node.setExtra(args[0], shareIndex); } else if (name.equals("toDataNodeExecutor")) { return node.toDataNodeExecutor(shareIndex); } else if (name.equals("getActualTableName")) { return ((TableNode) node).getActualTableName(shareIndex); } else if (name.equals("setActualTableName")) { return ((TableNode) node).setActualTableName((String) args[0], shareIndex); } else if (name.equals("toString")) { if (args.length == 0) { return node.toString(0, shareIndex); } else { return node.toString((Integer) args[0], shareIndex); } } else { throw new OptimizerException("impossible proxy method : " + name); } }
Example #12
Source File: DefaultProxyFactory.java From ymate-platform-v2 with Apache License 2.0 | 5 votes |
@Override @SuppressWarnings("unchecked") public <T> T createProxy(final Class<?> targetClass, final List<IProxy> proxies) { return (T) Enhancer.create(targetClass, new MethodInterceptor() { @Override public Object intercept(Object targetObject, Method targetMethod, Object[] methodParams, final MethodProxy methodProxy) throws Throwable { return new AbstractProxyChain(DefaultProxyFactory.this, targetClass, targetObject, targetMethod, methodParams, proxies) { @Override protected Object doInvoke() throws Throwable { return methodProxy.invokeSuper(getTargetObject(), getMethodParams()); } }.doProxyChain(); } }); }
Example #13
Source File: RenderMointorIntercetor.java From gecco with MIT License | 5 votes |
@Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if(method.getName().equals("inject")) { try { Object o = proxy.invokeSuper(obj, args); return o; } catch(RenderException ex) { RenderMonitor.incrException(ex.getSpiderBeanClass().getName()); throw ex; } } else { return proxy.invokeSuper(obj, args); } }
Example #14
Source File: SeiFactoryImpl.java From tomee with Apache License 2.0 | 5 votes |
void initialize(Object serviceImpl, ClassLoader classLoader) throws ClassNotFoundException { this.serviceImpl = serviceImpl; Class serviceEndpointBaseClass = classLoader.loadClass(serviceEndpointClassName); serviceEndpointClass = enhanceServiceEndpointInterface(serviceEndpointBaseClass, classLoader); Class[] constructorTypes = new Class[]{classLoader.loadClass(GenericServiceEndpoint.class.getName())}; this.constructor = FastClass.create(serviceEndpointClass).getConstructor(constructorTypes); this.handlerInfoChainFactory = new HandlerInfoChainFactory(handlerInfos); sortedOperationInfos = new OperationInfo[FastClass.create(serviceEndpointClass).getMaxIndex() + 1]; String encodingStyle = ""; for (int i = 0; i < operationInfos.length; i++) { OperationInfo operationInfo = operationInfos[i]; Signature signature = operationInfo.getSignature(); MethodProxy methodProxy = MethodProxy.find(serviceEndpointClass, signature); if (methodProxy == null) { throw new ServerRuntimeException("No method proxy for operationInfo " + signature); } int index = methodProxy.getSuperIndex(); sortedOperationInfos[index] = operationInfo; if (operationInfo.getOperationDesc().getUse() == Use.ENCODED) { encodingStyle = org.apache.axis.Constants.URI_SOAP11_ENC; } } //register our type descriptors Service service = ((ServiceImpl) serviceImpl).getService(); AxisEngine axisEngine = service.getEngine(); TypeMappingRegistry typeMappingRegistry = axisEngine.getTypeMappingRegistry(); TypeMapping typeMapping = typeMappingRegistry.getOrMakeTypeMapping(encodingStyle); typeMapping.register(BigInteger.class, Constants.XSD_UNSIGNEDLONG, new SimpleSerializerFactory(BigInteger.class, Constants.XSD_UNSIGNEDLONG), new SimpleDeserializerFactory(BigInteger.class, Constants.XSD_UNSIGNEDLONG)); typeMapping.register(URI.class, Constants.XSD_ANYURI, new SimpleSerializerFactory(URI.class, Constants.XSD_ANYURI), new SimpleDeserializerFactory(URI.class, Constants.XSD_ANYURI)); //It is essential that the types be registered before the typeInfos create the serializer/deserializers. for (Iterator iter = typeInfo.iterator(); iter.hasNext(); ) { TypeInfo info = (TypeInfo) iter.next(); TypeDesc.registerTypeDescForClass(info.getClazz(), info.buildTypeDesc()); } TypeInfo.register(typeInfo, typeMapping); }
Example #15
Source File: JedisCglibProxy.java From limiter with MIT License | 5 votes |
@Override public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { Jedis jedis = null; init(); try { jedis = getJedis(); return method.invoke(jedis, args); } finally { if (null != jedis) { jedis.close(); } } }
Example #16
Source File: ProxyChain.java From smart-framework with Apache License 2.0 | 5 votes |
public ProxyChain(Class<?> targetClass, Object targetObject, Method targetMethod, MethodProxy methodProxy, Object[] methodParams, List<Proxy> proxyList) { this.targetClass = targetClass; this.targetObject = targetObject; this.targetMethod = targetMethod; this.methodProxy = methodProxy; this.methodParams = methodParams; this.proxyList = proxyList; }
Example #17
Source File: ProxyManager.java From smart-framework with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public static <T> T createProxy(final Class<?> targetClass, final List<Proxy> proxyList) { return (T) Enhancer.create(targetClass, new MethodInterceptor() { @Override public Object intercept(Object targetObject, Method targetMethod, Object[] methodParams, MethodProxy methodProxy) throws Throwable { return new ProxyChain(targetClass, targetObject, targetMethod, methodProxy, methodParams, proxyList).doProxyChain(); } }); }
Example #18
Source File: TransactionInterceptor.java From snakerflow with Apache License 2.0 | 5 votes |
/** * 方法执行的拦截器,拦截条件为: * 1、数据库访问类是DBTransaction的实现类 * 2、方法名称匹配初始化的事务方法列表 */ public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { Object result = null; TransactionStatus status = null; if(isMatch(method.getName())) { if(log.isDebugEnabled()) { log.debug("intercept method is[name=" + method.getName() + "]"); } try { status = getTransaction(); AssertHelper.notNull(status); //调用具体无事务支持的业务逻辑 result = proxy.invokeSuper(obj, args); //如果整个执行过程无异常抛出,则提交TransactionStatus持有的transaction对象 if(status.isNewTransaction()) { commit(status); } } catch (Exception e) { rollback(status); throw new SnakerException(e); } } else { if(log.isDebugEnabled()) { log.debug("****don't intercept method is[name=" + method.getName() + "]"); } result = proxy.invokeSuper(obj, args); } return result; }
Example #19
Source File: CglibProxyFactory.java From mybaties with Apache License 2.0 | 5 votes |
@Override public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { final String methodName = method.getName(); try { synchronized (lazyLoader) { if (WRITE_REPLACE_METHOD.equals(methodName)) { Object original = null; if (constructorArgTypes.isEmpty()) { original = objectFactory.create(type); } else { original = objectFactory.create(type, constructorArgTypes, constructorArgs); } PropertyCopier.copyBeanProperties(type, enhanced, original); if (lazyLoader.size() > 0) { return new CglibSerialStateHolder(original, lazyLoader.getProperties(), objectFactory, constructorArgTypes, constructorArgs); } else { return original; } } else { //这里是关键,延迟加载就是调用ResultLoaderMap.loadAll() if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) { if (aggressive || lazyLoadTriggerMethods.contains(methodName)) { lazyLoader.loadAll(); } else if (PropertyNamer.isProperty(methodName)) { //或者调用ResultLoaderMap.load() final String property = PropertyNamer.methodToProperty(methodName); if (lazyLoader.hasLoader(property)) { lazyLoader.load(property); } } } } } return methodProxy.invokeSuper(enhanced, args); } catch (Throwable t) { throw ExceptionUtil.unwrapThrowable(t); } }
Example #20
Source File: InterceptorStackCallback.java From businessworks with Apache License 2.0 | 5 votes |
public InterceptedMethodInvocation(Object proxy, MethodProxy methodProxy, Object[] arguments, int index) { this.proxy = proxy; this.methodProxy = methodProxy; this.arguments = arguments; this.index = index; }
Example #21
Source File: CGLibProxy.java From thinking-in-spring with Apache License 2.0 | 5 votes |
@Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { Object obj = null; // 过滤方法 if ("addUser".equals(method.getName())) { // 检查权限 checkPopedom(); } obj = method.invoke(targetObject, args); return obj; }
Example #22
Source File: MyMethodInterceptor.java From thinking-in-spring with Apache License 2.0 | 5 votes |
/** * sub:cglib生成的代理对象 * method:被代理对象方法 * objects:方法入参 * methodProxy: 代理方法 */ @Override public Object intercept(Object sub, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println("======插入前置通知======"); Object object = methodProxy.invokeSuper(sub, objects); System.out.println("======插入后者通知======"); return object; }
Example #23
Source File: Cglib2AopProxy.java From tiny-spring with Apache License 2.0 | 5 votes |
@Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if (advised.getMethodMatcher() == null || advised.getMethodMatcher().matches(method, advised.getTargetSource().getTargetClass())) { return delegateMethodInterceptor.invoke(new CglibMethodInvocation(advised.getTargetSource().getTarget(), method, args, proxy)); } return new CglibMethodInvocation(advised.getTargetSource().getTarget(), method, args, proxy).proceed(); }
Example #24
Source File: BeanMethodInterceptorChain.java From nuls with MIT License | 5 votes |
/** * 将一个方法放入该拦截器链中执行,获取返回结果 * Puts a method in the interceptor chain to retrieve the returned result. * * @param annotation 拦截方法的注解实例/Annotation instances of the intercepting method. * @param object 方法所属对象/Method owner * @param method 方法定义/Method definition * @param params 方法参数列表/Method parameter list * @param methodProxy 方法代理器 * @return 返回拦截的方法的返回值,可以对该值进行处理和替换/Returns the return value of the intercepting method, which can be processed and replaced. * @throws Throwable 该方法可能抛出异常,请谨慎处理/This method may throw an exception, handle with care. */ public Object startInterceptor(Annotation annotation, Object object, Method method, Object[] params, MethodProxy methodProxy) throws Throwable { methodProxyThreadLocal.set(methodProxy); index.set(-1); Object result = null; try { result = execute(annotation, object, method, params); } finally { index.remove(); methodProxyThreadLocal.remove(); } return result; }
Example #25
Source File: ServiceMethodInterceptor.java From tomee with Apache License 2.0 | 5 votes |
/** * * @param o Object * @param method Method * @param objects Object[] * @param methodProxy MethodProxy * @return Object * @throws Throwable */ @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { if (objects.length == 0) { String methodName = method.getName(); if (methodName.startsWith("get")) { String portName = methodName.substring(3); SeiFactory seiFactory = (SeiFactory) seiFactoryMap.get(portName); if (seiFactory != null) { return seiFactory.createServiceEndpoint(); } } } throw new ServiceException("Unrecognized method name or argument list: " + method.getName()); }
Example #26
Source File: StandardJaxrsActionProxy.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
/** * 根据调用方法的注释判断该类的操作是否需要记录到审计日志 * 如果需要记录,则执行审计日志记录方法 * @param o * @param method * @param objects * @param methodProxy * @throws ClassNotFoundException */ private void tryToRecordAuditLog(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws ClassNotFoundException { //分析调用过程,记录审计日志 Annotation[] annotations_auditLog = method.getAnnotationsByType(AuditLog.class); //该方法是否有AuditLog注解,如果有,则需要记录审计日志 if( ArrayUtils.isNotEmpty( annotations_auditLog )){ //获取操作名称,首选AuditLog的value属性属性,如果没有,则选择JaxrsMethodDescribe的value属性 String operationName = getOperationName( method, annotations_auditLog ); //有AuditLog注解,说明需要记录审计日志 doRecordAuditLog( method, objects, operationName ); } }
Example #27
Source File: StandardJaxrsActionProxy.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
@Override public Object intercept( Object o, Method method, Object[] objects, MethodProxy methodProxy ) throws Throwable { Object result = methodProxy.invokeSuper(o, objects); try{ //尝试记录审计日志 if( Config.logLevel().audit().enable() ){ tryToRecordAuditLog( o, method, objects, methodProxy ); } }catch(Exception e){ e.printStackTrace(); } return result; }
Example #28
Source File: CachedMessagePriorityDaoInterceptor.java From hermes with Apache License 2.0 | 5 votes |
@Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if ("findIdAfter".equals(method.getName())) { String topic = (String) args[0]; if (m_config.shouldCache(topic)) { int partition = (int) args[1]; boolean isPriority = ((int) args[2]) == 0 ? true : false; MessageCache<MessagePriority> cache = isPriority ? m_priorityMessageCache : m_nonpriorityMessageCache; return cache.getOffsetAfter(topic, partition, (long) args[3], (int) args[4]); } } return method.invoke(m_target, args); }
Example #29
Source File: Cglib2AopProxy.java From tiny-spring with Apache License 2.0 | 5 votes |
@Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { // 如果advised.getMethodMatcher()为空(编程式的使用aop,例如:Cglib2AopProxyTest.java),拦截该类的所有方法 // 如果有方法匹配器(声明式的在xml文件里配置了AOP)并且匹配方法成功就拦截指定的方法 if (advised.getMethodMatcher() == null || advised.getMethodMatcher().matches(method, advised.getTargetSource().getTargetClass())) { // delegateMethodInterceptor通过advised.getMethodInterceptor()得到用户写的方法拦截器 // 返回去调用用户写的拦截器的invoke方法(用户根据需要在调用proceed方法前后添加相应行为,例如:TimerInterceptor.java) return delegateMethodInterceptor.invoke(new CglibMethodInvocation(advised.getTargetSource().getTarget(), method, args, proxy)); } // 有AspectJ表达式,但没有匹配该方法,返回通过methodProxy调用原始对象的该方法 return new CglibMethodInvocation(advised.getTargetSource().getTarget(), method, args, proxy).proceed(); }
Example #30
Source File: MetaSpaceOOM.java From Java-Interview with MIT License | 5 votes |
public static void main(String[] args) { while (true){ Enhancer enhancer = new Enhancer() ; enhancer.setSuperclass(HeapOOM.class); enhancer.setUseCache(false) ; enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { return methodProxy.invoke(o,objects) ; } }); enhancer.create() ; } }