View Javadoc
Minimize
Table

Bug Overview

linebug prioritybugbug descriptionexisting sinceauthor
51mediumTODODer Code ist an dieser Stelle eventuell unfertig oder fehlerhaft. Bitte überprüfen!18.06.2012-10:31Unbekannt

1   /*
2    * Copyright 2003-2009 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.syntax;
17  
18  import org.codehaus.groovy.ast.ClassNode;
19  import org.codehaus.groovy.ast.ModuleNode;
20  import org.codehaus.groovy.ast.AnnotationNode;
21  import org.codehaus.groovy.ast.PackageNode;
22  import org.codehaus.groovy.control.SourceUnit;
23  
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  /**
30   * A common base class of AST helper methods which can be shared across the classic and new parsers
31   *
32   * @author Jochen Theodorou
33   * @author James Strachan
34   * @author Bob McWhirter
35   * @author Sam Pullara
36   * @author Chris Poirier
37   */
38  public class ASTHelper {
39  
40      /** The SourceUnit controlling us */
41      private SourceUnit controller;
42  
43      /** Our ClassLoader, which provides information on external types */
44      private ClassLoader classLoader;
45  
46      protected ModuleNode output;
47  
48      /** The package name in which the module sits */
49      private String packageName;
50  
51 bug overview        // TODO should this really be static??? 
52      protected static Map resolutions = new HashMap();  // cleared on build(), to be safe
53  
54      public ASTHelper(SourceUnit controller, ClassLoader classLoader) {
55          this();
56          this.controller = controller;
57          this.classLoader = classLoader;
58      }
59  
60      public ASTHelper() {
61      }
62  
63      public String getPackageName() {
64          return packageName;
65      }
66  
67      public void setPackageName(String packageName) {
68          setPackage(packageName, new ArrayList<AnnotationNode>());
69      }
70  
71      public PackageNode setPackage(String packageName, List<AnnotationNode> annotations) {
72          this.packageName = packageName;
73          if (packageName != null && packageName.length() > 0) {
74              packageName += '.';
75          }
76          PackageNode packageNode = new PackageNode(packageName);
77          packageNode.addAnnotations(annotations);
78          output.setPackage(packageNode);
79          return packageNode;
80      }
81  
82      /**
83       * Returns our class loader (as supplied on construction).
84       */
85      public ClassLoader getClassLoader() {
86          return classLoader;
87      }
88  
89      public void setClassLoader(ClassLoader classLoader) {
90          this.classLoader = classLoader;
91      }
92  
93      public SourceUnit getController() {
94          return controller;
95      }
96  
97      public void setController(SourceUnit controller) {
98          this.controller = controller;
99      }
100     
101     /**
102      * @return Two names joined by a dot. If the base name is
103      * empty, returns the name unchanged.
104      * @param base typically a package
105      * @param name typically a simple unqualified class name
106      */
107     public static String dot(String base, String name) {
108         if (base != null && base.length() > 0) {
109             return base + "." + name;
110         }
111         return name;
112     }
113 
114     protected void makeModule() {
115         this.output = new ModuleNode(controller);
116         resolutions.clear();
117     }
118 
119     /**
120      * A synonym for <code>dot( base, "" )</code>.
121      */
122     protected String dot(String base) {
123         return dot(base, "");
124     }
125 
126     protected void addImport(ClassNode type, String name, String aliasName) {
127         addImport(type, name, aliasName, new ArrayList<AnnotationNode>());
128     }
129 
130     protected void addImport(ClassNode type, String name, String aliasName, List<AnnotationNode> annotations) {
131         if (aliasName == null) aliasName=name;
132         output.addImport(aliasName, type, annotations);
133     }
134 
135     protected void addStaticImport(ClassNode type, String name, String alias) {
136         addStaticImport(type, name, alias, new ArrayList<AnnotationNode>());
137     }
138 
139     protected void addStaticImport(ClassNode type, String name, String alias, List<AnnotationNode> annotations) {
140         if (alias == null) alias = name;
141         output.addStaticImport(type, name, alias, annotations);
142     }
143 
144     protected void addStaticStarImport(ClassNode type, String importClass) {
145         addStaticStarImport(type, importClass, new ArrayList<AnnotationNode>());
146     }
147 
148     protected void addStaticStarImport(ClassNode type, String importClass, List<AnnotationNode> annotations) {
149         output.addStaticStarImport(importClass, type, annotations);
150     }
151 
152     protected void addStarImport(String importPackage) {
153         addStarImport(importPackage, new ArrayList<AnnotationNode>());
154     }
155 
156     protected void addStarImport(String importPackage, List<AnnotationNode> annotations) {
157         output.addStarImport( dot(importPackage), annotations );
158     }
159 }