1 33 package org.jruby.javasupport; 34 35 import java.lang.reflect.AccessibleObject ; 36 import java.lang.reflect.Field ; 37 import java.lang.reflect.Modifier ; 38 39 import org.jruby.Ruby; 40 import org.jruby.RubyBoolean; 41 import org.jruby.RubyClass; 42 import org.jruby.RubyModule; 43 import org.jruby.RubyString; 44 import org.jruby.runtime.CallbackFactory; 45 import org.jruby.runtime.ObjectAllocator; 46 import org.jruby.runtime.builtin.IRubyObject; 47 48 public class JavaField extends JavaAccessibleObject { 49 private Field field; 50 51 public static RubyClass createJavaFieldClass(Ruby runtime, RubyModule javaModule) { 52 RubyClass result = javaModule.defineClassUnder("JavaField", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR); 55 CallbackFactory callbackFactory = runtime.callbackFactory(JavaField.class); 56 57 JavaAccessibleObject.registerRubyMethods(runtime, result); 58 result.defineFastMethod("value_type", callbackFactory.getFastMethod("value_type")); 59 result.defineFastMethod("public?", callbackFactory.getFastMethod("public_p")); 60 result.defineFastMethod("static?", callbackFactory.getFastMethod("static_p")); 61 result.defineFastMethod("value", callbackFactory.getFastMethod("value", IRubyObject.class)); 62 result.defineFastMethod("set_value", callbackFactory.getFastMethod("set_value", IRubyObject.class, IRubyObject.class)); 63 result.defineFastMethod("final?", callbackFactory.getFastMethod("final_p")); 64 result.defineFastMethod("static_value", callbackFactory.getFastMethod("static_value")); 65 result.defineFastMethod("name", callbackFactory.getFastMethod("name")); 66 result.defineFastMethod("==", callbackFactory.getFastMethod("equal", IRubyObject.class)); 67 result.defineAlias("===", "=="); 68 69 return result; 70 } 71 72 public JavaField(Ruby runtime, Field field) { 73 super(runtime, (RubyClass) runtime.getModule("Java").getClass("JavaField")); 74 this.field = field; 75 } 76 77 public RubyString value_type() { 78 return getRuntime().newString(field.getType().getName()); 79 } 80 81 public IRubyObject equal(IRubyObject other) { 82 if (!(other instanceof JavaField)) { 83 return getRuntime().getFalse(); 84 } 85 86 return getRuntime().newBoolean(field.equals(((JavaField) other).field)); 87 } 88 89 public RubyBoolean public_p() { 90 return getRuntime().newBoolean(Modifier.isPublic(field.getModifiers())); 91 } 92 93 public RubyBoolean static_p() { 94 return getRuntime().newBoolean(Modifier.isStatic(field.getModifiers())); 95 } 96 97 public JavaObject value(IRubyObject object) { 98 if (! (object instanceof JavaObject)) { 99 throw getRuntime().newTypeError("not a java object"); 100 } 101 Object javaObject = ((JavaObject) object).getValue(); 102 try { 103 return JavaObject.wrap(getRuntime(), field.get(javaObject)); 104 } catch (IllegalAccessException iae) { 105 throw getRuntime().newTypeError("illegal access"); 106 } 107 } 108 109 public JavaObject set_value(IRubyObject object, IRubyObject value) { 110 if (! (object instanceof JavaObject)) { 111 throw getRuntime().newTypeError("not a java object: " + object); 112 } 113 if (! (value instanceof JavaObject)) { 114 throw getRuntime().newTypeError("not a java object:" + value); 115 } 116 Object javaObject = ((JavaObject) object).getValue(); 117 try { 118 Object convertedValue = JavaUtil.convertArgument(((JavaObject) value).getValue(), 119 field.getType()); 120 121 field.set(javaObject, convertedValue); 122 } catch (IllegalAccessException iae) { 123 throw getRuntime().newTypeError( 124 "illegal access on setting variable: " + iae.getMessage()); 125 } catch (IllegalArgumentException iae) { 126 throw getRuntime().newTypeError( 127 "wrong type for " + field.getType().getName() + ": " + 128 ((JavaObject) value).getValue().getClass().getName()); 129 } 130 return (JavaObject) value; 131 } 132 133 public RubyBoolean final_p() { 134 return getRuntime().newBoolean(Modifier.isFinal(field.getModifiers())); 135 } 136 137 public JavaObject static_value() { 138 try { 139 field.setAccessible(true); 144 return JavaObject.wrap(getRuntime(), field.get(null)); 145 } catch (IllegalAccessException iae) { 146 throw getRuntime().newTypeError("illegal static value access: " + iae.getMessage()); 147 } 148 } 149 150 public RubyString name() { 151 return getRuntime().newString(field.getName()); 152 } 153 154 protected AccessibleObject accesibleObject() { 155 return field; 156 } 157 } 158 | Popular Tags |