//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2007 Michael Bayne
// 
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.samskivert.util;

import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Utility methods for code that generates code.
 */
public class GenUtil
{
    /**
     * Returns the name of the supplied class as it would likely appear in code using the class (no
     * package prefix, arrays specified as <code>type[]</code>).
     */
    public static String simpleName (Field field)
    {
        return simpleName(field.getGenericType());
    }

    /**
     * Returns the name of the supplied class as it would likely appear in code using the class (no
     * package prefix, arrays specified as <code>type[]</code>).
     */
    public static String simpleName (Type type)
    {
        if (type instanceof GenericArrayType) {
            return simpleName(((GenericArrayType)type).getGenericComponentType()) + "[]";
        } else if (type instanceof Class) {
            Class<?> clazz = (Class<?>)type;
            if (clazz.isArray()) {
                return simpleName(clazz.getComponentType()) + "[]";
            } else {
                Package pkg = clazz.getPackage();
                int offset = (pkg == null) ? 0 : pkg.getName().length()+1;
                return StringUtil.replace(clazz.getName().substring(offset), "$", ".");
            }

        } else if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType)type;
            StringBuilder buf = new StringBuilder();
            for (Type arg : pt.getActualTypeArguments()) {
                if (buf.length() > 0) {
                    buf.append(", ");
                }
                buf.append(simpleName(arg));
            }
            return simpleName(pt.getRawType()) + "<" + buf + ">";
        } else {
            throw new IllegalArgumentException("Can't generate simple name [type=" + type +
                                               ", tclass=" + StringUtil.shortClassName(type) + "]");
        }
    }
}

@@ -1024,16 +1024,55 @@
 ct.Type;
+%0Aimport java.lang.reflect.WildcardType;
 %0A%0A/**%0A *
@@ -2703,16 +2703,1101 @@
  + %22%3E%22;%0A
+%0A        %7D else if (type instanceof WildcardType) %7B%0A            WildcardType wt = (WildcardType)type;%0A            if (wt.getLowerBounds().length %3E 0) %7B%0A                String errmsg = %22Generation of simple name for wildcard type with lower bounds %22 +%0A                    %22not implemented %5Btype=%22 + type +%0A                    %22, lbounds=%22 + StringUtil.toString(wt.getLowerBounds()) + %22%5D%22;%0A                throw new IllegalArgumentException(errmsg);%0A            %7D%0A            if (wt.getUpperBounds().length %3E 1) %7B%0A                String errmsg = %22Generation of simple name for wildcard type with multiple upper %22 +%0A                    %22bounds not implemented %5Btype=%22 + type +%0A                    %22, ubounds=%22 + StringUtil.toString(wt.getUpperBounds()) + %22%5D%22;%0A                throw new IllegalArgumentException(errmsg);%0A            %7D%0A            StringBuilder buf = new StringBuilder(%22?%22);%0A            if (!Object.class.equals(wt.getUpperBounds()%5B0%5D)) %7B%0A                buf.append(%22 extends %22).append(simpleName(wt.getUpperBounds()%5B0%5D));%0A            %7D%0A            return buf.toString();%0A%0A
         

Handle uncomplicated wildcard types.