public static Class castToClass(Object object) { if (object==null) return null; if (object instanceof Class) return (Class) object; try { return Class.forName(object.toString()); } catch (Exception e) { throw new GroovyCastException(object, Class.class, e); } }
/** * @param objectToCast object we tried to cast * @param classToCastTo class we tried to cast to */ public GroovyCastException(Object objectToCast, Class classToCastTo) { super(makeMessage(objectToCast,classToCastTo)); }
private static String makeMessage(Object objectToCast, Class classToCastTo) { String classToCastFrom; Object msgObject = objectToCast; if (objectToCast!=null) { classToCastFrom = objectToCast.getClass().getName(); } else { msgObject = "null"; classToCastFrom = "null"; } String msg = "Cannot cast object '" + msgObject + "' " + "with class '" + classToCastFrom + "' " + "to class '" + classToCastTo.getName() + "'"; if (objectToCast==null){ msg += getWrapper(classToCastTo); } return msg; }
/** * this class requires that the supplied enum is not fitting a * Collection case for casting */ public static Enum castToEnum(Object object, Class<? extends Enum> type) { if (object==null) return null; if (type.isInstance(object)) return (Enum) object; if (object instanceof String || object instanceof GString) { return Enum.valueOf(type, object.toString()); } throw new GroovyCastException(object, type); }
/** * @param objectToCast object we tried to cast * @param classToCastTo class we tried to cast to * @param cause not kept but we pass on message from this Exception if any */ public GroovyCastException(Object objectToCast, Class classToCastTo, Exception cause) { super(makeMessage(objectToCast,classToCastTo) + " due to: " + cause.getClass().getName() + (cause.getMessage() == null ? "" : ": " + cause.getMessage())); }
private static String makeMessage(Object objectToCast, Class classToCastTo) { String classToCastFrom; Object msgObject = objectToCast; if (objectToCast!=null) { classToCastFrom = objectToCast.getClass().getName(); } else { msgObject = "null"; classToCastFrom = "null"; } String msg = "Cannot cast object '" + msgObject + "' " + "with class '" + classToCastFrom + "' " + "to class '" + classToCastTo.getName() + "'"; if (objectToCast==null){ msg += getWrapper(classToCastTo); } return msg; }
public static Character castToChar(Object object) { if (object==null) return null; if (object instanceof Character) { return (Character) object; } else if (object instanceof Number) { Number value = (Number) object; return (char) value.intValue(); } String text = object.toString(); if (text.length() == 1) { return text.charAt(0); } else { throw new GroovyCastException(text,char.class); } } }
/** * @param objectToCast object we tried to cast * @param classToCastTo class we tried to cast to */ public GroovyCastException(Object objectToCast, Class classToCastTo) { super(makeMessage(objectToCast,classToCastTo)); }
@Deprecated public static char castToChar(Object object) { if (object instanceof Character) { return (Character) object; } else if (object instanceof Number) { Number value = (Number) object; return (char) value.intValue(); } else { String text = object.toString(); if (text.length() == 1) { return text.charAt(0); } else { throw new GroovyCastException(text, char.class); } } }
public GroovyCastException(Object objectToCast, Class classToCastTo) { super(makeMessage(objectToCast,classToCastTo)); }
public static Number castToNumber(Object object, Class type) { if (object instanceof Number) return (Number) object; if (object instanceof Character) { return (int) (Character) object; } if (object instanceof GString) { String c = ((GString) object).toString(); if (c.length() == 1) { return (int) c.charAt(0); } else { throw new GroovyCastException(c, type); } } if (object instanceof String) { String c = (String) object; if (c.length() == 1) { return (int) c.charAt(0); } else { throw new GroovyCastException(c, type); } } throw new GroovyCastException(object, type); }
public GroovyCastException(Object objectToCast, Class classToCastTo) { super(makeMessage(objectToCast,classToCastTo)); }
@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); }
public GroovyCastException(Object objectToCast, Class classToCastTo) { super(makeMessage(objectToCast,classToCastTo)); }
answer = (Collection) type.newInstance(); } catch (Exception e) { throw new GroovyCastException("Could not instantiate instance of: " + type.getName() + ". Reason: " + e);
/** * @param objectToCast object we tried to cast * @param classToCastTo class we tried to cast to * @param cause not kept but we pass on message from this Exception if any */ public GroovyCastException(Object objectToCast, Class classToCastTo, Exception cause) { super(makeMessage(objectToCast,classToCastTo) + " due to: " + cause.getClass().getName() + (cause.getMessage() == null ? "" : ": " + cause.getMessage())); }
/** * 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()); } } }
private static Object castToPrimitive(Object object, Class type) { if (type == boolean.class) { return booleanUnbox(object); } else if (type == byte.class) { return byteUnbox(object); } else if (type == char.class) { return charUnbox(object); } else if (type == short.class) { return shortUnbox(object); } else if (type == int.class) { return intUnbox(object); } else if (type == long.class) { return longUnbox(object); } else if (type == float.class) { return floatUnbox(object); } else if (type == double.class) { Double answer = doubleUnbox(object); //throw a runtime exception if conversion would be out-of-range for the type. if (!(object instanceof Double) && (answer == Double.NEGATIVE_INFINITY || answer == Double.POSITIVE_INFINITY)) { throw new GroovyRuntimeException("Automatic coercion of " + object.getClass().getName() + " value " + object + " to double failed. Value is out of range."); } return answer; } //nothing else possible throw new GroovyCastException(object, type); }
return (T) ProxyGenerator.INSTANCE.instantiateAggregateFromBaseClass(cl, clazz); } catch (GroovyRuntimeException cause) { throw new GroovyCastException("Error casting closure to " + clazz.getName() + ", Reason: " + cause.getMessage());
gce = new GroovyCastException(object, type, nested); } else { gce = new GroovyCastException(object, type);