package org.jruby.javasupport;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import org.jruby.Ruby;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
@JRubyClass(name="Java::JavaField")
public class JavaField extends JavaAccessibleObject {
private final Field field;
public final Field getValue() { return field; }
public static RubyClass createJavaFieldClass(Ruby runtime, RubyModule javaModule) {
RubyClass result = javaModule.defineClassUnder("JavaField", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
JavaAccessibleObject.registerRubyMethods(runtime, result);
result.defineAnnotatedMethods(JavaField.class);
return result;
}
public JavaField(Ruby runtime, Field field) {
super(runtime, runtime.getJavaSupport().getJavaFieldClass());
this.field = field;
}
public final boolean equals(Object other) {
return other instanceof JavaField && this.field.equals( ((JavaField) other).field );
}
public final int hashCode() {
return field.hashCode();
}
@JRubyMethod
public RubyString value_type() {
return getRuntime().newString(field.getType().getName());
}
@JRubyMethod(name = "public?")
public RubyBoolean public_p() {
return getRuntime().newBoolean(Modifier.isPublic(field.getModifiers()));
}
@JRubyMethod(name = "static?")
public RubyBoolean static_p() {
return getRuntime().newBoolean(Modifier.isStatic(field.getModifiers()));
}
@JRubyMethod(name = "enum_constant?")
public RubyBoolean enum_constant_p() {
return getRuntime().newBoolean(field.isEnumConstant());
}
@JRubyMethod
public RubyString to_generic_string() {
return getRuntime().newString(field.toGenericString());
}
@JRubyMethod(name = "type")
public IRubyObject field_type() {
return JavaClass.get(getRuntime(), field.getType());
}
@JRubyMethod
public IRubyObject value(ThreadContext context, IRubyObject object) {
final Ruby runtime = context.runtime;
final Object javaObject;
if ( ! Modifier.isStatic( field.getModifiers() ) ) {
javaObject = unwrapJavaObject(object);
}
else {
javaObject = null;
}
try {
return convertToRuby(runtime, field.get(javaObject));
}
catch (IllegalAccessException iae) {
throw runtime.newTypeError("illegal access");
}
}
@JRubyMethod
public IRubyObject set_value(IRubyObject object, IRubyObject value) {
final Object javaObject;
if ( ! Modifier.isStatic( field.getModifiers() ) ) {
javaObject = unwrapJavaObject(object);
}
else {
javaObject = null;
}
final Object javaValue = convertValueToJava(value);
try {
field.set(javaObject, javaValue);
}
catch (IllegalAccessException iae) {
throw getRuntime().newTypeError("illegal access on setting variable: " + iae.getMessage());
}
catch (IllegalArgumentException iae) {
throw getRuntime().newTypeError("wrong type for " + field.getType().getName() + ": " +
( javaValue == null ? null : javaValue.getClass().getName() ) );
}
return value;
}
@JRubyMethod(name = "final?")
public RubyBoolean final_p() {
return getRuntime().newBoolean(Modifier.isFinal(field.getModifiers()));
}
@JRubyMethod
public IRubyObject static_value() {
try {
return convertToRuby( getRuntime(), field.get(null) );
}
catch (IllegalAccessException iae) {
throw getRuntime().newTypeError("illegal static value access: " + iae.getMessage());
}
}
@JRubyMethod
public IRubyObject set_static_value(IRubyObject value) {
if ( ! ( value instanceof JavaObject ) ) {
throw getRuntime().newTypeError("not a java object:" + value);
}
final Object javaValue = convertValueToJava(value);
try {
field.set(null, javaValue);
}
catch (IllegalAccessException iae) {
throw getRuntime().newTypeError(
"illegal access on setting static variable: " + iae.getMessage());
}
catch (IllegalArgumentException iae) {
throw getRuntime().newTypeError("wrong type for " + field.getType().getName() + ": " +
( javaValue == null ? null : javaValue.getClass().getName() ) );
}
return value;
}
@JRubyMethod
public RubyString name() {
return getRuntime().newString(field.getName());
}
@Override
public AccessibleObject accessibleObject() {
return field;
}
private Object unwrapJavaObject(final IRubyObject object) throws RaiseException {
Object javaObject = JavaUtil.unwrapJavaValue(object);
if ( javaObject == null ) {
throw getRuntime().newTypeError("not a java object: " + object);
}
return javaObject;
}
private Object convertValueToJava(IRubyObject value) {
Object val = value.dataGetStruct();
if ( val instanceof JavaObject ) value = (IRubyObject) val;
return value.toJava( field.getType() );
}
private static IRubyObject convertToRuby(final Ruby runtime, final Object javaValue) {
return JavaUtil.convertJavaToUsableRubyObject(runtime, javaValue);
}
}