Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: ognl/ObjectPropertyAccessor.java


1   //--------------------------------------------------------------------------
2   //  Copyright (c) 1998-2004, Drew Davidson and Luke Blanshard
3   //  All rights reserved.
4   //
5   //  Redistribution and use in source and binary forms, with or without
6   //  modification, are permitted provided that the following conditions are
7   //  met:
8   //
9   //  Redistributions of source code must retain the above copyright notice,
10  //  this list of conditions and the following disclaimer.
11  //  Redistributions in binary form must reproduce the above copyright
12  //  notice, this list of conditions and the following disclaimer in the
13  //  documentation and/or other materials provided with the distribution.
14  //  Neither the name of the Drew Davidson nor the names of its contributors
15  //  may be used to endorse or promote products derived from this software
16  //  without specific prior written permission.
17  //
18  //  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  //  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  //  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  //  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  //  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  //  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  //  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25  //  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  //  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  //  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28  //  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29  //  DAMAGE.
30  //--------------------------------------------------------------------------
31  package ognl;
32  
33  import java.beans.*;
34  import java.lang.reflect.*;
35  import java.util.*;
36  
37  /**
38   * Implementation of PropertyAccessor that uses reflection on the target object's class to
39   * find a field or a pair of set/get methods with the given property name.
40   * @author Luke Blanshard (blanshlu@netscape.net)
41   * @author Drew Davidson (drew@ognl.org)
42   */
43  public class ObjectPropertyAccessor implements PropertyAccessor
44  {
45      /**
46          Returns OgnlRuntime.NotFound if the property does not exist.
47       */
48      public Object getPossibleProperty( Map context, Object target, String name) throws OgnlException
49      {
50          Object          result;
51        OgnlContext     ognlContext = (OgnlContext)context;
52  
53          try {
54            if ((result = OgnlRuntime.getMethodValue(ognlContext, target, name, true)) == OgnlRuntime.NotFound) {
55              result = OgnlRuntime.getFieldValue(ognlContext, target, name, true);
56          }
57      } catch (IntrospectionException ex) {
58        throw new OgnlException(name, ex);
59      } catch (OgnlException ex) {
60        throw ex;
61      } catch (Exception ex) {
62        throw new OgnlException(name, ex);
63      }
64      return result;
65      }
66  
67      /**
68          Returns OgnlRuntime.NotFound if the property does not exist.
69       */
70      public Object setPossibleProperty( Map context, Object target, String name, Object value) throws OgnlException
71      {
72          Object          result = null;
73        OgnlContext     ognlContext = (OgnlContext)context;
74  
75          try {
76            if (!OgnlRuntime.setMethodValue(ognlContext, target, name, value, true)) {
77              result = OgnlRuntime.setFieldValue(ognlContext, target, name, value) ? null : OgnlRuntime.NotFound;
78          }
79      } catch (IntrospectionException ex) {
80        throw new OgnlException(name, ex);
81      } catch (OgnlException ex) {
82        throw ex;
83      } catch (Exception ex) {
84        throw new OgnlException(name, ex);
85      }
86      return result;
87      }
88  
89      public boolean hasGetProperty( OgnlContext context, Object target, Object oname ) throws OgnlException
90      {
91        try {
92          return OgnlRuntime.hasGetProperty( context, target, oname );
93        } catch (IntrospectionException ex) {
94          throw new OgnlException("checking if " + target + " has gettable property " + oname, ex);
95        }
96      }
97  
98      public boolean hasGetProperty( Map context, Object target, Object oname ) throws OgnlException
99      {
100         return hasGetProperty((OgnlContext)context, target, oname);
101     }
102 
103     public boolean hasSetProperty( OgnlContext context, Object target, Object oname ) throws OgnlException
104     {
105       try {
106         return OgnlRuntime.hasSetProperty( context, target, oname );
107       } catch (IntrospectionException ex) {
108         throw new OgnlException("checking if " + target + " has settable property " + oname, ex);
109       }
110     }
111 
112     public boolean hasSetProperty( Map context, Object target, Object oname ) throws OgnlException
113     {
114         return hasSetProperty((OgnlContext)context, target, oname);
115     }
116 
117     public Object getProperty( Map context, Object target, Object oname ) throws OgnlException
118     {
119         Object              result = null;
120         String              name = oname.toString();
121 
122         if ((result = getPossibleProperty(context, target, name)) == OgnlRuntime.NotFound) {
123       throw new NoSuchPropertyException(target, name);
124     }
125         return result;
126     }
127 
128     public void setProperty( Map context, Object target, Object oname, Object value ) throws OgnlException
129     {
130         String          name = oname.toString();
131 
132         if (setPossibleProperty(context, target, name, value) == OgnlRuntime.NotFound) {
133             throw new NoSuchPropertyException(target, name);
134     }
135     }
136 }