Java Code Examples for org.jruby.javasupport.JavaEmbedUtils#javaToRuby()

The following examples show how to use org.jruby.javasupport.JavaEmbedUtils#javaToRuby() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RubyScriptTest.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
@Test(enabled = false)
public void resultsCapturesJavaError() throws Exception {
    RubyScript script = new RubyScript(out, err, converToCode(SCRIPT_CONTENTS_ERROR_FROM_JAVA),
            new File(System.getProperty(Constants.PROP_PROJECT_DIR), "dummyfile.rb").getAbsolutePath(), false, null,
            Constants.FRAMEWORK_SWING);
    script.setDriverURL("");
    Ruby interpreter = script.getInterpreter();
    assertTrue("Collector not defined", interpreter.isClassDefined("Collector"));
    RubyClass collectorClass = interpreter.getClass("Collector");
    IRubyObject presult = JavaEmbedUtils.javaToRuby(interpreter, result);
    IRubyObject collector = collectorClass.newInstance(interpreter.getCurrentContext(), new IRubyObject[0], new Block(null));
    IRubyObject rubyObject = interpreter.evalScriptlet("proc { my_function }");
    try {
        collector.callMethod(interpreter.getCurrentContext(), "callprotected", new IRubyObject[] { rubyObject, presult });
    } catch (Throwable t) {

    }
    assertEquals(1, result.failureCount());
    Failure[] failures = result.failures();
    assertTrue("Should end with TestRubyScript.java. but has " + failures[0].getTraceback()[0].fileName,
            failures[0].getTraceback()[0].fileName.endsWith("TestRubyScript.java"));
    assertEquals("throwError", failures[0].getTraceback()[0].functionName);
}
 
Example 2
Source File: ConverterProxy.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@JRubyMethod(required = 1, optional = 2)
public IRubyObject convert(ThreadContext context, IRubyObject[] args) {
    ContentNode node = NodeConverter.createASTNode(args[0]);

    T ret = null;
    if (args.length == 1) {
        ret = delegate.convert(
                node,
                null,
                Collections.emptyMap());
    } else if (args.length == 2) {
        ret = delegate.convert(
                node,
                (String) JavaEmbedUtils.rubyToJava(getRuntime(), args[1], String.class),
                Collections.emptyMap());//RubyString.objAsString(context, args[1]).asJavaString());
    } else if (args.length == 3) {
        ret = (T) delegate.convert(
                node,
                (String) JavaEmbedUtils.rubyToJava(getRuntime(), args[1], String.class),
                (Map) JavaEmbedUtils.rubyToJava(getRuntime(), args[2], Map.class));
    }
    this.output = ret;
    return JavaEmbedUtils.javaToRuby(getRuntime(), ret);
}
 
Example 3
Source File: RubyHashUtil.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
private static IRubyObject toRubyObject(Ruby rubyRuntime, Object value) {

        if (value instanceof List) {
            return toRubyArray(rubyRuntime, (List<Object>) value);
        } else if (value instanceof Map) {
        	return convertMapToRubyHashWithStrings(rubyRuntime, (Map<String, Object>) value);
        } else {

            if (isNotARubySymbol(value)) {
                CharSequence stringValue = ((CharSequence) value);

                if (stringValue.length() > 0 && stringValue.charAt(0) == ':') {
                    return RubyUtils.toSymbol(rubyRuntime, stringValue.subSequence(1, stringValue.length()).toString());
                }
            }

            IRubyObject iRubyObject = JavaEmbedUtils.javaToRuby(rubyRuntime, value);
            return iRubyObject;
        }
    }
 
Example 4
Source File: RubyObjectWrapper.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
public IRubyObject getRubyProperty(String propertyName, Object... args) {
    ThreadContext threadContext = runtime.getThreadService().getCurrentContext();

    IRubyObject result;
    if (propertyName.startsWith("@")) {
        if (args != null && args.length > 0) {
            throw new IllegalArgumentException("No args allowed for direct field access");
        }
        result = rubyNode.getInstanceVariables().getInstanceVariable(propertyName);
    } else {
        if (args == null) {
            result = rubyNode.callMethod(threadContext, propertyName);
        } else {
            IRubyObject[] rubyArgs = new IRubyObject[args.length];
            for (int i = 0; i < args.length; i++) {
                if (args[i] instanceof RubyObjectWrapper) {
                    rubyArgs[i] = ((RubyObjectWrapper) args[i]).getRubyObject();
                } else {
                    rubyArgs[i] = JavaEmbedUtils.javaToRuby(runtime, args[i]);
                }
            }
            result = rubyNode.callMethod(threadContext, propertyName, rubyArgs);
        }
    }
    return result;
}
 
Example 5
Source File: RubyScript.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private void defineVariable(String variable, Object value) {
    try {
        GlobalVariable v = new GlobalVariable(interpreter, "$" + variable, JavaEmbedUtils.javaToRuby(interpreter, value));
        interpreter.defineVariable(v, Scope.GLOBAL);
    } catch (Throwable t) {
        throw new ScriptException("Failed to define variable " + variable + " value = " + value + ":" + t.getMessage(), t);
    }
}
 
Example 6
Source File: JRubyScriptUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private IRubyObject[] convertToRuby(Object[] javaArgs) {
	if (javaArgs == null || javaArgs.length == 0) {
		return new IRubyObject[0];
	}
	IRubyObject[] rubyArgs = new IRubyObject[javaArgs.length];
	for (int i = 0; i < javaArgs.length; ++i) {
		rubyArgs[i] = JavaEmbedUtils.javaToRuby(this.ruby, javaArgs[i]);
	}
	return rubyArgs;
}
 
Example 7
Source File: JRubyScriptUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private IRubyObject[] convertToRuby(Object[] javaArgs) {
	if (javaArgs == null || javaArgs.length == 0) {
		return new IRubyObject[0];
	}
	IRubyObject[] rubyArgs = new IRubyObject[javaArgs.length];
	for (int i = 0; i < javaArgs.length; ++i) {
		rubyArgs[i] = JavaEmbedUtils.javaToRuby(this.ruby, javaArgs[i]);
	}
	return rubyArgs;
}
 
Example 8
Source File: AbstractProcessorProxy.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
protected IRubyObject convertProcessorResult(Object o) {
    if (o instanceof ContentNodeImpl) {
        return ((ContentNodeImpl) o).getRubyObject();
    } else {
        return JavaEmbedUtils.javaToRuby(getRuntime(), o);
    }
}
 
Example 9
Source File: RubyAttributesMapDecorator.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
private IRubyObject convertJavaValue(Object value) {
    if (value == null) {
        return null;
    } else if (value instanceof String && ((String) value).startsWith(":")) {
        return rubyHash.getRuntime().getSymbolTable().getSymbol(((String) value).substring(1));
    } else {
        return JavaEmbedUtils.javaToRuby(rubyHash.getRuntime(), value);
    }
}
 
Example 10
Source File: RubyHashMapDecorator.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
private IRubyObject convertJavaValue(Object value) {
    if (value == null) {
        return null;
    } else if (value instanceof String && ((String) value).startsWith(":")) {
        return rubyHash.getRuntime().getSymbolTable().getSymbol(((String) value).substring(1));
    } else {
        return JavaEmbedUtils.javaToRuby(rubyHash.getRuntime(), value);
    }
}
 
Example 11
Source File: AbstractMacroProcessorProxy.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@JRubyMethod(name = "name", required = 0)
public IRubyObject getName(ThreadContext context) {
    return JavaEmbedUtils.javaToRuby(getRuntime(), getProcessor().getName());
}
 
Example 12
Source File: DocinfoProcessorProxy.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@JRubyMethod(name = "process", required = 1)
public IRubyObject process(ThreadContext context, IRubyObject document) {
    return JavaEmbedUtils.javaToRuby(
            getRuntime(),
            getProcessor().process((Document) NodeConverter.createASTNode(document)));
}
 
Example 13
Source File: BlockProcessorProxy.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@JRubyMethod(name = "name", required = 0)
public IRubyObject getName(ThreadContext context) {
    return JavaEmbedUtils.javaToRuby(getRuntime(), getProcessor().getName());
}
 
Example 14
Source File: IncludeProcessorProxy.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@JRubyMethod(name = "handles?", required = 1)
public IRubyObject handles(ThreadContext context, IRubyObject target) {
    boolean b = getProcessor().handles(RubyUtils.rubyToJava(getRuntime(), target, String.class));
    return JavaEmbedUtils.javaToRuby(getRuntime(), b);
}