src/share/classes/com/sun/tools/classfile/ClassFile.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1863
8e3d391c88c6
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2007, 2013, 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.classfile;
aoqi@0 27
aoqi@0 28 import java.io.File;
aoqi@0 29 import java.io.IOException;
aoqi@0 30 import java.io.InputStream;
aoqi@0 31 import java.nio.file.Files;
aoqi@0 32 import java.nio.file.Path;
aoqi@0 33
aoqi@0 34 import static com.sun.tools.classfile.AccessFlags.*;
aoqi@0 35
aoqi@0 36 /**
aoqi@0 37 * See JVMS, section 4.2.
aoqi@0 38 *
aoqi@0 39 * <p><b>This is NOT part of any supported API.
aoqi@0 40 * If you write code that depends on this, you do so at your own risk.
aoqi@0 41 * This code and its internal interfaces are subject to change or
aoqi@0 42 * deletion without notice.</b>
aoqi@0 43 */
aoqi@0 44 public class ClassFile {
aoqi@0 45 public static ClassFile read(File file)
aoqi@0 46 throws IOException, ConstantPoolException {
aoqi@0 47 return read(file.toPath(), new Attribute.Factory());
aoqi@0 48 }
aoqi@0 49
aoqi@0 50 public static ClassFile read(Path input)
aoqi@0 51 throws IOException, ConstantPoolException {
aoqi@0 52 return read(input, new Attribute.Factory());
aoqi@0 53 }
aoqi@0 54
aoqi@0 55 public static ClassFile read(Path input, Attribute.Factory attributeFactory)
aoqi@0 56 throws IOException, ConstantPoolException {
aoqi@0 57 try (InputStream in = Files.newInputStream(input)) {
aoqi@0 58 return new ClassFile(in, attributeFactory);
aoqi@0 59 }
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 public static ClassFile read(File file, Attribute.Factory attributeFactory)
aoqi@0 63 throws IOException, ConstantPoolException {
aoqi@0 64 return read(file.toPath(), attributeFactory);
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 public static ClassFile read(InputStream in)
aoqi@0 68 throws IOException, ConstantPoolException {
aoqi@0 69 return new ClassFile(in, new Attribute.Factory());
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 public static ClassFile read(InputStream in, Attribute.Factory attributeFactory)
aoqi@0 73 throws IOException, ConstantPoolException {
aoqi@0 74 return new ClassFile(in, attributeFactory);
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 ClassFile(InputStream in, Attribute.Factory attributeFactory) throws IOException, ConstantPoolException {
aoqi@0 78 ClassReader cr = new ClassReader(this, in, attributeFactory);
aoqi@0 79 magic = cr.readInt();
aoqi@0 80 minor_version = cr.readUnsignedShort();
aoqi@0 81 major_version = cr.readUnsignedShort();
aoqi@0 82 constant_pool = new ConstantPool(cr);
aoqi@0 83 access_flags = new AccessFlags(cr);
aoqi@0 84 this_class = cr.readUnsignedShort();
aoqi@0 85 super_class = cr.readUnsignedShort();
aoqi@0 86
aoqi@0 87 int interfaces_count = cr.readUnsignedShort();
aoqi@0 88 interfaces = new int[interfaces_count];
aoqi@0 89 for (int i = 0; i < interfaces_count; i++)
aoqi@0 90 interfaces[i] = cr.readUnsignedShort();
aoqi@0 91
aoqi@0 92 int fields_count = cr.readUnsignedShort();
aoqi@0 93 fields = new Field[fields_count];
aoqi@0 94 for (int i = 0; i < fields_count; i++)
aoqi@0 95 fields[i] = new Field(cr);
aoqi@0 96
aoqi@0 97 int methods_count = cr.readUnsignedShort();
aoqi@0 98 methods = new Method[methods_count];
aoqi@0 99 for (int i = 0; i < methods_count; i++)
aoqi@0 100 methods[i] = new Method(cr);
aoqi@0 101
aoqi@0 102 attributes = new Attributes(cr);
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 public ClassFile(int magic, int minor_version, int major_version,
aoqi@0 106 ConstantPool constant_pool, AccessFlags access_flags,
aoqi@0 107 int this_class, int super_class, int[] interfaces,
aoqi@0 108 Field[] fields, Method[] methods, Attributes attributes) {
aoqi@0 109 this.magic = magic;
aoqi@0 110 this.minor_version = minor_version;
aoqi@0 111 this.major_version = major_version;
aoqi@0 112 this.constant_pool = constant_pool;
aoqi@0 113 this.access_flags = access_flags;
aoqi@0 114 this.this_class = this_class;
aoqi@0 115 this.super_class = super_class;
aoqi@0 116 this.interfaces = interfaces;
aoqi@0 117 this.fields = fields;
aoqi@0 118 this.methods = methods;
aoqi@0 119 this.attributes = attributes;
aoqi@0 120 }
aoqi@0 121
aoqi@0 122 public String getName() throws ConstantPoolException {
aoqi@0 123 return constant_pool.getClassInfo(this_class).getName();
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 public String getSuperclassName() throws ConstantPoolException {
aoqi@0 127 return constant_pool.getClassInfo(super_class).getName();
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 public String getInterfaceName(int i) throws ConstantPoolException {
aoqi@0 131 return constant_pool.getClassInfo(interfaces[i]).getName();
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 public Attribute getAttribute(String name) {
aoqi@0 135 return attributes.get(name);
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 public boolean isClass() {
aoqi@0 139 return !isInterface();
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 public boolean isInterface() {
aoqi@0 143 return access_flags.is(ACC_INTERFACE);
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 public int byteLength() {
aoqi@0 147 return 4 + // magic
aoqi@0 148 2 + // minor
aoqi@0 149 2 + // major
aoqi@0 150 constant_pool.byteLength() +
aoqi@0 151 2 + // access flags
aoqi@0 152 2 + // this_class
aoqi@0 153 2 + // super_class
aoqi@0 154 byteLength(interfaces) +
aoqi@0 155 byteLength(fields) +
aoqi@0 156 byteLength(methods) +
aoqi@0 157 attributes.byteLength();
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 private int byteLength(int[] indices) {
aoqi@0 161 return 2 + 2 * indices.length;
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 private int byteLength(Field[] fields) {
aoqi@0 165 int length = 2;
aoqi@0 166 for (Field f: fields)
aoqi@0 167 length += f.byteLength();
aoqi@0 168 return length;
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 private int byteLength(Method[] methods) {
aoqi@0 172 int length = 2;
aoqi@0 173 for (Method m: methods)
aoqi@0 174 length += m.byteLength();
aoqi@0 175 return length;
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 public final int magic;
aoqi@0 179 public final int minor_version;
aoqi@0 180 public final int major_version;
aoqi@0 181 public final ConstantPool constant_pool;
aoqi@0 182 public final AccessFlags access_flags;
aoqi@0 183 public final int this_class;
aoqi@0 184 public final int super_class;
aoqi@0 185 public final int[] interfaces;
aoqi@0 186 public final Field[] fields;
aoqi@0 187 public final Method[] methods;
aoqi@0 188 public final Attributes attributes;
aoqi@0 189 }

mercurial