langtools/src/share/classes/com/sun/tools/javac/jvm/ClassFile.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 10 06bc494ca11e
child 1260 a772ba9ba43d
permissions -rw-r--r--
Initial load
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 1999-2005 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.javac.jvm;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
/** A JVM class file.
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 *  <p>Generic Java classfiles have one additional attribute for classes,
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 *  methods and fields:
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *  <pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *   "Signature" (u4 attr-length, u2 signature-index)
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *  </pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *  <p>A signature gives the full Java type of a method or field. When
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 *  used as a class attribute, it indicates type parameters, followed
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *  by supertype, followed by all interfaces.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *  <pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *     methodOrFieldSignature ::= type
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *     classSignature         ::= [ typeparams ] supertype { interfacetype }
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *  </pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *  <p>The type syntax in signatures is extended as follows:
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *  <pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *     type       ::= ... | classtype | methodtype | typevar
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *     classtype  ::= classsig { '.' classsig }
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 *     classig    ::= 'L' name [typeargs] ';'
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 *     methodtype ::= [ typeparams ] '(' { type } ')' type
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *     typevar    ::= 'T' name ';'
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 *     typeargs   ::= '<' type { type } '>'
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 *     typeparams ::= '<' typeparam { typeparam } '>'
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 *     typeparam  ::= name ':' type
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 *  </pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 *  <p>This class defines constants used in class files as well
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *  as routines to convert between internal ``.'' and external ``/''
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 *  separators in class names.
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 *  deletion without notice.</b> */
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
public class ClassFile {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    public final static int JAVA_MAGIC = 0xCAFEBABE;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    // see Target
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    public final static int CONSTANT_Utf8 = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    public final static int CONSTANT_Unicode = 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    public final static int CONSTANT_Integer = 3;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    public final static int CONSTANT_Float = 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    public final static int CONSTANT_Long = 5;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    public final static int CONSTANT_Double = 6;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    public final static int CONSTANT_Class = 7;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    public final static int CONSTANT_String = 8;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    public final static int CONSTANT_Fieldref = 9;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    public final static int CONSTANT_Methodref = 10;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    public final static int CONSTANT_InterfaceMethodref = 11;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    public final static int CONSTANT_NameandType = 12;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    public final static int MAX_PARAMETERS = 0xff;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    public final static int MAX_DIMENSIONS = 0xff;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    public final static int MAX_CODE = 0xffff;
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    public final static int MAX_LOCALS = 0xffff;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    public final static int MAX_STACK = 0xffff;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
/************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
 * String Translation Routines
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
 ***********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    /** Return internal representation of buf[offset..offset+len-1],
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     *  converting '/' to '.'.
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    public static byte[] internalize(byte[] buf, int offset, int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        byte[] translated = new byte[len];
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        for (int j = 0; j < len; j++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            byte b = buf[offset + j];
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            if (b == '/') translated[j] = (byte) '.';
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            else translated[j] = b;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        return translated;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    /** Return internal representation of given name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     *  converting '/' to '.'.
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    public static byte[] internalize(Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        return internalize(name.table.names, name.index, name.len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    /** Return external representation of buf[offset..offset+len-1],
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     *  converting '.' to '/'.
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    public static byte[] externalize(byte[] buf, int offset, int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        byte[] translated = new byte[len];
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        for (int j = 0; j < len; j++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            byte b = buf[offset + j];
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            if (b == '.') translated[j] = (byte) '/';
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            else translated[j] = b;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        return translated;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    /** Return external representation of given name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     *  converting '/' to '.'.
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    public static byte[] externalize(Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        return externalize(name.table.names, name.index, name.len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
/************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
 * Name-and-type
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
 ***********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    /** A class for the name-and-type signature of a method or field.
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    public static class NameAndType {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        Name name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        Type type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        NameAndType(Name name, Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            this.name = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            this.type = type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        public boolean equals(Object other) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                other instanceof NameAndType &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                name == ((NameAndType) other).name &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
                type.equals(((NameAndType) other).type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            return name.hashCode() * type.hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
}