001: /*
002: * Copyright 1999-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javac.jvm;
027:
028: import com.sun.tools.javac.code.*;
029: import com.sun.tools.javac.util.*;
030:
031: /** A JVM class file.
032: *
033: * <p>Generic Java classfiles have one additional attribute for classes,
034: * methods and fields:
035: * <pre>
036: * "Signature" (u4 attr-length, u2 signature-index)
037: * </pre>
038: *
039: * <p>A signature gives the full Java type of a method or field. When
040: * used as a class attribute, it indicates type parameters, followed
041: * by supertype, followed by all interfaces.
042: * <pre>
043: * methodOrFieldSignature ::= type
044: * classSignature ::= [ typeparams ] supertype { interfacetype }
045: * </pre>
046: * <p>The type syntax in signatures is extended as follows:
047: * <pre>
048: * type ::= ... | classtype | methodtype | typevar
049: * classtype ::= classsig { '.' classsig }
050: * classig ::= 'L' name [typeargs] ';'
051: * methodtype ::= [ typeparams ] '(' { type } ')' type
052: * typevar ::= 'T' name ';'
053: * typeargs ::= '<' type { type } '>'
054: * typeparams ::= '<' typeparam { typeparam } '>'
055: * typeparam ::= name ':' type
056: * </pre>
057: * <p>This class defines constants used in class files as well
058: * as routines to convert between internal ``.'' and external ``/''
059: * separators in class names.
060: *
061: * <p><b>This is NOT part of any API supported by Sun Microsystems. If
062: * you write code that depends on this, you do so at your own risk.
063: * This code and its internal interfaces are subject to change or
064: * deletion without notice.</b> */
065: @Version("@(#)ClassFile.java 1.35 07/05/05")
066: public class ClassFile {
067:
068: public final static int JAVA_MAGIC = 0xCAFEBABE;
069:
070: // see Target
071: public final static int CONSTANT_Utf8 = 1;
072: public final static int CONSTANT_Unicode = 2;
073: public final static int CONSTANT_Integer = 3;
074: public final static int CONSTANT_Float = 4;
075: public final static int CONSTANT_Long = 5;
076: public final static int CONSTANT_Double = 6;
077: public final static int CONSTANT_Class = 7;
078: public final static int CONSTANT_String = 8;
079: public final static int CONSTANT_Fieldref = 9;
080: public final static int CONSTANT_Methodref = 10;
081: public final static int CONSTANT_InterfaceMethodref = 11;
082: public final static int CONSTANT_NameandType = 12;
083:
084: public final static int MAX_PARAMETERS = 0xff;
085: public final static int MAX_DIMENSIONS = 0xff;
086: public final static int MAX_CODE = 0xffff;
087: public final static int MAX_LOCALS = 0xffff;
088: public final static int MAX_STACK = 0xffff;
089:
090: /************************************************************************
091: * String Translation Routines
092: ***********************************************************************/
093:
094: /** Return internal representation of buf[offset..offset+len-1],
095: * converting '/' to '.'.
096: */
097: public static byte[] internalize(byte[] buf, int offset, int len) {
098: byte[] translated = new byte[len];
099: for (int j = 0; j < len; j++) {
100: byte b = buf[offset + j];
101: if (b == '/')
102: translated[j] = (byte) '.';
103: else
104: translated[j] = b;
105: }
106: return translated;
107: }
108:
109: /** Return internal representation of given name,
110: * converting '/' to '.'.
111: */
112: public static byte[] internalize(Name name) {
113: return internalize(name.table.names, name.index, name.len);
114: }
115:
116: /** Return external representation of buf[offset..offset+len-1],
117: * converting '.' to '/'.
118: */
119: public static byte[] externalize(byte[] buf, int offset, int len) {
120: byte[] translated = new byte[len];
121: for (int j = 0; j < len; j++) {
122: byte b = buf[offset + j];
123: if (b == '.')
124: translated[j] = (byte) '/';
125: else
126: translated[j] = b;
127: }
128: return translated;
129: }
130:
131: /** Return external representation of given name,
132: * converting '/' to '.'.
133: */
134: public static byte[] externalize(Name name) {
135: return externalize(name.table.names, name.index, name.len);
136: }
137:
138: /************************************************************************
139: * Name-and-type
140: ***********************************************************************/
141:
142: /** A class for the name-and-type signature of a method or field.
143: */
144: public static class NameAndType {
145: Name name;
146: Type type;
147:
148: NameAndType(Name name, Type type) {
149: this .name = name;
150: this .type = type;
151: }
152:
153: public boolean equals(Object other) {
154: return other instanceof NameAndType
155: && name == ((NameAndType) other).name
156: && type.equals(((NameAndType) other).type);
157: }
158:
159: public int hashCode() {
160: return name.hashCode() * type.hashCode();
161: }
162: }
163: }
|