jdk/src/share/classes/sun/tools/java/Identifier.java
author ntoda
Thu, 31 Jul 2014 17:01:24 -0700
changeset 25799 1afc4675dc75
parent 25522 10d789df41bb
permissions -rw-r--r--
8044867: Fix raw and unchecked lint warnings in sun.tools.* Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.java;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A class to represent identifiers.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * An identifier instance is very similar to a String. The difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * is that identifier can't be instanciated directly, instead they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * looked up in a hash table. This means that identifiers with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * name map to the same identifier object. This makes comparisons of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * identifiers much faster.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A lot of identifiers are qualified, that is they have '.'s in them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Each qualified identifier is chopped up into the qualifier and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * name. The qualifier is cached in the value field.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Unqualified identifiers can have a type. This type is an integer that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * can be used by a scanner as a token value. This value has to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * using the setType method.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
class Identifier implements Constants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * The hashtable of identifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 25522
diff changeset
    61
    static Hashtable<String, Identifier> hash = new Hashtable<>(3001, 0.5f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * The name of the identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The value of the identifier, for keywords this is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * instance of class Integer, for qualified names this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * another identifier (the qualifier).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * The Type which corresponds to this Identifier.  This is used as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * cache for Type.tClass() and shouldn't be used outside of that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    Type typeObject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * The index of INNERCLASS_PREFIX in the name, or -1 if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private int ipos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Construct an identifier. Don't call this directly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * use lookup instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @see Identifier.lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private Identifier(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.ipos = name.indexOf(INNERCLASS_PREFIX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Get the type of the identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return ((value != null) && (value instanceof Integer)) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                ((Integer)value).intValue() : IDENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Set the type of the identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    void setType(int t) {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 5506
diff changeset
   109
        value = t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        //System.out.println("type(" + this + ")=" + t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Lookup an identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public static synchronized Identifier lookup(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        //System.out.println("lookup(" + s + ")");
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 25522
diff changeset
   118
        Identifier id = hash.get(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (id == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            hash.put(s, id = new Identifier(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Lookup a qualified identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static Identifier lookup(Identifier q, Identifier n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // lookup("", x) => x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (q == idNull)  return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // lookup(lookupInner(c, ""), n) => lookupInner(c, lookup("", n))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (q.name.charAt(q.name.length()-1) == INNERCLASS_PREFIX)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return lookup(q.name+n.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Identifier id = lookup(q + "." + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (!n.isQualified() && !q.isInner())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            id.value = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Lookup an inner identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * (Note:  n can be idNull.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public static Identifier lookupInner(Identifier c, Identifier n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        Identifier id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (c.isInner()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (c.name.charAt(c.name.length()-1) == INNERCLASS_PREFIX)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                id = lookup(c.name+n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                id = lookup(c, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            id = lookup(c + "." + INNERCLASS_PREFIX + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        id.value = c.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Convert to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Check if the name is qualified (ie: it contains a '.').
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public boolean isQualified() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            int idot = ipos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (idot <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                idot = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                idot -= 1;      // back up over previous dot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            int index = name.lastIndexOf('.', idot-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            value = (index < 0) ? idNull : Identifier.lookup(name.substring(0, index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return (value instanceof Identifier) && (value != idNull);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Return the qualifier. The null identifier is returned if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * the name was not qualified.  The qualifier does not include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * any inner part of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public Identifier getQualifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return isQualified() ? (Identifier)value : idNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Return the unqualified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * In the case of an inner name, the unqualified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * will itself contain components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public Identifier getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return isQualified() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            Identifier.lookup(name.substring(((Identifier)value).name.length() + 1)) : this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /** A space character, which precedes the first inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *  name in a qualified name, and thus marks the qualification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *  as involving inner classes, instead of merely packages.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *  Ex:  <tt>java.util.Vector. Enumerator</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public static final char INNERCLASS_PREFIX = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /* Explanation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Since much of the compiler's low-level name resolution code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * operates in terms of Identifier objects.  This includes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * code which walks around the file system and reports what
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * classes are where.  It is important to get nesting information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * right as early as possible, since it affects the spelling of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * signatures.  Thus, the low-level import and resolve code must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * be able Identifier type must be able to report the nesting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * of types, which implied that that information must be carried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * by Identifiers--or that the low-level interfaces be significantly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Check if the name is inner (ie: it contains a ' ').
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public boolean isInner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return (ipos > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Return the class name, without its qualifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * and with any nesting flattened into a new qualfication structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * If the original identifier is inner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * the result will be qualified, and can be further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * decomposed by means of <tt>getQualifier</tt> and <tt>getName</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Identifier id = Identifier.lookup("pkg.Foo. Bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * id.getName().name      =>  "Foo. Bar"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * id.getFlatName().name  =>  "Foo.Bar"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public Identifier getFlatName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (isQualified()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return getName().getFlatName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (ipos > 0 && name.charAt(ipos-1) == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (ipos+1 == name.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                // last component is idNull
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                return Identifier.lookup(name.substring(0,ipos-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            String n = name.substring(ipos+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            String t = name.substring(0,ipos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return Identifier.lookup(t+n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // Not inner.  Just return the same as getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public Identifier getTopName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (!isInner())  return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return Identifier.lookup(getQualifier(), getFlatName().getHead());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Yet another way to slice qualified identifiers:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * The head of an identifier is its first qualifier component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * and the tail is the rest of them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public Identifier getHead() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        Identifier id = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        while (id.isQualified())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            id = id.getQualifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @see getHead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public Identifier getTail() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        Identifier id = getHead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (id == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return idNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return Identifier.lookup(name.substring(id.name.length() + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    // Unfortunately, the current structure of the compiler requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    // that the resolveName() family of methods (which appear in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    // Environment.java, Context.java, and ClassDefinition.java) raise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    // no exceptions and emit no errors.  When we are in resolveName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    // and we find a method that is ambiguous, we need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    // unambiguously mark it as such, so that later stages of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    // compiler realize that they should give an ambig.class rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    // a class.not.found error.  To mark it we add a special prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    // which cannot occur in the program source.  The routines below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    // are used to check, add, and remove this prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    // (part of solution for 4059855).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * A special prefix to add to ambiguous names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    private static final String ambigPrefix = "<<ambiguous>>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Determine whether an Identifier has been marked as ambiguous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public boolean hasAmbigPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return (name.startsWith(ambigPrefix));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Add ambigPrefix to `this' to make a new Identifier marked as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * ambiguous.  It is important that this new Identifier not refer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * to an existing class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public Identifier addAmbigPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return Identifier.lookup(ambigPrefix + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Remove the ambigPrefix from `this' to get the original identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public Identifier removeAmbigPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (hasAmbigPrefix()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return Identifier.lookup(name.substring(ambigPrefix.length()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
}