@SuppressWarnings("unchecked") public static <T> T coerce(Object obj, Class<T> type) { // can't use `type.cast()` because it will fail if `type` is a primitive type return (T) DefaultTypeTransformation.castToType(obj, type); }
/** * Provides a hook for type casting of the given object to the required type * * @param type of object to convert the given object to * @param object the object to be converted * @return the original object or a new converted value * @throws Throwable if the type casting fails */ public static Object castToType(Object object, Class type) throws Throwable { return DefaultTypeTransformation.castToType(object, type); }
private static Object makeCommonArray(Object[] arguments, int offset, Class baseClass) { Object[] result = (Object[]) Array.newInstance(baseClass, arguments.length - offset); for (int i = offset; i < arguments.length; i++) { Object v = arguments[i]; v = DefaultTypeTransformation.castToType(v, baseClass); result[i - offset] = v; } return result; }
public static Object castToVargsArray(Object[] origin, int firstVargsPos, Class<?> arrayType) { Class<?> componentType = arrayType.getComponentType(); if (firstVargsPos >= origin.length) return Array.newInstance(componentType, 0); int length = origin.length - firstVargsPos; if (length == 1 && arrayType.isInstance(origin[firstVargsPos])) return origin[firstVargsPos]; Object newArray = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { Object convertedValue = castToType(origin[firstVargsPos + i], componentType); Array.set(newArray, i, convertedValue); } return newArray; }
private static Object adjustNewValue(Object[] objects, Object newValue) { Class arrayComponentClass = objects.getClass().getComponentType(); Object adjustedNewVal = newValue; if (newValue instanceof Number) { if (!arrayComponentClass.equals(newValue.getClass())) { adjustedNewVal = DefaultTypeTransformation.castToType(newValue, arrayComponentClass); } } else if (Character.class.isAssignableFrom(arrayComponentClass)) { adjustedNewVal = DefaultTypeTransformation.getCharFromSizeOneString(newValue); } else if (Number.class.isAssignableFrom(arrayComponentClass)) { if (newValue instanceof Character || newValue instanceof String || newValue instanceof GString) { Character ch = DefaultTypeTransformation.getCharFromSizeOneString(newValue); adjustedNewVal = DefaultTypeTransformation.castToType(ch, arrayComponentClass); } } else if (arrayComponentClass.isArray()) { adjustedNewVal = DefaultTypeTransformation.castToType(newValue, arrayComponentClass); } return adjustedNewVal; }
public static Object asArray(Object object, Class type) { if (type.isAssignableFrom(object.getClass())) { return object; } Collection list = asCollection(object); int size = list.size(); Class elementType = type.getComponentType(); Object array = Array.newInstance(elementType, size); int idx = 0; for (Iterator iter = list.iterator(); iter.hasNext(); idx++) { Object element = iter.next(); Array.set(array, idx, castToType(element, elementType)); } return array; }
int minSize = Integer.MAX_VALUE; for (Object listLike : lists) { List list = (List) DefaultTypeTransformation.castToType(listLike, List.class); if (list.size() < minSize) minSize = list.size(); List list = (List) DefaultTypeTransformation.castToType(listLike, List.class); for (int i = 0; i < minSize; i++) { List resultList = (List) result.get(i);
public Object invoke(Object object, Object[] args) { final short[] objects = (short[]) object; final int index = normaliseIndex((Integer) args[0], objects.length); Object newValue = args[1]; if (!(newValue instanceof Short)) { if (newValue instanceof Character || newValue instanceof String || newValue instanceof GString) { Character ch = DefaultTypeTransformation.getCharFromSizeOneString(newValue); objects[index] = (Short) DefaultTypeTransformation.castToType(ch, Short.class); } else { objects[index] = ((Number) newValue).shortValue(); } } else objects[index] = (Short) args[1]; return null; }
public Object invoke(Object object, Object[] args) { final double[] objects = (double[]) object; final int index = normaliseIndex((Integer) args[0], objects.length); Object newValue = args[1]; if (!(newValue instanceof Double)) { if (newValue instanceof Character || newValue instanceof String || newValue instanceof GString) { Character ch = DefaultTypeTransformation.getCharFromSizeOneString(newValue); objects[index] = (Double) DefaultTypeTransformation.castToType(ch, Double.class); } else { objects[index] = ((Number) newValue).doubleValue(); } } else objects[index] = (Double) args[1]; return null; }
public Object invoke(Object object, Object[] args) { final int[] objects = (int[]) object; final int index = normaliseIndex((Integer) args[0], objects.length); Object newValue = args[1]; if (!(newValue instanceof Integer)) { if (newValue instanceof Character || newValue instanceof String || newValue instanceof GString) { Character ch = DefaultTypeTransformation.getCharFromSizeOneString(newValue); objects[index] = (Integer) DefaultTypeTransformation.castToType(ch, Integer.class); } else { objects[index] = ((Number) newValue).intValue(); } } else objects[index] = (Integer) args[1]; return null; }
public Object invoke(Object object, Object[] args) { final long[] objects = (long[]) object; final int index = normaliseIndex((Integer) args[0], objects.length); Object newValue = args[1]; if (!(newValue instanceof Long)) { if (newValue instanceof Character || newValue instanceof String || newValue instanceof GString) { Character ch = DefaultTypeTransformation.getCharFromSizeOneString(newValue); objects[index] = (Long) DefaultTypeTransformation.castToType(ch, Long.class); } else { objects[index] = ((Number) newValue).longValue(); } } else objects[index] = (Long) args[1]; return null; }
public Object invoke(Object object, Object[] args) { final float[] objects = (float[]) object; final int index = normaliseIndex((Integer) args[0], objects.length); Object newValue = args[1]; if (!(newValue instanceof Float)) { if (newValue instanceof Character || newValue instanceof String || newValue instanceof GString) { Character ch = DefaultTypeTransformation.getCharFromSizeOneString(newValue); objects[index] = (Float) DefaultTypeTransformation.castToType(ch, Float.class); } else { objects[index] = ((Number) newValue).floatValue(); } } else objects[index] = (Float) args[1]; return null; }
/** * Sets the property on the given object to the new value * * @param object on which to set the property * @param newValue the new value of the property * @throws RuntimeException if the property could not be set */ public void setProperty(final Object object, Object newValue) { AccessPermissionChecker.checkAccessPermission(field); final Object goalValue = DefaultTypeTransformation.castToType(newValue, field.getType()); if (isFinal()) { throw new GroovyRuntimeException("Cannot set the property '" + name + "' because the backing field is final."); } try { field.set(object, goalValue); } catch (IllegalAccessException ex) { throw new GroovyRuntimeException("Cannot set the property '" + name + "'.", ex); } } }
return (T) DefaultTypeTransformation.castToType(obj, type);
/** * Set the property on the given object to the new value. * * @param object on which to set the property * @param newValue the new value of the property * @throws RuntimeException if the property could not be set */ public void setProperty(Object object, Object newValue) { MetaMethod setter = getSetter(); if (setter == null) { if (field != null && !Modifier.isFinal(field.getModifiers())) { field.setProperty(object, newValue); return; } throw new GroovyRuntimeException("Cannot set read-only property: " + name); } newValue = DefaultTypeTransformation.castToType(newValue, getType()); setter.invoke(object, new Object[]{newValue}); }
newValue = DefaultTypeTransformation.castToType( newValue, method.getParameterTypes()[0].getTheClass()); arguments[0] = newValue; } else { newValue = DefaultTypeTransformation.castToType( newValue, method.getParameterTypes()[1].getTheClass());
/** * Provides a hook for type casting of the given object to the required type * * @param type of object to convert the given object to * @param object the object to be converted * @return the original object or a new converted value * @throws Throwable if the type casting fails */ public static Object castToType(Object object, Class type) throws Throwable { return DefaultTypeTransformation.castToType(object, type); }
/** * Converts a given object to a type. This method is used through * the "as" operator and is overloadable as any other operator. * * @param obj the object to convert * @param type the goal type * @return the resulting object */ public static Object asType(Object obj, Class type) { return DefaultTypeTransformation.castToType(obj, type); }
public static Double putAt(double[] array, int idx, Object newValue) { if (!(newValue instanceof Double)) { if(newValue instanceof Character || newValue instanceof String || newValue instanceof GString) { Character ch = DefaultTypeTransformation.getCharFromSizeOneString(newValue); newValue = DefaultTypeTransformation.castToType(ch, Double.class); } else { Number n = (Number) newValue; newValue = new Double(n.doubleValue()); } } return (Double) primitiveArrayPut(array, idx, newValue); }
public static Long putAt(long[] array, int idx, Object newValue) { if (!(newValue instanceof Long)) { if(newValue instanceof Character || newValue instanceof String || newValue instanceof GString) { Character ch = DefaultTypeTransformation.getCharFromSizeOneString(newValue); newValue = DefaultTypeTransformation.castToType(ch, Long.class); } else { Number n = (Number) newValue; newValue = new Long(n.longValue()); } } return (Long) primitiveArrayPut(array, idx, newValue); }