Search in sources :

Example 16 with GroovyRuntimeException

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

the class StandardPropertiesModuleFactory method newModule.

@Override
@SuppressWarnings("unchecked")
public ExtensionModule newModule(final Properties properties, final ClassLoader classLoader) {
    String factoryName = properties.getProperty(MODULE_FACTORY_KEY);
    if (factoryName != null) {
        try {
            Class<? extends PropertiesModuleFactory> factoryClass = (Class<? extends PropertiesModuleFactory>) classLoader.loadClass(factoryName);
            PropertiesModuleFactory delegate = factoryClass.newInstance();
            return delegate.newModule(properties, classLoader);
        } catch (ClassNotFoundException e) {
            throw new GroovyRuntimeException("Unable to load module factory [" + factoryName + "]", e);
        } catch (InstantiationException e) {
            throw new GroovyRuntimeException("Unable to instantiate module factory [" + factoryName + "]", e);
        } catch (IllegalAccessException e) {
            throw new GroovyRuntimeException("Unable to instantiate module factory [" + factoryName + "]", e);
        }
    }
    return MetaInfExtensionModule.newModule(properties, classLoader);
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException)

Example 17 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy-core by groovy.

the class SourceExtensionHandler method getRegisteredExtensions.

public static Set<String> getRegisteredExtensions(ClassLoader loader) {
    Set<String> extensions = new LinkedHashSet<String>();
    extensions.add("groovy");
    try {
        Enumeration<URL> globalServices = loader.getResources("META-INF/services/org.codehaus.groovy.source.Extensions");
        while (globalServices.hasMoreElements()) {
            BufferedReader svcIn = null;
            URL service = globalServices.nextElement();
            try {
                svcIn = new BufferedReader(new InputStreamReader(service.openStream()));
                String extension = svcIn.readLine();
                while (extension != null) {
                    extension = extension.trim();
                    if (!extension.startsWith("#") && extension.length() > 0) {
                        extensions.add(extension);
                    }
                    extension = svcIn.readLine();
                }
            } catch (IOException ex) {
                throw new GroovyRuntimeException("IO Exception attempting to load registered source extension " + service.toExternalForm() + ". Exception: " + ex.toString());
            } finally {
                if (svcIn != null)
                    svcIn.close();
            }
        }
    } catch (IOException ex) {
        throw new GroovyRuntimeException("IO Exception getting registered source extensions. Exception: " + ex.toString());
    }
    return extensions;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) URL(java.net.URL)

Example 18 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy-core by groovy.

the class JUnit4Utils method realRunJUnit4Test.

/**
     * Utility method to run a JUnit4 test.
     *
     * @param scriptClass the class we want to run as a test
     * @return the result of running the test
     */
static Object realRunJUnit4Test(Class scriptClass, GroovyClassLoader loader) {
    try {
        Class junitCoreClass = loader.loadClass("org.junit.runner.JUnitCore");
        Object result = InvokerHelper.invokeStaticMethod(junitCoreClass, "runClasses", new Object[] { scriptClass });
        System.out.print("JUnit 4 Runner, Tests: " + InvokerHelper.getProperty(result, "runCount"));
        System.out.print(", Failures: " + InvokerHelper.getProperty(result, "failureCount"));
        System.out.println(", Time: " + InvokerHelper.getProperty(result, "runTime"));
        List failures = (List) InvokerHelper.getProperty(result, "failures");
        for (int i = 0; i < failures.size(); i++) {
            Object f = failures.get(i);
            System.out.println("Test Failure: " + InvokerHelper.getProperty(f, "description"));
            System.out.println(InvokerHelper.getProperty(f, "trace"));
        }
        return result;
    } catch (ClassNotFoundException e) {
        throw new GroovyRuntimeException("Error running JUnit 4 test.", e);
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) List(java.util.List)

Example 19 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy-core by groovy.

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 20 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy-core by groovy.

the class ConversionHandler method invoke.

/**
     * This method is a default implementation for the invoke method given in
     * InvocationHandler. Any call to a method with a declaring class that is
     * not Object, excluding toString() and default methods is redirected to invokeCustom.
     * <p>
     * Methods like equals and hashcode are called on the class itself instead
     * of the delegate because they are considered fundamental methods that should
     * not be overwritten. The toString() method gets special treatment as it is
     * deemed to be a method that you might wish to override when called from Groovy.
     * Interface default methods from Java 8 on the other hand are considered being
     * default implementations you don't normally want to change. So they are called
     * directly too
     * </p><p>
     * In many scenarios, it is better to overwrite the invokeCustom method where
     * the core Object related methods are filtered out.
     *</p>
     * @param proxy  the proxy
     * @param method the method
     * @param args   the arguments
     * @return the result of the invocation by method or delegate
     * @throws Throwable if caused by the delegate or the method
     * @see #invokeCustom(Object, Method, Object[])
     * @see InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
     */
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    VMPlugin plugin = VMPluginFactory.getPlugin();
    if (plugin.getVersion() >= 7 && isDefaultMethod(method)) {
        Object handle = handleCache.get(method);
        if (handle == null) {
            handle = plugin.getInvokeSpecialHandle(method, proxy);
            handleCache.put(method, handle);
        }
        return plugin.invokeHandle(handle, args);
    }
    if (!checkMethod(method)) {
        try {
            return invokeCustom(proxy, method, args);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    }
    try {
        return method.invoke(this, args);
    } catch (InvocationTargetException ite) {
        throw ite.getTargetException();
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) VMPlugin(org.codehaus.groovy.vmplugin.VMPlugin) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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