public static Object createProxy(Object realObject) { try { MethodInterceptor interceptor = new HammerKiller(); Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(realObject.getClass()); enhancer.setCallbackType(interceptor.getClass()); Class classForProxy = enhancer.createClass(); Enhancer.registerCallbacks(classForProxy, new Callback[]{interceptor}); Object createdProxy = classForProxy.newInstance(); for (Field realField : FieldUtils.getAllFieldsList(realObject.getClass())) { if (Modifier.isStatic(realField.getModifiers())) continue; realField.setAccessible(true); realField.set(createdProxy, realField.get(realObject)); } CreeperKiller.LOG.info("Removed HammerCore main menu hook, ads will no longer be displayed."); return createdProxy; } catch (Exception e) { CreeperKiller.LOG.error("Failed to create a proxy for HammerCore ads, they will not be removed.", e); } return realObject; }
ReflectiveParserImpl(Class<?> base, List<Property<?, ?>> properties) { InjectionChecks.checkInjectableCGLibProxyBase(base); this.properties = properties; this.propertyNames = properties.stream() .flatMap(property -> property.parser.names().stream()) .collect(Collectors.toImmutableSet()); final Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(base); enhancer.setInterfaces(new Class[]{ type.getRawType() }); enhancer.setCallbackType(MethodInterceptor.class); enhancer.setUseFactory(true); this.impl = enhancer.createClass(); this.injector = getMembersInjector((Class<T>) impl); }
/** * Adds transaction support to the page. The transaction support captures execution time of methods annotated with * {@link io.devcon5.pageobjects.tx.Transaction} * * @param <T> * * @param transactionSupport * the transactionSupport element to be enhanced. * @return */ @SuppressWarnings("unchecked") public static <T extends TransactionSupport> T addTransactionSupport(TransactionSupport transactionSupport) { return (T) Enhancer.create(transactionSupport.getClass(), (MethodInterceptor) (obj, method, args, proxy) -> { final Optional<String> txName = getTxName(transactionSupport, method); try { txName.ifPresent(transactionSupport::txBegin); Object result = method.invoke(transactionSupport, args); //dynamically enhance return values, if they are transactionSupport and not yet enhanced //this is required, i.e. if method return 'this' or create new objects which will //not be enhanced if (!isCGLibProxy(result) && result instanceof TransactionSupport) { result = addTransactionSupport(transactionSupport); } return result; } finally { txName.ifPresent(transactionSupport::txEnd); } }); }
public static void main(String[] args) { while(true) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(ClassPermGenOOM.class); enhancer.setUseCache(Boolean.FALSE); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable { return arg3.invokeSuper(arg0, arg2); } }); enhancer.create(); } }
public static void main(String[] args) { Tmp tmp = new Tmp(); while (!Thread.interrupted()) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(Tmp.class); enhancer.setUseCache(false); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable { return arg3.invokeSuper(arg0, arg2); } }); enhancer.create(); } System.out.println(tmp.hashCode()); }
/** * Wraps the specified API object to dump caller stacktraces right before invoking * native methods * * @param api API * @return wrapped API */ static <T> T wrapWithCrashStackLogging(final Class<T> apiClazz, final T api) { try { return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() { @Override public T run() throws Exception { MethodInterceptor handler = new MethodInterceptorWithStacktraceLogging<T>(api); T wrapperWithStacktraceLogging = (T) Enhancer.create(apiClazz, handler); return wrapperWithStacktraceLogging; } }); } catch (PrivilegedActionException e) { e.printStackTrace(); return api; } }
@SuppressWarnings("unchecked") public T decode(final BitBuffer buffer, final Resolver resolver, final Builder builder) throws DecodingException { final int size = wrapped.getSize().eval(resolver); final long pos = buffer.getBitPos(); ClassLoader loader = this.getClass().getClassLoader(); Enhancer enhancer = new Enhancer(); enhancer.setClassLoader(loader); enhancer.setSuperclass(type); enhancer.setCallback(new MethodInterceptor() { private Object actual; public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable { if (actual == null) { buffer.setBitPos(pos); actual = wrapped.decode(buffer, resolver, builder); } return proxy.invoke(actual, args); } }); buffer.setBitPos(pos + size); return (T) enhancer.create(); }
@SuppressWarnings("unchecked") protected static <T> Class<T> createEnhancedClass(Class<T> proxiedClass, Class<?>... implementedInterfaces) { Enhancer enhancer = new Enhancer(); Set<Class<?>> interfaces = new HashSet<Class<?>>(); if (proxiedClass.isInterface()) { enhancer.setSuperclass(Object.class); interfaces.add(proxiedClass); } else { enhancer.setSuperclass(proxiedClass); } if (implementedInterfaces != null && implementedInterfaces.length > 0) { interfaces.addAll(asList(implementedInterfaces)); } if (!interfaces.isEmpty()) { enhancer.setInterfaces(interfaces.toArray(new Class<?>[interfaces.size()])); } enhancer.setCallbackType(MethodInterceptor.class); enhancer.setUseFactory(true); return enhancer.createClass(); }
@Override public <T> T createDynamicProxy(Class<T> type, Supplier<T> targetSupplier, String descriptionPattern, Object... descriptionParams) { final String description = String.format(descriptionPattern, descriptionParams); final Enhancer enhancer = new Enhancer(); enhancer.setClassLoader(new DelegatingClassLoader(type.getClassLoader(), Enhancer.class.getClassLoader())); enhancer.setInterfaces(new Class[]{type}); enhancer.setSuperclass(Object.class); enhancer.setCallbackFilter(FILTER); enhancer.setCallbacks(new Callback[]{ (Dispatcher) targetSupplier::get, (MethodInterceptor) (proxy, method, params, methodProxy) -> proxy == params[0], (MethodInterceptor) (proxy, method, params, methodProxy) -> System.identityHashCode(proxy), (MethodInterceptor) (proxy, method, params, methodProxy) -> description }); return type.cast(enhancer.create()); }
@SuppressWarnings("unchecked") public T bind() { if (__bound == null) { __bound = (T) ClassUtils.wrapper(__source).duplicate(Enhancer.create(__targetClass, new MethodInterceptor() { public Object intercept(Object targetObject, Method targetMethod, Object[] methodParams, MethodProxy methodProxy) throws Throwable { Object _result = methodProxy.invokeSuper(targetObject, methodParams); PropertyStateMeta _meta = __propertyStates.get(targetMethod.getName()); if (_meta != null && ArrayUtils.isNotEmpty(methodParams) && !ObjectUtils.equals(_meta.getOriginalValue(), methodParams[0])) { _meta.setNewValue(methodParams[0]); } return _result; } })); } return __bound; }
public void testMe(){ Article a = (Article) Enhancer.create(Article.class, new Class[]{DynamicUpdatable.class}, null, new Callback[]{ new MethodInterceptor(){ public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { proxy.invokeSuper(obj, args) ; System.out.println(method.getName()) ; return null; } } }) ; assertTrue(a instanceof DynamicUpdatable) ; a.getContent() ; }
@Override protected void decorateInstance(T result, final MethodIntercept methodIntercept) { MethodInterceptor methodInterceptor = new MethodInterceptor() { public Object intercept(final Object proxy, Method method, Object[] args, final MethodProxy superProxy) throws Throwable { return methodIntercept.intercept(proxy, new MethodAdapter(method), args, new MethodIntercept.SuperInvoker() { public Object invokeSuper(Object[] superArgs) throws Throwable { return superProxy.invokeSuper(proxy, superArgs); } }); } }; Factory factory = (Factory) result; int callbackCount = factory.getCallbacks().length; for(int i = 0; i < callbackCount; i++) { factory.setCallback(i, methodInterceptor); } }
/** * Creates a dynamic proxy * * @param interceptor The interceptor that manages the invocations to the created proxy * @param clazz The class to be proxied * @param failSafe If true return null if it is not possible to proxy the request class, otherwise throws an UnproxableClassException * @param implementedInterface The interfaces that has to be implemented by the new proxy * @return The newly created proxy */ public static <I extends MethodInterceptor & InvocationHandler, T> T createProxy(I interceptor, Class<T> clazz, boolean failSafe, Class<?>... implementedInterface) { if (clazz.isInterface()) return (T) createNativeJavaProxy(clazz.getClassLoader(), interceptor, concatClasses(new Class<?>[]{clazz}, implementedInterface)); RuntimeException e; try { return (T) createEnhancer(interceptor, clazz, implementedInterface).create(); } catch (RuntimeException ex) { e = ex; } if (Proxy.isProxyClass(clazz)) return (T) createNativeJavaProxy(clazz.getClassLoader(), interceptor, concatClasses(implementedInterface, clazz.getInterfaces())); if (isProxable(clazz)) return ClassImposterizer.INSTANCE.imposterise(interceptor, clazz, implementedInterface); if (failSafe) return null; throw e; }
private static Object newProxyByCglib(Typing typing, Handler handler) { Enhancer enhancer = new Enhancer() { /** includes all constructors */ protected void filterConstructors(Class sc, List constructors) {} }; enhancer.setClassLoader(Thread.currentThread().getContextClassLoader()); enhancer.setUseFactory(true); enhancer.setSuperclass(typing.superclass); enhancer.setInterfaces(typing.interfaces.toArray(new Class[0])); enhancer.setCallbackTypes(new Class[] { MethodInterceptor.class, NoOp.class }); enhancer.setCallbackFilter(new CallbackFilter() { /** ignores bridge methods */ public int accept(Method method) { return method.isBridge() ? 1 : 0; } }); Class<?> proxyClass = enhancer.createClass(); Factory proxy = (Factory) new ObjenesisStd().newInstance(proxyClass); proxy.setCallbacks(new Callback[] { asMethodInterceptor(handler), new SerializableNoOp() }); return proxy; }
/** * 根据目标类targetClass和代理链proxyList,生成代理对象 * @param targetClass * @param proxyList * @return */ @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(); } }); }
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() ; } }
public static final <T> T createProxy(Class<T> clazz,MethodInterceptor interceptor) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(clazz); enhancer.setCallback(new NulsMethodInterceptor(interceptor)); return (T) enhancer.create(); }
@Override public <T, D extends Decorator<T>> DecoratorGenerator.Meta<T, D> implement(Class<T> type, Class<D> decorator) { final Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(decorator); enhancer.setCallbackType(MethodInterceptor.class); final Class<? extends D> impl = enhancer.createClass(); enhancer.setCallback(new Callback<>(type, decorator)); return new CGMeta(type, decorator, impl, enhancer); }
@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(); } }); }
public static <T> T createProxy(final Class<?> targetClass, final List<Proxy> proxyList){ return (T) Enhancer.create(targetClass, new MethodInterceptor() { public Object intercept(Object targetObject, Method targetMethod, Object[] methodParams, MethodProxy methodProxy) throws Throwable { return new ProxyChain(targetClass,targetObject,targetMethod ,methodProxy,methodParams,proxyList).doProxyChain(); } }); }
@SuppressWarnings("unchecked") public <T> T createProxy(final Class<?> targetClass, final List<Proxy> proxyList) { return (T) Enhancer.create(targetClass, new MethodInterceptor() { @Override public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable { ProxyChain proxyChain = new ProxyChain(targetClass, target, method, args, proxy, proxyList); return proxyChain.doProxyChain(); } }); }
@SuppressWarnings("unchecked") public static <T> T createProxy(Class<T> targetClass) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(targetClass); enhancer.setUseCache(false); enhancer.setCallback(new MethodInterceptor() { public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { System.out.println("方法调用前 先执行我。。。。。"); Object obj = methodProxy.invokeSuper(object, args); System.out.println("方法调用后 再执行我。。。。。"); return obj; } }); return (T)enhancer.create(); }
public static <S, W> W createWrapper(final S source, final Class<W> wrapperClass) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(wrapperClass); Set<Class> allInterfaces = new HashSet<Class>(); allInterfaces.addAll(Arrays.asList(source.getClass().getInterfaces())); allInterfaces.addAll(Arrays.asList(wrapperClass.getInterfaces())); enhancer.setInterfaces(allInterfaces.toArray(new Class[allInterfaces.size()])); // enhancer.setInterfaces(wrapperClass.getInterfaces()); enhancer.setCallback(new MethodInterceptor() { final Set<Method> wrapperClassDeclaredMethods = ImmutableSet.copyOf(wrapperClass.getDeclaredMethods()); public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { if ("getSource".equals(method.getName()) && Wrapper.class.equals(method.getDeclaringClass())) { return source; } // if (!Modifier.isAbstract(method.getModifiers())) { // return methodProxy.invokeSuper(proxy, args); // } if (wrapperClassDeclaredMethods.contains(method)) { return methodProxy.invokeSuper(proxy, args); } return methodProxy.invoke(source, args); } }); return (W) enhancer.create(); }
/** * 在某个类的前后增加代理方法 * @author Administrator * @param t * @param before * @param after * @return */ @SuppressWarnings("unchecked") public static <T> T proxySurround(T t, CustomMethod before, CustomMethod after) { MethodInterceptor methodInterceptor = new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { before.execute(obj, method, args); Object result = null; try { result = proxy.invokeSuper(obj, args); } finally { after.execute(obj, method, args); } return result; } }; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(t.getClass()); // 回调方法 enhancer.setCallback(methodInterceptor); // 创建代理对象 return (T)enhancer.create(); }
/** * 代理某一个对象指定方法,并在这个方法执行前后加入新方法,可指定过滤掉非代理的方法 * @author nan.li * @param t * @param before 执行目标方法前要执行的方法 * @param after 执行目标方法后要执行的方法 * @return */ @SuppressWarnings("unchecked") public static <T> T proxySurround(T t, CustomMethod before, CustomMethod after, CallbackFilter callbackFilter) { MethodInterceptor methodInterceptor = new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { before.execute(obj, method, args); Object result = null; try { result = proxy.invokeSuper(obj, args); } finally { after.execute(obj, method, args); } return result; } }; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(t.getClass()); // 回调方法 // enhancer.setCallback(methodInterceptor); enhancer.setCallbacks(new Callback[] {methodInterceptor, NoOp.INSTANCE}); //NoOp回调把对方法的调用直接委派到这个方法在父类中的实现。 //Methods using this Enhancer callback ( NoOp.INSTANCE ) will delegate directly to the default (super) implementation in the base class. //setCallbacks中定义了所使用的拦截器,其中NoOp.INSTANCE是CGlib所提供的实际是一个没有任何操作的拦截器, 他们是有序的。一定要和CallbackFilter里面的顺序一致。 enhancer.setCallbackFilter(callbackFilter); // 创建代理对象 return (T)enhancer.create(); }
/** * 根据权限验证来设置代理 * @author Administrator * @param t * @param checkPrivilege * @return */ @SuppressWarnings("unchecked") public static <T> T proxyByPrivilege(T t, CustomMethodWithRet checkPrivilege) { MethodInterceptor methodInterceptor = new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { boolean checkResult = checkPrivilege.execute(obj, method, args);//权限验证的结果 if (checkResult) { //验证通过,才执行这个方法 Object result = null; result = proxy.invokeSuper(obj, args); return result; } else { return null; } } }; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(t.getClass()); // 回调方法 enhancer.setCallback(methodInterceptor); // 创建代理对象 return (T)enhancer.create(); }
@SuppressWarnings("unchecked") public static <T> T createProxy(Class<?> targetClass, List<Proxy> proxyList) { return (T)Enhancer.create(targetClass, new MethodInterceptor() { @Override public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable { ProxyChain proxyChain = new ProxyChain(targetClass, target, method, args, proxy, proxyList); proxyChain.doProxyChain(); return proxyChain.getMethodResult(); } }); }
public T getTarget() { return (T) Enhancer.create(_targetObject.getClass(), new MethodInterceptor() { @Override public Object intercept(final Object arg0, final Method arg1, final Object[] arg2, final MethodProxy arg3) throws Throwable { _callbackMethod = arg1; return null; } }); }
public static <T> T createProxy(final Class<?> targetClass,final List<Proxy> proxyList){ return (T) Enhancer.create(targetClass, new MethodInterceptor() { public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { return new ProxyChain(targetClass, obj, method, proxy, args, proxyList).doProxyChain(); } }); }
private HttpServletRequest getSuperPoweredHttpRequest(HttpServletRequest httpServletRequest) { if (isSuperPowered(httpServletRequest)) { return httpServletRequest; } Enhancer enhancer = new Enhancer(); enhancer.setInterfaces(new Class[]{HttpServletRequest.class, SuperPoweredHttpServletRequest.class}); enhancer.setCallback(new MethodInterceptor() { private String contextPath; private String pathInfo; @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws InvocationTargetException, IllegalAccessException { if ("setContextPath".equals(method.getName())) { contextPath = (String) objects[0]; return null; } if ("getContextPath".equals(method.getName())) { return contextPath; } if ("setPathInfo".equals(method.getName())) { pathInfo = (String) objects[0]; return null; } if ("getPathInfo".equals(method.getName())) { return pathInfo; } return method.invoke(httpServletRequest, objects); } }); return (HttpServletRequest) enhancer.create(); }
private static void testCglibCreation() { long start = System.currentTimeMillis(); for (int i = 0; i < creation; i++) { MethodInterceptor methodInterceptor = new SubjectInterceptor(); Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(ConcreteSubject.class); enhancer.setCallback(methodInterceptor); enhancer.create(); } long stop = System.currentTimeMillis(); LOG.info("cglib creation time : {} ms", stop - start); }
private static void testCglibExecution() { MethodInterceptor methodInterceptor = new SubjectInterceptor(); Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(ConcreteSubject.class); enhancer.setCallback(methodInterceptor); ISubject subject = (ISubject) enhancer.create(); long start = System.currentTimeMillis(); for (int i = 0; i < execution; i++) { subject.action(); } long stop = System.currentTimeMillis(); LOG.info("cglib execution time : {} ms", stop - start); }
public static void main(String[] args) { MethodInterceptor methodInterceptor = new SubjectInterceptor(); Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(ConcreteSubject.class); enhancer.setCallback(methodInterceptor); ISubject subject = (ISubject)enhancer.create(); subject.action(); }
@SuppressWarnings("unchecked") public <T> T createProxy(final Class<?> targetClass, final List<IProxy> proxies) { final IProxyFactory _owner = this; return (T) Enhancer.create(targetClass, new MethodInterceptor() { public Object intercept(Object targetObject, Method targetMethod, Object[] methodParams, MethodProxy methodProxy) throws Throwable { return new DefaultProxyChain(_owner, targetClass, targetObject, targetMethod, methodProxy, methodParams, proxies).doProxyChain(); } }); }
/** * An instance of the supplied controller class is returned. As soon as an * action method is called on the returned instance, the invoked invocation * is appended to the path. */ protected @SuppressWarnings("unchecked") <T> T createActionPath(final Class<T> controllerClass) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(controllerClass); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object obj, Method thisMethod, Object[] args, MethodProxy proxy) throws Throwable { if (!util.isActionMethod(thisMethod)) { // collect methods for error message ArrayList<String> methods = new ArrayList<>(); for (Method method : controllerClass.getMethods()) { if (util.isActionMethod(method)) { methods.add(method.toString()); } } throw new RuntimeException("The method " + thisMethod.getName() + " wich is no action method has been called on a controller of type " + controllerClass.getName() + " while generating an ActionPath. Available Methods:\n" + Joiner.on("\n").join(methods)); } // create invocation MethodInvocation<Object> methodInvocation = new MethodInvocation<>(controllerClass, thisMethod); methodInvocation.getArguments().addAll(Arrays.asList(args)); invocation.methodInvocation = methodInvocation; return invocation; } }); try { return (T) enhancer.create(); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Patch tabsPainter * * @param component */ private void patchPainter(final JBEditorTabs component) { final JBEditorTabsPainter painter = ReflectionUtil.getField(JBEditorTabs.class, component, JBEditorTabsPainter.class, "myDarkPainter"); if (painter instanceof MTTabsPainter) { return; } final MTTabsPainter tabsPainter = new MTTabsPainter(component); final JBEditorTabsPainter proxy = (MTTabsPainter) Enhancer.create(MTTabsPainter.class, (MethodInterceptor) (o, method, objects, methodProxy) -> { final Object result = method.invoke(tabsPainter, objects); final Color defaultColor = theme.getBorderColor(); // Custom props final boolean isColorEnabled = config.isHighlightColorEnabled(); final Color borderColor = isColorEnabled ? config.getHighlightColor() : defaultColor; final int borderThickness = config.getHighlightThickness(); if ("paintSelectionAndBorder".equals(method.getName())) { paintSelectionAndBorder(objects, borderColor, borderThickness, tabsPainter); } return result; }); ReflectionUtil.setField(JBEditorTabs.class, component, JBEditorTabsPainter.class, "myDarkPainter", proxy); }
/** * Creates new instance of fixture. * @param clazz class to instantiate. * @param constructorTypes types of arguments used to determine which constructor to use. * @param constructorArgs arguments to pass to constructor of clazz. * @param <T> type to create. * @return instance of clazz (subclass, actually) that will have #aroundSlimInvoke() invoked on each method call. */ public <T extends InteractionAwareFixture> T create(Class<T> clazz, Class<?>[] constructorTypes, Object[] constructorArgs) { MethodInterceptor callback = createCallback(); T result; if (FACTORIES.containsKey(clazz)) { Factory factory = FACTORIES.get(clazz); result = createUsingFactory(callback, factory, constructorTypes, constructorArgs); } else { result = createFirst(callback, clazz, constructorTypes, constructorArgs); FACTORIES.put(clazz, (Factory) result); } return result; }
@SuppressWarnings("unchecked") public <T> T getEnhancedProxy(final Class<T> requiredClazz, final Class<?>[] argTypes, final Object[] args, final MethodInterceptor interceptor) { final Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(requiredClazz); enhancer.setCallback(interceptor); return (T) enhancer.create(argTypes, args); }
private static MethodInterceptor newMethodInterceptor(Class<?> clazz, PatchTree patchTree, PathSpec pathSpec) { if (RecordTemplate.class.isAssignableFrom(clazz)) return new GeneratePatchMethodInterceptor(clazz, (RecordDataSchema) DataTemplateUtil.getSchema(clazz), pathSpec, patchTree); throw new IllegalArgumentException("Attempted to proxy unsupported class " + clazz.getCanonicalName() + " with a PathSpec of " + pathSpec + "! Only RecordTemplate subclasses can be proxied!"); }
private Callback interceptor(final Supplier<S> current) { return new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { return method.invoke(current.get(), args); } }; }