KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > fieldaccess > PersistentFieldDirectAccessImpl


1 package org.apache.ojb.broker.metadata.fieldaccess;
2
3 /* Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.ojb.broker.core.proxy.ProxyHelper;
19 import org.apache.ojb.broker.metadata.MetadataException;
20
21 import java.lang.reflect.Field JavaDoc;
22
23 /**
24  * A {@link PersistentField} implementation
25  * is a high-speed version of the access strategies.
26  * It does not cooperate with an AccessController,
27  * but accesses the fields directly. This implementation persistent
28  * attributes don't need getters and setters
29  * and don't have to be declared public or protected
30  *
31  * @author <a HREF="mailto:thma@apache.org">Thomas Mahler<a>
32  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
33  * @version $Id: PersistentFieldDirectAccessImpl.java,v 1.13 2004/04/09 13:22:29 tomdz Exp $
34  */

35 public class PersistentFieldDirectAccessImpl extends AbstractPersistentField
36 {
37     private static final long serialVersionUID = -5458024240998909205L;
38
39     public PersistentFieldDirectAccessImpl()
40     {
41     }
42
43     public PersistentFieldDirectAccessImpl(Class JavaDoc type, String JavaDoc fieldname)
44     {
45         super(type, fieldname);
46     }
47
48     /**
49      * Sets the field represented by this PersistentField object on the specified object argument to the specified new value.
50      * The new value is automatically unwrapped if the underlying field has a primitive type.
51      * This implementation invokes set() on its underlying Field object if the argument <b>is not null</b>.
52      * OBS IllegalArgumentExceptions are wrapped as PersistenceBrokerExceptions.
53      *
54      * @throws MetadataException if there is an error setting this field value on obj
55      * @see java.lang.reflect.Field
56      */

57     public void doSet(Object JavaDoc obj, Object JavaDoc value) throws MetadataException
58     {
59         Field JavaDoc fld = getField();
60         Class JavaDoc type = fld.getType();
61         try
62         {
63             /**
64              * MBAIRD
65              * we need to be able to set values to null. We can only set something to null if
66              * the type is not a primitive (assignable from Object).
67              */

68             // thanks to Tomasz Wysocki for this trick
69
if ((value != null) || !type.isPrimitive())
70             {
71                 fld.set(ProxyHelper.getRealObject(obj), value);
72             }
73         }
74         catch (NullPointerException JavaDoc ignored)
75         {
76             getLog().info("Value for field " + (fld != null ? fld.getName() : null) +
77                     " of type " + type.getName() + " is null. Can't write into null.", ignored);
78         }
79         catch (IllegalAccessException JavaDoc e)
80         {
81             getLog().error("while set field: " + buildMessageString(obj, value, fld));
82             throw new MetadataException("IllegalAccess error setting field:" +
83                     (fld != null ? fld.getName() : null) + " in object:" + obj.getClass().getName(), e);
84         }
85         catch (Exception JavaDoc e)
86         {
87             getLog().error("while set field: " + buildMessageString(obj, value, fld), e);
88             throw new MetadataException("Error setting field:" + (fld != null ? fld.getName() : null) +
89                     " in object:" + obj.getClass().getName(), e);
90         }
91     }
92
93     /**
94      * Returns the value of the field represented by this PersistentField, on the specified object.
95      * This implementation invokes get() on its underlying Field object.
96      *
97      * @param obj - the object instance which we are trying to get the field value from
98      * @throws MetadataException if there is an error getting this field value from obj
99      * @see java.lang.reflect.Field
100      */

101     public Object JavaDoc doGet(Object JavaDoc obj) throws MetadataException
102     {
103         if(obj == null) return null;
104         Field JavaDoc fld = getField();
105         try
106         {
107             Object JavaDoc result = fld.get(ProxyHelper.getRealObject(obj));
108             return result;
109         }
110         catch (IllegalAccessException JavaDoc e)
111         {
112             throw new MetadataException(
113                     "IllegalAccess error getting field:" +
114                     (fld != null ? fld.getName() : null) + " in object:"
115                     + obj != null ? obj.getClass().getName() : null, e);
116         }
117         catch (Throwable JavaDoc e)
118         {
119             throw new MetadataException("Error getting field:" +
120                     (fld != null ? fld.getName() : null) + " in object:" +
121                     (obj != null ? obj.getClass().getName() : null) , e);
122         }
123     }
124
125     /**
126      * This implementation returns always 'false'.
127      * @see AbstractPersistentField#makeAccessible()
128      */

129     public boolean makeAccessible()
130     {
131         return true;
132     }
133
134     /**
135      * Always returns 'false'.
136      * @see PersistentField#usesAccessorsAndMutators
137      */

138     public boolean usesAccessorsAndMutators()
139     {
140         return false;
141     }
142 }
143
Popular Tags