001 
002 /*
003  *  JavaDoq 1.0 - DOCUment JAVA In Source
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.javadoq.com>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package com.javadoq.jjtree.ast;
021 
022 import java.util.ArrayList;
023 import java.util.List;
024 
025 import com.javadoq.JavaName;
026 import com.javadoq.JavaNestedType;
027 import com.javadoq.JavaType;
028 import com.javadoq.SourceFile;
029 import com.javadoq.jjtree.ASTCompilationUnit;
030 import com.javadoq.jjtree.ASTGrammar;
031 import com.javadoq.jjtree.ASTImportName;
032 import com.javadoq.jjtree.ASTName;
033 import com.javadoq.jjtree.ASTNewName;
034 import com.javadoq.jjtree.ASTNewType;
035 import com.javadoq.jjtree.ASTPackageName;
036 import com.javadoq.jjtree.ASTSuperList;
037 import com.javadoq.jjtree.JJTreeParserVisitor;
038 import com.javadoq.jjtree.SimpleNode;
039 
040 /**
041  * <p>A JJTree visitor to collect super Java types extended in a source file.</p>
042  * 
043  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.javadoq.com" target="_blank">http://www.javadoq.com</a>
044  */
045 public class SuperTypeVisitor implements JJTreeParserVisitor
046 {
047     /**
048      * <p>The source file where this visitor is collecting super types.</p>
049      * @since 1.0
050      */
051     public final SourceFile file;
052 
053     /**
054      * <p>Constructs a {@link SuperTypeVisitor}.</p>
055      * @param file The source file where this visitor is collecting super types.
056      * @since 1.0
057      */
058     public SuperTypeVisitor(SourceFile file) {
059         this.file = file;
060     }
061 
062     private List<JavaType> stack = new ArrayList<JavaType>();
063 
064     /**
065      * <p>Visits a JJTree node with the specific type.</p>
066      * @param node The JJTree node to visit.
067      * @param data Visitor data.
068      * @return A visitor data.
069      * @since 1.0
070      */
071     public Object visit(ASTNewType node, Object data) {
072         JavaType t;
073         if (stack.isEmpty()) {
074             t = file.pckg.types.get(node.name);
075         } else {
076             JavaType outer = stack.get(stack.size() - 1);
077             t = outer.inners.get(node.name);
078             
079         }
080         stack.add(t);
081         data = node.childrenAccept(this, data);
082         stack.remove(stack.size() - 1);
083         return data;
084     }
085 
086     /**
087      * <p>Visits a JJTree node with the specific type.</p>
088      * @param node The JJTree node to visit.
089      * @param data Visitor data.
090      * @return A visitor data.
091      * @since 1.0
092      */
093     public Object visit(ASTSuperList node, Object data) {
094         data = node.childrenAccept(this, data);
095         JavaType current = stack.get(stack.size() - 1);
096         for (String s : node.list) {
097             JavaName name = new JavaName(s);
098             JavaType t = null;
099             if (current instanceof JavaNestedType) {
100                 t = ((JavaNestedType)current).outer.findType(name);
101             }
102             if (t == null) {
103                 t = current.file.findType(name);
104             }
105             if (t != null) {
106                 current.supers.put(t.qname.text, t);
107             }
108         }
109         return data;
110     }
111 
112     /**
113      * <p>Visits a JJTree node with the specific type.</p>
114      * @param node The JJTree node to visit.
115      * @param data Visitor data.
116      * @return A visitor data.
117      * @since 1.0
118      */
119     public Object visit(SimpleNode node, Object data) {
120         if (node instanceof ASTCompilationUnit) {
121             return visit((ASTCompilationUnit)node, data);
122         } else if (node instanceof ASTNewType) {
123             return visit((ASTNewType)node, data);
124         } else if (node instanceof ASTSuperList) {
125             return visit((ASTSuperList)node, data);
126         }
127         return data;
128     }
129 
130     /**
131      * <p>Visits a JJTree node with the specific type.</p>
132      * @param node The JJTree node to visit.
133      * @param data Visitor data.
134      * @return A visitor data.
135      * @since 1.0
136      */
137     public Object visit(ASTCompilationUnit node, Object data) {
138         if (node.packageName.equals(file.pckg.name.text)) {
139             return node.childrenAccept(this, data);
140         }
141         return data;
142     }
143 
144     /**
145      * <p>Visits a JJTree node with the specific type.</p>
146      * <p>This method simply throws an UnsupportedOperationException.</p>
147      * @param node The JJTree node to visit.
148      * @param data Visitor data.
149      * @return A visitor data.
150      * @since 1.0
151      */
152     public Object visit(ASTName node, Object data) {
153         throw new UnsupportedOperationException();
154     }
155 
156     /**
157      * <p>Visits a JJTree node with the specific type.</p>
158      * <p>This method simply throws an UnsupportedOperationException.</p>
159      * @param node The JJTree node to visit.
160      * @param data Visitor data.
161      * @return A visitor data.
162      * @since 1.0
163      */
164     public Object visit(ASTImportName node, Object data) {
165         throw new UnsupportedOperationException();
166     }
167 
168     /**
169      * <p>Visits a JJTree node with the specific type.</p>
170      * <p>This method simply throws an UnsupportedOperationException.</p>
171      * @param node The JJTree node to visit.
172      * @param data Visitor data.
173      * @return A visitor data.
174      * @since 1.0
175      */
176     public Object visit(ASTNewName node, Object data) {
177         throw new UnsupportedOperationException();
178     }
179 
180     /**
181      * <p>Visits a JJTree node with the specific type.</p>
182      * <p>This method simply throws an UnsupportedOperationException.</p>
183      * @param node The JJTree node to visit.
184      * @param data Visitor data.
185      * @return A visitor data.
186      * @since 1.0
187      */
188     public Object visit(ASTPackageName node, Object data) {
189         throw new UnsupportedOperationException();
190     }
191 
192     /**
193      * <p>Visits a JJTree node with the specific type.</p>
194      * <p>This method simply throws an UnsupportedOperationException.</p>
195      * @param node The JJTree node to visit.
196      * @param data Visitor data.
197      * @return A visitor data.
198      * @since 1.0
199      */
200     public Object visit(ASTGrammar node, Object data) {
201         throw new UnsupportedOperationException();
202     }
203 }