001    /*
002     * Copyright (C) 2006-2007 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.codehaus.gmaven.runtime.support.stubgen.parser;
018    
019    import java.lang.reflect.Field;
020    import java.lang.reflect.Modifier;
021    import java.util.LinkedHashMap;
022    import java.util.Map;
023    
024    /**
025     * Discovers token names and values from a class dynamically.
026     *
027     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
028     * @version $Id: DynamicTokens.java 18 2009-07-16 09:39:40Z user57 $
029     */
030    public class DynamicTokens
031        implements Tokens
032    {
033        /** Maps token name to value. */
034        private final Map names = new LinkedHashMap();
035    
036        /** Maps token value to name. */
037        private final Map values = new LinkedHashMap();
038    
039        public DynamicTokens(final Class type) {
040            assert type != null;
041    
042            try {
043                discover(type);
044            }
045            catch (IllegalAccessException e) {
046                throw new RuntimeException(e);
047            }
048        }
049    
050        private void discover(final Class type) throws IllegalAccessException {
051            assert type != null;
052    
053            Field[] fields = type.getDeclaredFields();
054    
055            for (int i = 0; i < fields.length; i++) {
056                discover(fields[i]);
057            }
058        }
059    
060        private void discover(final Field field) throws IllegalAccessException {
061            assert field != null;
062    
063            int mod = field.getModifiers();
064    
065            if (Modifier.isPublic(mod) && Modifier.isStatic(mod) && Modifier.isFinal(mod)) {
066                String name = field.getName();
067                Class type = field.getType();
068                Object value = field.get(type);
069    
070                // Token types are only numbers, so ignore everything else
071                if (value instanceof Number) {
072                    names.put(name, value);
073                    values.put(value, name);
074                }
075            }
076        }
077    
078        public int value(final String name) {
079            assert name != null;
080    
081            Number value = (Number) names.get(name);
082            if (value == null) {
083                throw new IllegalArgumentException("Unknown token name: " + name);
084            }
085    
086            return value.intValue();
087        }
088    
089        public String name(final int value) {
090            String name = (String)values.get(new Integer(value));
091            if (name == null) {
092                throw new IllegalArgumentException("Unknown token value: " + value);
093            }
094    
095            return name;
096        }
097    }