/** * Converts the given iterable to another type. * * @param iterable a Iterable * @param clazz the desired class * @return the object resulting from this type conversion * @see #asType(Collection, Class) * @since 2.4.12 */ @SuppressWarnings("unchecked") public static <T> T asType(Iterable iterable, Class<T> clazz) { if (Collection.class.isAssignableFrom(clazz)) { return asType((Collection) toList(iterable), clazz); } return asType((Object) iterable, clazz); }
/** * Converts this File to a {@link groovy.lang.Writable} or delegates to default * {@link DefaultGroovyMethods#asType(java.lang.Object, java.lang.Class)}. * * @param f a File * @param c the desired class * @return the converted object * @since 1.0 */ @SuppressWarnings("unchecked") public static <T> T asType(File f, Class<T> c) { if (c == Writable.class) { return (T) asWritable(f); } return DefaultGroovyMethods.asType((Object) f, c); }
/** * Converts the given array to either a List, Set, or * SortedSet. If the given class is something else, the * call is deferred to {@link #asType(Object,Class)}. * * @param ary an array * @param clazz the desired class * @return the object resulting from this type conversion * @see #asType(java.lang.Object, java.lang.Class) * @since 1.5.1 */ @SuppressWarnings("unchecked") public static <T> T asType(Object[] ary, Class<T> clazz) { if (clazz == List.class) { return (T) new ArrayList(Arrays.asList(ary)); } if (clazz == Set.class) { return (T) new HashSet(Arrays.asList(ary)); } if (clazz == SortedSet.class) { return (T) new TreeSet(Arrays.asList(ary)); } return asType((Object) ary, clazz); }
/** * Converts a class implementing some trait into a target class. If the trait is a dynamic proxy and * that the target class is assignable to the target object of the proxy, then the target object is * returned. Otherwise, falls back to {@link org.codehaus.groovy.runtime.DefaultGroovyMethods#asType(java.lang.Object, Class)} * @param self an object to be coerced to some class * @param clazz the class to be coerced to * @return the object coerced to the target class, or the proxy instance if it is compatible with the target class. */ @SuppressWarnings("unchecked") public static <T> T getAsType(Object self, Class<T> clazz) { if (self instanceof GeneratedGroovyProxy) { Object proxyTarget = ((GeneratedGroovyProxy)self).getProxyTarget(); if (clazz.isAssignableFrom(proxyTarget.getClass())) { return (T) proxyTarget; } } return DefaultGroovyMethods.asType(self, clazz); }
if (parameters != null) { if (parameters.containsKey("connectTimeout")) { connection.setConnectTimeout(DefaultGroovyMethods.asType(parameters.get("connectTimeout"), Integer.class)); connection.setReadTimeout(DefaultGroovyMethods.asType(parameters.get("readTimeout"), Integer.class)); connection.setUseCaches(DefaultGroovyMethods.asType(parameters.get("useCaches"), Boolean.class)); connection.setAllowUserInteraction(DefaultGroovyMethods.asType(parameters.get("allowUserInteraction"), Boolean.class));
/** * Converts the GString to a File, or delegates to the default * {@link org.codehaus.groovy.runtime.DefaultGroovyMethods#asType(Object, Class)} * * @param self a GString * @param c the desired class * @return the converted object * @since 1.5.0 */ @SuppressWarnings("unchecked") public static <T> T asType(GString self, Class<T> c) { if (c == File.class) { return (T) new File(self.toString()); } else if (Number.class.isAssignableFrom(c) || c.isPrimitive()) { return asType(self.toString(), c); } return DefaultGroovyMethods.asType((Object) self, c); }
Number maxDepthNumber = DefaultGroovyMethods.asType(options.remove("maxDepth"), Number.class); int maxDepth = maxDepthNumber == null ? -1 : maxDepthNumber.intValue(); Boolean visitRoot = DefaultGroovyMethods.asType(get(options, "visitRoot", false), Boolean.class); Boolean preRoot = DefaultGroovyMethods.asType(get(options, "preRoot", false), Boolean.class); Boolean postRoot = DefaultGroovyMethods.asType(get(options, "postRoot", false), Boolean.class); final Closure pre = (Closure) options.get("preDir"); final Closure post = (Closure) options.get("postDir");
private static <K, V> void addEntry(Map<K, V> result, Object newEntry) { if (newEntry instanceof Map) { leftShift(result, (Map)newEntry); } else if (newEntry instanceof List) { List list = (List) newEntry; // def (key, value) == list Object key = list.isEmpty() ? null : list.get(0); Object value = list.size() <= 1 ? null : list.get(1); leftShift(result, new MapEntry(key, value)); } else if (newEntry.getClass().isArray()) { Object[] array = (Object[]) newEntry; // def (key, value) == array.toList() Object key = array.length == 0 ? null : array[0]; Object value = array.length <= 1 ? null : array[1]; leftShift(result, new MapEntry(key, value)); } else { // TODO: enforce stricter behavior? // given Map.Entry is an interface, we get a proxy which gives us lots // of flexibility but sometimes the error messages might be unexpected leftShift(result, asType(newEntry, Map.Entry.class)); } }
private static Object convertConstant(Number source, ClassNode target) { if (ClassHelper.Byte_TYPE.equals(target)) { return source.byteValue(); } if (ClassHelper.Short_TYPE.equals(target)) { return source.shortValue(); } if (ClassHelper.Integer_TYPE.equals(target)) { return source.intValue(); } if (ClassHelper.Long_TYPE.equals(target)) { return source.longValue(); } if (ClassHelper.Float_TYPE.equals(target)) { return source.floatValue(); } if (ClassHelper.Double_TYPE.equals(target)) { return source.doubleValue(); } if (ClassHelper.BigInteger_TYPE.equals(target)) { return DefaultGroovyMethods.asType(source, BigInteger.class); } if (ClassHelper.BigDecimal_TYPE.equals(target)) { return DefaultGroovyMethods.asType(source, BigDecimal.class); } throw new IllegalArgumentException("Unsupported conversion"); }
/** * 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()); } } }
return asType((Object) col, clazz);
return asType((Object) cl, clazz); } catch (GroovyCastException ce) { try {
protected static boolean getBooleanAnnotationParameter(AnnotationNode node, String parameterName, boolean defaultValue) { Expression member = node.getMember(parameterName); if (member != null) { if (member instanceof ConstantExpression) { try { return DefaultGroovyMethods.asType(((ConstantExpression) member).getValue(), Boolean.class); } catch (Exception e) { internalError("Expecting boolean value for " + parameterName + " annotation parameter. Found " + member + "member"); } } else { internalError("Expecting boolean value for " + parameterName + " annotation parameter. Found " + member + "member"); } } return defaultValue; }
/** * Transform this number to a the given type, using the 'as' operator. The * following types are supported in addition to the default * {@link #asType(java.lang.Object, java.lang.Class)}: * <ul> * <li>BigDecimal</li> * <li>BigInteger</li> * <li>Double</li> * <li>Float</li> * </ul> * @param self this number * @param c the desired type of the transformed result * @return an instance of the given type * @since 1.0 */ @SuppressWarnings("unchecked") public static <T> T asType(Number self, Class<T> c) { if (c == BigDecimal.class) { return (T) toBigDecimal(self); } else if (c == BigInteger.class) { return (T) toBigInteger(self); } else if (c == Double.class) { return (T) toDouble(self); } else if (c == Float.class) { return (T) toFloat(self); } return asType((Object) self, c); }
return (T) InvokerHelper.invokeMethod(c, "valueOf", new Object[]{ self }); return DefaultGroovyMethods.asType((Object) self, c);
@Override public final AttributeCheckStatus getStatus(final Attribute attribute) { attributeStorage.set(attribute); try { return DefaultGroovyMethods.asType(run(), AttributeCheckStatus.class); } finally { attributeStorage.remove(); } }
/** * Converts this File to a {@link Writable} or delegates to default * {@link #asType(Object,Class)}. * * @param f a File * @param c the desired class * @return the converted object */ public static Object asType(File f, Class c) { if (c == Writable.class) { return asWritable(f); } return asType((Object) f, c); }
public void addModelStoreListener(String presentationModelType, Closure listener) { getModelStore().addModelStoreListener(presentationModelType, asType(listener, ModelStoreListener.class)); }
private static <K, V> void addEntry(Map<K, V> result, Object newEntry) { if (newEntry instanceof Map) { leftShift(result, (Map)newEntry); } else if (newEntry instanceof List && ((List)newEntry).size() == 2) { List list = (List) newEntry; leftShift(result, new MapEntry(list.get(0), list.get(1))); } else { // TODO: enforce stricter behavior? // given Map.Entry is an interface, we get a proxy which gives us lots // of flexibility but sometimes the error messages might be unexpected leftShift(result, asType(newEntry, Map.Entry.class)); } }
public void afterVisit(@DelegatesTo(value=TreeContext.class, strategy=Closure.DELEGATE_FIRST) Closure<?> action) { Closure<?> clone = MatcherUtils.cloneWithDelegate(action, this); afterVisit(DefaultGroovyMethods.asType(clone, TreeContextAction.class)); }