src/share/classes/com/sun/tools/javac/jvm/ClassFile.java

changeset 1
9a66ca7c79fa
child 113
eff38cc97183
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/ClassFile.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,160 @@
     1.4 +/*
     1.5 + * Copyright 1999-2005 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.jvm;
    1.30 +
    1.31 +import com.sun.tools.javac.code.*;
    1.32 +import com.sun.tools.javac.util.*;
    1.33 +
    1.34 +/** A JVM class file.
    1.35 + *
    1.36 + *  <p>Generic Java classfiles have one additional attribute for classes,
    1.37 + *  methods and fields:
    1.38 + *  <pre>
    1.39 + *   "Signature" (u4 attr-length, u2 signature-index)
    1.40 + *  </pre>
    1.41 + *
    1.42 + *  <p>A signature gives the full Java type of a method or field. When
    1.43 + *  used as a class attribute, it indicates type parameters, followed
    1.44 + *  by supertype, followed by all interfaces.
    1.45 + *  <pre>
    1.46 + *     methodOrFieldSignature ::= type
    1.47 + *     classSignature         ::= [ typeparams ] supertype { interfacetype }
    1.48 + *  </pre>
    1.49 + *  <p>The type syntax in signatures is extended as follows:
    1.50 + *  <pre>
    1.51 + *     type       ::= ... | classtype | methodtype | typevar
    1.52 + *     classtype  ::= classsig { '.' classsig }
    1.53 + *     classig    ::= 'L' name [typeargs] ';'
    1.54 + *     methodtype ::= [ typeparams ] '(' { type } ')' type
    1.55 + *     typevar    ::= 'T' name ';'
    1.56 + *     typeargs   ::= '<' type { type } '>'
    1.57 + *     typeparams ::= '<' typeparam { typeparam } '>'
    1.58 + *     typeparam  ::= name ':' type
    1.59 + *  </pre>
    1.60 + *  <p>This class defines constants used in class files as well
    1.61 + *  as routines to convert between internal ``.'' and external ``/''
    1.62 + *  separators in class names.
    1.63 + *
    1.64 + *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    1.65 + *  you write code that depends on this, you do so at your own risk.
    1.66 + *  This code and its internal interfaces are subject to change or
    1.67 + *  deletion without notice.</b> */
    1.68 +public class ClassFile {
    1.69 +
    1.70 +    public final static int JAVA_MAGIC = 0xCAFEBABE;
    1.71 +
    1.72 +    // see Target
    1.73 +    public final static int CONSTANT_Utf8 = 1;
    1.74 +    public final static int CONSTANT_Unicode = 2;
    1.75 +    public final static int CONSTANT_Integer = 3;
    1.76 +    public final static int CONSTANT_Float = 4;
    1.77 +    public final static int CONSTANT_Long = 5;
    1.78 +    public final static int CONSTANT_Double = 6;
    1.79 +    public final static int CONSTANT_Class = 7;
    1.80 +    public final static int CONSTANT_String = 8;
    1.81 +    public final static int CONSTANT_Fieldref = 9;
    1.82 +    public final static int CONSTANT_Methodref = 10;
    1.83 +    public final static int CONSTANT_InterfaceMethodref = 11;
    1.84 +    public final static int CONSTANT_NameandType = 12;
    1.85 +
    1.86 +    public final static int MAX_PARAMETERS = 0xff;
    1.87 +    public final static int MAX_DIMENSIONS = 0xff;
    1.88 +    public final static int MAX_CODE = 0xffff;
    1.89 +    public final static int MAX_LOCALS = 0xffff;
    1.90 +    public final static int MAX_STACK = 0xffff;
    1.91 +
    1.92 +
    1.93 +/************************************************************************
    1.94 + * String Translation Routines
    1.95 + ***********************************************************************/
    1.96 +
    1.97 +    /** Return internal representation of buf[offset..offset+len-1],
    1.98 +     *  converting '/' to '.'.
    1.99 +     */
   1.100 +    public static byte[] internalize(byte[] buf, int offset, int len) {
   1.101 +        byte[] translated = new byte[len];
   1.102 +        for (int j = 0; j < len; j++) {
   1.103 +            byte b = buf[offset + j];
   1.104 +            if (b == '/') translated[j] = (byte) '.';
   1.105 +            else translated[j] = b;
   1.106 +        }
   1.107 +        return translated;
   1.108 +    }
   1.109 +
   1.110 +    /** Return internal representation of given name,
   1.111 +     *  converting '/' to '.'.
   1.112 +     */
   1.113 +    public static byte[] internalize(Name name) {
   1.114 +        return internalize(name.table.names, name.index, name.len);
   1.115 +    }
   1.116 +
   1.117 +    /** Return external representation of buf[offset..offset+len-1],
   1.118 +     *  converting '.' to '/'.
   1.119 +     */
   1.120 +    public static byte[] externalize(byte[] buf, int offset, int len) {
   1.121 +        byte[] translated = new byte[len];
   1.122 +        for (int j = 0; j < len; j++) {
   1.123 +            byte b = buf[offset + j];
   1.124 +            if (b == '.') translated[j] = (byte) '/';
   1.125 +            else translated[j] = b;
   1.126 +        }
   1.127 +        return translated;
   1.128 +    }
   1.129 +
   1.130 +    /** Return external representation of given name,
   1.131 +     *  converting '/' to '.'.
   1.132 +     */
   1.133 +    public static byte[] externalize(Name name) {
   1.134 +        return externalize(name.table.names, name.index, name.len);
   1.135 +    }
   1.136 +
   1.137 +/************************************************************************
   1.138 + * Name-and-type
   1.139 + ***********************************************************************/
   1.140 +
   1.141 +    /** A class for the name-and-type signature of a method or field.
   1.142 +     */
   1.143 +    public static class NameAndType {
   1.144 +        Name name;
   1.145 +        Type type;
   1.146 +
   1.147 +        NameAndType(Name name, Type type) {
   1.148 +            this.name = name;
   1.149 +            this.type = type;
   1.150 +        }
   1.151 +
   1.152 +        public boolean equals(Object other) {
   1.153 +            return
   1.154 +                other instanceof NameAndType &&
   1.155 +                name == ((NameAndType) other).name &&
   1.156 +                type.equals(((NameAndType) other).type);
   1.157 +        }
   1.158 +
   1.159 +        public int hashCode() {
   1.160 +            return name.hashCode() * type.hashCode();
   1.161 +        }
   1.162 +    }
   1.163 +}

mercurial