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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1452
de1ec6fc93fe
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

duke@1 1 /*
jjg@1326 2 * Copyright (c) 1999, 2012, 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;
vromero@1452 29 import com.sun.tools.javac.code.Types;
vromero@1452 30 import com.sun.tools.javac.code.Types.UniqueType;
jjg@256 31 import com.sun.tools.javac.util.Name;
jjg@256 32
duke@1 33
duke@1 34 /** A JVM class file.
duke@1 35 *
duke@1 36 * <p>Generic Java classfiles have one additional attribute for classes,
duke@1 37 * methods and fields:
duke@1 38 * <pre>
duke@1 39 * "Signature" (u4 attr-length, u2 signature-index)
duke@1 40 * </pre>
duke@1 41 *
duke@1 42 * <p>A signature gives the full Java type of a method or field. When
duke@1 43 * used as a class attribute, it indicates type parameters, followed
duke@1 44 * by supertype, followed by all interfaces.
duke@1 45 * <pre>
duke@1 46 * methodOrFieldSignature ::= type
duke@1 47 * classSignature ::= [ typeparams ] supertype { interfacetype }
duke@1 48 * </pre>
duke@1 49 * <p>The type syntax in signatures is extended as follows:
jjg@1326 50 * <pre>{@literal
duke@1 51 * type ::= ... | classtype | methodtype | typevar
duke@1 52 * classtype ::= classsig { '.' classsig }
duke@1 53 * classig ::= 'L' name [typeargs] ';'
duke@1 54 * methodtype ::= [ typeparams ] '(' { type } ')' type
duke@1 55 * typevar ::= 'T' name ';'
duke@1 56 * typeargs ::= '<' type { type } '>'
duke@1 57 * typeparams ::= '<' typeparam { typeparam } '>'
duke@1 58 * typeparam ::= name ':' type
jjg@1326 59 * }</pre>
duke@1 60 * <p>This class defines constants used in class files as well
duke@1 61 * as routines to convert between internal ``.'' and external ``/''
duke@1 62 * separators in class names.
duke@1 63 *
jjg@581 64 * <p><b>This is NOT part of any supported API.
jjg@581 65 * If you write code that depends on this, you do so at your own risk.
duke@1 66 * This code and its internal interfaces are subject to change or
duke@1 67 * deletion without notice.</b> */
duke@1 68 public class ClassFile {
duke@1 69
duke@1 70 public final static int JAVA_MAGIC = 0xCAFEBABE;
duke@1 71
duke@1 72 // see Target
duke@1 73 public final static int CONSTANT_Utf8 = 1;
duke@1 74 public final static int CONSTANT_Unicode = 2;
duke@1 75 public final static int CONSTANT_Integer = 3;
duke@1 76 public final static int CONSTANT_Float = 4;
duke@1 77 public final static int CONSTANT_Long = 5;
duke@1 78 public final static int CONSTANT_Double = 6;
duke@1 79 public final static int CONSTANT_Class = 7;
duke@1 80 public final static int CONSTANT_String = 8;
duke@1 81 public final static int CONSTANT_Fieldref = 9;
duke@1 82 public final static int CONSTANT_Methodref = 10;
duke@1 83 public final static int CONSTANT_InterfaceMethodref = 11;
duke@1 84 public final static int CONSTANT_NameandType = 12;
ksrini@826 85 public final static int CONSTANT_MethodHandle = 15;
ksrini@826 86 public final static int CONSTANT_MethodType = 16;
ksrini@826 87 public final static int CONSTANT_InvokeDynamic = 18;
duke@1 88
mcimadamore@1336 89 public final static int REF_getField = 1;
mcimadamore@1336 90 public final static int REF_getStatic = 2;
mcimadamore@1336 91 public final static int REF_putField = 3;
mcimadamore@1336 92 public final static int REF_putStatic = 4;
mcimadamore@1336 93 public final static int REF_invokeVirtual = 5;
mcimadamore@1336 94 public final static int REF_invokeStatic = 6;
mcimadamore@1336 95 public final static int REF_invokeSpecial = 7;
mcimadamore@1336 96 public final static int REF_newInvokeSpecial = 8;
mcimadamore@1336 97 public final static int REF_invokeInterface = 9;
mcimadamore@1336 98
duke@1 99 public final static int MAX_PARAMETERS = 0xff;
duke@1 100 public final static int MAX_DIMENSIONS = 0xff;
duke@1 101 public final static int MAX_CODE = 0xffff;
duke@1 102 public final static int MAX_LOCALS = 0xffff;
duke@1 103 public final static int MAX_STACK = 0xffff;
duke@1 104
jjg@256 105 public enum Version {
jjg@256 106 V45_3(45, 3), // base level for all attributes
jjg@256 107 V49(49, 0), // JDK 1.5: enum, generics, annotations
jjg@256 108 V50(50, 0), // JDK 1.6: stackmaps
jjg@1418 109 V51(51, 0), // JDK 1.7
jjg@1418 110 V52(52, 0); // JDK 1.8: lambda, type annos, param names
jjg@256 111 Version(int major, int minor) {
jjg@256 112 this.major = major;
jjg@256 113 this.minor = minor;
jjg@256 114 }
jjg@256 115 public final int major, minor;
jjg@256 116 }
jjg@256 117
duke@1 118
duke@1 119 /************************************************************************
duke@1 120 * String Translation Routines
duke@1 121 ***********************************************************************/
duke@1 122
duke@1 123 /** Return internal representation of buf[offset..offset+len-1],
duke@1 124 * converting '/' to '.'.
duke@1 125 */
duke@1 126 public static byte[] internalize(byte[] buf, int offset, int len) {
duke@1 127 byte[] translated = new byte[len];
duke@1 128 for (int j = 0; j < len; j++) {
duke@1 129 byte b = buf[offset + j];
duke@1 130 if (b == '/') translated[j] = (byte) '.';
duke@1 131 else translated[j] = b;
duke@1 132 }
duke@1 133 return translated;
duke@1 134 }
duke@1 135
duke@1 136 /** Return internal representation of given name,
duke@1 137 * converting '/' to '.'.
duke@1 138 */
duke@1 139 public static byte[] internalize(Name name) {
jjg@113 140 return internalize(name.getByteArray(), name.getByteOffset(), name.getByteLength());
duke@1 141 }
duke@1 142
duke@1 143 /** Return external representation of buf[offset..offset+len-1],
duke@1 144 * converting '.' to '/'.
duke@1 145 */
duke@1 146 public static byte[] externalize(byte[] buf, int offset, int len) {
duke@1 147 byte[] translated = new byte[len];
duke@1 148 for (int j = 0; j < len; j++) {
duke@1 149 byte b = buf[offset + j];
duke@1 150 if (b == '.') translated[j] = (byte) '/';
duke@1 151 else translated[j] = b;
duke@1 152 }
duke@1 153 return translated;
duke@1 154 }
duke@1 155
duke@1 156 /** Return external representation of given name,
duke@1 157 * converting '/' to '.'.
duke@1 158 */
duke@1 159 public static byte[] externalize(Name name) {
jjg@113 160 return externalize(name.getByteArray(), name.getByteOffset(), name.getByteLength());
duke@1 161 }
duke@1 162
duke@1 163 /************************************************************************
duke@1 164 * Name-and-type
duke@1 165 ***********************************************************************/
duke@1 166
duke@1 167 /** A class for the name-and-type signature of a method or field.
duke@1 168 */
duke@1 169 public static class NameAndType {
duke@1 170 Name name;
vromero@1452 171 UniqueType uniqueType;
vromero@1452 172 Types types;
duke@1 173
vromero@1452 174 NameAndType(Name name, Type type, Types types) {
duke@1 175 this.name = name;
vromero@1452 176 this.uniqueType = new UniqueType(type, types);
vromero@1452 177 this.types = types;
duke@1 178 }
duke@1 179
vromero@1452 180 void setType(Type type) {
vromero@1452 181 this.uniqueType = new UniqueType(type, types);
duke@1 182 }
duke@1 183
vromero@1452 184 @Override
vromero@1452 185 public boolean equals(Object other) {
vromero@1452 186 return (other instanceof NameAndType &&
vromero@1452 187 name == ((NameAndType) other).name &&
vromero@1452 188 uniqueType.equals(((NameAndType) other).uniqueType));
vromero@1452 189 }
vromero@1452 190
vromero@1452 191 @Override
duke@1 192 public int hashCode() {
vromero@1452 193 return name.hashCode() * uniqueType.hashCode();
duke@1 194 }
duke@1 195 }
duke@1 196 }

mercurial