本文整理匯總了Java中groovy.util.ProxyGenerator類的典型用法代碼示例。如果您正苦於以下問題:Java ProxyGenerator類的具體用法?Java ProxyGenerator怎麽用?Java ProxyGenerator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ProxyGenerator類屬於groovy.util包,在下文中一共展示了ProxyGenerator類的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: asType
import groovy.util.ProxyGenerator; //導入依賴的package包/類
/**
* Coerces this map to the given type, using the map's keys as the public
* method names, and values as the implementation. Typically the value
* would be a closure which behaves like the method implementation.
*
* @param map this map
* @param clazz the target type
* @return a Proxy of the given type, which defers calls to this map's elements.
* @since 1.0
*/
@SuppressWarnings("unchecked")
public static <T> T asType(Map map, Class<T> clazz) {
if (!(clazz.isInstance(map)) && clazz.isInterface() && !Traits.isTrait(clazz)) {
return (T) Proxy.newProxyInstance(
clazz.getClassLoader(),
new Class[]{clazz},
new ConvertedMap(map));
}
try {
return asType((Object) map, clazz);
} catch (GroovyCastException ce) {
try {
return (T) ProxyGenerator.INSTANCE.instantiateAggregateFromBaseClass(map, clazz);
} catch (GroovyRuntimeException cause) {
throw new GroovyCastException("Error casting map to " + clazz.getName() +
", Reason: " + cause.getMessage());
}
}
}
示例2: coerceToSAM
import groovy.util.ProxyGenerator; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public static Object coerceToSAM(Closure argument, Method method, Class clazz, boolean isInterface) {
if (argument!=null && clazz.isAssignableFrom(argument.getClass())) {
return argument;
}
if (isInterface) {
if (Traits.isTrait(clazz)) {
Map<String,Closure> impl = Collections.singletonMap(
method.getName(),
argument
);
return ProxyGenerator.INSTANCE.instantiateAggregate(impl,Collections.singletonList(clazz));
}
return Proxy.newProxyInstance(
clazz.getClassLoader(),
new Class[]{clazz},
new ConvertedClosure(argument));
} else {
Map<String, Object> m = new HashMap<String,Object>();
m.put(method.getName(), argument);
return ProxyGenerator.INSTANCE.
instantiateAggregateFromBaseClass(m, clazz);
}
}
示例3: withTraits
import groovy.util.ProxyGenerator; //導入依賴的package包/類
/**
* Dynamically wraps an instance into something which implements the
* supplied trait classes. It is guaranteed that the returned object
* will implement the trait interfaces, but the original type of the
* object is lost (replaced with a proxy).
* @param self object to be wrapped
* @param traits a list of trait classes
* @return a proxy implementing the trait interfaces
*/
public static Object withTraits(Object self, Class<?>... traits) {
List<Class> interfaces = new ArrayList<Class>();
Collections.addAll(interfaces, traits);
return ProxyGenerator.INSTANCE.instantiateDelegate(interfaces, self);
}