Coverage Report - org.kuali.rice.core.util.JSTLConstants
 
Classes in this File Line Coverage Branch Coverage Complexity
JSTLConstants
0%
0/40
0%
0/20
3.125
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.core.util;
 18  
 
 19  
 import java.lang.reflect.Field;
 20  
 import java.lang.reflect.Modifier;
 21  
 import java.util.HashMap;
 22  
 import java.util.Map;
 23  
 
 24  
 /**
 25  
  * Have constants class extend this class to expose them to JSTL as a HashMap.
 26  
  * 
 27  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 28  
  */
 29  
 public class JSTLConstants extends HashMap {
 30  
 
 31  
     private static final long serialVersionUID = 6701136401021219281L;
 32  0
     private boolean initialised = false;
 33  
 
 34  0
     public JSTLConstants() {
 35  0
             publishFields(this, this.getClass());
 36  0
         initialised = true;
 37  0
     }
 38  
     
 39  0
     public JSTLConstants(Class constantsClass) {
 40  0
         publishFields(this, constantsClass);
 41  0
         initialised = true;
 42  0
     }
 43  
 
 44  
     /**
 45  
      * Publishes all of the static, final, non-private fields of the given Class as entries in the given HashMap instance
 46  
      * 
 47  
      * @param constantMap
 48  
      * @param c
 49  
      */
 50  
     protected void publishFields(Map constantMap, Class c) {
 51  0
         Field[] fields = c.getDeclaredFields();
 52  0
         for (Field field : fields) {
 53  0
             int modifier = field.getModifiers();
 54  
 
 55  
             // publish values of static, final, non-private members
 56  0
             if (Modifier.isStatic(modifier) && Modifier.isFinal(modifier) && !Modifier.isPrivate(modifier)) {
 57  
                 try {
 58  0
                     String fieldName = field.getName();
 59  
 
 60  0
                     constantMap.put(fieldName, field.get(null));
 61  0
                 } catch (IllegalAccessException e) {}
 62  
             }
 63  
         }
 64  
 
 65  
         // publish values of appropriate fields of member classes
 66  0
         publishMemberClassFields(constantMap, c);
 67  0
     }
 68  
 
 69  
     /**
 70  
      * Publishes all of the static, final, non-private fields of the non-anonymous member classes of the given Class as entries in
 71  
      * the given HashMap instance
 72  
      * 
 73  
      * @param constantMap
 74  
      * @param c
 75  
      */
 76  
     protected void publishMemberClassFields(Map constantMap, Class c) {
 77  0
         Class[] memberClasses = c.getClasses();
 78  
 
 79  0
         for (Class memberClass : memberClasses) {
 80  0
             if (!memberClass.isAnonymousClass()) {
 81  0
                 String memberPrefix = memberClass.getSimpleName();
 82  
 
 83  0
                 Map subclassMap = new HashMap();
 84  0
                 publishFields(subclassMap, memberClass);
 85  0
                 constantMap.put(memberClass.getSimpleName(), subclassMap);
 86  
             }
 87  
         }
 88  0
     }
 89  
 
 90  
     public void clear() {
 91  0
         if (!initialised)
 92  0
             super.clear();
 93  
         else
 94  0
             throw new UnsupportedOperationException("Cannot modify this map");
 95  0
     }
 96  
 
 97  
     public Object put(Object key, Object value) {
 98  0
         if (!initialised)
 99  0
             return super.put(key, value);
 100  
         else
 101  0
             throw new UnsupportedOperationException("Cannot modify this map");
 102  
     }
 103  
 
 104  
     public void putAll(Map m) {
 105  0
         if (!initialised)
 106  0
             super.putAll(m);
 107  
         else
 108  0
             throw new UnsupportedOperationException("Cannot modify this map");
 109  0
     }
 110  
 
 111  
     public Object remove(Object key) {
 112  0
         if (!initialised)
 113  0
             return super.remove(key);
 114  
         else
 115  0
             throw new UnsupportedOperationException("Cannot modify this map");
 116  
     }
 117  
 }