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;
}
}
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;
}
}
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);
}
}
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);
}
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;
}
Aggregations