本文整理汇总了Java中org.codehaus.groovy.runtime.typehandling.GroovyCastException类的典型用法代码示例。如果您正苦于以下问题:Java GroovyCastException类的具体用法?Java GroovyCastException怎么用?Java GroovyCastException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
GroovyCastException类属于org.codehaus.groovy.runtime.typehandling包,在下文中一共展示了GroovyCastException类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: asType
import org.codehaus.groovy.runtime.typehandling.GroovyCastException; //导入依赖的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: instantiateAggregate
import org.codehaus.groovy.runtime.typehandling.GroovyCastException; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public GroovyObject instantiateAggregate(Map closureMap, List<Class> interfaces, Class clazz, Object[] constructorArgs) {
if (clazz != null && Modifier.isFinal(clazz.getModifiers())) {
throw new GroovyCastException("Cannot coerce a map to class " + clazz.getName() + " because it is a final class");
}
Map<Object,Object> map = closureMap != null ? closureMap : EMPTY_CLOSURE_MAP;
ProxyGeneratorAdapter adapter = createAdapter(map, interfaces, null, clazz);
return adapter.proxy(map, constructorArgs);
}
示例3: parseCastWrongClassScript_throwGroovyCastException
import org.codehaus.groovy.runtime.typehandling.GroovyCastException; //导入依赖的package包/类
@Test(expected = GroovyCastException.class)
public void parseCastWrongClassScript_throwGroovyCastException() {
//when
parseClassFromScript("def plot = new Plot() \n" + "Line line = (Line) Plot ");
}