Coverage Report - org.apache.commons.ognl.DefaultMemberAccess
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultMemberAccess
84%
32/38
100%
12/12
1.545
 
 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  18
     private boolean allowPrivateAccess = false;
 40  
 
 41  18
     private boolean allowProtectedAccess = false;
 42  
 
 43  18
     private boolean allowPackageProtectedAccess = false;
 44  
 
 45  
     /*
 46  
      * =================================================================== Constructors
 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  
      * =================================================================== Public methods
 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  
      * =================================================================== MemberAccess interface
 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  
      * 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  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  
 }