jjg@46: /* ohair@554: * Copyright (c) 2007, 2008, Oracle and/or its affiliates. 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 ohair@554: * published by the Free Software Foundation. Oracle designates this jjg@46: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle 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: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@46: */ jjg@46: jjg@46: package com.sun.tools.javap; jjg@46: jjg@88: import java.net.URI; jjg@283: import java.text.DateFormat; jjg@46: import java.util.Collection; jjg@283: import java.util.Date; 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@87: import com.sun.tools.classfile.ConstantValue_attribute; 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@581: *
This is NOT part of any supported API.
jjg@581: * If 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@88: void setDigest(String name, byte[] digest) {
jjg@88: this.digestName = name;
jjg@88: this.digest = digest;
jjg@88: }
jjg@88:
jjg@88: void setFile(URI uri) {
jjg@88: this.uri = uri;
jjg@88: }
jjg@88:
jjg@88: void setFileSize(int size) {
jjg@88: this.size = size;
jjg@88: }
jjg@88:
jjg@88: void setLastModified(long lastModified) {
jjg@88: this.lastModified = lastModified;
jjg@88: }
jjg@88:
jjg@300: protected ClassFile getClassFile() {
jjg@46: return classFile;
jjg@46: }
jjg@46:
jjg@300: protected void setClassFile(ClassFile cf) {
jjg@300: classFile = cf;
jjg@300: constant_pool = classFile.constant_pool;
jjg@300: }
jjg@300:
jjg@300: protected Method getMethod() {
jjg@46: return method;
jjg@46: }
jjg@46:
jjg@300: protected void setMethod(Method m) {
jjg@300: method = m;
jjg@300: }
jjg@300:
jjg@46: public void write(ClassFile cf) {
jjg@300: setClassFile(cf);
jjg@46:
jjg@88: if ((options.sysInfo || options.verbose) && !options.compat) {
jjg@88: if (uri != null) {
jjg@88: if (uri.getScheme().equals("file"))
jjg@88: println("Classfile " + uri.getPath());
jjg@88: else
jjg@88: println("Classfile " + uri);
jjg@88: }
jjg@348: indent(+1);
jjg@88: if (lastModified != -1) {
jjg@88: Date lm = new Date(lastModified);
jjg@88: DateFormat df = DateFormat.getDateInstance();
jjg@88: if (size > 0) {
jjg@88: println("Last modified " + df.format(lm) + "; size " + size + " bytes");
jjg@88: } else {
jjg@88: println("Last modified " + df.format(lm));
jjg@88: }
jjg@88: } else if (size > 0) {
jjg@88: println("Size " + size + " bytes");
jjg@88: }
jjg@88: if (digestName != null && digest != null) {
jjg@88: StringBuilder sb = new StringBuilder();
jjg@88: for (byte b: digest)
jjg@88: sb.append(String.format("%02x", b));
jjg@88: println(digestName + " checksum " + sb);
jjg@88: }
jjg@88: }
jjg@88:
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@348: if ((options.sysInfo || options.verbose) && !options.compat) {
jjg@348: indent(-1);
jjg@348: }
jjg@348:
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@427: print(getJavaName(t.toString()));
jjg@65: else {
jjg@46: print(" extends ");
jjg@427: print(getJavaName(t.toString()));
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@348: indent(+1);
jjg@46: attrWriter.write(cf, cf.attributes, constant_pool);
jjg@348: println("minor version: " + cf.minor_version);
jjg@348: println("major version: " + cf.major_version);
jjg@46: if (!options.compat)
jjg@348: writeList("flags: ", flags.getClassFlags(), NEWLINE);
jjg@348: indent(-1);
jjg@46: constantWriter.writeConstantPool();
jjg@46: } else {
jjg@348: print(" ");
jjg@46: }
jjg@46:
jjg@46: println("{");
jjg@348: indent(+1);
jjg@46: writeFields();
jjg@46: writeMethods();
jjg@348: indent(-1);
jjg@46: println("}");
jjg@46: }
jjg@46:
jjg@300: protected void writeFields() {
jjg@46: for (Field f: classFile.fields) {
jjg@46: writeField(f);
jjg@46: }
jjg@46: }
jjg@46:
jjg@300: protected void writeField(Field f) {
jjg@46: if (!options.checkAccess(f.access_flags))
jjg@46: return;
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@528: print(getJavaFieldType(f.descriptor));
jjg@46: else {
jjg@46: try {
jjg@46: Type t = sigAttr.getParsedSignature().getType(constant_pool);
jjg@528: print(getJavaName(t.toString()));
jjg@46: } catch (ConstantPoolException e) {
jjg@46: // report error?
jjg@46: // fall back on non-generic descriptor
jjg@528: print(getJavaFieldType(f.descriptor));
jjg@46: }
jjg@46: }
jjg@46: print(" ");
jjg@46: print(getFieldName(f));
jjg@87: if (options.showConstants && !options.compat) { // BUG 4111861 print static final field contents
jjg@87: Attribute a = f.attributes.get(Attribute.ConstantValue);
jjg@87: if (a instanceof ConstantValue_attribute) {
jjg@87: print(" = ");
jjg@87: ConstantValue_attribute cv = (ConstantValue_attribute) a;
jjg@87: print(getConstantValue(f.descriptor, cv.constantvalue_index));
jjg@87: }
jjg@87: }
jjg@46: print(";");
jjg@46: println();
jjg@46:
jjg@348: indent(+1);
jjg@348:
jjg@46: if (options.showInternalSignatures)
jjg@348: println("Signature: " + getValue(f.descriptor));
jjg@46:
jjg@46: if (options.verbose && !options.compat)
jjg@348: 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@348: indent(-1);
jjg@348:
jjg@46: if (options.showDisassembled || options.showLineAndLocalVariableTables)
jjg@46: println();
jjg@46: }
jjg@46:
jjg@300: protected void writeMethods() {
jjg@46: for (Method m: classFile.methods)
jjg@46: writeMethod(m);
jjg@348: setPendingNewline(false);
jjg@46: }
jjg@46:
jjg@300: protected 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: 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@427: writeListIfNotEmpty("<", methodType.typeParamTypes, "> ");
jjg@46: }
jjg@46: if (getName(m).equals("