001    /*
002     $Id: SummaryCollector.java 3626 2006-02-20 15:16:17Z jez $
003    
004     Copyright 2005 (C) Jeremy Rayner. All Rights Reserved.
005    
006     Redistribution and use of this software and associated documentation
007     ("Software"), with or without modification, are permitted provided
008     that the following conditions are met:
009    
010     1. Redistributions of source code must retain copyright
011        statements and notices.  Redistributions must also contain a
012        copy of this document.
013    
014     2. Redistributions in binary form must reproduce the
015        above copyright notice, this list of conditions and the
016        following disclaimer in the documentation and/or other
017        materials provided with the distribution.
018    
019     3. The name "groovy" must not be used to endorse or promote
020        products derived from this Software without prior written
021        permission of The Codehaus.  For written permission,
022        please contact info@codehaus.org.
023    
024     4. Products derived from this Software may not be called "groovy"
025        nor may "groovy" appear in their names without prior written
026        permission of The Codehaus. "groovy" is a registered
027        trademark of The Codehaus.
028    
029     5. Due credit should be given to The Codehaus -
030        http://groovy.codehaus.org/
031    
032     THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033     ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034     NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
036     THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037     INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041     STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043     OF THE POSSIBILITY OF SUCH DAMAGE.
044    
045     */
046    package org.codehaus.groovy.antlr.treewalker;
047    
048    import org.codehaus.groovy.antlr.GroovySourceAST;
049    import org.codehaus.groovy.antlr.AntlrSourceSummary;
050    import org.codehaus.groovy.antlr.syntax.AntlrClassSource;
051    import org.codehaus.groovy.antlr.parser.GroovyTokenTypes;
052    import org.codehaus.groovy.syntax.ClassSource;
053    import org.codehaus.groovy.syntax.SourceSummary;
054    
055    import java.util.Stack;
056    
057    /**
058     * A visitor for the antlr ast that creates a summary of the parsed source unit
059     *
060     * @author Jeremy Rayner
061     */
062    public class SummaryCollector extends VisitorAdapter {
063        private SourceSummary sourceSummary;
064        private Stack stack;
065    
066        public SourceSummary getSourceSummary() {
067            return sourceSummary;
068        }
069    
070        public SummaryCollector() {
071            sourceSummary = new AntlrSourceSummary();
072            stack = new Stack();
073        }
074    
075        public void setUp() {
076        }
077    
078        public void visitClassDef(GroovySourceAST t, int visit) {
079            if (visit == OPENING_VISIT) {
080                ClassSource classSource = new AntlrClassSource(
081                        t.childOfType(GroovyTokenTypes.IDENT).getText(),t
082                );
083                // Here we assume that the optional restrictions (JLS$7.6)
084                // on compilation units in file-based implementations apply
085                // http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#26783
086                GroovySourceAST modifiers = t.childOfType(GroovyTokenTypes.MODIFIERS);
087    
088                if (isPublic(modifiers)) {
089                    sourceSummary.addPublic(classSource);
090                }
091            }
092        }
093    
094        /* figue out if this modifier node represents a public entity */
095        private boolean isPublic(GroovySourceAST modifiers) {
096            boolean isPublic = true;
097            if (modifiers != null) {
098                GroovySourceAST modprot = modifiers.childOfType(GroovyTokenTypes.LITERAL_protected);
099                GroovySourceAST modpriv = modifiers.childOfType(GroovyTokenTypes.LITERAL_private);
100                if (modpriv != null || modprot != null) {
101                    isPublic = false;
102                }
103            }
104            return isPublic;
105        }
106    
107        public void tearDown() {
108        }
109    
110        public void push(GroovySourceAST t) {
111            stack.push(t);
112        }
113        public GroovySourceAST pop() {
114            if (!stack.empty()) {
115                return (GroovySourceAST) stack.pop();
116            }
117            return null;
118        }
119    
120    }