Search in sources :

Example 1 with GroovyCastException

use of org.codehaus.groovy.runtime.typehandling.GroovyCastException in project groovy-core by groovy.

the class DefaultGroovyMethods method asType.

/**
     * Converts the given collection to another type. A default concrete
     * type is used for List, Set, or SortedSet. If the given type has
     * a constructor taking a collection, that is used. Otherwise, the
     * call is deferred to {link #asType(Object,Class)}.  If this
     * collection is already of the given type, the same instance is
     * returned.
     *
     * @param col   a collection
     * @param clazz the desired class
     * @return the object resulting from this type conversion
     * @see #asType(java.lang.Object, java.lang.Class)
     * @since 1.0
     */
@SuppressWarnings("unchecked")
public static <T> T asType(Collection col, Class<T> clazz) {
    if (col.getClass() == clazz) {
        return (T) col;
    }
    if (clazz == List.class) {
        return (T) asList((Iterable) col);
    }
    if (clazz == Set.class) {
        if (col instanceof Set)
            return (T) col;
        return (T) new LinkedHashSet(col);
    }
    if (clazz == SortedSet.class) {
        if (col instanceof SortedSet)
            return (T) col;
        return (T) new TreeSet(col);
    }
    if (clazz == Queue.class) {
        if (col instanceof Queue)
            return (T) col;
        return (T) new LinkedList(col);
    }
    if (clazz == Stack.class) {
        if (col instanceof Stack)
            return (T) col;
        final Stack stack = new Stack();
        stack.addAll(col);
        return (T) stack;
    }
    if (clazz != String[].class && ReflectionCache.isArray(clazz)) {
        try {
            return (T) asArrayType(col, clazz);
        } catch (GroovyCastException e) {
        /* ignore */
        }
    }
    Object[] args = { col };
    try {
        return (T) InvokerHelper.invokeConstructorOf(clazz, args);
    } catch (Exception e) {
    // ignore, the constructor that takes a Collection as an argument may not exist
    }
    if (Collection.class.isAssignableFrom(clazz)) {
        try {
            Collection result = (Collection) InvokerHelper.invokeConstructorOf(clazz, null);
            result.addAll(col);
            return (T) result;
        } catch (Exception e) {
        // ignore, the no arg constructor might not exist.
        }
    }
    return asType((Object) col, clazz);
}
Also used : URISyntaxException(java.net.URISyntaxException) GroovyCastException(org.codehaus.groovy.runtime.typehandling.GroovyCastException) MalformedURLException(java.net.MalformedURLException) MissingPropertyExceptionNoStack(org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack) GroovyCastException(org.codehaus.groovy.runtime.typehandling.GroovyCastException) BlockingQueue(java.util.concurrent.BlockingQueue)

Example 2 with GroovyCastException

use of org.codehaus.groovy.runtime.typehandling.GroovyCastException in project groovy by apache.

the class DefaultGroovyMethods method asType.

/**
     * Converts the given collection to another type. A default concrete
     * type is used for List, Set, or SortedSet. If the given type has
     * a constructor taking a collection, that is used. Otherwise, the
     * call is deferred to {@link #asType(Object,Class)}.  If this
     * collection is already of the given type, the same instance is
     * returned.
     *
     * @param col   a collection
     * @param clazz the desired class
     * @return the object resulting from this type conversion
     * @see #asType(java.lang.Object, java.lang.Class)
     * @since 1.0
     */
@SuppressWarnings("unchecked")
public static <T> T asType(Collection col, Class<T> clazz) {
    if (col.getClass() == clazz) {
        return (T) col;
    }
    if (clazz == List.class) {
        return (T) asList((Iterable) col);
    }
    if (clazz == Set.class) {
        if (col instanceof Set)
            return (T) col;
        return (T) new LinkedHashSet(col);
    }
    if (clazz == SortedSet.class) {
        if (col instanceof SortedSet)
            return (T) col;
        return (T) new TreeSet(col);
    }
    if (clazz == Queue.class) {
        if (col instanceof Queue)
            return (T) col;
        return (T) new LinkedList(col);
    }
    if (clazz == Stack.class) {
        if (col instanceof Stack)
            return (T) col;
        final Stack stack = new Stack();
        stack.addAll(col);
        return (T) stack;
    }
    if (clazz != String[].class && ReflectionCache.isArray(clazz)) {
        try {
            return (T) asArrayType(col, clazz);
        } catch (GroovyCastException e) {
        /* ignore */
        }
    }
    Object[] args = { col };
    try {
        return (T) InvokerHelper.invokeConstructorOf(clazz, args);
    } catch (Exception e) {
    // ignore, the constructor that takes a Collection as an argument may not exist
    }
    if (Collection.class.isAssignableFrom(clazz)) {
        try {
            Collection result = (Collection) InvokerHelper.invokeConstructorOf(clazz, null);
            result.addAll(col);
            return (T) result;
        } catch (Exception e) {
        // ignore, the no arg constructor might not exist.
        }
    }
    return asType((Object) col, clazz);
}
Also used : URISyntaxException(java.net.URISyntaxException) GroovyCastException(org.codehaus.groovy.runtime.typehandling.GroovyCastException) MalformedURLException(java.net.MalformedURLException) MissingPropertyExceptionNoStack(org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack) GroovyCastException(org.codehaus.groovy.runtime.typehandling.GroovyCastException) BlockingQueue(java.util.concurrent.BlockingQueue)

Aggregations

MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 MissingPropertyExceptionNoStack (org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack)2 GroovyCastException (org.codehaus.groovy.runtime.typehandling.GroovyCastException)2