org.jruby.RubyObject Java Examples

The following examples show how to use org.jruby.RubyObject. 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: JavaDissectorLibrary.java    From logstash-filter-dissect with Apache License 2.0 6 votes vote down vote up
@JRubyMethod(name = "initialize", required = 2, optional = 2)
public IRubyObject rubyInitialize(final ThreadContext ctx, final IRubyObject[] args) {
    final Ruby ruby = ctx.runtime;
    try {
        dissectors = DissectPair.createArrayFromHash((RubyHash) args[0]);
    } catch (final InvalidFieldException e) {
        throw new RaiseException(e, JavaDissectorLibrary.NativeExceptions.newFieldFormatError(ruby, e));
    }
    plugin = (RubyObject) args[1];
    pluginMetaClass = plugin.getMetaClass();
    conversions = ConvertPair.createArrayFromHash((RubyHash) args[2]);
    for (final ConvertPair convertPair : conversions) {
        if (convertPair.converter().isInvalid()) {
            final RubyClass klass = ruby.getModule("LogStash").getClass("ConvertDatatypeFormatError");
            final String errorMessage = String.format("Dissector datatype conversion, datatype not supported: %s", convertPair.type());
            throw new RaiseException(ruby, klass, errorMessage, true);
        }
    }
    runMatched = args[3] == null || args[3].isTrue();
    failureTags = fetchFailureTags(ctx);
    return ctx.nil;
}
 
Example #2
Source File: ConcurrentHashMapV8.java    From thread_safe with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the TreeNode (or null if not found) for the given key
 * starting at given root.
 */
@SuppressWarnings("unchecked") final TreeNode getTreeNode
(int h, RubyObject k, TreeNode p) {
    RubyClass c = k.getMetaClass(); boolean kNotComparable = !k.respondsTo("<=>");
    while (p != null) {
        int dir, ph;  RubyObject pk; RubyClass pc;
        if ((ph = p.hash) == h) {
            if ((pk = (RubyObject)p.key) == k || k.equals(pk))
                return p;
            if (c != (pc = (RubyClass)pk.getMetaClass()) ||
                    kNotComparable ||
                    (dir = rubyCompare(k, pk)) == 0) {
                dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
                if (dir == 0) { // if still stuck, need to check both sides
                    TreeNode r = null, pl, pr;
                    // try to recurse on the right
                    if ((pr = p.right) != null && h >= pr.hash && (r = getTreeNode(h, k, pr)) != null)
                        return r;
                    // try to continue iterating on the left side
                    else if ((pl = p.left) != null && h <= pl.hash)
                        dir = -1;
                    else // no matching node found
                        return null;
                }
            }
        }
        else
            dir = (h < ph) ? -1 : 1;
        p = (dir > 0) ? p.right : p.left;
    }
    return null;
}
 
Example #3
Source File: ConcurrentHashMapV8.java    From thread_safe with Apache License 2.0 5 votes vote down vote up
int rubyCompare(RubyObject l, RubyObject r) {
    ThreadContext context = l.getMetaClass().getRuntime().getCurrentContext();
    IRubyObject result;
    try {
        result = l.callMethod(context, "<=>", r);
    } catch (RaiseException e) {
        // handle objects "lying" about responding to <=>, ie: an Array containing non-comparable keys
        if (context.runtime.getNoMethodError().isInstance(e.getException())) {
            return 0;
        }
        throw e;
    }

    return result.isNil() ? 0 : RubyNumeric.num2int(result.convertToInteger());
}
 
Example #4
Source File: ConcurrentHashMapV8.java    From thread_safe with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the TreeNode (or null if not found) for the given key
 * starting at given root.
 */
@SuppressWarnings("unchecked") final TreeNode getTreeNode
(int h, RubyObject k, TreeNode p) {
    RubyClass c = k.getMetaClass(); boolean kNotComparable = !k.respondsTo("<=>");
    while (p != null) {
        int dir, ph;  RubyObject pk; RubyClass pc;
        if ((ph = p.hash) == h) {
            if ((pk = (RubyObject)p.key) == k || k.equals(pk))
                return p;
            if (c != (pc = (RubyClass)pk.getMetaClass()) ||
                    kNotComparable ||
                    (dir = rubyCompare(k, pk)) == 0) {
                dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
                if (dir == 0) { // if still stuck, need to check both sides
                    TreeNode r = null, pl, pr;
                    // try to recurse on the right
                    if ((pr = p.right) != null && h >= pr.hash && (r = getTreeNode(h, k, pr)) != null)
                        return r;
                    // try to continue iterating on the left side
                    else if ((pl = p.left) != null && h <= pl.hash)
                        dir = -1;
                    else // no matching node found
                        return null;
                }
            }
        }
        else
            dir = (h < ph) ? -1 : 1;
        p = (dir > 0) ? p.right : p.left;
    }
    return null;
}
 
Example #5
Source File: ConcurrentHashMapV8.java    From thread_safe with Apache License 2.0 5 votes vote down vote up
int rubyCompare(RubyObject l, RubyObject r) {
    ThreadContext context = l.getMetaClass().getRuntime().getCurrentContext();
    IRubyObject result;
    try {
        result = l.callMethod(context, "<=>", r);
    } catch (RaiseException e) {
        // handle objects "lying" about responding to <=>, ie: an Array containing non-comparable keys
        if (context.runtime.getNoMethodError().isInstance(e.getException())) {
            return 0;
        }
        throw e;
    }

    return result.isNil() ? 0 : RubyNumeric.num2int(result.convertToInteger());
}
 
Example #6
Source File: ProcessorProxyUtil.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
/**
 * Defines the annotated methods of the given class and all super classes as
 * {@link org.jruby.RubyClass#defineAnnotatedMethods(Class)} does not handle inherited methods.
 * @param rubyClass
 * @param proxyClass
 */
public static void defineAnnotatedMethods(RubyClass rubyClass, Class<?> proxyClass) {
    Class<?> currentClass = proxyClass;
    while (currentClass != RubyObject.class) {
        rubyClass.defineAnnotatedMethods(currentClass);
        currentClass = currentClass.getSuperclass();
    }
}
 
Example #7
Source File: ConcurrentHashMapV8.java    From thread_safe with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked") final TreeNode getTreeNode
(int h, Object k, TreeNode p) {
    return getTreeNode(h, (RubyObject)k, p);
}
 
Example #8
Source File: ConcurrentHashMapV8.java    From thread_safe with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked") final TreeNode putTreeNode
        (int h, Object k, Object v) {
    return putTreeNode(h, (RubyObject)k, v);
}
 
Example #9
Source File: ConcurrentHashMapV8.java    From thread_safe with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked") final TreeNode getTreeNode
(int h, Object k, TreeNode p) {
    return getTreeNode(h, (RubyObject)k, p);
}
 
Example #10
Source File: ConcurrentHashMapV8.java    From thread_safe with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked") final TreeNode putTreeNode
        (int h, Object k, Object v) {
    return putTreeNode(h, (RubyObject)k, v);
}