langtools/src/share/classes/com/sun/tools/apt/mirror/type/TypeMaker.java
author darcy
Sun, 26 Jul 2009 21:27:11 -0700
changeset 3378 22011d9a9398
parent 10 06bc494ca11e
child 5520 86e4b9a9da40
permissions -rw-r--r--
6381698: Warn of decommissioning of apt Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.apt.mirror.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.Collection;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.ArrayList;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.mirror.type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.mirror.type.PrimitiveType.Kind;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.apt.mirror.AptEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.util.Context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import static com.sun.tools.javac.code.TypeTags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * Utilities for constructing type objects.
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 */
3378
22011d9a9398 6381698: Warn of decommissioning of apt
darcy
parents: 10
diff changeset
    44
@SuppressWarnings("deprecation")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
public class TypeMaker {
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    private final AptEnv env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    private final VoidType voidType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    private PrimitiveType[] primTypes = new PrimitiveType[VOID];
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
                                                // VOID is past all prim types
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    private static final Context.Key<TypeMaker> typeMakerKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
            new Context.Key<TypeMaker>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    public static TypeMaker instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        TypeMaker instance = context.get(typeMakerKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        if (instance == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
            instance = new TypeMaker(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    private TypeMaker(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        context.put(typeMakerKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        env = AptEnv.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        voidType = new VoidTypeImpl(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        primTypes[BOOLEAN] = new PrimitiveTypeImpl(env, Kind.BOOLEAN);
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        primTypes[BYTE]    = new PrimitiveTypeImpl(env, Kind.BYTE);
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        primTypes[SHORT]   = new PrimitiveTypeImpl(env, Kind.SHORT);
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        primTypes[INT]     = new PrimitiveTypeImpl(env, Kind.INT);
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        primTypes[LONG]    = new PrimitiveTypeImpl(env, Kind.LONG);
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        primTypes[CHAR]    = new PrimitiveTypeImpl(env, Kind.CHAR);
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        primTypes[FLOAT]   = new PrimitiveTypeImpl(env, Kind.FLOAT);
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        primTypes[DOUBLE]  = new PrimitiveTypeImpl(env, Kind.DOUBLE);
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * Returns the TypeMirror corresponding to a javac Type object.
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    public TypeMirror getType(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        if (t.isPrimitive()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
            return primTypes[t.tag];
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        case ERROR:     // fall through
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        case CLASS:     return getDeclaredType((Type.ClassType) t);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        case WILDCARD:  return new WildcardTypeImpl(env, (Type.WildcardType) t);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        case TYPEVAR:   return new TypeVariableImpl(env, (Type.TypeVar) t);
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        case ARRAY:     return new ArrayTypeImpl(env, (Type.ArrayType) t);
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        case VOID:      return voidType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        default:        throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * Returns the declared type corresponding to a given ClassType.
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    public DeclaredType getDeclaredType(Type.ClassType t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            hasFlag(t.tsym, Flags.ANNOTATION) ? new AnnotationTypeImpl(env, t) :
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            hasFlag(t.tsym, Flags.INTERFACE)  ? new InterfaceTypeImpl(env, t) :
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
            hasFlag(t.tsym, Flags.ENUM)       ? new EnumTypeImpl(env, t) :
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                                                new ClassTypeImpl(env, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * Returns a collection of types corresponding to a list of javac Type
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * objects.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    public Collection<TypeMirror> getTypes(Iterable<Type> types) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        return getTypes(types, TypeMirror.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     * Returns a collection of types corresponding to a list of javac Type
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     * objects.  The element type of the result is specified explicitly.
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    public <T extends TypeMirror> Collection<T> getTypes(Iterable<Type> types,
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                                                         Class<T> resType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        ArrayList<T> res = new ArrayList<T>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        for (Type t : types) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            TypeMirror mir = getType(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            if (resType.isInstance(mir)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                res.add(resType.cast(mir));
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
     * Returns the string representation of a type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     * Bounds of type variables are not included; bounds of wildcard types are.
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     * Type names are qualified.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    public String typeToString(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            return typeToString(env.jctypes.elemtype(t)) + "[]";
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            Type.ClassType c = (Type.ClassType) t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
            return DeclaredTypeImpl.toString(env, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        case WILDCARD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            Type.WildcardType a = (Type.WildcardType) t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            return WildcardTypeImpl.toString(env, a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            return t.tsym.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
     * Does a symbol have a given flag?
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    private static boolean hasFlag(Symbol s, long flag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        return AptEnv.hasFlag(s, flag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
}