View Javadoc

1   package org.apache.commons.ognl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.AccessibleObject;
23  import java.lang.reflect.Member;
24  import java.lang.reflect.Modifier;
25  import java.util.Map;
26  
27  /**
28   * This class provides methods for setting up and restoring access in a Field. Java 2 provides access utilities for
29   * setting and getting fields that are non-public. This object provides coarse-grained access controls to allow access
30   * to private, protected and package protected members. This will apply to all classes and members.
31   *
32   * @author Luke Blanshard (blanshlu@netscape.net)
33   * @author Drew Davidson (drew@ognl.org)
34   * @version 15 October 1999
35   */
36  public class DefaultMemberAccess
37      implements MemberAccess
38  {
39      private boolean allowPrivateAccess = false;
40  
41      private boolean allowProtectedAccess = false;
42  
43      private boolean allowPackageProtectedAccess = false;
44  
45      /*
46       * =================================================================== Constructors
47       * ===================================================================
48       */
49      public DefaultMemberAccess( boolean allowAllAccess )
50      {
51          this( allowAllAccess, allowAllAccess, allowAllAccess );
52      }
53  
54      public DefaultMemberAccess( boolean allowPrivateAccess, boolean allowProtectedAccess,
55                                  boolean allowPackageProtectedAccess )
56      {
57          super();
58          this.allowPrivateAccess = allowPrivateAccess;
59          this.allowProtectedAccess = allowProtectedAccess;
60          this.allowPackageProtectedAccess = allowPackageProtectedAccess;
61      }
62  
63      /*
64       * =================================================================== Public methods
65       * ===================================================================
66       */
67      public boolean getAllowPrivateAccess()
68      {
69          return allowPrivateAccess;
70      }
71  
72      public void setAllowPrivateAccess( boolean value )
73      {
74          allowPrivateAccess = value;
75      }
76  
77      public boolean getAllowProtectedAccess()
78      {
79          return allowProtectedAccess;
80      }
81  
82      public void setAllowProtectedAccess( boolean value )
83      {
84          allowProtectedAccess = value;
85      }
86  
87      public boolean getAllowPackageProtectedAccess()
88      {
89          return allowPackageProtectedAccess;
90      }
91  
92      public void setAllowPackageProtectedAccess( boolean value )
93      {
94          allowPackageProtectedAccess = value;
95      }
96  
97      /*
98       * =================================================================== MemberAccess interface
99       * ===================================================================
100      */
101     public Object setup( Map<String, Object> context, Object target, Member member, String propertyName )
102     {
103         Object result = null;
104 
105         if ( isAccessible( context, target, member, propertyName ) )
106         {
107             AccessibleObject accessible = (AccessibleObject) member;
108 
109             if ( !accessible.isAccessible() )
110             {
111                 result = Boolean.TRUE;
112                 accessible.setAccessible( true );
113             }
114         }
115         return result;
116     }
117 
118     public void restore( Map<String, Object> context, Object target, Member member, String propertyName, Object state )
119     {
120         if ( state != null )
121         {
122             ( (AccessibleObject) member ).setAccessible( (Boolean) state );
123         }
124     }
125 
126     /**
127      * Returns true if the given member is accessible or can be made accessible by this object.
128      */
129     public boolean isAccessible( Map<String, Object> context, Object target, Member member, String propertyName )
130     {
131         int modifiers = member.getModifiers();
132         boolean result = Modifier.isPublic( modifiers );
133 
134         if ( !result )
135         {
136             if ( Modifier.isPrivate( modifiers ) )
137             {
138                 result = getAllowPrivateAccess();
139             }
140             else
141             {
142                 if ( Modifier.isProtected( modifiers ) )
143                 {
144                     result = getAllowProtectedAccess();
145                 }
146                 else
147                 {
148                     result = getAllowPackageProtectedAccess();
149                 }
150             }
151         }
152         return result;
153     }
154 }