001 package org.apache.commons.ognl; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import java.lang.reflect.AccessibleObject; 023 import java.lang.reflect.Member; 024 import java.lang.reflect.Modifier; 025 import java.util.Map; 026 027 /** 028 * This class provides methods for setting up and restoring access in a Field. Java 2 provides access utilities for 029 * setting and getting fields that are non-public. This object provides coarse-grained access controls to allow access 030 * to private, protected and package protected members. This will apply to all classes and members. 031 * 032 * @author Luke Blanshard (blanshlu@netscape.net) 033 * @author Drew Davidson (drew@ognl.org) 034 * @version 15 October 1999 035 */ 036 public class DefaultMemberAccess 037 implements MemberAccess 038 { 039 private boolean allowPrivateAccess = false; 040 041 private boolean allowProtectedAccess = false; 042 043 private boolean allowPackageProtectedAccess = false; 044 045 /* 046 * =================================================================== Constructors 047 * =================================================================== 048 */ 049 public DefaultMemberAccess( boolean allowAllAccess ) 050 { 051 this( allowAllAccess, allowAllAccess, allowAllAccess ); 052 } 053 054 public DefaultMemberAccess( boolean allowPrivateAccess, boolean allowProtectedAccess, 055 boolean allowPackageProtectedAccess ) 056 { 057 super(); 058 this.allowPrivateAccess = allowPrivateAccess; 059 this.allowProtectedAccess = allowProtectedAccess; 060 this.allowPackageProtectedAccess = allowPackageProtectedAccess; 061 } 062 063 /* 064 * =================================================================== Public methods 065 * =================================================================== 066 */ 067 public boolean getAllowPrivateAccess() 068 { 069 return allowPrivateAccess; 070 } 071 072 public void setAllowPrivateAccess( boolean value ) 073 { 074 allowPrivateAccess = value; 075 } 076 077 public boolean getAllowProtectedAccess() 078 { 079 return allowProtectedAccess; 080 } 081 082 public void setAllowProtectedAccess( boolean value ) 083 { 084 allowProtectedAccess = value; 085 } 086 087 public boolean getAllowPackageProtectedAccess() 088 { 089 return allowPackageProtectedAccess; 090 } 091 092 public void setAllowPackageProtectedAccess( boolean value ) 093 { 094 allowPackageProtectedAccess = value; 095 } 096 097 /* 098 * =================================================================== MemberAccess interface 099 * =================================================================== 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 }