Home | History | Annotate | Download | only in visitor
      1 /*
      2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
      3  *             of Java bytecode.
      4  *
      5  * Copyright (c) 2002-2014 Eric Lafortune (eric (at) graphics.cornell.edu)
      6  *
      7  * This program is free software; you can redistribute it and/or modify it
      8  * under the terms of the GNU General Public License as published by the Free
      9  * Software Foundation; either version 2 of the License, or (at your option)
     10  * any later version.
     11  *
     12  * This program is distributed in the hope that it will be useful, but WITHOUT
     13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
     15  * more details.
     16  *
     17  * You should have received a copy of the GNU General Public License along
     18  * with this program; if not, write to the Free Software Foundation, Inc.,
     19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
     20  */
     21 package proguard.classfile.visitor;
     22 
     23 import proguard.classfile.*;
     24 import proguard.classfile.util.ClassUtil;
     25 
     26 import java.io.PrintStream;
     27 
     28 
     29 /**
     30  * This <code>ClassVisitor</code> and <code>MemberVisitor</code>
     31  * prints out the class names of the classes it visits, and the full class
     32  * member descriptions of the class members it visits. The names are printed
     33  * in a readable, Java-like format. The access modifiers can be included or not.
     34  *
     35  * @author Eric Lafortune
     36  */
     37 public class SimpleClassPrinter
     38 implements   ClassVisitor,
     39              MemberVisitor
     40 {
     41     private final boolean     printAccessModifiers;
     42     private final PrintStream ps;
     43 
     44 
     45     /**
     46      * Creates a new SimpleClassPrinter that prints to
     47      * <code>System.out</code>, including the access modifiers.
     48      */
     49     public SimpleClassPrinter()
     50     {
     51         this(true);
     52     }
     53 
     54     /**
     55      * Creates a new SimpleClassPrinter that prints to
     56      * <code>System.out</code>, with or without the access modifiers.
     57      */
     58     public SimpleClassPrinter(boolean printAccessModifiers)
     59     {
     60         this(printAccessModifiers, System.out);
     61     }
     62 
     63     /**
     64      * Creates a new SimpleClassPrinter that prints to the given
     65      * <code>PrintStream</code>, with or without the access modifiers.
     66      */
     67     public SimpleClassPrinter(boolean     printAccessModifiers,
     68                               PrintStream printStream)
     69     {
     70         this.printAccessModifiers = printAccessModifiers;
     71         this.ps                   = printStream;
     72     }
     73 
     74 
     75     // Implementations for ClassVisitor.
     76 
     77     public void visitProgramClass(ProgramClass programClass)
     78     {
     79         ps.println(ClassUtil.externalFullClassDescription(
     80                        printAccessModifiers ?
     81                            programClass.getAccessFlags() :
     82                            0,
     83                        programClass.getName()));
     84     }
     85 
     86 
     87     public void visitLibraryClass(LibraryClass libraryClass)
     88     {
     89         ps.println(ClassUtil.externalFullClassDescription(
     90                        printAccessModifiers ?
     91                            libraryClass.getAccessFlags() :
     92                            0,
     93                        libraryClass.getName()));
     94     }
     95 
     96 
     97     // Implementations for MemberVisitor.
     98 
     99     public void visitProgramField(ProgramClass programClass, ProgramField programField)
    100     {
    101         ps.println(ClassUtil.externalFullClassDescription(
    102                        printAccessModifiers ?
    103                            programClass.getAccessFlags() :
    104                            0,
    105                        programClass.getName()) +
    106                    ": " +
    107                    ClassUtil.externalFullFieldDescription(
    108                        printAccessModifiers ?
    109                            programField.getAccessFlags() :
    110                            0,
    111                        programField.getName(programClass),
    112                        programField.getDescriptor(programClass)));
    113     }
    114 
    115 
    116     public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
    117     {
    118         ps.println(ClassUtil.externalFullClassDescription(
    119                        printAccessModifiers ?
    120                            programClass.getAccessFlags() :
    121                            0,
    122                        programClass.getName()) +
    123                    ": " +
    124                    ClassUtil.externalFullMethodDescription(
    125                        programClass.getName(),
    126                        printAccessModifiers ?
    127                            programMethod.getAccessFlags() :
    128                            0,
    129                        programMethod.getName(programClass),
    130                        programMethod.getDescriptor(programClass)));
    131     }
    132 
    133 
    134     public void visitLibraryField(LibraryClass libraryClass, LibraryField libraryField)
    135     {
    136         ps.println(ClassUtil.externalFullClassDescription(
    137                        printAccessModifiers ?
    138                            libraryClass.getAccessFlags() :
    139                            0,
    140                        libraryClass.getName()) +
    141                    ": " +
    142                    ClassUtil.externalFullFieldDescription(
    143                        printAccessModifiers ?
    144                            libraryField.getAccessFlags() :
    145                            0,
    146                        libraryField.getName(libraryClass),
    147                        libraryField.getDescriptor(libraryClass)));
    148     }
    149 
    150 
    151     public void visitLibraryMethod(LibraryClass libraryClass, LibraryMethod libraryMethod)
    152     {
    153         ps.println(ClassUtil.externalFullClassDescription(
    154                        printAccessModifiers ?
    155                            libraryClass.getAccessFlags() :
    156                            0,
    157                        libraryClass.getName()) +
    158                    ": " +
    159                    ClassUtil.externalFullMethodDescription(
    160                        libraryClass.getName(),
    161                        printAccessModifiers ?
    162                            libraryMethod.getAccessFlags() :
    163                            0,
    164                        libraryMethod.getName(libraryClass),
    165                        libraryMethod.getDescriptor(libraryClass)));
    166     }
    167 }
    168