Search in sources :

Example 1 with GroovyRuntimeException

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

the class SignatureCodecVersion1 method encode.

public String encode(final ClassNode node) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
    DataOutputStream dos = new DataOutputStream(baos);
    StringWriter wrt = new StringWriter();
    String encoded = null;
    try {
        doEncode(node, dos);
        EncodingGroovyMethods.encodeBase64(baos.toByteArray()).writeTo(wrt);
        encoded = wrt.toString();
    } catch (IOException e) {
        throw new GroovyRuntimeException("Unable to serialize type information", e);
    }
    return encoded;
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException)

Example 2 with GroovyRuntimeException

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

the class PropertyBinding method updateTargetValue.

public void updateTargetValue(final Object newValue) {
    Runnable runnable = new Runnable() {

        public void run() {
            Object sourceValue = getSourceValue();
            // if (isNonChangeCheck()) {
            if ((sourceValue == null && newValue == null) || DefaultTypeTransformation.compareEqual(sourceValue, newValue)) {
                // not a change, don't fire it
                return;
            }
            // }
            setBeanProperty(newValue);
        }
    };
    switch(updateStrategy) {
        case MIXED:
            if (SwingUtilities.isEventDispatchThread()) {
                runnable.run();
            } else {
                SwingUtilities.invokeLater(runnable);
            }
            break;
        case ASYNC:
            SwingUtilities.invokeLater(runnable);
            break;
        case SYNC:
            if (SwingUtilities.isEventDispatchThread()) {
                runnable.run();
            } else {
                try {
                    SwingUtilities.invokeAndWait(runnable);
                } catch (InterruptedException e) {
                    LOG.log(Level.WARNING, "Error notifying propertyChangeListener", e);
                    throw new GroovyRuntimeException(e);
                } catch (InvocationTargetException e) {
                    LOG.log(Level.WARNING, "Error notifying propertyChangeListener", e.getTargetException());
                    throw new GroovyRuntimeException(e.getTargetException());
                }
            }
            break;
        case SAME:
            runnable.run();
            break;
        case OUTSIDE:
            if (SwingUtilities.isEventDispatchThread()) {
                DEFAULT_EXECUTOR_SERVICE.submit(runnable);
            } else {
                runnable.run();
            }
            break;
        case DEFER:
            DEFAULT_EXECUTOR_SERVICE.submit(runnable);
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with GroovyRuntimeException

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

the class XmlTemplateEngine method createTemplate.

public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException {
    Node root;
    try {
        root = xmlParser.parse(reader);
    } catch (SAXException e) {
        throw new RuntimeException("Parsing XML source failed.", e);
    }
    if (root == null) {
        throw new IOException("Parsing XML source failed: root node is null.");
    }
    StringWriter writer = new StringWriter(1024);
    writer.write("/* Generated by XmlTemplateEngine */\n");
    new GspPrinter(new PrintWriter(writer), indentation).print(root);
    Script script;
    try {
        script = groovyShell.parse(writer.toString(), "XmlTemplateScript" + counter++ + ".groovy");
    } catch (Exception e) {
        throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
    }
    return new XmlTemplate(script);
}
Also used : Script(groovy.lang.Script) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) StringWriter(java.io.StringWriter) Node(groovy.util.Node) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SAXException(org.xml.sax.SAXException) PrintWriter(java.io.PrintWriter)

Example 4 with GroovyRuntimeException

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

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

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

the class BinaryExpressionHelper method eval.

public void eval(BinaryExpression expression) {
    switch(expression.getOperation().getType()) {
        case // = assignment
        EQUAL:
            evaluateEqual(expression, false);
            break;
        case // ==
        COMPARE_EQUAL:
            evaluateCompareExpression(compareEqualMethod, expression);
            break;
        case COMPARE_NOT_EQUAL:
            evaluateCompareExpression(compareNotEqualMethod, expression);
            break;
        case COMPARE_TO:
            evaluateCompareTo(expression);
            break;
        case COMPARE_GREATER_THAN:
            evaluateCompareExpression(compareGreaterThanMethod, expression);
            break;
        case COMPARE_GREATER_THAN_EQUAL:
            evaluateCompareExpression(compareGreaterThanEqualMethod, expression);
            break;
        case COMPARE_LESS_THAN:
            evaluateCompareExpression(compareLessThanMethod, expression);
            break;
        case COMPARE_LESS_THAN_EQUAL:
            evaluateCompareExpression(compareLessThanEqualMethod, expression);
            break;
        case LOGICAL_AND:
            evaluateLogicalAndExpression(expression);
            break;
        case LOGICAL_OR:
            evaluateLogicalOrExpression(expression);
            break;
        case BITWISE_AND:
            evaluateBinaryExpression("and", expression);
            break;
        case BITWISE_AND_EQUAL:
            evaluateBinaryExpressionWithAssignment("and", expression);
            break;
        case BITWISE_OR:
            evaluateBinaryExpression("or", expression);
            break;
        case BITWISE_OR_EQUAL:
            evaluateBinaryExpressionWithAssignment("or", expression);
            break;
        case BITWISE_XOR:
            evaluateBinaryExpression("xor", expression);
            break;
        case BITWISE_XOR_EQUAL:
            evaluateBinaryExpressionWithAssignment("xor", expression);
            break;
        case PLUS:
            evaluateBinaryExpression("plus", expression);
            break;
        case PLUS_EQUAL:
            evaluateBinaryExpressionWithAssignment("plus", expression);
            break;
        case MINUS:
            evaluateBinaryExpression("minus", expression);
            break;
        case MINUS_EQUAL:
            evaluateBinaryExpressionWithAssignment("minus", expression);
            break;
        case MULTIPLY:
            evaluateBinaryExpression("multiply", expression);
            break;
        case MULTIPLY_EQUAL:
            evaluateBinaryExpressionWithAssignment("multiply", expression);
            break;
        case DIVIDE:
            evaluateBinaryExpression("div", expression);
            break;
        case DIVIDE_EQUAL:
            //SPG don't use divide since BigInteger implements directly
            //and we want to dispatch through DefaultGroovyMethods to get a BigDecimal result
            evaluateBinaryExpressionWithAssignment("div", expression);
            break;
        case INTDIV:
            evaluateBinaryExpression("intdiv", expression);
            break;
        case INTDIV_EQUAL:
            evaluateBinaryExpressionWithAssignment("intdiv", expression);
            break;
        case MOD:
            evaluateBinaryExpression("mod", expression);
            break;
        case MOD_EQUAL:
            evaluateBinaryExpressionWithAssignment("mod", expression);
            break;
        case POWER:
            evaluateBinaryExpression("power", expression);
            break;
        case POWER_EQUAL:
            evaluateBinaryExpressionWithAssignment("power", expression);
            break;
        case LEFT_SHIFT:
            evaluateBinaryExpression("leftShift", expression);
            break;
        case LEFT_SHIFT_EQUAL:
            evaluateBinaryExpressionWithAssignment("leftShift", expression);
            break;
        case RIGHT_SHIFT:
            evaluateBinaryExpression("rightShift", expression);
            break;
        case RIGHT_SHIFT_EQUAL:
            evaluateBinaryExpressionWithAssignment("rightShift", expression);
            break;
        case RIGHT_SHIFT_UNSIGNED:
            evaluateBinaryExpression("rightShiftUnsigned", expression);
            break;
        case RIGHT_SHIFT_UNSIGNED_EQUAL:
            evaluateBinaryExpressionWithAssignment("rightShiftUnsigned", expression);
            break;
        case KEYWORD_INSTANCEOF:
            evaluateInstanceof(expression);
            break;
        case FIND_REGEX:
            evaluateCompareExpression(findRegexMethod, expression);
            break;
        case MATCH_REGEX:
            evaluateCompareExpression(matchRegexMethod, expression);
            break;
        case LEFT_SQUARE_BRACKET:
            if (controller.getCompileStack().isLHS()) {
                evaluateEqual(expression, false);
            } else {
                evaluateBinaryExpression("getAt", expression);
            }
            break;
        case KEYWORD_IN:
            evaluateCompareExpression(isCaseMethod, expression);
            break;
        case COMPARE_IDENTICAL:
        case COMPARE_NOT_IDENTICAL:
            Token op = expression.getOperation();
            Throwable cause = new SyntaxException("Operator " + op + " not supported", op.getStartLine(), op.getStartColumn(), op.getStartLine(), op.getStartColumn() + 3);
            throw new GroovyRuntimeException(cause);
        default:
            throw new GroovyBugError("Operation: " + expression.getOperation() + " not supported");
    }
}
Also used : SyntaxException(org.codehaus.groovy.syntax.SyntaxException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyBugError(org.codehaus.groovy.GroovyBugError) Token(org.codehaus.groovy.syntax.Token)

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