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.javap; jjg@46: jjg@46: import java.util.Collection; jjg@46: import java.util.List; jjg@46: jjg@46: import com.sun.tools.classfile.AccessFlags; jjg@46: import com.sun.tools.classfile.Attribute; jjg@46: import com.sun.tools.classfile.Attributes; jjg@46: import com.sun.tools.classfile.ClassFile; jjg@46: import com.sun.tools.classfile.Code_attribute; jjg@46: import com.sun.tools.classfile.ConstantPool; jjg@46: import com.sun.tools.classfile.ConstantPoolException; jjg@46: import com.sun.tools.classfile.Descriptor; jjg@46: import com.sun.tools.classfile.DescriptorException; jjg@46: import com.sun.tools.classfile.Exceptions_attribute; jjg@46: import com.sun.tools.classfile.Field; jjg@46: import com.sun.tools.classfile.Method; jjg@46: import com.sun.tools.classfile.Signature; jjg@46: import com.sun.tools.classfile.Signature_attribute; jjg@46: import com.sun.tools.classfile.SourceFile_attribute; jjg@46: import com.sun.tools.classfile.Type; jjg@46: jjg@46: import static com.sun.tools.classfile.AccessFlags.*; jjg@46: jjg@46: /* jjg@46: * The main javap class to write the contents of a class file as text. 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 ClassWriter extends BasicWriter {
jjg@46: static ClassWriter instance(Context context) {
jjg@46: ClassWriter instance = context.get(ClassWriter.class);
jjg@46: if (instance == null)
jjg@46: instance = new ClassWriter(context);
jjg@46: return instance;
jjg@46: }
jjg@46:
jjg@46: protected ClassWriter(Context context) {
jjg@46: super(context);
jjg@46: context.put(ClassWriter.class, this);
jjg@46: options = Options.instance(context);
jjg@46: attrWriter = AttributeWriter.instance(context);
jjg@46: codeWriter = CodeWriter.instance(context);
jjg@46: constantWriter = ConstantWriter.instance(context);
jjg@46: }
jjg@46:
jjg@46: ClassFile getClassFile() {
jjg@46: return classFile;
jjg@46: }
jjg@46:
jjg@46: Method getMethod() {
jjg@46: return method;
jjg@46: }
jjg@46:
jjg@46: public void write(ClassFile cf) {
jjg@46: classFile = cf;
jjg@46: constant_pool = classFile.constant_pool;
jjg@46:
jjg@46: Attribute sfa = cf.getAttribute(Attribute.SourceFile);
jjg@46: if (sfa instanceof SourceFile_attribute) {
jjg@46: println("Compiled from \"" + getSourceFile((SourceFile_attribute) sfa) + "\"");
jjg@46: }
jjg@46:
jjg@46: String name = getJavaName(classFile);
jjg@46: AccessFlags flags = cf.access_flags;
jjg@46:
jjg@46: writeModifiers(flags.getClassModifiers());
jjg@46:
jjg@46: if (classFile.isClass())
jjg@46: print("class ");
jjg@46: else if (classFile.isInterface())
jjg@46: print("interface ");
jjg@46:
jjg@46: print(name);
jjg@46:
jjg@46: Signature_attribute sigAttr = getSignature(cf.attributes);
jjg@46: if (sigAttr == null) {
jjg@46: // use info from class file header
jjg@65: if (classFile.isClass() && classFile.super_class != 0 ) {
jjg@65: String sn = getJavaSuperclassName(cf);
jjg@65: print(" extends ");
jjg@65: print(sn);
jjg@46: }
jjg@46: for (int i = 0; i < classFile.interfaces.length; i++) {
jjg@46: print(i == 0 ? (classFile.isClass() ? " implements " : " extends ") : ",");
jjg@46: print(getJavaInterfaceName(classFile, i));
jjg@46: }
jjg@46: } else {
jjg@46: try {
jjg@46: Type t = sigAttr.getParsedSignature().getType(constant_pool);
jjg@46: // The signature parser cannot disambiguate between a
jjg@46: // FieldType and a ClassSignatureType that only contains a superclass type.
jjg@46: if (t instanceof Type.ClassSigType)
jjg@46: print(t);
jjg@65: else {
jjg@46: print(" extends ");
jjg@46: print(t);
jjg@46: }
jjg@46: } catch (ConstantPoolException e) {
jjg@46: print(report(e));
jjg@46: }
jjg@46: }
jjg@46:
jjg@46: if (options.verbose) {
jjg@46: println();
jjg@46: attrWriter.write(cf, cf.attributes, constant_pool);
jjg@46: println(" minor version: " + cf.minor_version);
jjg@46: println(" major version: " + cf.major_version);
jjg@46: if (!options.compat)
jjg@46: writeList(" flags: ", flags.getClassFlags(), NEWLINE);
jjg@46: constantWriter.writeConstantPool();
jjg@46: println();
jjg@46: } else {
jjg@46: if (!options.compat)
jjg@46: print(" ");
jjg@46: }
jjg@46:
jjg@46: println("{");
jjg@46: writeFields();
jjg@46: writeMethods();
jjg@46: println("}");
jjg@46: println();
jjg@46: }
jjg@46:
jjg@46: void writeFields() {
jjg@46: for (Field f: classFile.fields) {
jjg@46: writeField(f);
jjg@46: }
jjg@46: }
jjg@46:
jjg@46: void writeField(Field f) {
jjg@46: if (!options.checkAccess(f.access_flags))
jjg@46: return;
jjg@46:
jjg@46: if (!(options.showLineAndLocalVariableTables
jjg@46: || options.showDisassembled
jjg@46: || options.verbose
jjg@46: || options.showInternalSignatures
jjg@46: || options.showAllAttrs)) {
jjg@46: print(" ");
jjg@46: }
jjg@46:
jjg@46: AccessFlags flags = f.access_flags;
jjg@46: writeModifiers(flags.getFieldModifiers());
jjg@46: Signature_attribute sigAttr = getSignature(f.attributes);
jjg@46: if (sigAttr == null)
jjg@46: print(getFieldType(f.descriptor));
jjg@46: else {
jjg@46: try {
jjg@46: Type t = sigAttr.getParsedSignature().getType(constant_pool);
jjg@46: print(t);
jjg@46: } catch (ConstantPoolException e) {
jjg@46: // report error?
jjg@46: // fall back on non-generic descriptor
jjg@46: print(getFieldType(f.descriptor));
jjg@46: }
jjg@46: }
jjg@46: print(" ");
jjg@46: print(getFieldName(f));
jjg@46: print(";");
jjg@46: println();
jjg@46:
jjg@46: if (options.showInternalSignatures)
jjg@46: println(" Signature: " + getValue(f.descriptor));
jjg@46:
jjg@46: if (options.verbose && !options.compat)
jjg@46: writeList(" flags: ", flags.getFieldFlags(), NEWLINE);
jjg@46:
jjg@46: if (options.showAllAttrs) {
jjg@46: for (Attribute attr: f.attributes)
jjg@46: attrWriter.write(f, attr, constant_pool);
jjg@46: println();
jjg@46: }
jjg@46:
jjg@46: if (options.showDisassembled || options.showLineAndLocalVariableTables)
jjg@46: println();
jjg@46: }
jjg@46:
jjg@46: void writeMethods() {
jjg@46: for (Method m: classFile.methods)
jjg@46: writeMethod(m);
jjg@46: }
jjg@46:
jjg@46: void writeMethod(Method m) {
jjg@46: if (!options.checkAccess(m.access_flags))
jjg@46: return;
jjg@46:
jjg@46: method = m;
jjg@46:
jjg@46: if (!(options.showLineAndLocalVariableTables
jjg@46: || options.showDisassembled
jjg@46: || options.verbose
jjg@46: || options.showInternalSignatures
jjg@46: || options.showAllAttrs)) {
jjg@46: print(" ");
jjg@46: }
jjg@46:
jjg@46: AccessFlags flags = m.access_flags;
jjg@46:
jjg@46: Descriptor d;
jjg@46: Type.MethodType methodType;
jjg@46: List extends Type> methodExceptions;
jjg@46:
jjg@46: Signature_attribute sigAttr = getSignature(m.attributes);
jjg@46: if (sigAttr == null) {
jjg@46: d = m.descriptor;
jjg@46: methodType = null;
jjg@46: methodExceptions = null;
jjg@46: } else {
jjg@46: Signature methodSig = sigAttr.getParsedSignature();
jjg@46: d = methodSig;
jjg@46: try {
jjg@46: methodType = (Type.MethodType) methodSig.getType(constant_pool);
jjg@46: methodExceptions = methodType.throwsTypes;
jjg@46: if (methodExceptions != null && methodExceptions.size() == 0)
jjg@46: methodExceptions = null;
jjg@46: } catch (ConstantPoolException e) {
jjg@46: // report error?
jjg@46: // fall back on standard descriptor
jjg@46: methodType = null;
jjg@46: methodExceptions = null;
jjg@46: }
jjg@46: }
jjg@46:
jjg@46: writeModifiers(flags.getMethodModifiers());
jjg@46: if (methodType != null) {
jjg@46: writeListIfNotEmpty("<", methodType.typeArgTypes, "> ");
jjg@46: }
jjg@46: if (getName(m).equals("