001 /*
002 * Copyright 1999-2008 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 public class ClassFile {
066
067 public final static int JAVA_MAGIC = 0xCAFEBABE;
068
069 // see Target
070 public final static int CONSTANT_Utf8 = 1;
071 public final static int CONSTANT_Unicode = 2;
072 public final static int CONSTANT_Integer = 3;
073 public final static int CONSTANT_Float = 4;
074 public final static int CONSTANT_Long = 5;
075 public final static int CONSTANT_Double = 6;
076 public final static int CONSTANT_Class = 7;
077 public final static int CONSTANT_String = 8;
078 public final static int CONSTANT_Fieldref = 9;
079 public final static int CONSTANT_Methodref = 10;
080 public final static int CONSTANT_InterfaceMethodref = 11;
081 public final static int CONSTANT_NameandType = 12;
082
083 public final static int MAX_PARAMETERS = 0xff;
084 public final static int MAX_DIMENSIONS = 0xff;
085 public final static int MAX_CODE = 0xffff;
086 public final static int MAX_LOCALS = 0xffff;
087 public final static int MAX_STACK = 0xffff;
088
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 == '/') 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.getByteArray(), name.getByteOffset(), name.getByteLength());
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.getByteArray(), name.getByteOffset(), name.getByteLength());
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 }