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

changeset 46
7708bd6d800d
child 54
eaf608c64fec
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/classfile/ClassFile.java	Tue Jun 03 13:26:47 2008 -0700
     1.3 @@ -0,0 +1,153 @@
     1.4 +/*
     1.5 + * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.classfile;
    1.30 +
    1.31 +import java.io.File;
    1.32 +import java.io.FileInputStream;
    1.33 +import java.io.IOException;
    1.34 +import java.io.InputStream;
    1.35 +
    1.36 +import static com.sun.tools.classfile.AccessFlags.*;
    1.37 +
    1.38 +/**
    1.39 + * See JVMS3, section 4.2.
    1.40 + *
    1.41 + *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    1.42 + *  you write code that depends on this, you do so at your own risk.
    1.43 + *  This code and its internal interfaces are subject to change or
    1.44 + *  deletion without notice.</b>
    1.45 + */
    1.46 +public class ClassFile {
    1.47 +    public static ClassFile read(File file)
    1.48 +            throws IOException, ConstantPoolException {
    1.49 +        return read(file, new Attribute.Factory());
    1.50 +    }
    1.51 +
    1.52 +    public static ClassFile read(File file, Attribute.Factory attributeFactory)
    1.53 +            throws IOException, ConstantPoolException {
    1.54 +        FileInputStream in = new FileInputStream(file);
    1.55 +        try {
    1.56 +            return new ClassFile(in, attributeFactory);
    1.57 +        } finally {
    1.58 +            try {
    1.59 +                in.close();
    1.60 +            } catch (IOException e) {
    1.61 +                // ignore
    1.62 +            }
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66 +    public static ClassFile read(InputStream in)
    1.67 +            throws IOException, ConstantPoolException {
    1.68 +        return new ClassFile(in, new Attribute.Factory());
    1.69 +    }
    1.70 +
    1.71 +    public static ClassFile read(InputStream in, Attribute.Factory attributeFactory)
    1.72 +            throws IOException, ConstantPoolException {
    1.73 +        return new ClassFile(in, attributeFactory);
    1.74 +    }
    1.75 +
    1.76 +    ClassFile(InputStream in, Attribute.Factory attributeFactory) throws IOException, ConstantPoolException {
    1.77 +        ClassReader cr = new ClassReader(this, in, attributeFactory);
    1.78 +        magic = cr.readInt();
    1.79 +        minor_version = cr.readUnsignedShort();
    1.80 +        major_version = cr.readUnsignedShort();
    1.81 +        constant_pool = new ConstantPool(cr);
    1.82 +        access_flags = new AccessFlags(cr);
    1.83 +        this_class = cr.readUnsignedShort();
    1.84 +        super_class = cr.readUnsignedShort();
    1.85 +
    1.86 +        int interfaces_count = cr.readUnsignedShort();
    1.87 +        interfaces = new int[interfaces_count];
    1.88 +        for (int i = 0; i < interfaces_count; i++)
    1.89 +            interfaces[i] = cr.readUnsignedShort();
    1.90 +
    1.91 +        int fields_count = cr.readUnsignedShort();
    1.92 +        fields = new Field[fields_count];
    1.93 +        for (int i = 0; i < fields_count; i++)
    1.94 +            fields[i] = new Field(cr);
    1.95 +
    1.96 +        int methods_count = cr.readUnsignedShort();
    1.97 +        methods = new Method[methods_count];
    1.98 +        for (int i = 0; i < methods_count; i++)
    1.99 +            methods[i] = new Method(cr);
   1.100 +
   1.101 +        attributes = new Attributes(cr);
   1.102 +    }
   1.103 +
   1.104 +    public ClassFile(int magic, int minor_version, int major_version,
   1.105 +            ConstantPool constant_pool, AccessFlags access_flags,
   1.106 +            int this_class, int super_class, int[] interfaces,
   1.107 +            Field[] fields, Method[] methods, Attributes attributes) {
   1.108 +        this.magic = magic;
   1.109 +        this.minor_version = minor_version;
   1.110 +        this.major_version = major_version;
   1.111 +        this.constant_pool = constant_pool;
   1.112 +        this.access_flags = access_flags;
   1.113 +        this.this_class = this_class;
   1.114 +        this.super_class = super_class;
   1.115 +        this.interfaces = interfaces;
   1.116 +        this.fields = fields;
   1.117 +        this.methods = methods;
   1.118 +        this.attributes = attributes;
   1.119 +    }
   1.120 +
   1.121 +    public String getName() throws ConstantPoolException {
   1.122 +        return constant_pool.getClassInfo(this_class).getName();
   1.123 +    }
   1.124 +
   1.125 +    public String getSuperclassName() throws ConstantPoolException {
   1.126 +        return constant_pool.getClassInfo(super_class).getName();
   1.127 +    }
   1.128 +
   1.129 +    public String getInterfaceName(int i) throws ConstantPoolException {
   1.130 +        return constant_pool.getClassInfo(interfaces[i]).getName();
   1.131 +    }
   1.132 +
   1.133 +    public Attribute getAttribute(String name) {
   1.134 +        return attributes.get(name);
   1.135 +    }
   1.136 +
   1.137 +    public boolean isClass() {
   1.138 +        return !isInterface();
   1.139 +    }
   1.140 +
   1.141 +    public boolean isInterface() {
   1.142 +        return access_flags.is(ACC_INTERFACE);
   1.143 +    }
   1.144 +
   1.145 +    public final int magic;
   1.146 +    public final int minor_version;
   1.147 +    public final int major_version;
   1.148 +    public final ConstantPool constant_pool;
   1.149 +    public final AccessFlags access_flags;
   1.150 +    public final int this_class;
   1.151 +    public final int super_class;
   1.152 +    public final int[] interfaces;
   1.153 +    public final Field[] fields;
   1.154 +    public final Method[] methods;
   1.155 +    public final Attributes attributes;
   1.156 +}

mercurial