| 1 | package org.apache.velocity.app; |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2001,2004 The Apache Software Foundation. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | import java.lang.Class; |
| 20 | import java.lang.reflect.Field; |
| 21 | import java.lang.reflect.Modifier; |
| 22 | import java.util.HashMap; |
| 23 | |
| 24 | /** |
| 25 | * <p> |
| 26 | * This is a small utility class allow easy access to static fields in a class, |
| 27 | * such as string constants. Velocity will not introspect for class |
| 28 | * fields (and won't in the future :), but writing setter/getter methods to do |
| 29 | * this really is a pain, so use this if you really have |
| 30 | * to access fields. |
| 31 | * |
| 32 | * <p> |
| 33 | * The idea it so enable access to the fields just like you would in Java. |
| 34 | * For example, in Java, you would access a static field like |
| 35 | * <blockquote><pre> |
| 36 | * MyClass.STRING_CONSTANT |
| 37 | * </pre></blockquote> |
| 38 | * and that is the same thing we are trying to allow here. |
| 39 | * |
| 40 | * <p> |
| 41 | * So to use in your Java code, do something like this : |
| 42 | * <blockquote><pre> |
| 43 | * context.put("runtime", new FieldMethodizer( "org.apache.velocity.runtime.Runtime" )); |
| 44 | * </pre></blockquote> |
| 45 | * and then in your template, you can access any of your static fields in this way : |
| 46 | * <blockquote><pre> |
| 47 | * $runtime.RUNTIME_LOG_WARN_STACKTRACE |
| 48 | * </pre></blockquote> |
| 49 | * |
| 50 | * <p> |
| 51 | * Right now, this class only methodizes <code>public static</code> fields. It seems |
| 52 | * that anything else is too dangerous. This class is for convenience accessing |
| 53 | * 'constants'. If you have fields that aren't <code>static</code> it may be better |
| 54 | * to handle them by explicitly placing them into the context. |
| 55 | * |
| 56 | * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> |
| 57 | * @version $Id: FieldMethodizer.java,v 1.3.14.1 2004/03/03 23:22:53 geirm Exp $ |
| 58 | */ |
| 59 | public class FieldMethodizer |
| 60 | { |
| 61 | /** Hold the field objects by field name */ |
| 62 | private HashMap fieldHash = new HashMap(); |
| 63 | |
| 64 | /** Hold the class objects by field name */ |
| 65 | private HashMap classHash = new HashMap(); |
| 66 | |
| 67 | /** |
| 68 | * Allow object to be initialized without any data. You would use |
| 69 | * addObject() to add data later. |
| 70 | */ |
| 71 | public FieldMethodizer() |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Constructor that takes as it's arg the name of the class |
| 77 | * to methodize. |
| 78 | * |
| 79 | * @param s Name of class to methodize. |
| 80 | */ |
| 81 | public FieldMethodizer( String s ) |
| 82 | { |
| 83 | try |
| 84 | { |
| 85 | addObject(s); |
| 86 | } |
| 87 | catch( Exception e ) |
| 88 | { |
| 89 | System.out.println( e ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Constructor that takes as it's arg a living |
| 95 | * object to methodize. Note that it will still |
| 96 | * only methodized the public static fields of |
| 97 | * the class. |
| 98 | * |
| 99 | * @param s Name of class to methodize. |
| 100 | */ |
| 101 | public FieldMethodizer( Object o ) |
| 102 | { |
| 103 | try |
| 104 | { |
| 105 | addObject(o); |
| 106 | } |
| 107 | catch( Exception e ) |
| 108 | { |
| 109 | System.out.println( e ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Add the Name of the class to methodize |
| 115 | */ |
| 116 | public void addObject ( String s ) |
| 117 | throws Exception |
| 118 | { |
| 119 | inspect(Class.forName(s)); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Add an Object to methodize |
| 124 | */ |
| 125 | public void addObject ( Object o ) |
| 126 | throws Exception |
| 127 | { |
| 128 | inspect(o.getClass()); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Accessor method to get the fields by name. |
| 133 | * |
| 134 | * @param fieldName Name of static field to retrieve |
| 135 | * |
| 136 | * @return The value of the given field. |
| 137 | */ |
| 138 | public Object get( String fieldName ) |
| 139 | { |
| 140 | try |
| 141 | { |
| 142 | Field f = (Field) fieldHash.get( fieldName ); |
| 143 | if (f != null) |
| 144 | return f.get( (Class) classHash.get(fieldName) ); |
| 145 | } |
| 146 | catch( Exception e ) |
| 147 | { |
| 148 | } |
| 149 | return null; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Method that retrieves all public static fields |
| 154 | * in the class we are methodizing. |
| 155 | */ |
| 156 | private void inspect(Class clas) |
| 157 | { |
| 158 | Field[] fields = clas.getFields(); |
| 159 | for( int i = 0; i < fields.length; i++) |
| 160 | { |
| 161 | /* |
| 162 | * only if public and static |
| 163 | */ |
| 164 | int mod = fields[i].getModifiers(); |
| 165 | if ( Modifier.isStatic(mod) && Modifier.isPublic(mod) ) |
| 166 | { |
| 167 | fieldHash.put(fields[i].getName(), fields[i]); |
| 168 | classHash.put(fields[i].getName(), clas); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |