
com.github.mustachejava.jruby.JRubyObjectHandler Maven / Gradle / Ivy
package com.github.mustachejava.jruby;
import java.lang.reflect.Method;
import java.util.List;
import javax.annotation.Nullable;
import com.google.common.base.Predicate;
import com.github.mustachejava.reflect.ReflectionObjectHandler;
import com.github.mustachejava.util.Wrapper;
import org.jruby.RubyBoolean;
import org.jruby.RubyHash;
import org.jruby.RubyObject;
import org.jruby.RubySymbol;
public class JRubyObjectHandler extends ReflectionObjectHandler {
private static final Method CALL_METHOD;
static {
try {
CALL_METHOD = RubyHash.class.getMethod("callMethod", String.class);
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
}
@Override
public Object coerce(Object object) {
if (object instanceof RubyBoolean) {
RubyBoolean rb = (RubyBoolean) object;
return rb.toJava(Boolean.class);
}
return object;
}
@Override
protected Wrapper findWrapper(final int scopeIndex, final Wrapper[] wrappers, final List> guards, final Object scope, final String name) {
Wrapper wrapper = super.findWrapper(scopeIndex, wrappers, guards, scope, name);
if (wrapper == null) {
if (scope instanceof RubyHash) {
RubyHash hash = (RubyHash) scope;
final RubySymbol rs = RubySymbol.newSymbol(hash.getRuntime(), name);
if (hash.get(rs) != null) {
guards.add(new Predicate
© 2015 - 2025 Weber Informatics LLC | Privacy Policy