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

Thu, 12 Oct 2017 19:50:01 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:50:01 +0800
changeset 2702
9ca8d8713094
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

merge

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

mercurial