Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ClassFile.java
41161 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.classfile;2627import java.io.File;28import java.io.IOException;29import java.io.InputStream;30import java.nio.file.Files;31import java.nio.file.Path;3233import static com.sun.tools.classfile.AccessFlags.*;3435/**36* See JVMS, section 4.2.37*38* <p><b>This is NOT part of any supported API.39* If you write code that depends on this, you do so at your own risk.40* This code and its internal interfaces are subject to change or41* deletion without notice.</b>42*/43public class ClassFile {44public static ClassFile read(File file)45throws IOException, ConstantPoolException {46return read(file.toPath(), new Attribute.Factory());47}4849public static ClassFile read(Path input)50throws IOException, ConstantPoolException {51return read(input, new Attribute.Factory());52}5354public static ClassFile read(Path input, Attribute.Factory attributeFactory)55throws IOException, ConstantPoolException {56try (InputStream in = Files.newInputStream(input)) {57return new ClassFile(in, attributeFactory);58}59}6061public static ClassFile read(File file, Attribute.Factory attributeFactory)62throws IOException, ConstantPoolException {63return read(file.toPath(), attributeFactory);64}6566public static ClassFile read(InputStream in)67throws IOException, ConstantPoolException {68return new ClassFile(in, new Attribute.Factory());69}7071public static ClassFile read(InputStream in, Attribute.Factory attributeFactory)72throws IOException, ConstantPoolException {73return new ClassFile(in, attributeFactory);74}7576ClassFile(InputStream in, Attribute.Factory attributeFactory) throws IOException, ConstantPoolException {77ClassReader cr = new ClassReader(this, in, attributeFactory);78magic = cr.readInt();79minor_version = cr.readUnsignedShort();80major_version = cr.readUnsignedShort();81constant_pool = new ConstantPool(cr);82access_flags = new AccessFlags(cr);83this_class = cr.readUnsignedShort();84super_class = cr.readUnsignedShort();8586int interfaces_count = cr.readUnsignedShort();87interfaces = new int[interfaces_count];88for (int i = 0; i < interfaces_count; i++)89interfaces[i] = cr.readUnsignedShort();9091int fields_count = cr.readUnsignedShort();92fields = new Field[fields_count];93for (int i = 0; i < fields_count; i++)94fields[i] = new Field(cr);9596int methods_count = cr.readUnsignedShort();97methods = new Method[methods_count];98for (int i = 0; i < methods_count; i++)99methods[i] = new Method(cr);100101attributes = new Attributes(cr);102}103104public ClassFile(int magic, int minor_version, int major_version,105ConstantPool constant_pool, AccessFlags access_flags,106int this_class, int super_class, int[] interfaces,107Field[] fields, Method[] methods, Attributes attributes) {108this.magic = magic;109this.minor_version = minor_version;110this.major_version = major_version;111this.constant_pool = constant_pool;112this.access_flags = access_flags;113this.this_class = this_class;114this.super_class = super_class;115this.interfaces = interfaces;116this.fields = fields;117this.methods = methods;118this.attributes = attributes;119}120121public String getName() throws ConstantPoolException {122return constant_pool.getClassInfo(this_class).getName();123}124125public String getSuperclassName() throws ConstantPoolException {126return constant_pool.getClassInfo(super_class).getName();127}128129public String getInterfaceName(int i) throws ConstantPoolException {130return constant_pool.getClassInfo(interfaces[i]).getName();131}132133public Attribute getAttribute(String name) {134return attributes.get(name);135}136137public boolean isClass() {138return !isInterface();139}140141public boolean isInterface() {142return access_flags.is(ACC_INTERFACE);143}144145public int byteLength() {146return 4 + // magic1472 + // minor1482 + // major149constant_pool.byteLength() +1502 + // access flags1512 + // this_class1522 + // super_class153byteLength(interfaces) +154byteLength(fields) +155byteLength(methods) +156attributes.byteLength();157}158159private int byteLength(int[] indices) {160return 2 + 2 * indices.length;161}162163private int byteLength(Field[] fields) {164int length = 2;165for (Field f: fields)166length += f.byteLength();167return length;168}169170private int byteLength(Method[] methods) {171int length = 2;172for (Method m: methods)173length += m.byteLength();174return length;175}176177public final int magic;178public final int minor_version;179public final int major_version;180public final ConstantPool constant_pool;181public final AccessFlags access_flags;182public final int this_class;183public final int super_class;184public final int[] interfaces;185public final Field[] fields;186public final Method[] methods;187public final Attributes attributes;188}189190191