aoqi@0: /* aoqi@0: * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.classfile; aoqi@0: aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: import java.io.InputStream; aoqi@0: import java.nio.file.Files; aoqi@0: import java.nio.file.Path; aoqi@0: aoqi@0: import static com.sun.tools.classfile.AccessFlags.*; aoqi@0: aoqi@0: /** aoqi@0: * See JVMS, section 4.2. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: */ aoqi@0: public class ClassFile { aoqi@0: public static ClassFile read(File file) aoqi@0: throws IOException, ConstantPoolException { aoqi@0: return read(file.toPath(), new Attribute.Factory()); aoqi@0: } aoqi@0: aoqi@0: public static ClassFile read(Path input) aoqi@0: throws IOException, ConstantPoolException { aoqi@0: return read(input, new Attribute.Factory()); aoqi@0: } aoqi@0: aoqi@0: public static ClassFile read(Path input, Attribute.Factory attributeFactory) aoqi@0: throws IOException, ConstantPoolException { aoqi@0: try (InputStream in = Files.newInputStream(input)) { aoqi@0: return new ClassFile(in, attributeFactory); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static ClassFile read(File file, Attribute.Factory attributeFactory) aoqi@0: throws IOException, ConstantPoolException { aoqi@0: return read(file.toPath(), attributeFactory); aoqi@0: } aoqi@0: aoqi@0: public static ClassFile read(InputStream in) aoqi@0: throws IOException, ConstantPoolException { aoqi@0: return new ClassFile(in, new Attribute.Factory()); aoqi@0: } aoqi@0: aoqi@0: public static ClassFile read(InputStream in, Attribute.Factory attributeFactory) aoqi@0: throws IOException, ConstantPoolException { aoqi@0: return new ClassFile(in, attributeFactory); aoqi@0: } aoqi@0: aoqi@0: ClassFile(InputStream in, Attribute.Factory attributeFactory) throws IOException, ConstantPoolException { aoqi@0: ClassReader cr = new ClassReader(this, in, attributeFactory); aoqi@0: magic = cr.readInt(); aoqi@0: minor_version = cr.readUnsignedShort(); aoqi@0: major_version = cr.readUnsignedShort(); aoqi@0: constant_pool = new ConstantPool(cr); aoqi@0: access_flags = new AccessFlags(cr); aoqi@0: this_class = cr.readUnsignedShort(); aoqi@0: super_class = cr.readUnsignedShort(); aoqi@0: aoqi@0: int interfaces_count = cr.readUnsignedShort(); aoqi@0: interfaces = new int[interfaces_count]; aoqi@0: for (int i = 0; i < interfaces_count; i++) aoqi@0: interfaces[i] = cr.readUnsignedShort(); aoqi@0: aoqi@0: int fields_count = cr.readUnsignedShort(); aoqi@0: fields = new Field[fields_count]; aoqi@0: for (int i = 0; i < fields_count; i++) aoqi@0: fields[i] = new Field(cr); aoqi@0: aoqi@0: int methods_count = cr.readUnsignedShort(); aoqi@0: methods = new Method[methods_count]; aoqi@0: for (int i = 0; i < methods_count; i++) aoqi@0: methods[i] = new Method(cr); aoqi@0: aoqi@0: attributes = new Attributes(cr); aoqi@0: } aoqi@0: aoqi@0: public ClassFile(int magic, int minor_version, int major_version, aoqi@0: ConstantPool constant_pool, AccessFlags access_flags, aoqi@0: int this_class, int super_class, int[] interfaces, aoqi@0: Field[] fields, Method[] methods, Attributes attributes) { aoqi@0: this.magic = magic; aoqi@0: this.minor_version = minor_version; aoqi@0: this.major_version = major_version; aoqi@0: this.constant_pool = constant_pool; aoqi@0: this.access_flags = access_flags; aoqi@0: this.this_class = this_class; aoqi@0: this.super_class = super_class; aoqi@0: this.interfaces = interfaces; aoqi@0: this.fields = fields; aoqi@0: this.methods = methods; aoqi@0: this.attributes = attributes; aoqi@0: } aoqi@0: aoqi@0: public String getName() throws ConstantPoolException { aoqi@0: return constant_pool.getClassInfo(this_class).getName(); aoqi@0: } aoqi@0: aoqi@0: public String getSuperclassName() throws ConstantPoolException { aoqi@0: return constant_pool.getClassInfo(super_class).getName(); aoqi@0: } aoqi@0: aoqi@0: public String getInterfaceName(int i) throws ConstantPoolException { aoqi@0: return constant_pool.getClassInfo(interfaces[i]).getName(); aoqi@0: } aoqi@0: aoqi@0: public Attribute getAttribute(String name) { aoqi@0: return attributes.get(name); aoqi@0: } aoqi@0: aoqi@0: public boolean isClass() { aoqi@0: return !isInterface(); aoqi@0: } aoqi@0: aoqi@0: public boolean isInterface() { aoqi@0: return access_flags.is(ACC_INTERFACE); aoqi@0: } aoqi@0: aoqi@0: public int byteLength() { aoqi@0: return 4 + // magic aoqi@0: 2 + // minor aoqi@0: 2 + // major aoqi@0: constant_pool.byteLength() + aoqi@0: 2 + // access flags aoqi@0: 2 + // this_class aoqi@0: 2 + // super_class aoqi@0: byteLength(interfaces) + aoqi@0: byteLength(fields) + aoqi@0: byteLength(methods) + aoqi@0: attributes.byteLength(); aoqi@0: } aoqi@0: aoqi@0: private int byteLength(int[] indices) { aoqi@0: return 2 + 2 * indices.length; aoqi@0: } aoqi@0: aoqi@0: private int byteLength(Field[] fields) { aoqi@0: int length = 2; aoqi@0: for (Field f: fields) aoqi@0: length += f.byteLength(); aoqi@0: return length; aoqi@0: } aoqi@0: aoqi@0: private int byteLength(Method[] methods) { aoqi@0: int length = 2; aoqi@0: for (Method m: methods) aoqi@0: length += m.byteLength(); aoqi@0: return length; aoqi@0: } aoqi@0: aoqi@0: public final int magic; aoqi@0: public final int minor_version; aoqi@0: public final int major_version; aoqi@0: public final ConstantPool constant_pool; aoqi@0: public final AccessFlags access_flags; aoqi@0: public final int this_class; aoqi@0: public final int super_class; aoqi@0: public final int[] interfaces; aoqi@0: public final Field[] fields; aoqi@0: public final Method[] methods; aoqi@0: public final Attributes attributes; aoqi@0: }