001: /*
002: * ProGuard -- shrinking, optimization, obfuscation, and preverification
003: * of Java bytecode.
004: *
005: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the Free
009: * Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
015: * more details.
016: *
017: * You should have received a copy of the GNU General Public License along
018: * with this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: package proguard.classfile.visitor;
022:
023: import proguard.classfile.*;
024: import proguard.classfile.util.ClassUtil;
025:
026: import java.io.PrintStream;
027:
028: /**
029: * This <code>ClassVisitor</code> and <code>MemberVisitor</code>
030: * prints out the class names of the classes it visits, and the full class
031: * member descriptions of the class members it visits. The names are printed
032: * in a readable, Java-like format. The access modifiers can be included or not.
033: *
034: * @author Eric Lafortune
035: */
036: public class SimpleClassPrinter implements ClassVisitor, MemberVisitor {
037: private final boolean printAccessModifiers;
038: private final PrintStream ps;
039:
040: /**
041: * Creates a new SimpleClassPrinter that prints to
042: * <code>System.out</code>, including the access modifiers.
043: */
044: public SimpleClassPrinter() {
045: this (true);
046: }
047:
048: /**
049: * Creates a new SimpleClassPrinter that prints to
050: * <code>System.out</code>, with or without the access modifiers.
051: */
052: public SimpleClassPrinter(boolean printAccessModifiers) {
053: this (printAccessModifiers, System.out);
054: }
055:
056: /**
057: * Creates a new SimpleClassPrinter that prints to the given
058: * <code>PrintStream</code>, with or without the access modifiers.
059: */
060: public SimpleClassPrinter(boolean printAccessModifiers,
061: PrintStream printStream) {
062: this .printAccessModifiers = printAccessModifiers;
063: this .ps = printStream;
064: }
065:
066: // Implementations for ClassVisitor.
067:
068: public void visitProgramClass(ProgramClass programClass) {
069: ps.println(ClassUtil.externalFullClassDescription(
070: printAccessModifiers ? programClass.getAccessFlags()
071: : 0, programClass.getName()));
072: }
073:
074: public void visitLibraryClass(LibraryClass libraryClass) {
075: ps.println(ClassUtil.externalFullClassDescription(
076: printAccessModifiers ? libraryClass.getAccessFlags()
077: : 0, libraryClass.getName()));
078: }
079:
080: // Implementations for MemberVisitor.
081:
082: public void visitProgramField(ProgramClass programClass,
083: ProgramField programField) {
084: ps.println(ClassUtil.externalFullClassDescription(
085: printAccessModifiers ? programClass.getAccessFlags()
086: : 0, programClass.getName())
087: + ": "
088: + ClassUtil.externalFullFieldDescription(
089: printAccessModifiers ? programField
090: .getAccessFlags() : 0, programField
091: .getName(programClass), programField
092: .getDescriptor(programClass)));
093: }
094:
095: public void visitProgramMethod(ProgramClass programClass,
096: ProgramMethod programMethod) {
097: ps.println(ClassUtil.externalFullClassDescription(
098: printAccessModifiers ? programClass.getAccessFlags()
099: : 0, programClass.getName())
100: + ": "
101: + ClassUtil.externalFullMethodDescription(programClass
102: .getName(),
103: printAccessModifiers ? programMethod
104: .getAccessFlags() : 0, programMethod
105: .getName(programClass), programMethod
106: .getDescriptor(programClass)));
107: }
108:
109: public void visitLibraryField(LibraryClass libraryClass,
110: LibraryField libraryField) {
111: ps.println(ClassUtil.externalFullClassDescription(
112: printAccessModifiers ? libraryClass.getAccessFlags()
113: : 0, libraryClass.getName())
114: + ": "
115: + ClassUtil.externalFullFieldDescription(
116: printAccessModifiers ? libraryField
117: .getAccessFlags() : 0, libraryField
118: .getName(libraryClass), libraryField
119: .getDescriptor(libraryClass)));
120: }
121:
122: public void visitLibraryMethod(LibraryClass libraryClass,
123: LibraryMethod libraryMethod) {
124: ps.println(ClassUtil.externalFullClassDescription(
125: printAccessModifiers ? libraryClass.getAccessFlags()
126: : 0, libraryClass.getName())
127: + ": "
128: + ClassUtil.externalFullMethodDescription(libraryClass
129: .getName(),
130: printAccessModifiers ? libraryMethod
131: .getAccessFlags() : 0, libraryMethod
132: .getName(libraryClass), libraryMethod
133: .getDescriptor(libraryClass)));
134: }
135: }
|