summaryrefslogtreecommitdiff
path: root/sources/meta/java/Type.java
blob: 0d4545cbd48b5d581c2d2c27e4cb79381f787d25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package meta.java;

/** A representation for Java types. */
public class Type {

    //########################################################################
    // Public Constants

    /** The Java primitive type void */
    public static final Type VOID    = Primitive("void");

    /** The Java primitive type boolean */
    public static final Type BOOLEAN = Primitive("boolean");

    /** The Java primitive type byte */
    public static final Type BYTE    = Primitive("byte");

    /** The Java primitive type short */
    public static final Type SHORT   = Primitive("short");

    /** The Java primitive type char */
    public static final Type CHAR    = Primitive("char");

    /** The Java primitive type int */
    public static final Type INT     = Primitive("int");

    /** The Java primitive type long */
    public static final Type LONG    = Primitive("long");

    /** The Java primitive type float */
    public static final Type FLOAT   = Primitive("float");

    /** The Java primitive type double */
    public static final Type DOUBLE  = Primitive("double");

    //########################################################################
    // Public Cases

    /** A primitive type */
    public case Primitive(String name);

    /** A reference type (the owner may be null) */
    public case Reference(String owner, String name);

    /** An array type */
    public case Array(Type item);

    //########################################################################
    // Public Methods

    /** Returns the type's fully qualified name. */
    public String getFullName() {
        return getName(true);
    }

    /** Returns the type's short name. */
    public String getName() {
        return getName(false);
    }

    /** Returns the type's (possibly fully qualified) name. */
    public String getName(boolean qualified) {
        switch (this) {
        case Primitive(String name):
            return name;
        case Reference(String owner, String name):
            return qualified && owner != null ? owner + "." + name : name;
        case Array(Type item):
            return item.getName(qualified) + "[]";
        default:
            throw new Error("illegal case: " + getName(true));
        }
    }

    /** Returns the type's owner (its package or enclosing type). */
    public String getOwner() {
        switch (this) {
        case Primitive(_):
            return null;
        case Reference(String owner, _):
            return owner;
        case Array(Type item):
            return item.getOwner();
        default:
            throw new Error("illegal case: " + getName(true));
        }
    }

    /** If this is an array type, returns the type of the elements. */
    public Type getItemType() {
        switch (this) {
        case Array(Type item):
            return item;
        default:
            throw new Error("not an array type: " + getName(true));
        }
    }

    /** Returns the base type of this type. */
    public Type getBaseType() {
        return isArray() ? getItemType() : this;
    }

    /** Returns true if this is a primitive type. */
    public boolean isPrimitive() {
        switch (this) {
        case Primitive(_):
            return true;
        default:
            return false;
        }
    }

    /** Returns true if this is an array type. */
    public boolean isArray() {
        switch (this) {
        case Array(_):
            return true;
        default:
            return false;
        }
    }

    /**
     * Returns the string representation of an array instantiation
     * with the given bounds and whose elements are of this type.
     */
    public String newArray(String bounds) {
        switch (this) {
        case Array(Type item):
            return item.newArray(bounds + "[]");
        default:
            return this + bounds;
        }
    }


    /** Returns the string representation of this type. */
    public String toString() {
        return getName();
    }

    //########################################################################
}