本文整理汇总了Java中net.sf.cglib.proxy.Enhancer.setCallback方法的典型用法代码示例。如果您正苦于以下问题:Java Enhancer.setCallback方法的具体用法?Java Enhancer.setCallback怎么用?Java Enhancer.setCallback使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.sf.cglib.proxy.Enhancer
的用法示例。
在下文中一共展示了Enhancer.setCallback方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
}
示例2: 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;
}
示例3: 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;
}
示例4: 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());
}
示例5: 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();
}
示例6: NaviDataServiceProxy
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public NaviDataServiceProxy(IBaseDataService realService, Class<?> inter) {
this.realService = realService;
Class<?>[] inters = realService.getClass().getInterfaces();
//直接实现目标的接口的情况下使用java原生动态代理,否则使用cglib
if (find(inters, inter)) {
this.proxyService = Proxy.newProxyInstance(
realService.getClass().getClassLoader(), realService.getClass().getInterfaces(), this
);
} else {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(this.realService.getClass());
// 回调方法
enhancer.setCallback(this);
// 创建代理对象
this.proxyService = enhancer.create();
}
}
示例7: wrapIfDirty
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* @return A proxy object whose getters will return null for unchanged entity's properties
* (and new values for changed properties), or the entity itself if the entity was not
* enhanced for dirty checking.
* @throws com.currencycloud.client.CurrencyCloudClient.NoChangeException if the entity was dirty-checked
* but there were no changes.
*/
static <E extends Entity> E wrapIfDirty(E entity, Class<E> entityClass) throws NoChangeException {
if (entity != null) {
Map<String, Object> values = getDirtyProperties(entity);
if (values != null) {
if (values.isEmpty()) {
throw new NoChangeException();
}
values = new HashMap<>(values);
values.put("id", entity.getId());
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(entityClass);
enhancer.setCallback(new ModifiedValueProvider(values));
return (E) enhancer.create();
}
}
return entity;
}
示例8: create
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <T> T create(final Class<T> clazz) {
checkNotNull(clazz, "clazz cannot be null");
if (!isAbstract(clazz)) {
return null;
}
checkIfSupported(clazz);
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(clazz);
enhancer.setCallback(handler);
return (T) enhancer.create();
}
示例9: testSupportForClassBasedProxyWithAdditionalInterface
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public void testSupportForClassBasedProxyWithAdditionalInterface()
throws NullPointerException {
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(HashMap.class);
enhancer.setCallback(NoOp.INSTANCE);
enhancer.setInterfaces(new Class[]{Runnable.class});
final Map orig = (Map)enhancer.create();
final String xml = ""
+ "<CGLIB-enhanced-proxy>\n"
+ " <type>java.util.HashMap</type>\n"
+ " <interfaces>\n"
+ " <java-class>java.lang.Runnable</java-class>\n"
+ " </interfaces>\n"
+ " <hasFactory>true</hasFactory>\n"
+ " <net.sf.cglib.proxy.NoOp_-1/>\n"
+ "</CGLIB-enhanced-proxy>";
final Object serialized = assertBothWays(orig, xml);
assertTrue(serialized instanceof HashMap);
assertTrue(serialized instanceof Map);
assertTrue(serialized instanceof Runnable);
}
示例10: testSupportsProxiesWithMultipleInterfaces
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public void testSupportsProxiesWithMultipleInterfaces() throws NullPointerException {
final Enhancer enhancer = new Enhancer();
enhancer.setCallback(NoOp.INSTANCE);
enhancer.setInterfaces(new Class[]{Map.class, Runnable.class});
final Map orig = (Map)enhancer.create();
final String xml = ""
+ "<CGLIB-enhanced-proxy>\n"
+ " <type>java.lang.Object</type>\n"
+ " <interfaces>\n"
+ " <java-class>java.util.Map</java-class>\n"
+ " <java-class>java.lang.Runnable</java-class>\n"
+ " </interfaces>\n"
+ " <hasFactory>true</hasFactory>\n"
+ " <net.sf.cglib.proxy.NoOp_-1/>\n"
+ "</CGLIB-enhanced-proxy>";
final Object serialized = assertBothWays(orig, xml);
assertTrue(serialized instanceof Map);
assertTrue(serialized instanceof Runnable);
}
示例11: testSupportProxiesWithMultipleCallbackSetToNull
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public void testSupportProxiesWithMultipleCallbackSetToNull() throws NullPointerException {
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(HashMap.class);
enhancer.setCallback(NoOp.INSTANCE);
final HashMap orig = (HashMap)enhancer.create();
((Factory)orig).setCallback(0, null);
final String xml = ""
+ "<CGLIB-enhanced-proxy>\n"
+ " <type>java.util.HashMap</type>\n"
+ " <interfaces/>\n"
+ " <hasFactory>true</hasFactory>\n"
+ " <null/>\n"
+ "</CGLIB-enhanced-proxy>";
assertBothWays(orig, xml);
}
示例12: testSupportsSerialVersionUID
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public void testSupportsSerialVersionUID()
throws NullPointerException, NoSuchFieldException, IllegalAccessException {
final Enhancer enhancer = new Enhancer();
enhancer.setCallback(NoOp.INSTANCE);
enhancer.setInterfaces(new Class[]{Runnable.class});
enhancer.setSerialVersionUID(new Long(20060804L));
final Runnable orig = (Runnable)enhancer.create();
final String xml = ""
+ "<CGLIB-enhanced-proxy>\n"
+ " <type>java.lang.Object</type>\n"
+ " <interfaces>\n"
+ " <java-class>java.lang.Runnable</java-class>\n"
+ " </interfaces>\n"
+ " <hasFactory>true</hasFactory>\n"
+ " <net.sf.cglib.proxy.NoOp_-1/>\n"
+ " <serialVersionUID>20060804</serialVersionUID>\n"
+ "</CGLIB-enhanced-proxy>";
final Object serialized = assertBothWays(orig, xml);
final Field field = serialized.getClass().getDeclaredField("serialVersionUID");
field.setAccessible(true);
assertEquals(20060804L, field.getLong(null));
}
示例13: testProxyTypeCanBeAliased
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public void testProxyTypeCanBeAliased() throws MalformedURLException {
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(HashMap.class);
enhancer.setCallback(new DelegatingHandler(new HashMap()));
final Map orig = (Map)enhancer.create();
orig.put("URL", new URL("http://xstream.codehaus.org"));
xstream.aliasType("cglib", Map.class);
final String expected = ""
+ "<cglib>\n"
+ " <type>java.util.HashMap</type>\n"
+ " <interfaces/>\n"
+ " <hasFactory>true</hasFactory>\n"
+ " <com.thoughtworks.acceptance.CglibCompatibilityTest_-DelegatingHandler>\n"
+ " <delegate class=\"map\">\n"
+ " <entry>\n"
+ " <string>URL</string>\n"
+ " <url>http://xstream.codehaus.org</url>\n"
+ " </entry>\n"
+ " </delegate>\n"
+ " </com.thoughtworks.acceptance.CglibCompatibilityTest_-DelegatingHandler>\n"
+ "</cglib>";
assertEquals(expected, xstream.toXML(orig));
}
示例14: createAdapter
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* Create an adapter for the given {@code object} in a specific {@code type}.
*
* @param adaptableObject the object to adapt
* @param adapterType the class in which the object must be adapted
*
* @return an adapted object in the given {@code type}
*/
private static Object createAdapter(Object adaptableObject, Class<?> adapterType) {
/*
* Compute the interfaces that the proxy has to implement
* These are the current interfaces + PersistentEObject
*/
List<Class<?>> interfaces = ClassUtils.getAllInterfaces(adaptableObject.getClass());
interfaces.add(PersistentEObject.class);
// Create the proxy
Enhancer proxy = new Enhancer();
/*
* Use the ClassLoader of the type, otherwise it will cause OSGi troubles (like project trying to
* create an PersistentEObject while it does not have a dependency to NeoEMF core)
*/
proxy.setClassLoader(adapterType.getClassLoader());
proxy.setSuperclass(adaptableObject.getClass());
proxy.setInterfaces(interfaces.toArray(new Class[interfaces.size()]));
proxy.setCallback(new PersistentEObjectProxyHandler());
return proxy.create();
}
示例15: createProxy
import net.sf.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* Creates a proxy object that will write-replace with a wrapper around the {@link EntityManager}.
* @param factory a factory to generate EntityManagers.
* @return the proxied instance.
*/
static EntityManager createProxy(final HibernateEntityManagerFactory factory) {
final EntityManagerInterceptor handler = new EntityManagerInterceptor(factory);
final Enhancer e = new Enhancer();
// make sure we're Serializable and have a write replace method
e.setInterfaces(new Class[] { EntityManager.class, Serializable.class, IWriteReplace.class });
e.setSuperclass(Object.class);
e.setCallback(handler);
e.setNamingPolicy(new DefaultNamingPolicy() {
@Override
public String getClassName(final String prefix,
final String source,
final Object key,
final Predicate names) {
return super.getClassName("CROQUET_ENTITY_MANAGER_PROXY_" + prefix, source, key, names);
}
});
LOG.trace("Created proxy for an EntityManager");
return (EntityManager)e.create();
}