jjg@46: /* xdono@54: * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. jjg@46: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@46: * jjg@46: * This code is free software; you can redistribute it and/or modify it jjg@46: * under the terms of the GNU General Public License version 2 only, as jjg@46: * published by the Free Software Foundation. Sun designates this jjg@46: * particular file as subject to the "Classpath" exception as provided jjg@46: * by Sun in the LICENSE file that accompanied this code. jjg@46: * jjg@46: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@46: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@46: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@46: * version 2 for more details (a copy is included in the LICENSE file that jjg@46: * accompanied this code). jjg@46: * jjg@46: * You should have received a copy of the GNU General Public License version jjg@46: * 2 along with this work; if not, write to the Free Software Foundation, jjg@46: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@46: * jjg@46: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, jjg@46: * CA 95054 USA or visit www.sun.com if you need additional information or jjg@46: * have any questions. jjg@46: */ jjg@46: jjg@46: package com.sun.tools.classfile; jjg@46: jjg@46: import java.io.File; jjg@46: import java.io.FileInputStream; jjg@46: import java.io.IOException; jjg@46: import java.io.InputStream; jjg@46: jjg@46: import static com.sun.tools.classfile.AccessFlags.*; jjg@46: jjg@46: /** jjg@46: * See JVMS3, section 4.2. jjg@46: * jjg@46: *

This is NOT part of any API supported by Sun Microsystems. If jjg@46: * you write code that depends on this, you do so at your own risk. jjg@46: * This code and its internal interfaces are subject to change or jjg@46: * deletion without notice. jjg@46: */ jjg@46: public class ClassFile { jjg@46: public static ClassFile read(File file) jjg@46: throws IOException, ConstantPoolException { jjg@46: return read(file, new Attribute.Factory()); jjg@46: } jjg@46: jjg@46: public static ClassFile read(File file, Attribute.Factory attributeFactory) jjg@46: throws IOException, ConstantPoolException { jjg@46: FileInputStream in = new FileInputStream(file); jjg@46: try { jjg@46: return new ClassFile(in, attributeFactory); jjg@46: } finally { jjg@46: try { jjg@46: in.close(); jjg@46: } catch (IOException e) { jjg@46: // ignore jjg@46: } jjg@46: } jjg@46: } jjg@46: jjg@46: public static ClassFile read(InputStream in) jjg@46: throws IOException, ConstantPoolException { jjg@46: return new ClassFile(in, new Attribute.Factory()); jjg@46: } jjg@46: jjg@46: public static ClassFile read(InputStream in, Attribute.Factory attributeFactory) jjg@46: throws IOException, ConstantPoolException { jjg@46: return new ClassFile(in, attributeFactory); jjg@46: } jjg@46: jjg@46: ClassFile(InputStream in, Attribute.Factory attributeFactory) throws IOException, ConstantPoolException { jjg@46: ClassReader cr = new ClassReader(this, in, attributeFactory); jjg@46: magic = cr.readInt(); jjg@46: minor_version = cr.readUnsignedShort(); jjg@46: major_version = cr.readUnsignedShort(); jjg@46: constant_pool = new ConstantPool(cr); jjg@46: access_flags = new AccessFlags(cr); jjg@46: this_class = cr.readUnsignedShort(); jjg@46: super_class = cr.readUnsignedShort(); jjg@46: jjg@46: int interfaces_count = cr.readUnsignedShort(); jjg@46: interfaces = new int[interfaces_count]; jjg@46: for (int i = 0; i < interfaces_count; i++) jjg@46: interfaces[i] = cr.readUnsignedShort(); jjg@46: jjg@46: int fields_count = cr.readUnsignedShort(); jjg@46: fields = new Field[fields_count]; jjg@46: for (int i = 0; i < fields_count; i++) jjg@46: fields[i] = new Field(cr); jjg@46: jjg@46: int methods_count = cr.readUnsignedShort(); jjg@46: methods = new Method[methods_count]; jjg@46: for (int i = 0; i < methods_count; i++) jjg@46: methods[i] = new Method(cr); jjg@46: jjg@46: attributes = new Attributes(cr); jjg@46: } jjg@46: jjg@46: public ClassFile(int magic, int minor_version, int major_version, jjg@46: ConstantPool constant_pool, AccessFlags access_flags, jjg@46: int this_class, int super_class, int[] interfaces, jjg@46: Field[] fields, Method[] methods, Attributes attributes) { jjg@46: this.magic = magic; jjg@46: this.minor_version = minor_version; jjg@46: this.major_version = major_version; jjg@46: this.constant_pool = constant_pool; jjg@46: this.access_flags = access_flags; jjg@46: this.this_class = this_class; jjg@46: this.super_class = super_class; jjg@46: this.interfaces = interfaces; jjg@46: this.fields = fields; jjg@46: this.methods = methods; jjg@46: this.attributes = attributes; jjg@46: } jjg@46: jjg@46: public String getName() throws ConstantPoolException { jjg@46: return constant_pool.getClassInfo(this_class).getName(); jjg@46: } jjg@46: jjg@46: public String getSuperclassName() throws ConstantPoolException { jjg@46: return constant_pool.getClassInfo(super_class).getName(); jjg@46: } jjg@46: jjg@46: public String getInterfaceName(int i) throws ConstantPoolException { jjg@46: return constant_pool.getClassInfo(interfaces[i]).getName(); jjg@46: } jjg@46: jjg@46: public Attribute getAttribute(String name) { jjg@46: return attributes.get(name); jjg@46: } jjg@46: jjg@46: public boolean isClass() { jjg@46: return !isInterface(); jjg@46: } jjg@46: jjg@46: public boolean isInterface() { jjg@46: return access_flags.is(ACC_INTERFACE); jjg@46: } jjg@46: jjg@46: public final int magic; jjg@46: public final int minor_version; jjg@46: public final int major_version; jjg@46: public final ConstantPool constant_pool; jjg@46: public final AccessFlags access_flags; jjg@46: public final int this_class; jjg@46: public final int super_class; jjg@46: public final int[] interfaces; jjg@46: public final Field[] fields; jjg@46: public final Method[] methods; jjg@46: public final Attributes attributes; jjg@46: }