Coverage Report - org.kuali.rice.core.api.util.ConstantsMap
 
Classes in this File Line Coverage Branch Coverage Complexity
ConstantsMap
0%
0/44
0%
0/12
1.4
ConstantsMap$JSTLConstantExporterException
0%
0/2
N/A
1.4
 
 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.api.util;
 18  
 
 19  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 20  
 import org.springframework.beans.factory.InitializingBean;
 21  
 
 22  
 import java.lang.reflect.Field;
 23  
 import java.lang.reflect.Modifier;
 24  
 import java.util.Collection;
 25  
 import java.util.Collections;
 26  
 import java.util.HashMap;
 27  
 import java.util.Map;
 28  
 import java.util.Set;
 29  
 
 30  
 /**
 31  
  * This class is a map as a SpringBean for Constant properties. 
 32  
  * 
 33  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 34  
  */
 35  0
 public class ConstantsMap implements InitializingBean, Map<String, Object> {
 36  
     
 37  
         private Map<String, Object> map;
 38  
         private Class<?> constantClass;
 39  
 
 40  
         @Override
 41  
     public void afterPropertiesSet() throws Exception {
 42  0
             final Map<String, Object> m = new HashMap<String, Object>();
 43  
             
 44  0
             publishFields(m, constantClass);
 45  0
             map = Collections.unmodifiableMap(m);
 46  0
     }
 47  
 
 48  
     /**
 49  
      * Publishes all of the static, final, non-private fields of the given Class as entries in the given HashMap instance
 50  
      * 
 51  
      * @param constantMap
 52  
      * @param c
 53  
      */
 54  
     protected void publishFields(Map<String, Object> constantMap, Class<?> c) {
 55  0
         final Field[] fields = c.getDeclaredFields();
 56  0
         for (final Field field : fields) {
 57  0
             final int modifier = field.getModifiers();
 58  
 
 59  
             // publish values of static, final, non-private members
 60  0
             if (Modifier.isStatic(modifier) && Modifier.isFinal(modifier) && !Modifier.isPrivate(modifier)) {
 61  
                 try {
 62  0
                     final String fieldName = field.getName();
 63  
 
 64  0
                     constantMap.put(fieldName, field.get(null));
 65  0
                 } catch (IllegalAccessException e) {
 66  0
                         throw new JSTLConstantExporterException(e);
 67  0
                 }
 68  
             }
 69  
         }
 70  
 
 71  
         // publish values of appropriate fields of member classes
 72  0
         publishMemberClassFields(constantMap, c);
 73  0
     }
 74  
 
 75  
     /**
 76  
      * Publishes all of the static, final, non-private fields of the non-anonymous member classes of the given Class as entries in
 77  
      * the given HashMap instance
 78  
      * 
 79  
      * @param constantMap
 80  
      * @param c
 81  
      */
 82  
     protected void publishMemberClassFields(Map<String, Object> constantMap, Class<?> c) {
 83  0
         final Class<?>[] memberClasses = c.getClasses();
 84  
 
 85  0
         for (final Class<?> memberClass : memberClasses) {
 86  0
             if (!memberClass.isAnonymousClass()) {
 87  0
                 final String memberPrefix = memberClass.getSimpleName();
 88  
 
 89  0
                 final Map<String, Object> subclassMap = new HashMap<String, Object>();
 90  0
                 publishFields(subclassMap, memberClass);
 91  0
                 constantMap.put(memberPrefix, Collections.unmodifiableMap(subclassMap));
 92  
             }
 93  
         }
 94  0
     }
 95  
     
 96  
     public Class<?> getConstantClass() {
 97  0
                 return this.constantClass;
 98  
         }
 99  
 
 100  
         public void setConstantClass(Class<?> constantClass) {
 101  0
                 this.constantClass = constantClass;
 102  0
         }
 103  
         
 104  
     //delegate methods
 105  
     
 106  
         @Override
 107  
         public int size() {
 108  0
                 return this.map.size();
 109  
         }
 110  
 
 111  
         @Override
 112  
         public boolean isEmpty() {
 113  0
                 return this.map.isEmpty();
 114  
         }
 115  
 
 116  
         @Override
 117  
         public boolean containsKey(Object key) {
 118  0
                 return this.map.containsKey(key);
 119  
         }
 120  
 
 121  
         @Override
 122  
         public boolean containsValue(Object value) {
 123  0
                 return this.map.containsValue(value);
 124  
         }
 125  
 
 126  
         @Override
 127  
         public Object get(Object key) {
 128  0
                 return this.map.get(key);
 129  
         }
 130  
 
 131  
         @Override
 132  
         public Object put(String key, Object value) {
 133  0
                 return this.map.put(key, value);
 134  
         }
 135  
 
 136  
         @Override
 137  
         public Object remove(Object key) {
 138  0
                 return this.map.remove(key);
 139  
         }
 140  
 
 141  
         @Override
 142  
         public void putAll(Map<? extends String, ? extends Object> m) {
 143  0
                 this.map.putAll(m);
 144  0
         }
 145  
 
 146  
         @Override
 147  
         public void clear() {
 148  0
                 this.map.clear();
 149  0
         }
 150  
 
 151  
         @Override
 152  
         public Set<String> keySet() {
 153  0
                 return this.map.keySet();
 154  
         }
 155  
 
 156  
         @Override
 157  
         public Collection<Object> values() {
 158  0
                 return this.map.values();
 159  
         }
 160  
 
 161  
         @Override
 162  
         public Set<java.util.Map.Entry<String, Object>> entrySet() {
 163  0
                 return this.map.entrySet();
 164  
         }
 165  
 
 166  
         @Override
 167  
         public boolean equals(Object o) {
 168  0
                 return this.map.equals(o);
 169  
         }
 170  
 
 171  
         @Override
 172  
         public int hashCode() {
 173  0
                 return this.map.hashCode();
 174  
         }
 175  
 
 176  0
         private static class JSTLConstantExporterException extends RiceRuntimeException {
 177  
 
 178  
                 public JSTLConstantExporterException(Throwable t) {
 179  0
                         super(t);
 180  0
                 }
 181  
         }
 182  
 }