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

Mon, 29 Sep 2008 11:38:56 -0700

author
martin
date
Mon, 29 Sep 2008 11:38:56 -0700
changeset 124
b81a9aa785ba
parent 113
eff38cc97183
child 117
24a47c3062fe
permissions
-rw-r--r--

6739427: -Xlint:processing not recognized as an option
Reviewed-by: darcy, jjg
Contributed-by: lipeng@google.com

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

mercurial