KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > FieldInstance


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util;
23
24 import java.lang.reflect.Field JavaDoc;
25
26 /**
27  * A <tt>FieldInstance</tt> refers to a specific reflected field on an object.
28  *
29  * @version <tt>$Revision: 1958 $</tt>
30  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
31  */

32 public class FieldInstance
33 {
34    /** Field */
35    protected final Field JavaDoc field;
36
37    /** Instance */
38    protected final Object JavaDoc instance;
39
40    /**
41     * Construct a new field instance.
42     *
43     * @param instance The instance object the given field belongs to.
44     * @param fieldName The name of the field to find in the instance.
45     *
46     * @throws NullArgumentException Instance or fieldName is <tt>null</tt>.
47     * @throws NoSuchFieldException
48     */

49    public FieldInstance(final Object JavaDoc instance, final String JavaDoc fieldName)
50       throws NoSuchFieldException JavaDoc
51    {
52       if (instance == null)
53          throw new NullArgumentException("instance");
54       if (fieldName == null)
55          throw new NullArgumentException("fieldName");
56
57       // Get the field object
58
field = instance.getClass().getField(fieldName);
59
60       // Check if the field is assignable ?
61
if (! field.getDeclaringClass().isAssignableFrom(instance.getClass()))
62          throw new IllegalArgumentException JavaDoc
63             ("field does not belong to instance class");
64
65       this.instance = instance;
66    }
67
68    /**
69     * Get the field.
70     *
71     * @return Field.
72     */

73    public final Field JavaDoc getField() {
74       return field;
75    }
76
77    /**
78     * Get the instance.
79     *
80     * @return Instance.
81     */

82    public final Object JavaDoc getInstance() {
83       return instance;
84    }
85
86    /**
87     * Get the value of the field instance.
88     *
89     * @return Field value.
90     *
91     * @throws IllegalAccessException Failed to get field value.
92     */

93    public final Object JavaDoc get() throws IllegalAccessException JavaDoc {
94       return field.get(instance);
95    }
96
97    /**
98     * Set the value of the field instance
99     *
100     * @param value Field value.
101     *
102     * @throws IllegalAccessException Failed to set field value.
103     */

104    public final void set(final Object JavaDoc value) throws IllegalAccessException JavaDoc {
105       field.set(instance, value);
106    }
107 }
108
Popular Tags