1 package org.apache.commons.ognl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
29
30
31
32
33
34
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
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
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
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
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 }