All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jruby.java.invokers.InstanceFieldSetter Maven / Gradle / Ivy

There is a newer version: 9.4.7.0
Show newest version
package org.jruby.java.invokers;

import java.lang.reflect.Field;
import org.jruby.RubyModule;
import org.jruby.java.proxies.JavaProxy;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

public class InstanceFieldSetter extends FieldMethodOne {

    public InstanceFieldSetter(String name, RubyModule host, Field field) {
        super(name, host, field);
    }

    @Override
    public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg) {
        try {
            JavaProxy proxy = InstanceMethodInvoker.castJavaProxy(self);
            Object newValue = arg.toJava(field.getType());
            field.set(proxy.getObject(), newValue);
        } catch (IllegalAccessException iae) {
            throw context.getRuntime().newSecurityError(iae.getMessage());
        } catch (IllegalArgumentException iae) {
            throw context.getRuntime().newTypeError(iae.getMessage());
        }
        return arg;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy