本文整理汇总了Java中javassist.util.proxy.ProxyFactory.create方法的典型用法代码示例。如果您正苦于以下问题:Java ProxyFactory.create方法的具体用法?Java ProxyFactory.create怎么用?Java ProxyFactory.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javassist.util.proxy.ProxyFactory
的用法示例。
在下文中一共展示了ProxyFactory.create方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createStyledTextRenderer
import javassist.util.proxy.ProxyFactory; //导入方法依赖的package包/类
private static /* org.eclipse.swt.custom.StyledTextRenderer */ Object createStyledTextRenderer(
StyledText styledText, ILineSpacingProvider lineSpacingProvider) throws Exception {
// get the org.eclipse.swt.custom.StyledTextRenderer instance of
// StyledText
/* org.eclipse.swt.custom.StyledTextRenderer */ Object originalRenderer = getRendererField(styledText)
.get(styledText);
// Create a Javassist proxy
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(originalRenderer.getClass());
StyledTextRenderer renderer = new StyledTextRenderer(styledText, originalRenderer.getClass());
renderer.setLineSpacingProvider(lineSpacingProvider);
factory.setHandler(renderer);
return factory.create(new Class[] { Device.class, StyledText.class },
new Object[] { styledText.getDisplay(), styledText });
}
示例2: of
import javassist.util.proxy.ProxyFactory; //导入方法依赖的package包/类
/**
* Constructs a partial instance of abstract type {@code cls}, passing {@code args} into its
* constructor.
*
* <p>The returned object will throw an {@link UnsupportedOperationException} from any
* unimplemented methods.
*/
public static <T> T of(Class<T> cls, Object... args) {
checkIsValidPartial(cls);
try {
Constructor<?> constructor = cls.getDeclaredConstructors()[0];
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(cls);
factory.setFilter(new MethodFilter() {
@Override public boolean isHandled(Method m) {
return Modifier.isAbstract(m.getModifiers());
}
});
@SuppressWarnings("unchecked")
T partial = (T) factory.create(
constructor.getParameterTypes(), args, new ThrowingMethodHandler());
return partial;
} catch (Exception e) {
throw new RuntimeException("Failed to instantiate " + cls, e);
}
}
示例3: build
import javassist.util.proxy.ProxyFactory; //导入方法依赖的package包/类
/**
* Creates a proxy object out of the specified root object and the specified interfaces
* and implementations.
*
* @return a proxy object that is still of type T but additionally implements all specified
* interfaces.
* @throws ProxyException if the proxy could not be created.
*/
public T build() throws ProxyException {
if (this.root == null) {
throw new IllegalArgumentException("root must not be null!");
}
if (this.interfacesToImplementations.isEmpty()) {
// nothing to proxy
return this.root;
}
try {
ProxyMethodHandler methodHandler = new ProxyMethodHandler(root,
interfacesToImplementations);
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setSuperclass(root.getClass());
proxyFactory.setInterfaces(interfacesToImplementations.keySet().toArray(new Class[]{}));
return (T) proxyFactory.create(new Class[0], new Object[0], methodHandler);
} catch (Exception e) {
throw new ProxyException(e);
}
}
示例4: create
import javassist.util.proxy.ProxyFactory; //导入方法依赖的package包/类
public static LineNumberChangeRulerColumn create(ISharedTextColors sharedColors) {
try {
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(LineNumberChangeRulerColumn.class);
factory.setHandler(new LineNumberChangeRulerColumnMethodHandler());
return (LineNumberChangeRulerColumn) factory.create(new Class[] { ISharedTextColors.class },
new Object[] { sharedColors });
} catch (Exception e) {
e.printStackTrace();
return new LineNumberChangeRulerColumn(sharedColors);
}
}
示例5: createInternal
import javassist.util.proxy.ProxyFactory; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
protected <E> E createInternal(final Class<E> klass) {
try {
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(klass);
factory.setFilter(new MethodFilter() {
@Override
public boolean isHandled(Method m) {
return getPropName(m) != null;
}
});
MethodHandler handler = new MethodHandler() {
@Override
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
return DefaultProxy.this.invoke(self, thisMethod, args);
}
};
return (E) factory.create(new Class[0], new Object[0], handler);
} catch(Exception e) {
throw new BeanException(e);
}
}
示例6: newInstance
import javassist.util.proxy.ProxyFactory; //导入方法依赖的package包/类
/**
* Creates a proxy class of bean and returns an instance of that class.
*
* @param context the activity context
* @param beanRule the bean rule
* @param constructorArgs the arguments passed to a constructor
* @param constructorArgTypes the parameter types for a constructor
* @return a new proxy bean object
*/
public static Object newInstance(ActivityContext context, BeanRule beanRule, Object[] constructorArgs, Class<?>[] constructorArgTypes) {
try {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setSuperclass(beanRule.getBeanClass());
MethodHandler methodHandler = new JavassistDynamicBeanProxy(context, beanRule);
return proxyFactory.create(constructorArgTypes, constructorArgs, methodHandler);
} catch (Exception e) {
throw new ProxyBeanInstantiationException(beanRule.getBeanClass(), e);
}
}
示例7: newInstance
import javassist.util.proxy.ProxyFactory; //导入方法依赖的package包/类
private <T> T newInstance(final Object entity, final Session em,
final MainInstance principal, final Class<T> fieldType)
throws NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(fieldType);
MethodHandler handler = new MainInstanceMethodHandler<T>(em, principal,
entity, fieldType);
return (T) factory.create(new Class<?>[0], new Object[0], handler);
}
示例8: checkForCreateListTypeType
import javassist.util.proxy.ProxyFactory; //导入方法依赖的package包/类
private void checkForCreateListTypeType(Configuration conf, final Class<?> propertyType) throws NoSuchMethodException,
IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
if (!listTypeProxyCreatedSet.contains(propertyType)) {
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(ListTypeUserType.class);
factory.setFilter(new MethodFilter() {
@Override
public boolean isHandled(Method method) {
return Modifier.isAbstract(method.getModifiers());
}
});
MethodHandler handler = new MethodHandler() {
@Override
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
if (thisMethod.getName().equals("returnedClass") && args.length == 0) {
LOG.debug("Handling method returnedClass() of type ListTypeUserType", thisMethod);
return propertyType;
} else {
throw new UnsupportedOperationException();
}
}
};
Object type = factory.create(new Class<?>[0], new Object[0], handler);
conf.registerTypeOverride((UserType) type, new String[] { propertyType.getSimpleName(), propertyType.getName() });
listTypeProxyCreatedSet.add(propertyType);
}
}
示例9: createInstance
import javassist.util.proxy.ProxyFactory; //导入方法依赖的package包/类
private <T> Object createInstance(Class<T> type, MethodInvocation<? super T> handler, ProxyFactory factory) {
try {
return factory.create(null, null, new MethodInvocationAdapter<>(handler));
} catch (ReflectiveOperationException | IllegalArgumentException e) {
logger.error("An error occurs when create a proxy for type " + type, e);
throw new ProxyCreationException(e);
}
}
示例10: iface
import javassist.util.proxy.ProxyFactory; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*
* <p>
* iface.
* </p>
*/
@SuppressWarnings("unchecked")
@Override
public <X extends TServiceClient> X iface(Class<X> ifaceClass,
Function<TTransport, TProtocol> protocolProvider, int hash) {
List<ThriftServerInfo> servers = serverInfoProvider.get();
if (servers == null || servers.isEmpty()) {
throw new NoBackendException();
}
hash = Math.abs(hash);
hash = hash < 0 ? 0 : hash;
ThriftServerInfo selected = servers.get(hash % servers.size());
logger.trace("get connection for [{}]->{} with hash:{}", ifaceClass, selected, hash);
TTransport transport = poolProvider.getConnection(selected);
TProtocol protocol = protocolProvider.apply(transport);
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(ifaceClass);
factory.setFilter(m -> ThriftClientUtils.getInterfaceMethodNames(ifaceClass).contains(
m.getName()));
try {
X x = (X) factory.create(new Class[] { org.apache.thrift.protocol.TProtocol.class },
new Object[] { protocol });
((Proxy) x).setHandler((self, thisMethod, proceed, args) -> {
boolean success = false;
try {
Object result = proceed.invoke(self, args);
success = true;
return result;
} finally {
if (success) {
poolProvider.returnConnection(selected, transport);
} else {
poolProvider.returnBrokenConnection(selected, transport);
}
}
});
return x;
} catch (NoSuchMethodException | IllegalArgumentException | InstantiationException
| IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("fail to create proxy.", e);
}
}