Search in sources :

Example 41 with GroovyRuntimeException

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

the class ExtensionModuleScanner method scanExtensionModuleFromMetaInf.

private void scanExtensionModuleFromMetaInf(final URL metadata) {
    Properties properties = new Properties();
    InputStream inStream = null;
    try {
        inStream = metadata.openStream();
        properties.load(inStream);
    } catch (IOException e) {
        throw new GroovyRuntimeException("Unable to load module META-INF descriptor", e);
    } finally {
        closeQuietly(inStream);
    }
    scanExtensionModuleFromProperties(properties);
}
Also used : InputStream(java.io.InputStream) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException)

Example 42 with GroovyRuntimeException

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

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)

Example 43 with GroovyRuntimeException

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

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

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

the class DataSet method visit.

private void visit(Closure closure, CodeVisitorSupport visitor) {
    if (closure != null) {
        ClassNode classNode = closure.getMetaClass().getClassNode();
        if (classNode == null) {
            throw new GroovyRuntimeException("DataSet unable to evaluate expression. AST not available for closure: " + closure.getMetaClass().getTheClass().getName() + ". Is the source code on the classpath?");
        }
        List methods = classNode.getDeclaredMethods("doCall");
        if (!methods.isEmpty()) {
            MethodNode method = (MethodNode) methods.get(0);
            if (method != null) {
                Statement statement = method.getCode();
                if (statement != null) {
                    statement.visit(visitor);
                }
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) Statement(org.codehaus.groovy.ast.stmt.Statement) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 45 with GroovyRuntimeException

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

the class GroovyAssert method shouldFail.

/**
     * Asserts that the given script fails when it is evaluated
     *
     * @param script the script expected to fail
     * @return the caught exception
     */
public static Throwable shouldFail(String script) {
    boolean failed = false;
    Throwable th = null;
    try {
        GroovyShell shell = new GroovyShell();
        shell.evaluate(script, genericScriptName());
    } catch (GroovyRuntimeException gre) {
        failed = true;
        th = ScriptBytecodeAdapter.unwrap(gre);
    } catch (Throwable e) {
        failed = true;
        th = e;
    }
    assertTrue("Script should have failed", failed);
    return th;
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyShell(groovy.lang.GroovyShell)

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