Search in sources :

Example 11 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy by apache.

the class BuilderSupport method doInvokeMethod.

protected Object doInvokeMethod(String methodName, Object name, Object args) {
    Object node = null;
    Closure closure = null;
    List list = InvokerHelper.asList(args);
    switch(list.size()) {
        case 0:
            node = proxyBuilder.createNode(name);
            break;
        case 1:
            {
                Object object = list.get(0);
                if (object instanceof Map) {
                    node = proxyBuilder.createNode(name, (Map) object);
                } else if (object instanceof Closure) {
                    closure = (Closure) object;
                    node = proxyBuilder.createNode(name);
                } else {
                    node = proxyBuilder.createNode(name, object);
                }
            }
            break;
        case 2:
            {
                Object object1 = list.get(0);
                Object object2 = list.get(1);
                if (object1 instanceof Map) {
                    if (object2 instanceof Closure) {
                        closure = (Closure) object2;
                        node = proxyBuilder.createNode(name, (Map) object1);
                    } else {
                        node = proxyBuilder.createNode(name, (Map) object1, object2);
                    }
                } else {
                    if (object2 instanceof Closure) {
                        closure = (Closure) object2;
                        node = proxyBuilder.createNode(name, object1);
                    } else if (object2 instanceof Map) {
                        node = proxyBuilder.createNode(name, (Map) object2, object1);
                    } else {
                        throw new MissingMethodException(name.toString(), getClass(), list.toArray(), false);
                    }
                }
            }
            break;
        case 3:
            {
                Object arg0 = list.get(0);
                Object arg1 = list.get(1);
                Object arg2 = list.get(2);
                if (arg0 instanceof Map && arg2 instanceof Closure) {
                    closure = (Closure) arg2;
                    node = proxyBuilder.createNode(name, (Map) arg0, arg1);
                } else if (arg1 instanceof Map && arg2 instanceof Closure) {
                    closure = (Closure) arg2;
                    node = proxyBuilder.createNode(name, (Map) arg1, arg0);
                } else {
                    throw new MissingMethodException(name.toString(), getClass(), list.toArray(), false);
                }
            }
            break;
        default:
            {
                throw new MissingMethodException(name.toString(), getClass(), list.toArray(), false);
            }
    }
    if (current != null) {
        proxyBuilder.setParent(current, node);
    }
    if (closure != null) {
        // push new node on stack
        Object oldCurrent = getCurrent();
        setCurrent(node);
        // let's register the builder as the delegate
        setClosureDelegate(closure, node);
        try {
            closure.call();
        } catch (Exception e) {
            throw new GroovyRuntimeException(e);
        }
        setCurrent(oldCurrent);
    }
    proxyBuilder.nodeCompleted(current, node);
    return proxyBuilder.postNodeCompletion(current, node);
}
Also used : MissingMethodException(groovy.lang.MissingMethodException) Closure(groovy.lang.Closure) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) List(java.util.List) Map(java.util.Map) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) MissingMethodException(groovy.lang.MissingMethodException)

Example 12 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy by apache.

the class IndentPrinter method println.

/**
     * Prints a string followed by an end of line character.
     *
     * @param  text String to be written
     */
public void println(String text) {
    try {
        if (autoIndent)
            printIndent();
        out.write(text);
        println();
    } catch (IOException ioe) {
        throw new GroovyRuntimeException(ioe);
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException)

Example 13 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy by apache.

the class ProxyGeneratorAdapter method delegatingProxy.

@SuppressWarnings("unchecked")
public GroovyObject delegatingProxy(Object delegate, Map<Object, Object> map, Object... constructorArgs) {
    if (constructorArgs == null && cachedNoArgConstructor != null) {
        // if there isn't any argument, we can make invocation faster using the cached constructor
        try {
            return (GroovyObject) cachedNoArgConstructor.newInstance(map, delegate);
        } catch (InstantiationException e) {
            throw new GroovyRuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new GroovyRuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new GroovyRuntimeException(e);
        }
    }
    if (constructorArgs == null)
        constructorArgs = EMPTY_ARGS;
    Object[] values = new Object[constructorArgs.length + 2];
    System.arraycopy(constructorArgs, 0, values, 0, constructorArgs.length);
    values[values.length - 2] = map;
    values[values.length - 1] = delegate;
    return DefaultGroovyMethods.<GroovyObject>newInstance(cachedClass, values);
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyObject(groovy.lang.GroovyObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) GroovyObject(groovy.lang.GroovyObject)

Example 14 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy by apache.

the class ProcessGroovyMethods method pipeTo.

/**
     * Allows one Process to asynchronously pipe data to another Process.
     *
     * @param left  a Process instance
     * @param right a Process to pipe output to
     * @return the second Process to allow chaining
     * @throws java.io.IOException if an IOException occurs.
     * @since 1.5.2
     */
public static Process pipeTo(final Process left, final Process right) throws IOException {
    new Thread(new Runnable() {

        public void run() {
            InputStream in = new BufferedInputStream(getIn(left));
            OutputStream out = new BufferedOutputStream(getOut(right));
            byte[] buf = new byte[8192];
            int next;
            try {
                while ((next = in.read(buf)) != -1) {
                    out.write(buf, 0, next);
                }
            } catch (IOException e) {
                throw new GroovyRuntimeException("exception while reading process stream", e);
            } finally {
                closeWithWarning(out);
            }
        }
    }).start();
    return right;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 15 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy by apache.

the class MetaInfExtensionModule method newModule.

public static MetaInfExtensionModule newModule(final Properties properties, final ClassLoader loader) {
    String name = properties.getProperty(PropertiesModuleFactory.MODULE_NAME_KEY);
    if (name == null)
        throw new GroovyRuntimeException("Module file hasn't set the module name using key [" + PropertiesModuleFactory.MODULE_NAME_KEY + "]");
    String version = properties.getProperty(PropertiesModuleFactory.MODULE_VERSION_KEY);
    if (version == null)
        throw new GroovyRuntimeException("Module file hasn't set the module version using key [" + PropertiesModuleFactory.MODULE_VERSION_KEY + "]");
    String[] extensionClasses = properties.getProperty(MODULE_INSTANCE_CLASSES_KEY, "").trim().split("[,; ]");
    String[] staticExtensionClasses = properties.getProperty(MODULE_STATIC_CLASSES_KEY, "").trim().split("[,; ]");
    List<Class> instanceClasses = new ArrayList<Class>(extensionClasses.length);
    List<Class> staticClasses = new ArrayList<Class>(staticExtensionClasses.length);
    List<String> errors = new LinkedList<String>();
    for (String extensionClass : extensionClasses) {
        try {
            extensionClass = extensionClass.trim();
            if (extensionClass.length() > 0) {
                instanceClasses.add(loader.loadClass(extensionClass));
            }
        } catch (ClassNotFoundException e) {
            errors.add(extensionClass);
        } catch (NoClassDefFoundError e) {
            errors.add(extensionClass);
        } catch (UnsupportedClassVersionError e) {
            errors.add(extensionClass);
        }
    }
    for (String extensionClass : staticExtensionClasses) {
        try {
            extensionClass = extensionClass.trim();
            if (extensionClass.length() > 0) {
                staticClasses.add(loader.loadClass(extensionClass));
            }
        } catch (ClassNotFoundException e) {
            errors.add(extensionClass);
        } catch (NoClassDefFoundError e) {
            errors.add(extensionClass);
        } catch (UnsupportedClassVersionError e) {
            errors.add(extensionClass);
        }
    }
    if (!errors.isEmpty()) {
        for (String error : errors) {
            LOG.warning("Module [" + name + "] - Unable to load extension class [" + error + "]");
        }
    }
    return new MetaInfExtensionModule(name, version, instanceClasses, staticClasses);
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Aggregations

GroovyRuntimeException (groovy.lang.GroovyRuntimeException)59 IOException (java.io.IOException)15 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 GroovyObject (groovy.lang.GroovyObject)8 ArrayList (java.util.ArrayList)6 Closure (groovy.lang.Closure)5 InputStream (java.io.InputStream)5 List (java.util.List)5 GroovyShell (groovy.lang.GroovyShell)4 MetaClass (groovy.lang.MetaClass)4 Map (java.util.Map)4 ClassNode (org.codehaus.groovy.ast.ClassNode)4 MethodNode (org.codehaus.groovy.ast.MethodNode)4 MetaMethod (groovy.lang.MetaMethod)3 Script (groovy.lang.Script)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 URL (java.net.URL)3 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)2 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)2