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

Fri, 30 Nov 2012 15:14:36 +0000

author
mcimadamore
date
Fri, 30 Nov 2012 15:14:36 +0000
changeset 1435
9b26c96f5138
parent 1418
2531de382983
child 1452
de1ec6fc93fe
permissions
-rw-r--r--

8004101: Add checks for method reference well-formedness
Summary: Bring method reference type-checking in sync with latest EDR
Reviewed-by: jjg

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;
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:
jjg@1326 48 * <pre>{@literal
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
jjg@1326 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;
ksrini@826 83 public final static int CONSTANT_MethodHandle = 15;
ksrini@826 84 public final static int CONSTANT_MethodType = 16;
ksrini@826 85 public final static int CONSTANT_InvokeDynamic = 18;
duke@1 86
mcimadamore@1336 87 public final static int REF_getField = 1;
mcimadamore@1336 88 public final static int REF_getStatic = 2;
mcimadamore@1336 89 public final static int REF_putField = 3;
mcimadamore@1336 90 public final static int REF_putStatic = 4;
mcimadamore@1336 91 public final static int REF_invokeVirtual = 5;
mcimadamore@1336 92 public final static int REF_invokeStatic = 6;
mcimadamore@1336 93 public final static int REF_invokeSpecial = 7;
mcimadamore@1336 94 public final static int REF_newInvokeSpecial = 8;
mcimadamore@1336 95 public final static int REF_invokeInterface = 9;
mcimadamore@1336 96
duke@1 97 public final static int MAX_PARAMETERS = 0xff;
duke@1 98 public final static int MAX_DIMENSIONS = 0xff;
duke@1 99 public final static int MAX_CODE = 0xffff;
duke@1 100 public final static int MAX_LOCALS = 0xffff;
duke@1 101 public final static int MAX_STACK = 0xffff;
duke@1 102
jjg@256 103 public enum Version {
jjg@256 104 V45_3(45, 3), // base level for all attributes
jjg@256 105 V49(49, 0), // JDK 1.5: enum, generics, annotations
jjg@256 106 V50(50, 0), // JDK 1.6: stackmaps
jjg@1418 107 V51(51, 0), // JDK 1.7
jjg@1418 108 V52(52, 0); // JDK 1.8: lambda, type annos, param names
jjg@256 109 Version(int major, int minor) {
jjg@256 110 this.major = major;
jjg@256 111 this.minor = minor;
jjg@256 112 }
jjg@256 113 public final int major, minor;
jjg@256 114 }
jjg@256 115
duke@1 116
duke@1 117 /************************************************************************
duke@1 118 * String Translation Routines
duke@1 119 ***********************************************************************/
duke@1 120
duke@1 121 /** Return internal representation of buf[offset..offset+len-1],
duke@1 122 * converting '/' to '.'.
duke@1 123 */
duke@1 124 public static byte[] internalize(byte[] buf, int offset, int len) {
duke@1 125 byte[] translated = new byte[len];
duke@1 126 for (int j = 0; j < len; j++) {
duke@1 127 byte b = buf[offset + j];
duke@1 128 if (b == '/') translated[j] = (byte) '.';
duke@1 129 else translated[j] = b;
duke@1 130 }
duke@1 131 return translated;
duke@1 132 }
duke@1 133
duke@1 134 /** Return internal representation of given name,
duke@1 135 * converting '/' to '.'.
duke@1 136 */
duke@1 137 public static byte[] internalize(Name name) {
jjg@113 138 return internalize(name.getByteArray(), name.getByteOffset(), name.getByteLength());
duke@1 139 }
duke@1 140
duke@1 141 /** Return external representation of buf[offset..offset+len-1],
duke@1 142 * converting '.' to '/'.
duke@1 143 */
duke@1 144 public static byte[] externalize(byte[] buf, int offset, int len) {
duke@1 145 byte[] translated = new byte[len];
duke@1 146 for (int j = 0; j < len; j++) {
duke@1 147 byte b = buf[offset + j];
duke@1 148 if (b == '.') translated[j] = (byte) '/';
duke@1 149 else translated[j] = b;
duke@1 150 }
duke@1 151 return translated;
duke@1 152 }
duke@1 153
duke@1 154 /** Return external representation of given name,
duke@1 155 * converting '/' to '.'.
duke@1 156 */
duke@1 157 public static byte[] externalize(Name name) {
jjg@113 158 return externalize(name.getByteArray(), name.getByteOffset(), name.getByteLength());
duke@1 159 }
duke@1 160
duke@1 161 /************************************************************************
duke@1 162 * Name-and-type
duke@1 163 ***********************************************************************/
duke@1 164
duke@1 165 /** A class for the name-and-type signature of a method or field.
duke@1 166 */
duke@1 167 public static class NameAndType {
duke@1 168 Name name;
duke@1 169 Type type;
duke@1 170
duke@1 171 NameAndType(Name name, Type type) {
duke@1 172 this.name = name;
duke@1 173 this.type = type;
duke@1 174 }
duke@1 175
duke@1 176 public boolean equals(Object other) {
duke@1 177 return
duke@1 178 other instanceof NameAndType &&
duke@1 179 name == ((NameAndType) other).name &&
duke@1 180 type.equals(((NameAndType) other).type);
duke@1 181 }
duke@1 182
duke@1 183 public int hashCode() {
duke@1 184 return name.hashCode() * type.hashCode();
duke@1 185 }
duke@1 186 }
duke@1 187 }

mercurial