001: /*
002: * Janino - An embedded Java[TM] compiler
003: *
004: * Copyright (c) 2006, Arno Unkrig
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above
014: * copyright notice, this list of conditions and the following
015: * disclaimer in the documentation and/or other materials
016: * provided with the distribution.
017: * 3. The name of the author may not be used to endorse or promote
018: * products derived from this software without specific prior
019: * written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
022: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
023: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
025: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
026: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
027: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
029: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
030: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
031: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032: */
033:
034: package org.codehaus.janino.samples;
035:
036: import java.io.*;
037:
038: import org.codehaus.janino.*;
039: import org.codehaus.janino.util.*;
040:
041: /**
042: * An example application for the {@link org.codehaus.janino.util.Traverser}:
043: * Reads, scans and parses the files named on the command line and counts
044: * several kinds of declarations.
045: */
046: public class DeclarationCounter extends Traverser {
047: public static void main(String[] args)
048: throws Scanner.ScanException, IOException,
049: Parser.ParseException {
050: DeclarationCounter dc = new DeclarationCounter();
051: for (int i = 0; i < args.length; ++i) {
052: String fileName = args[i];
053:
054: // Parse each compilation unit.
055: FileReader r = new FileReader(fileName);
056: Java.CompilationUnit cu;
057: try {
058: cu = new Parser(new Scanner(fileName, r))
059: .parseCompilationUnit();
060: } finally {
061: r.close();
062: }
063:
064: // Traverse it and count declarations.
065: dc.traverseCompilationUnit(cu);
066: }
067:
068: System.out.println("Class declarations: "
069: + dc.classDeclarationCount);
070: System.out.println("Interface declarations: "
071: + dc.interfaceDeclarationCount);
072: System.out.println("Fields: " + dc.fieldCount);
073: System.out.println("Local variables: "
074: + dc.localVariableCount);
075: }
076:
077: // Count class declarations.
078: public void traverseClassDeclaration(Java.ClassDeclaration cd) {
079: ++this .classDeclarationCount;
080: super .traverseClassDeclaration(cd);
081: }
082:
083: private int classDeclarationCount = 0;
084:
085: // Count interface declarations.
086: public void traverseInterfaceDeclaration(
087: Java.InterfaceDeclaration id) {
088: ++this .interfaceDeclarationCount;
089: super .traverseInterfaceDeclaration(id);
090: }
091:
092: private int interfaceDeclarationCount = 0;
093:
094: // Count fields.
095: public void traverseFieldDeclaration(Java.FieldDeclaration fd) {
096: this .fieldCount += fd.variableDeclarators.length;
097: super .traverseFieldDeclaration(fd);
098: }
099:
100: private int fieldCount = 0;
101:
102: // Count local variables.
103: public void traverseLocalVariableDeclarationStatement(
104: Java.LocalVariableDeclarationStatement lvds) {
105: this .localVariableCount += lvds.variableDeclarators.length;
106: super .traverseLocalVariableDeclarationStatement(lvds);
107: }
108:
109: private int localVariableCount = 0;
110: }
|