View Javadoc
Minimize
Table

Bug Overview

linebug prioritybugbug descriptionexisting sinceauthor
72mediumExcessiveMethodLengthDiese Methode ist sehr lang, ein Indiz dafür das diese Methode zu viel tut, es sollten Hilfsmethoden genutzt und Copy/Paste-Code entfernt werden. Falls dies beabsichtigt ist und der Fehler soll ignoriert werden, dann //NOBUG verwenden.  Details zum Fehler...18.06.2012-10:31Unbekannt
103mediumTODODer Code ist an dieser Stelle eventuell unfertig oder fehlerhaft. Bitte überprüfen!18.06.2012-10:31Unbekannt

1   /*
2    * Copyright 2003-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.groovy.antlr.treewalker;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  
21  import org.codehaus.groovy.antlr.GroovySourceAST;
22  import org.codehaus.groovy.antlr.parser.GroovyTokenTypes;
23  
24  /**
25   * A treewalker for the antlr generated AST that attempts to visit the
26   * AST nodes in the order needed to generate valid groovy source code.
27   *
28   * @author <a href="mailto:groovy@ross-rayner.com">Jeremy Rayner</a>
29   */
30  public class SourceCodeTraversal extends TraversalHelper {
31      /**
32       * Constructs a treewalker for the antlr generated AST that attempts to visit the
33       * AST nodes in the order needed to generate valid groovy source code.
34       * @param visitor the visitor implementation to call for each AST node.
35       */
36      public SourceCodeTraversal(Visitor visitor) {
37          super(visitor);
38      }
39  
40      /**
41       * gather, sort and process all unvisited nodes
42       * @param t the AST to process
43       */
44      public void setUp(GroovySourceAST t) {
45          super.setUp(t);
46          
47          // gather and sort all unvisited AST nodes
48          unvisitedNodes = new ArrayList<GroovySourceAST>();
49          traverse(t);
50          Collections.sort(unvisitedNodes);
51      }
52  
53      /**
54       * traverse an AST node
55       * @param t the AST node to traverse
56       */
57      private void traverse(GroovySourceAST t) {
58          if (t == null) { return; }
59          if (unvisitedNodes != null) {
60             unvisitedNodes.add(t);
61          }
62          GroovySourceAST child = (GroovySourceAST)t.getFirstChild();
63          if (child != null) {
64              traverse(child);
65          }
66          GroovySourceAST sibling = (GroovySourceAST)t.getNextSibling();
67          if (sibling != null) {
68              traverse(sibling);
69          }
70      }
71  
72 bug overview next bug        protected void accept(GroovySourceAST currentNode) {  HEALTH4J >>  :  ExcessiveMethodLength 
73          if (currentNode != null && unvisitedNodes != null && unvisitedNodes.size() > 0) {
74              GroovySourceAST t = currentNode;
75  
76              if (!(unvisitedNodes.contains(currentNode))) {
77                  return;
78              }
79              push(t);
80              switch (t.getType()) {
81                  case GroovyTokenTypes.QUESTION: // expr?foo:bar
82                      accept_FirstChild_v_SecondChild_v_ThirdChild_v(t);
83                      break;
84  
85                  case GroovyTokenTypes.CASE_GROUP: //
86                  case GroovyTokenTypes.LITERAL_instanceof: // foo instanceof MyType
87                      accept_FirstChild_v_SecondChildsChildren_v(t);
88                      break;
89  
90                  case GroovyTokenTypes.ANNOTATION:
91                      accept_v_FirstChild_2ndv_SecondChild_v___LastChild_v(t);
92                      break;
93  
94                  case GroovyTokenTypes.CLOSURE_LIST: // (a=1; a<10; a++)
95                  case GroovyTokenTypes.ELIST: // a,b,c
96                  case GroovyTokenTypes.PARAMETERS: // a,b,c
97                  case GroovyTokenTypes.TYPE_ARGUMENTS: // <String, Object>
98                  case GroovyTokenTypes.STRING_CONSTRUCTOR: // "foo${bar}wibble"
99                  case GroovyTokenTypes.TYPE_PARAMETER: // class Foo<T extends F>
100                 case GroovyTokenTypes.TYPE_PARAMETERS: // class Foo<T>
101                 case GroovyTokenTypes.TYPE_UPPER_BOUNDS: // class Foo<T extends F>
102                     accept_v_FirstChild_v_SecondChild_v___LastChild_v(t);
103 previous bug bug overview                       // todo : confirm that TYPE_LOWER_BOUNDS does not have multiple children 
104                     break;
105 
106                 case GroovyTokenTypes.VARIABLE_PARAMETER_DEF: // void f(String ... others) {}
107                     accept_v_FirstChild_SecondChild_v_ThirdChild_v(t);
108                     break;
109 
110                 case GroovyTokenTypes.INDEX_OP:
111                     //accept_FirstChild_v_SecondChild_v(t);
112                     accept_SecondChild_v_ThirdChild_v(t);
113                     break;
114 
115                 case GroovyTokenTypes.ENUM_CONSTANT_DEF: // enum Foo(THESE,ARE,THEY)
116                 case GroovyTokenTypes.EXPR:
117                 case GroovyTokenTypes.IMPORT:
118                 case GroovyTokenTypes.STATIC_IMPORT:
119                 case GroovyTokenTypes.VARIABLE_DEF:
120                 case GroovyTokenTypes.METHOD_DEF:
121                 case GroovyTokenTypes.OBJBLOCK: //class Foo {def bar()}  <-- this block
122                 case GroovyTokenTypes.PARAMETER_DEF: // void f(String me) {}
123                 case GroovyTokenTypes.SLIST: // list of expressions, variable defs etc
124                     accept_v_AllChildren_v(t);
125                     break;
126 
127                 case GroovyTokenTypes.ANNOTATION_MEMBER_VALUE_PAIR: // @Blue(foo=123)
128                 case GroovyTokenTypes.ASSIGN: // a = b
129                 case GroovyTokenTypes.BAND_ASSIGN: // a &= b
130                 case GroovyTokenTypes.BOR_ASSIGN: // a |= b
131                 case GroovyTokenTypes.BSR_ASSIGN: // a >>>= b
132                 case GroovyTokenTypes.BXOR_ASSIGN: // a ^= b
133                 case GroovyTokenTypes.COMPARE_TO: // a <=> b
134                 case GroovyTokenTypes.DIV_ASSIGN: // a /= b
135                 case GroovyTokenTypes.EQUAL: // a == b
136                 case GroovyTokenTypes.MINUS_ASSIGN: // a -= b
137                 case GroovyTokenTypes.MOD_ASSIGN: // a %= b
138                 case GroovyTokenTypes.NOT_EQUAL: // a != b
139                 case GroovyTokenTypes.PLUS_ASSIGN: // a += b
140                 case GroovyTokenTypes.REGEX_FIND: // a =~ b
141                 case GroovyTokenTypes.REGEX_MATCH: // a ==~ b
142                 case GroovyTokenTypes.SL_ASSIGN: // a <<= b
143                 case GroovyTokenTypes.SR_ASSIGN: // a >>= b
144                 case GroovyTokenTypes.STAR_ASSIGN: // a *= b
145                 case GroovyTokenTypes.STAR_STAR_ASSIGN: // x **= 3
146                     if (t.childAt(1) != null) {
147                         accept_FirstChild_v_RestOfTheChildren(t);
148                     } else {
149                         accept_v_FirstChild_v_RestOfTheChildren(t);
150                     }
151                     break;
152 
153                 case GroovyTokenTypes.ANNOTATION_FIELD_DEF: // @interface Foo{ int bar()...
154                     accept_FirstSecondAndThirdChild_v_v_ForthChild(t);
155                     break;
156                     
157                 case GroovyTokenTypes.ANNOTATION_DEF: // @interface Foo...
158                 case GroovyTokenTypes.BAND: // 1 & 2
159                 case GroovyTokenTypes.BOR: // 1 | 2
160                 case GroovyTokenTypes.BSR: // 1 >>> 2
161                 case GroovyTokenTypes.BXOR: // 1 ^ 2
162                 case GroovyTokenTypes.CLASS_DEF: // class Foo...
163                 case GroovyTokenTypes.CTOR_IDENT: // private Foo() {...
164                 case GroovyTokenTypes.DIV: //  3/4
165                 case GroovyTokenTypes.DOT: // foo.bar
166                 case GroovyTokenTypes.ENUM_DEF: // enum Foo...
167                 case GroovyTokenTypes.GE: // a >= b
168                 case GroovyTokenTypes.GT: // a > b
169                 case GroovyTokenTypes.INTERFACE_DEF: // interface Foo...
170                 case GroovyTokenTypes.LABELED_ARG: // myMethod(name:"Jez")
171                 case GroovyTokenTypes.LABELED_STAT: // foo:x=1                        
172                 case GroovyTokenTypes.LAND: // true && false
173                 case GroovyTokenTypes.LE: // a <= b
174                 case GroovyTokenTypes.LITERAL_as: // foo as Bar
175                 case GroovyTokenTypes.LITERAL_in: // if (i in myList) ...
176                 case GroovyTokenTypes.LOR: // true && false
177                 case GroovyTokenTypes.LT: // a < b
178                 case GroovyTokenTypes.MEMBER_POINTER: // this.&foo()
179                 case GroovyTokenTypes.MOD: //  4 % 3
180                 case GroovyTokenTypes.MINUS: // 1 - 1
181                 case GroovyTokenTypes.OPTIONAL_DOT: // foo?.bar
182                 case GroovyTokenTypes.PACKAGE_DEF:
183                 case GroovyTokenTypes.PLUS: // 1 + 1
184                 case GroovyTokenTypes.RANGE_EXCLUSIVE: // [1..<10]
185                 case GroovyTokenTypes.RANGE_INCLUSIVE: // [1..10]
186                 case GroovyTokenTypes.SL: // a << b
187                 case GroovyTokenTypes.SPREAD_DOT: // foo*.bar
188                 case GroovyTokenTypes.SR: // a >> b
189                 case GroovyTokenTypes.STAR: // a * b   or    import foo.*
190                 case GroovyTokenTypes.STAR_STAR: // x ** 3
191                     accept_FirstChild_v_RestOfTheChildren(t);
192                     break;
193 
194                 case GroovyTokenTypes.CTOR_CALL:
195                 case GroovyTokenTypes.METHOD_CALL:
196                     if (t.getNumberOfChildren() == 2 && t.childAt(1) != null && t.childAt(1).getType() == GroovyTokenTypes.CLOSABLE_BLOCK) {
197                         // myMethod {...
198                         accept_FirstChild_v_SecondChild(t);
199                     } else {
200                         GroovySourceAST lastChild = t.childAt(t.getNumberOfChildren() -1);
201                         if (lastChild != null && lastChild.getType() == GroovyTokenTypes.CLOSABLE_BLOCK) {
202                             // myMethod(a,b) {...
203                             accept_FirstChild_v_RestOfTheChildren_v_LastChild(t);
204                         } else {
205                             // myMethod(a,b)
206                             accept_FirstChild_v_RestOfTheChildren_v(t);
207                         }
208                     }
209                     break;
210 
211                 case GroovyTokenTypes.LITERAL_while:
212 //deprecated                case GroovyTokenTypes.LITERAL_with:
213                 case GroovyTokenTypes.TYPECAST: // (String)itr.next()
214                     accept_v_FirstChildsFirstChild_v_RestOfTheChildren(t);
215                     break;
216 
217                 case GroovyTokenTypes.LITERAL_if: // if (grandchild) {child1} else {child2} ...
218                     accept_v_FirstChildsFirstChild_v_Child2_Child3_v_Child4_v___v_LastChild(t);
219                     break;
220 
221                 case GroovyTokenTypes.CLOSABLE_BLOCK: // [1,2,3].each {foo(it)}  <-- Closure
222                     if (t.childAt(0) != null && t.childAt(0).getType() == GroovyTokenTypes.IMPLICIT_PARAMETERS) {
223                         accept_v_AllChildren_v(t);
224                     } else {
225                         accept_v_FirstChild_v_RestOfTheChildren_v(t);
226                     }
227                     break;
228 
229                 case GroovyTokenTypes.FOR_IN_ITERABLE:
230                 case GroovyTokenTypes.LITERAL_for:
231                 case GroovyTokenTypes.LITERAL_new:
232                 case GroovyTokenTypes.LITERAL_switch:
233                     accept_v_FirstChild_v_RestOfTheChildren_v(t);
234                     break;
235  
236                 case GroovyTokenTypes.ANNOTATIONS: // just like modifiers but for package/enum declarations
237                 case GroovyTokenTypes.LITERAL_assert:
238                 case GroovyTokenTypes.LITERAL_catch:
239                 case GroovyTokenTypes.LITERAL_synchronized:
240                 case GroovyTokenTypes.LITERAL_try:
241                 case GroovyTokenTypes.MODIFIERS:
242                     accept_v_FirstChild_v_RestOfTheChildren(t);
243                     break;
244                     
245                 case GroovyTokenTypes.WILDCARD_TYPE:
246                     accept_v_Siblings_v(t);
247                     break;
248 
249                 default:
250                     accept_v_FirstChild_v(t);
251                     break;
252             }
253             pop();
254         }
255     }
256 }