Mon, 11 Apr 2011 16:31:22 -0700
Merge
duke@1 | 1 | /* |
ksrini@826 | 2 | * Copyright (c) 1999, 2011, 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; |
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 | |
duke@1 | 87 | public final static int MAX_PARAMETERS = 0xff; |
duke@1 | 88 | public final static int MAX_DIMENSIONS = 0xff; |
duke@1 | 89 | public final static int MAX_CODE = 0xffff; |
duke@1 | 90 | public final static int MAX_LOCALS = 0xffff; |
duke@1 | 91 | public final static int MAX_STACK = 0xffff; |
duke@1 | 92 | |
jjg@256 | 93 | public enum Version { |
jjg@256 | 94 | V45_3(45, 3), // base level for all attributes |
jjg@256 | 95 | V49(49, 0), // JDK 1.5: enum, generics, annotations |
jjg@256 | 96 | V50(50, 0), // JDK 1.6: stackmaps |
jjg@256 | 97 | V51(51, 0); // JDK 1.7 |
jjg@256 | 98 | Version(int major, int minor) { |
jjg@256 | 99 | this.major = major; |
jjg@256 | 100 | this.minor = minor; |
jjg@256 | 101 | } |
jjg@256 | 102 | public final int major, minor; |
jjg@256 | 103 | } |
jjg@256 | 104 | |
duke@1 | 105 | |
duke@1 | 106 | /************************************************************************ |
duke@1 | 107 | * String Translation Routines |
duke@1 | 108 | ***********************************************************************/ |
duke@1 | 109 | |
duke@1 | 110 | /** Return internal representation of buf[offset..offset+len-1], |
duke@1 | 111 | * converting '/' to '.'. |
duke@1 | 112 | */ |
duke@1 | 113 | public static byte[] internalize(byte[] buf, int offset, int len) { |
duke@1 | 114 | byte[] translated = new byte[len]; |
duke@1 | 115 | for (int j = 0; j < len; j++) { |
duke@1 | 116 | byte b = buf[offset + j]; |
duke@1 | 117 | if (b == '/') translated[j] = (byte) '.'; |
duke@1 | 118 | else translated[j] = b; |
duke@1 | 119 | } |
duke@1 | 120 | return translated; |
duke@1 | 121 | } |
duke@1 | 122 | |
duke@1 | 123 | /** Return internal representation of given name, |
duke@1 | 124 | * converting '/' to '.'. |
duke@1 | 125 | */ |
duke@1 | 126 | public static byte[] internalize(Name name) { |
jjg@113 | 127 | return internalize(name.getByteArray(), name.getByteOffset(), name.getByteLength()); |
duke@1 | 128 | } |
duke@1 | 129 | |
duke@1 | 130 | /** Return external representation of buf[offset..offset+len-1], |
duke@1 | 131 | * converting '.' to '/'. |
duke@1 | 132 | */ |
duke@1 | 133 | public static byte[] externalize(byte[] buf, int offset, int len) { |
duke@1 | 134 | byte[] translated = new byte[len]; |
duke@1 | 135 | for (int j = 0; j < len; j++) { |
duke@1 | 136 | byte b = buf[offset + j]; |
duke@1 | 137 | if (b == '.') translated[j] = (byte) '/'; |
duke@1 | 138 | else translated[j] = b; |
duke@1 | 139 | } |
duke@1 | 140 | return translated; |
duke@1 | 141 | } |
duke@1 | 142 | |
duke@1 | 143 | /** Return external representation of given name, |
duke@1 | 144 | * converting '/' to '.'. |
duke@1 | 145 | */ |
duke@1 | 146 | public static byte[] externalize(Name name) { |
jjg@113 | 147 | return externalize(name.getByteArray(), name.getByteOffset(), name.getByteLength()); |
duke@1 | 148 | } |
duke@1 | 149 | |
duke@1 | 150 | /************************************************************************ |
duke@1 | 151 | * Name-and-type |
duke@1 | 152 | ***********************************************************************/ |
duke@1 | 153 | |
duke@1 | 154 | /** A class for the name-and-type signature of a method or field. |
duke@1 | 155 | */ |
duke@1 | 156 | public static class NameAndType { |
duke@1 | 157 | Name name; |
duke@1 | 158 | Type type; |
duke@1 | 159 | |
duke@1 | 160 | NameAndType(Name name, Type type) { |
duke@1 | 161 | this.name = name; |
duke@1 | 162 | this.type = type; |
duke@1 | 163 | } |
duke@1 | 164 | |
duke@1 | 165 | public boolean equals(Object other) { |
duke@1 | 166 | return |
duke@1 | 167 | other instanceof NameAndType && |
duke@1 | 168 | name == ((NameAndType) other).name && |
duke@1 | 169 | type.equals(((NameAndType) other).type); |
duke@1 | 170 | } |
duke@1 | 171 | |
duke@1 | 172 | public int hashCode() { |
duke@1 | 173 | return name.hashCode() * type.hashCode(); |
duke@1 | 174 | } |
duke@1 | 175 | } |
duke@1 | 176 | } |