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 | 18 | private boolean allowPrivateAccess = false; |
40 | |
|
41 | 18 | private boolean allowProtectedAccess = false; |
42 | |
|
43 | 18 | private boolean allowPackageProtectedAccess = false; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public DefaultMemberAccess( boolean allowAllAccess ) |
50 | |
{ |
51 | 18 | this( allowAllAccess, allowAllAccess, allowAllAccess ); |
52 | 18 | } |
53 | |
|
54 | |
public DefaultMemberAccess( boolean allowPrivateAccess, boolean allowProtectedAccess, |
55 | |
boolean allowPackageProtectedAccess ) |
56 | |
{ |
57 | 18 | super(); |
58 | 18 | this.allowPrivateAccess = allowPrivateAccess; |
59 | 18 | this.allowProtectedAccess = allowProtectedAccess; |
60 | 18 | this.allowPackageProtectedAccess = allowPackageProtectedAccess; |
61 | 18 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public boolean getAllowPrivateAccess() |
68 | |
{ |
69 | 15 | return allowPrivateAccess; |
70 | |
} |
71 | |
|
72 | |
public void setAllowPrivateAccess( boolean value ) |
73 | |
{ |
74 | 0 | allowPrivateAccess = value; |
75 | 0 | } |
76 | |
|
77 | |
public boolean getAllowProtectedAccess() |
78 | |
{ |
79 | 5 | return allowProtectedAccess; |
80 | |
} |
81 | |
|
82 | |
public void setAllowProtectedAccess( boolean value ) |
83 | |
{ |
84 | 0 | allowProtectedAccess = value; |
85 | 0 | } |
86 | |
|
87 | |
public boolean getAllowPackageProtectedAccess() |
88 | |
{ |
89 | 5 | return allowPackageProtectedAccess; |
90 | |
} |
91 | |
|
92 | |
public void setAllowPackageProtectedAccess( boolean value ) |
93 | |
{ |
94 | 0 | allowPackageProtectedAccess = value; |
95 | 0 | } |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public Object setup( Map<String, Object> context, Object target, Member member, String propertyName ) |
102 | |
{ |
103 | 18 | Object result = null; |
104 | |
|
105 | 18 | if ( isAccessible( context, target, member, propertyName ) ) |
106 | |
{ |
107 | 17 | AccessibleObject accessible = (AccessibleObject) member; |
108 | |
|
109 | 17 | if ( !accessible.isAccessible() ) |
110 | |
{ |
111 | 4 | result = Boolean.TRUE; |
112 | 4 | accessible.setAccessible( true ); |
113 | |
} |
114 | |
} |
115 | 18 | return result; |
116 | |
} |
117 | |
|
118 | |
public void restore( Map<String, Object> context, Object target, Member member, String propertyName, Object state ) |
119 | |
{ |
120 | 18 | if ( state != null ) |
121 | |
{ |
122 | 4 | ( (AccessibleObject) member ).setAccessible( (Boolean) state ); |
123 | |
} |
124 | 18 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public boolean isAccessible( Map<String, Object> context, Object target, Member member, String propertyName ) |
130 | |
{ |
131 | 2179 | int modifiers = member.getModifiers(); |
132 | 2179 | boolean result = Modifier.isPublic( modifiers ); |
133 | |
|
134 | 2179 | if ( !result ) |
135 | |
{ |
136 | 25 | if ( Modifier.isPrivate( modifiers ) ) |
137 | |
{ |
138 | 15 | result = getAllowPrivateAccess(); |
139 | |
} |
140 | |
else |
141 | |
{ |
142 | 10 | if ( Modifier.isProtected( modifiers ) ) |
143 | |
{ |
144 | 5 | result = getAllowProtectedAccess(); |
145 | |
} |
146 | |
else |
147 | |
{ |
148 | 5 | result = getAllowPackageProtectedAccess(); |
149 | |
} |
150 | |
} |
151 | |
} |
152 | 2179 | return result; |
153 | |
} |
154 | |
} |