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

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 581
f2fdd52e4e87
child 826
5cf6c432ef2f
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

duke@1 1 /*
ohair@554 2 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.jvm;
duke@1 27
jjg@256 28 import com.sun.tools.javac.code.Type;
jjg@256 29 import com.sun.tools.javac.util.Name;
jjg@256 30
duke@1 31
duke@1 32 /** A JVM class file.
duke@1 33 *
duke@1 34 * <p>Generic Java classfiles have one additional attribute for classes,
duke@1 35 * methods and fields:
duke@1 36 * <pre>
duke@1 37 * "Signature" (u4 attr-length, u2 signature-index)
duke@1 38 * </pre>
duke@1 39 *
duke@1 40 * <p>A signature gives the full Java type of a method or field. When
duke@1 41 * used as a class attribute, it indicates type parameters, followed
duke@1 42 * by supertype, followed by all interfaces.
duke@1 43 * <pre>
duke@1 44 * methodOrFieldSignature ::= type
duke@1 45 * classSignature ::= [ typeparams ] supertype { interfacetype }
duke@1 46 * </pre>
duke@1 47 * <p>The type syntax in signatures is extended as follows:
duke@1 48 * <pre>
duke@1 49 * type ::= ... | classtype | methodtype | typevar
duke@1 50 * classtype ::= classsig { '.' classsig }
duke@1 51 * classig ::= 'L' name [typeargs] ';'
duke@1 52 * methodtype ::= [ typeparams ] '(' { type } ')' type
duke@1 53 * typevar ::= 'T' name ';'
duke@1 54 * typeargs ::= '<' type { type } '>'
duke@1 55 * typeparams ::= '<' typeparam { typeparam } '>'
duke@1 56 * typeparam ::= name ':' type
duke@1 57 * </pre>
duke@1 58 * <p>This class defines constants used in class files as well
duke@1 59 * as routines to convert between internal ``.'' and external ``/''
duke@1 60 * separators in class names.
duke@1 61 *
jjg@581 62 * <p><b>This is NOT part of any supported API.
jjg@581 63 * If you write code that depends on this, you do so at your own risk.
duke@1 64 * This code and its internal interfaces are subject to change or
duke@1 65 * deletion without notice.</b> */
duke@1 66 public class ClassFile {
duke@1 67
duke@1 68 public final static int JAVA_MAGIC = 0xCAFEBABE;
duke@1 69
duke@1 70 // see Target
duke@1 71 public final static int CONSTANT_Utf8 = 1;
duke@1 72 public final static int CONSTANT_Unicode = 2;
duke@1 73 public final static int CONSTANT_Integer = 3;
duke@1 74 public final static int CONSTANT_Float = 4;
duke@1 75 public final static int CONSTANT_Long = 5;
duke@1 76 public final static int CONSTANT_Double = 6;
duke@1 77 public final static int CONSTANT_Class = 7;
duke@1 78 public final static int CONSTANT_String = 8;
duke@1 79 public final static int CONSTANT_Fieldref = 9;
duke@1 80 public final static int CONSTANT_Methodref = 10;
duke@1 81 public final static int CONSTANT_InterfaceMethodref = 11;
duke@1 82 public final static int CONSTANT_NameandType = 12;
duke@1 83
duke@1 84 public final static int MAX_PARAMETERS = 0xff;
duke@1 85 public final static int MAX_DIMENSIONS = 0xff;
duke@1 86 public final static int MAX_CODE = 0xffff;
duke@1 87 public final static int MAX_LOCALS = 0xffff;
duke@1 88 public final static int MAX_STACK = 0xffff;
duke@1 89
jjg@256 90 public enum Version {
jjg@256 91 V45_3(45, 3), // base level for all attributes
jjg@256 92 V49(49, 0), // JDK 1.5: enum, generics, annotations
jjg@256 93 V50(50, 0), // JDK 1.6: stackmaps
jjg@256 94 V51(51, 0); // JDK 1.7
jjg@256 95 Version(int major, int minor) {
jjg@256 96 this.major = major;
jjg@256 97 this.minor = minor;
jjg@256 98 }
jjg@256 99 public final int major, minor;
jjg@256 100 }
jjg@256 101
duke@1 102
duke@1 103 /************************************************************************
duke@1 104 * String Translation Routines
duke@1 105 ***********************************************************************/
duke@1 106
duke@1 107 /** Return internal representation of buf[offset..offset+len-1],
duke@1 108 * converting '/' to '.'.
duke@1 109 */
duke@1 110 public static byte[] internalize(byte[] buf, int offset, int len) {
duke@1 111 byte[] translated = new byte[len];
duke@1 112 for (int j = 0; j < len; j++) {
duke@1 113 byte b = buf[offset + j];
duke@1 114 if (b == '/') translated[j] = (byte) '.';
duke@1 115 else translated[j] = b;
duke@1 116 }
duke@1 117 return translated;
duke@1 118 }
duke@1 119
duke@1 120 /** Return internal representation of given name,
duke@1 121 * converting '/' to '.'.
duke@1 122 */
duke@1 123 public static byte[] internalize(Name name) {
jjg@113 124 return internalize(name.getByteArray(), name.getByteOffset(), name.getByteLength());
duke@1 125 }
duke@1 126
duke@1 127 /** Return external representation of buf[offset..offset+len-1],
duke@1 128 * converting '.' to '/'.
duke@1 129 */
duke@1 130 public static byte[] externalize(byte[] buf, int offset, int len) {
duke@1 131 byte[] translated = new byte[len];
duke@1 132 for (int j = 0; j < len; j++) {
duke@1 133 byte b = buf[offset + j];
duke@1 134 if (b == '.') translated[j] = (byte) '/';
duke@1 135 else translated[j] = b;
duke@1 136 }
duke@1 137 return translated;
duke@1 138 }
duke@1 139
duke@1 140 /** Return external representation of given name,
duke@1 141 * converting '/' to '.'.
duke@1 142 */
duke@1 143 public static byte[] externalize(Name name) {
jjg@113 144 return externalize(name.getByteArray(), name.getByteOffset(), name.getByteLength());
duke@1 145 }
duke@1 146
duke@1 147 /************************************************************************
duke@1 148 * Name-and-type
duke@1 149 ***********************************************************************/
duke@1 150
duke@1 151 /** A class for the name-and-type signature of a method or field.
duke@1 152 */
duke@1 153 public static class NameAndType {
duke@1 154 Name name;
duke@1 155 Type type;
duke@1 156
duke@1 157 NameAndType(Name name, Type type) {
duke@1 158 this.name = name;
duke@1 159 this.type = type;
duke@1 160 }
duke@1 161
duke@1 162 public boolean equals(Object other) {
duke@1 163 return
duke@1 164 other instanceof NameAndType &&
duke@1 165 name == ((NameAndType) other).name &&
duke@1 166 type.equals(((NameAndType) other).type);
duke@1 167 }
duke@1 168
duke@1 169 public int hashCode() {
duke@1 170 return name.hashCode() * type.hashCode();
duke@1 171 }
duke@1 172 }
duke@1 173 }

mercurial