当前位置: 首页>>代码示例>>Java>>正文


Java Enhancer类代码示例

本文整理汇总了Java中net.sf.cglib.proxy.Enhancer的典型用法代码示例。如果您正苦于以下问题:Java Enhancer类的具体用法?Java Enhancer怎么用?Java Enhancer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Enhancer类属于net.sf.cglib.proxy包,在下文中一共展示了Enhancer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: newProxyInstance

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
/**
 * 生成代理对象
 * @param targetClass  被代理对象的类型(类或接口)
 * @param target       被代理对象实例
 * @return             代理对象
 */
@SuppressWarnings("unchecked")
public <T> T newProxyInstance(final Class<T> targetClass, final Object target) {
    return (T) Enhancer.create(targetClass, new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            before(targetClass, method, args);
            Object ret = null;
            try {
                ret = method.invoke(target, args);
            } catch (Exception e) {
                exception(targetClass, method, args, e);
            }
            after(targetClass, method, args);
            return ret;
        }
    });
}
 
开发者ID:huhuics,项目名称:tauren,代码行数:24,代码来源:ProxyInterceptor.java

示例2: newProxyInstance

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public <T> T newProxyInstance(final Class<T> targetClass, final Object target) {
    return (T) Enhancer.create(targetClass, new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

            /**
             * 如果没有@Transaction,则直接调用原方法
             */
            if (!method.isAnnotationPresent(Transaction.class)) {
                return method.invoke(target, args);
            }

            before(targetClass, method, args);
            Object ret = null;
            try {
                ret = method.invoke(target, args);
            } catch (Exception e) {
                exception(targetClass, method, args, e);
            }
            after(targetClass, method, args);
            return ret;
        }
    });
}
 
开发者ID:huhuics,项目名称:tauren,代码行数:27,代码来源:TransactionPartialInterceptor.java

示例3: ReflectiveParserImpl

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
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);
}
 
开发者ID:OvercastNetwork,项目名称:ProjectAres,代码行数:17,代码来源:ReflectiveParserManifest.java

示例4: postProcessBeforeInstantiation

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
@Override
public Object postProcessBeforeInstantiation(final Class<?> beanClass, final String beanName) throws BeansException {
    if (_interceptors != null && _interceptors.size() > 0) {
        if (ComponentMethodInterceptable.class.isAssignableFrom(beanClass)) {
            final Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(beanClass);
            enhancer.setCallbacks(getCallbacks());
            enhancer.setCallbackFilter(getCallbackFilter());
            enhancer.setNamingPolicy(ComponentNamingPolicy.INSTANCE);

            final Object bean = enhancer.create();
            return bean;
        }
    }
    return null;
}
 
开发者ID:MissionCriticalCloud,项目名称:cosmic,代码行数:17,代码来源:ComponentInstantiationPostProcessor.java

示例5: createProxy

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
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;
}
 
开发者ID:darkevilmac,项目名称:CreeperKiller,代码行数:25,代码来源:HammerKiller.java

示例6: getProxyFactory

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
public static Factory getProxyFactory(Class persistentClass, Class[] interfaces) throws HibernateException {
	//note: interfaces is assumed to already contain HibernateProxy.class
	try {
		return (Factory) Enhancer.create(
			(interfaces.length==1) ?
				persistentClass :
				null,
			interfaces,
			NULL_METHOD_INTERCEPTOR
		);
	}
	catch (Throwable t) {
		LogFactory.getLog(LazyInitializer.class).error("CGLIB Enhancement failed", t);
		throw new HibernateException( "CGLIB Enhancement failed", t );
	}
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:17,代码来源:CGLIBLazyInitializer.java

示例7: getJedisProxy

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
/**
 * 使用 cglib 库创建 JedisProxy 的代理对象
 *
 * @return JedisProxy 代理
 */
public JedisProxy getJedisProxy() {
    if (jedisProxy != null) {
        return jedisProxy;
    }
    synchronized (JedisMethodInterceptor.class) {
        if (jedisProxy != null) {
            return jedisProxy;
        }
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(JedisProxy.class);
        enhancer.setCallback(this);
        jedisProxy = (JedisProxy) enhancer.create();
    }
    return jedisProxy;
}
 
开发者ID:ganpengyu,项目名称:gedis,代码行数:21,代码来源:JedisMethodInterceptor.java

示例8: create

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
public ConstructionProxy<T> create() throws ErrorsException {
  if (interceptors.isEmpty()) {
    return new DefaultConstructionProxyFactory<T>(injectionPoint).create();
  }

  @SuppressWarnings("unchecked")
  Class<? extends Callback>[] callbackTypes = new Class[callbacks.length];
  for (int i = 0; i < callbacks.length; i++) {
    if (callbacks[i] == net.sf.cglib.proxy.NoOp.INSTANCE) {
      callbackTypes[i] = net.sf.cglib.proxy.NoOp.class;
    } else {
      callbackTypes[i] = net.sf.cglib.proxy.MethodInterceptor.class;
    }
  }

  // Create the proxied class. We're careful to ensure that all enhancer state is not-specific
  // to this injector. Otherwise, the proxies for each injector will waste PermGen memory
  try {
  Enhancer enhancer = BytecodeGen.newEnhancer(declaringClass, visibility);
  enhancer.setCallbackFilter(new IndicesCallbackFilter(methods));
  enhancer.setCallbackTypes(callbackTypes);
  return new ProxyConstructor<T>(enhancer, injectionPoint, callbacks, interceptors);
  } catch (Throwable e) {
    throw new Errors().errorEnhancingClass(declaringClass, e).toException();
  }
}
 
开发者ID:maetrive,项目名称:businessworks,代码行数:27,代码来源:ProxyFactory.java

示例9: addTransactionSupport

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
/**
 * 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);
        }
    });
}
 
开发者ID:devcon5io,项目名称:pageobjects,代码行数:30,代码来源:TransactionHelper.java

示例10: main

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
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();
	}
}
 
开发者ID:hdcuican,项目名称:java_learn,代码行数:18,代码来源:ClassPermGenOOM.java

示例11: cglibcreate

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
public static <T> T cglibcreate(T o, OrientElement oe, Transaction transaction ) {
    // this is the main cglib api entry-point
    // this object will 'enhance' (in terms of CGLIB) with new capabilities
    // one can treat this class as a 'Builder' for the dynamic proxy
    Enhancer e = new Enhancer();

    // the class will extend from the real class
    e.setSuperclass(o.getClass());
    // we have to declare the interceptor  - the class whose 'intercept'
    // will be called when any method of the proxified object is called.
    ObjectProxy po = new ObjectProxy(o.getClass(),oe, transaction);
    e.setCallback(po);
    e.setInterfaces(new Class[]{IObjectProxy.class});

    // now the enhancer is configured and we'll create the proxified object
    T proxifiedObj = (T) e.create();
    
    po.___setProxyObject(proxifiedObj);
    
    // the object is ready to be used - return it
    return proxifiedObj;
}
 
开发者ID:mdre,项目名称:odbogm,代码行数:23,代码来源:ObjectProxyFactory.java

示例12: main

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
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());
}
 
开发者ID:zjulzq,项目名称:hotspot-gc-scenarios,代码行数:17,代码来源:Scenario4.java

示例13: wrapWithCrashStackLogging

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
/**
 * 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;
	}
}
 
开发者ID:klehmann,项目名称:domino-jna,代码行数:25,代码来源:NotesNativeAPI.java

示例14: decode

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
@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();
}
 
开发者ID:skaterkamp,项目名称:md380codeplug-tool,代码行数:26,代码来源:LazyLoadingCodecDecorator.java

示例15: proxy

import net.sf.cglib.proxy.Enhancer; //导入依赖的package包/类
public Object proxy() {
  Optional<Class> genericClassCollection = ReflectionUtils.getGenericClassCollection(fieldOrigin);

  if (!genericClassCollection.isPresent()) {
    throw new IllegalArgumentException("Invalid collection on field [" + fieldOrigin.getName() + "]");
  }

  Boolean hasPrimitive = PrimitiveTypeFields.getInstance().contains(genericClassCollection.get());

  if (hasPrimitive) {
    return hibernateCollection;
  } else {
    return this.hibernateCollection.stream()
        .map(item ->
            Enhancer.create(genericClassCollection.get(), ProxyInterceptor.create(item))
        ).collect(Collectors.toList());
  }
}
 
开发者ID:mmaico,项目名称:ddd-reflection,代码行数:19,代码来源:ProxyCollectionHandler.java


注:本文中的net.sf.cglib.proxy.Enhancer类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。