1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/ClassFile.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,196 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javac.jvm; 1.30 + 1.31 +import com.sun.tools.javac.code.Type; 1.32 +import com.sun.tools.javac.code.Types; 1.33 +import com.sun.tools.javac.code.Types.UniqueType; 1.34 +import com.sun.tools.javac.util.Name; 1.35 + 1.36 + 1.37 +/** A JVM class file. 1.38 + * 1.39 + * <p>Generic Java classfiles have one additional attribute for classes, 1.40 + * methods and fields: 1.41 + * <pre> 1.42 + * "Signature" (u4 attr-length, u2 signature-index) 1.43 + * </pre> 1.44 + * 1.45 + * <p>A signature gives the full Java type of a method or field. When 1.46 + * used as a class attribute, it indicates type parameters, followed 1.47 + * by supertype, followed by all interfaces. 1.48 + * <pre> 1.49 + * methodOrFieldSignature ::= type 1.50 + * classSignature ::= [ typeparams ] supertype { interfacetype } 1.51 + * </pre> 1.52 + * <p>The type syntax in signatures is extended as follows: 1.53 + * <pre>{@literal 1.54 + * type ::= ... | classtype | methodtype | typevar 1.55 + * classtype ::= classsig { '.' classsig } 1.56 + * classig ::= 'L' name [typeargs] ';' 1.57 + * methodtype ::= [ typeparams ] '(' { type } ')' type 1.58 + * typevar ::= 'T' name ';' 1.59 + * typeargs ::= '<' type { type } '>' 1.60 + * typeparams ::= '<' typeparam { typeparam } '>' 1.61 + * typeparam ::= name ':' type 1.62 + * }</pre> 1.63 + * <p>This class defines constants used in class files as well 1.64 + * as routines to convert between internal ``.'' and external ``/'' 1.65 + * separators in class names. 1.66 + * 1.67 + * <p><b>This is NOT part of any supported API. 1.68 + * If you write code that depends on this, you do so at your own risk. 1.69 + * This code and its internal interfaces are subject to change or 1.70 + * deletion without notice.</b> */ 1.71 +public class ClassFile { 1.72 + 1.73 + public final static int JAVA_MAGIC = 0xCAFEBABE; 1.74 + 1.75 + // see Target 1.76 + public final static int CONSTANT_Utf8 = 1; 1.77 + public final static int CONSTANT_Unicode = 2; 1.78 + public final static int CONSTANT_Integer = 3; 1.79 + public final static int CONSTANT_Float = 4; 1.80 + public final static int CONSTANT_Long = 5; 1.81 + public final static int CONSTANT_Double = 6; 1.82 + public final static int CONSTANT_Class = 7; 1.83 + public final static int CONSTANT_String = 8; 1.84 + public final static int CONSTANT_Fieldref = 9; 1.85 + public final static int CONSTANT_Methodref = 10; 1.86 + public final static int CONSTANT_InterfaceMethodref = 11; 1.87 + public final static int CONSTANT_NameandType = 12; 1.88 + public final static int CONSTANT_MethodHandle = 15; 1.89 + public final static int CONSTANT_MethodType = 16; 1.90 + public final static int CONSTANT_InvokeDynamic = 18; 1.91 + 1.92 + public final static int REF_getField = 1; 1.93 + public final static int REF_getStatic = 2; 1.94 + public final static int REF_putField = 3; 1.95 + public final static int REF_putStatic = 4; 1.96 + public final static int REF_invokeVirtual = 5; 1.97 + public final static int REF_invokeStatic = 6; 1.98 + public final static int REF_invokeSpecial = 7; 1.99 + public final static int REF_newInvokeSpecial = 8; 1.100 + public final static int REF_invokeInterface = 9; 1.101 + 1.102 + public final static int MAX_PARAMETERS = 0xff; 1.103 + public final static int MAX_DIMENSIONS = 0xff; 1.104 + public final static int MAX_CODE = 0xffff; 1.105 + public final static int MAX_LOCALS = 0xffff; 1.106 + public final static int MAX_STACK = 0xffff; 1.107 + 1.108 + public enum Version { 1.109 + V45_3(45, 3), // base level for all attributes 1.110 + V49(49, 0), // JDK 1.5: enum, generics, annotations 1.111 + V50(50, 0), // JDK 1.6: stackmaps 1.112 + V51(51, 0), // JDK 1.7 1.113 + V52(52, 0); // JDK 1.8: lambda, type annos, param names 1.114 + Version(int major, int minor) { 1.115 + this.major = major; 1.116 + this.minor = minor; 1.117 + } 1.118 + public final int major, minor; 1.119 + } 1.120 + 1.121 + 1.122 +/************************************************************************ 1.123 + * String Translation Routines 1.124 + ***********************************************************************/ 1.125 + 1.126 + /** Return internal representation of buf[offset..offset+len-1], 1.127 + * converting '/' to '.'. 1.128 + */ 1.129 + public static byte[] internalize(byte[] buf, int offset, int len) { 1.130 + byte[] translated = new byte[len]; 1.131 + for (int j = 0; j < len; j++) { 1.132 + byte b = buf[offset + j]; 1.133 + if (b == '/') translated[j] = (byte) '.'; 1.134 + else translated[j] = b; 1.135 + } 1.136 + return translated; 1.137 + } 1.138 + 1.139 + /** Return internal representation of given name, 1.140 + * converting '/' to '.'. 1.141 + */ 1.142 + public static byte[] internalize(Name name) { 1.143 + return internalize(name.getByteArray(), name.getByteOffset(), name.getByteLength()); 1.144 + } 1.145 + 1.146 + /** Return external representation of buf[offset..offset+len-1], 1.147 + * converting '.' to '/'. 1.148 + */ 1.149 + public static byte[] externalize(byte[] buf, int offset, int len) { 1.150 + byte[] translated = new byte[len]; 1.151 + for (int j = 0; j < len; j++) { 1.152 + byte b = buf[offset + j]; 1.153 + if (b == '.') translated[j] = (byte) '/'; 1.154 + else translated[j] = b; 1.155 + } 1.156 + return translated; 1.157 + } 1.158 + 1.159 + /** Return external representation of given name, 1.160 + * converting '/' to '.'. 1.161 + */ 1.162 + public static byte[] externalize(Name name) { 1.163 + return externalize(name.getByteArray(), name.getByteOffset(), name.getByteLength()); 1.164 + } 1.165 + 1.166 +/************************************************************************ 1.167 + * Name-and-type 1.168 + ***********************************************************************/ 1.169 + 1.170 + /** A class for the name-and-type signature of a method or field. 1.171 + */ 1.172 + public static class NameAndType { 1.173 + Name name; 1.174 + UniqueType uniqueType; 1.175 + Types types; 1.176 + 1.177 + NameAndType(Name name, Type type, Types types) { 1.178 + this.name = name; 1.179 + this.uniqueType = new UniqueType(type, types); 1.180 + this.types = types; 1.181 + } 1.182 + 1.183 + void setType(Type type) { 1.184 + this.uniqueType = new UniqueType(type, types); 1.185 + } 1.186 + 1.187 + @Override 1.188 + public boolean equals(Object other) { 1.189 + return (other instanceof NameAndType && 1.190 + name == ((NameAndType) other).name && 1.191 + uniqueType.equals(((NameAndType) other).uniqueType)); 1.192 + } 1.193 + 1.194 + @Override 1.195 + public int hashCode() { 1.196 + return name.hashCode() * uniqueType.hashCode(); 1.197 + } 1.198 + } 1.199 +}