Search in sources :

Example 11 with RaiseException

use of org.jruby.exceptions.RaiseException in project enumerable by hraberg.

the class JRubyTest method convertedRubyProcRaisesArgumentErrorWhenCalledWithTooManyArguments.

@Test(expected = RaiseException.class)
public void convertedRubyProcRaisesArgumentErrorWhenCalledWithTooManyArguments() throws ScriptException {
    Ruby ruby = Ruby.getGlobalRuntime();
    try {
        RubyProc proc = toProc(Lambda.λ(s, s.toUpperCase()));
        proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] { ruby.newString("hello"), ruby.newString("world") });
    } catch (RaiseException e) {
        assertEquals(ruby.getArgumentError(), e.getException().getType());
        throw e;
    }
}
Also used : RubyProc(org.jruby.RubyProc) RaiseException(org.jruby.exceptions.RaiseException) Ruby(org.jruby.Ruby) LambdaJRuby(org.enumerable.lambda.support.jruby.LambdaJRuby) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 12 with RaiseException

use of org.jruby.exceptions.RaiseException in project qi4j-sdk by Qi4j.

the class JRubyMixin method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
        // Get Ruby object for declaring class of the method
        Class declaringClass = method.getDeclaringClass();
        IRubyObject rubyObject = rubyObjects.get(declaringClass);
        // If not yet created, create one
        if (rubyObject == null) {
            // Create object instance
            try {
                rubyObject = runtime.evalScriptlet(declaringClass.getSimpleName() + ".new()");
            } catch (RaiseException e) {
                if (e.getException() instanceof RubyNameError) {
                    // Initialize Ruby class
                    String script = getFunction(method);
                    runtime.evalScriptlet(script);
                    // Try creating a Ruby instance again
                    rubyObject = runtime.evalScriptlet(declaringClass.getSimpleName() + ".new()");
                } else {
                    throw e;
                }
            }
            // Set @this variable to Composite
            IRubyObject meRuby = JavaEmbedUtils.javaToRuby(runtime, me);
            RubyClass rubyClass = meRuby.getMetaClass();
            if (!rubyClass.isFrozen()) {
                SetterDynamicMethod setter = new SetterDynamicMethod(runtime.getObjectSpaceModule(), Visibility.PUBLIC, null);
                GetterDynamicMethod getter = new GetterDynamicMethod(runtime.getObjectSpaceModule(), Visibility.PUBLIC, null);
                Method[] compositeMethods = me.getClass().getInterfaces()[0].getMethods();
                for (Method compositeMethod : compositeMethods) {
                    if (Property.class.isAssignableFrom(compositeMethod.getReturnType())) {
                        rubyClass.addMethod(compositeMethod.getName() + "=", setter);
                        rubyClass.addMethod(compositeMethod.getName(), getter);
                    }
                }
                rubyClass.freeze(ThreadContext.newContext(runtime));
            }
            RubyObjectAdapter rubyObjectAdapter = JavaEmbedUtils.newObjectAdapter();
            rubyObjectAdapter.setInstanceVariable(rubyObject, "@this", meRuby);
            rubyObjects.put(declaringClass, rubyObject);
        }
        // Convert method arguments and invoke the method
        IRubyObject rubyResult;
        if (args != null) {
            IRubyObject[] rubyArgs = new IRubyObject[args.length];
            for (int i = 0; i < args.length; i++) {
                Object arg = args[i];
                rubyArgs[i] = JavaEmbedUtils.javaToRuby(runtime, arg);
            }
            rubyResult = rubyObject.callMethod(runtime.getCurrentContext(), method.getName(), rubyArgs);
        } else {
            rubyResult = rubyObject.callMethod(runtime.getCurrentContext(), method.getName());
        }
        // Convert result to Java
        Object result = JavaEmbedUtils.rubyToJava(runtime, rubyResult, method.getReturnType());
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : RaiseException(org.jruby.exceptions.RaiseException) DynamicMethod(org.jruby.internal.runtime.methods.DynamicMethod) Method(java.lang.reflect.Method) IRubyObject(org.jruby.runtime.builtin.IRubyObject) RaiseException(org.jruby.exceptions.RaiseException) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 13 with RaiseException

use of org.jruby.exceptions.RaiseException in project nokogiri by sparklemotion.

the class XmlDomParserContext method getDocumentWithErrorsOrRaiseException.

public XmlDocument getDocumentWithErrorsOrRaiseException(ThreadContext context, RubyClass klazz, Exception ex) {
    if (options.recover) {
        XmlDocument xmlDocument = getInterruptedOrNewXmlDocument(context, klazz);
        this.addErrorsIfNecessary(context, xmlDocument);
        XmlSyntaxError xmlSyntaxError = XmlSyntaxError.createXMLSyntaxError(context.runtime);
        xmlSyntaxError.setException(ex);
        ((RubyArray) xmlDocument.getInstanceVariable("@errors")).append(xmlSyntaxError);
        return xmlDocument;
    } else {
        XmlSyntaxError xmlSyntaxError = XmlSyntaxError.createXMLSyntaxError(context.runtime);
        xmlSyntaxError.setException(ex);
        throw new RaiseException(xmlSyntaxError);
    }
}
Also used : RubyArray(org.jruby.RubyArray) XmlSyntaxError(nokogiri.XmlSyntaxError) RaiseException(org.jruby.exceptions.RaiseException) XmlDocument(nokogiri.XmlDocument)

Example 14 with RaiseException

use of org.jruby.exceptions.RaiseException in project nokogiri by sparklemotion.

the class XmlSchema method from_document.

/*
     * call-seq:
     *  from_document(doc)
     *
     * Create a new Schema from the Nokogiri::XML::Document +doc+
     */
@JRubyMethod(meta = true)
public static IRubyObject from_document(ThreadContext context, IRubyObject klazz, IRubyObject document) {
    XmlDocument doc = ((XmlDocument) ((XmlNode) document).document(context));
    RubyArray errors = (RubyArray) doc.getInstanceVariable("@errors");
    if (!errors.isEmpty()) {
        throw new RaiseException((XmlSyntaxError) errors.first());
    }
    DOMSource source = new DOMSource(doc.getDocument());
    IRubyObject uri = doc.url(context);
    if (!uri.isNil()) {
        source.setSystemId(uri.convertToString().asJavaString());
    }
    return getSchema(context, (RubyClass) klazz, source);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) RubyArray(org.jruby.RubyArray) RaiseException(org.jruby.exceptions.RaiseException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 15 with RaiseException

use of org.jruby.exceptions.RaiseException in project nokogiri by sparklemotion.

the class XmlNode method process_xincludes.

/**
     * call-seq:
     *   process_xincludes(options)
     *
     * Loads and substitutes all xinclude elements below the node. The
     * parser context will be initialized with +options+.
     * 
     */
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject process_xincludes(ThreadContext context, IRubyObject options) {
    XmlDocument xmlDocument = (XmlDocument) document(context);
    RubyArray errors = (RubyArray) xmlDocument.getInstanceVariable("@errors");
    while (errors.getLength() > 0) {
        XmlSyntaxError error = (XmlSyntaxError) errors.shift(context);
        if (error.toString().contains("Include operation failed")) {
            throw new RaiseException(error);
        }
    }
    return this;
}
Also used : RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) RaiseException(org.jruby.exceptions.RaiseException) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

RaiseException (org.jruby.exceptions.RaiseException)16 JRubyMethod (org.jruby.anno.JRubyMethod)8 RubyArray (org.jruby.RubyArray)7 IRubyObject (org.jruby.runtime.builtin.IRubyObject)7 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 ClosedStreamException (nokogiri.internals.ClosedStreamException)4 RubyException (org.jruby.RubyException)4 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)2 DOMSource (javax.xml.transform.dom.DOMSource)2 XmlDocument (nokogiri.XmlDocument)2 XmlSyntaxError (nokogiri.XmlSyntaxError)2 NokogiriHelpers.nodeArrayToRubyArray (nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray)2 ClojureTest (org.enumerable.lambda.support.clojure.ClojureTest)2 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)2 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)2 LambdaJRuby (org.enumerable.lambda.support.jruby.LambdaJRuby)2 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)2 Ruby (org.jruby.Ruby)2 RubyProc (org.jruby.RubyProc)2