KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > javasupport > JavaField


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
15  * Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
16  * Copyright (C) 2004-2005 Thomas E Enebo <enebo@acm.org>
17  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
18  * Copyright (C) 2004 David Corbin <dcorbin@users.sourceforge.net>
19  * Copyright (C) 2005 Charles O Nutter <headius@headius.com>
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * either of the GNU General Public License Version 2 or later (the "GPL"),
23  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
24  * in which case the provisions of the GPL or the LGPL are applicable instead
25  * of those above. If you wish to allow use of your version of this file only
26  * under the terms of either the GPL or the LGPL, and not to allow others to
27  * use your version of this file under the terms of the CPL, indicate your
28  * decision by deleting the provisions above and replace them with the notice
29  * and other provisions required by the GPL or the LGPL. If you do not delete
30  * the provisions above, a recipient may use your version of this file under
31  * the terms of any one of the CPL, the GPL or the LGPL.
32  ***** END LICENSE BLOCK *****/

33 package org.jruby.javasupport;
34
35 import java.lang.reflect.AccessibleObject JavaDoc;
36 import java.lang.reflect.Field JavaDoc;
37 import java.lang.reflect.Modifier JavaDoc;
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 JavaDoc field;
50
51     public static RubyClass createJavaFieldClass(Ruby runtime, RubyModule javaModule) {
52         // TODO: NOT_ALLOCATABLE_ALLOCATOR is probably ok here, since we don't intend for people to monkey with
53
// this type and it can't be marshalled. Confirm. JRUBY-415
54
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 JavaDoc 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 JavaDoc javaObject = ((JavaObject) object).getValue();
102         try {
103             return JavaObject.wrap(getRuntime(), field.get(javaObject));
104         } catch (IllegalAccessException JavaDoc 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 JavaDoc javaObject = ((JavaObject) object).getValue();
117         try {
118             Object JavaDoc convertedValue = JavaUtil.convertArgument(((JavaObject) value).getValue(),
119                                                              field.getType());
120
121             field.set(javaObject, convertedValue);
122         } catch (IllegalAccessException JavaDoc iae) {
123             throw getRuntime().newTypeError(
124                                 "illegal access on setting variable: " + iae.getMessage());
125         } catch (IllegalArgumentException JavaDoc 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         // TODO: Only setAccessible to account for pattern found by
140
// accessing constants included from a non-public interface.
141
// (aka java.util.zip.ZipConstants being implemented by many
142
// classes)
143
field.setAccessible(true);
144             return JavaObject.wrap(getRuntime(), field.get(null));
145         } catch (IllegalAccessException JavaDoc 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 JavaDoc accesibleObject() {
155         return field;
156     }
157 }
158
Popular Tags