langtools/src/share/classes/com/sun/tools/javac/jvm/ClassFile.java
changeset 10 06bc494ca11e
child 1260 a772ba9ba43d
equal deleted inserted replaced
0:fd16c54261b3 10:06bc494ca11e
       
     1 /*
       
     2  * Copyright 1999-2005 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package com.sun.tools.javac.jvm;
       
    27 
       
    28 import com.sun.tools.javac.code.*;
       
    29 import com.sun.tools.javac.util.*;
       
    30 
       
    31 /** A JVM class file.
       
    32  *
       
    33  *  <p>Generic Java classfiles have one additional attribute for classes,
       
    34  *  methods and fields:
       
    35  *  <pre>
       
    36  *   "Signature" (u4 attr-length, u2 signature-index)
       
    37  *  </pre>
       
    38  *
       
    39  *  <p>A signature gives the full Java type of a method or field. When
       
    40  *  used as a class attribute, it indicates type parameters, followed
       
    41  *  by supertype, followed by all interfaces.
       
    42  *  <pre>
       
    43  *     methodOrFieldSignature ::= type
       
    44  *     classSignature         ::= [ typeparams ] supertype { interfacetype }
       
    45  *  </pre>
       
    46  *  <p>The type syntax in signatures is extended as follows:
       
    47  *  <pre>
       
    48  *     type       ::= ... | classtype | methodtype | typevar
       
    49  *     classtype  ::= classsig { '.' classsig }
       
    50  *     classig    ::= 'L' name [typeargs] ';'
       
    51  *     methodtype ::= [ typeparams ] '(' { type } ')' type
       
    52  *     typevar    ::= 'T' name ';'
       
    53  *     typeargs   ::= '<' type { type } '>'
       
    54  *     typeparams ::= '<' typeparam { typeparam } '>'
       
    55  *     typeparam  ::= name ':' type
       
    56  *  </pre>
       
    57  *  <p>This class defines constants used in class files as well
       
    58  *  as routines to convert between internal ``.'' and external ``/''
       
    59  *  separators in class names.
       
    60  *
       
    61  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
       
    62  *  you write code that depends on this, you do so at your own risk.
       
    63  *  This code and its internal interfaces are subject to change or
       
    64  *  deletion without notice.</b> */
       
    65 public class ClassFile {
       
    66 
       
    67     public final static int JAVA_MAGIC = 0xCAFEBABE;
       
    68 
       
    69     // see Target
       
    70     public final static int CONSTANT_Utf8 = 1;
       
    71     public final static int CONSTANT_Unicode = 2;
       
    72     public final static int CONSTANT_Integer = 3;
       
    73     public final static int CONSTANT_Float = 4;
       
    74     public final static int CONSTANT_Long = 5;
       
    75     public final static int CONSTANT_Double = 6;
       
    76     public final static int CONSTANT_Class = 7;
       
    77     public final static int CONSTANT_String = 8;
       
    78     public final static int CONSTANT_Fieldref = 9;
       
    79     public final static int CONSTANT_Methodref = 10;
       
    80     public final static int CONSTANT_InterfaceMethodref = 11;
       
    81     public final static int CONSTANT_NameandType = 12;
       
    82 
       
    83     public final static int MAX_PARAMETERS = 0xff;
       
    84     public final static int MAX_DIMENSIONS = 0xff;
       
    85     public final static int MAX_CODE = 0xffff;
       
    86     public final static int MAX_LOCALS = 0xffff;
       
    87     public final static int MAX_STACK = 0xffff;
       
    88 
       
    89 
       
    90 /************************************************************************
       
    91  * String Translation Routines
       
    92  ***********************************************************************/
       
    93 
       
    94     /** Return internal representation of buf[offset..offset+len-1],
       
    95      *  converting '/' to '.'.
       
    96      */
       
    97     public static byte[] internalize(byte[] buf, int offset, int len) {
       
    98         byte[] translated = new byte[len];
       
    99         for (int j = 0; j < len; j++) {
       
   100             byte b = buf[offset + j];
       
   101             if (b == '/') translated[j] = (byte) '.';
       
   102             else translated[j] = b;
       
   103         }
       
   104         return translated;
       
   105     }
       
   106 
       
   107     /** Return internal representation of given name,
       
   108      *  converting '/' to '.'.
       
   109      */
       
   110     public static byte[] internalize(Name name) {
       
   111         return internalize(name.table.names, name.index, name.len);
       
   112     }
       
   113 
       
   114     /** Return external representation of buf[offset..offset+len-1],
       
   115      *  converting '.' to '/'.
       
   116      */
       
   117     public static byte[] externalize(byte[] buf, int offset, int len) {
       
   118         byte[] translated = new byte[len];
       
   119         for (int j = 0; j < len; j++) {
       
   120             byte b = buf[offset + j];
       
   121             if (b == '.') translated[j] = (byte) '/';
       
   122             else translated[j] = b;
       
   123         }
       
   124         return translated;
       
   125     }
       
   126 
       
   127     /** Return external representation of given name,
       
   128      *  converting '/' to '.'.
       
   129      */
       
   130     public static byte[] externalize(Name name) {
       
   131         return externalize(name.table.names, name.index, name.len);
       
   132     }
       
   133 
       
   134 /************************************************************************
       
   135  * Name-and-type
       
   136  ***********************************************************************/
       
   137 
       
   138     /** A class for the name-and-type signature of a method or field.
       
   139      */
       
   140     public static class NameAndType {
       
   141         Name name;
       
   142         Type type;
       
   143 
       
   144         NameAndType(Name name, Type type) {
       
   145             this.name = name;
       
   146             this.type = type;
       
   147         }
       
   148 
       
   149         public boolean equals(Object other) {
       
   150             return
       
   151                 other instanceof NameAndType &&
       
   152                 name == ((NameAndType) other).name &&
       
   153                 type.equals(((NameAndType) other).type);
       
   154         }
       
   155 
       
   156         public int hashCode() {
       
   157             return name.hashCode() * type.hashCode();
       
   158         }
       
   159     }
       
   160 }