package org.apache.velocity.app; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import org.apache.velocity.util.ClassUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.Map; /** *
* This is a small utility class allow easy access to static fields in a class, * such as string constants. Velocity will not introspect for class * fields (and won't in the future :), but writing setter/getter methods to do * this really is a pain, so use this if you really have * to access fields. * *
* The idea it so enable access to the fields just like you would in Java. * For example, in Java, you would access a static field like *
* and that is the same thing we are trying to allow here. * ** MyClass.STRING_CONSTANT *
* So to use in your Java code, do something like this : *
* and then in your template, you can access any of your static fields in this way : ** context.put("runtime", new FieldMethodizer( "org.apache.velocity.runtime.Runtime" )); *
* ** $runtime.COUNTER_NAME *
* Right now, this class only methodizes public static
fields. It seems
* that anything else is too dangerous. This class is for convenience accessing
* 'constants'. If you have fields that aren't static
it may be better
* to handle them by explicitly placing them into the context.
*
* @author Geir Magnusson Jr.
* @version $Id$
*/
public class FieldMethodizer
{
/** Hold the field objects by field name */
private Map