ClassFile.java revision 2942:08092deced3f
1/* 2 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.tools.classfile; 27 28import java.io.File; 29import java.io.IOException; 30import java.io.InputStream; 31import java.nio.file.Files; 32import java.nio.file.Path; 33 34import static com.sun.tools.classfile.AccessFlags.*; 35 36/** 37 * See JVMS, section 4.2. 38 * 39 * <p><b>This is NOT part of any supported API. 40 * If you write code that depends on this, you do so at your own risk. 41 * This code and its internal interfaces are subject to change or 42 * deletion without notice.</b> 43 */ 44public class ClassFile { 45 public static ClassFile read(File file) 46 throws IOException, ConstantPoolException { 47 return read(file.toPath(), new Attribute.Factory()); 48 } 49 50 public static ClassFile read(Path input) 51 throws IOException, ConstantPoolException { 52 return read(input, new Attribute.Factory()); 53 } 54 55 public static ClassFile read(Path input, Attribute.Factory attributeFactory) 56 throws IOException, ConstantPoolException { 57 try (InputStream in = Files.newInputStream(input)) { 58 return new ClassFile(in, attributeFactory); 59 } 60 } 61 62 public static ClassFile read(File file, Attribute.Factory attributeFactory) 63 throws IOException, ConstantPoolException { 64 return read(file.toPath(), attributeFactory); 65 } 66 67 public static ClassFile read(InputStream in) 68 throws IOException, ConstantPoolException { 69 return new ClassFile(in, new Attribute.Factory()); 70 } 71 72 public static ClassFile read(InputStream in, Attribute.Factory attributeFactory) 73 throws IOException, ConstantPoolException { 74 return new ClassFile(in, attributeFactory); 75 } 76 77 ClassFile(InputStream in, Attribute.Factory attributeFactory) throws IOException, ConstantPoolException { 78 ClassReader cr = new ClassReader(this, in, attributeFactory); 79 magic = cr.readInt(); 80 minor_version = cr.readUnsignedShort(); 81 major_version = cr.readUnsignedShort(); 82 constant_pool = new ConstantPool(cr); 83 access_flags = new AccessFlags(cr); 84 this_class = cr.readUnsignedShort(); 85 super_class = cr.readUnsignedShort(); 86 87 int interfaces_count = cr.readUnsignedShort(); 88 interfaces = new int[interfaces_count]; 89 for (int i = 0; i < interfaces_count; i++) 90 interfaces[i] = cr.readUnsignedShort(); 91 92 int fields_count = cr.readUnsignedShort(); 93 fields = new Field[fields_count]; 94 for (int i = 0; i < fields_count; i++) 95 fields[i] = new Field(cr); 96 97 int methods_count = cr.readUnsignedShort(); 98 methods = new Method[methods_count]; 99 for (int i = 0; i < methods_count; i++) 100 methods[i] = new Method(cr); 101 102 attributes = new Attributes(cr); 103 } 104 105 public ClassFile(int magic, int minor_version, int major_version, 106 ConstantPool constant_pool, AccessFlags access_flags, 107 int this_class, int super_class, int[] interfaces, 108 Field[] fields, Method[] methods, Attributes attributes) { 109 this.magic = magic; 110 this.minor_version = minor_version; 111 this.major_version = major_version; 112 this.constant_pool = constant_pool; 113 this.access_flags = access_flags; 114 this.this_class = this_class; 115 this.super_class = super_class; 116 this.interfaces = interfaces; 117 this.fields = fields; 118 this.methods = methods; 119 this.attributes = attributes; 120 } 121 122 public String getName() throws ConstantPoolException { 123 return constant_pool.getClassInfo(this_class).getName(); 124 } 125 126 public String getSuperclassName() throws ConstantPoolException { 127 return constant_pool.getClassInfo(super_class).getName(); 128 } 129 130 public String getInterfaceName(int i) throws ConstantPoolException { 131 return constant_pool.getClassInfo(interfaces[i]).getName(); 132 } 133 134 public Attribute getAttribute(String name) { 135 return attributes.get(name); 136 } 137 138 public boolean isClass() { 139 return !isInterface(); 140 } 141 142 public boolean isInterface() { 143 return access_flags.is(ACC_INTERFACE); 144 } 145 146 public int byteLength() { 147 return 4 + // magic 148 2 + // minor 149 2 + // major 150 constant_pool.byteLength() + 151 2 + // access flags 152 2 + // this_class 153 2 + // super_class 154 byteLength(interfaces) + 155 byteLength(fields) + 156 byteLength(methods) + 157 attributes.byteLength(); 158 } 159 160 private int byteLength(int[] indices) { 161 return 2 + 2 * indices.length; 162 } 163 164 private int byteLength(Field[] fields) { 165 int length = 2; 166 for (Field f: fields) 167 length += f.byteLength(); 168 return length; 169 } 170 171 private int byteLength(Method[] methods) { 172 int length = 2; 173 for (Method m: methods) 174 length += m.byteLength(); 175 return length; 176 } 177 178 public final int magic; 179 public final int minor_version; 180 public final int major_version; 181 public final ConstantPool constant_pool; 182 public final AccessFlags access_flags; 183 public final int this_class; 184 public final int super_class; 185 public final int[] interfaces; 186 public final Field[] fields; 187 public final Method[] methods; 188 public final Attributes attributes; 189} 190