View Javadoc
Minimize
Table

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.ast;
17  
18  import org.codehaus.groovy.ast.expr.*;
19  
20  
21  /**
22   * Represents a parameter on a constructor or method call. The type name is
23   * optional - it defaults to java.lang.Object if unknown.
24   * 
25   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
26   * @version $Revision: 21187 $
27   */
28  public class Parameter extends AnnotatedNode implements Variable {
29  
30      public static final Parameter[] EMPTY_ARRAY = {
31      };
32  
33      private ClassNode type;
34      private final String name;
35      private boolean dynamicTyped;
36      private Expression defaultValue;
37      private boolean hasDefaultValue;
38      private boolean inStaticContext;
39      private boolean closureShare=false;
40      private int modifiers;
41      private ClassNode originType=ClassHelper.DYNAMIC_TYPE;
42  
43      public Parameter(ClassNode type, String name) {
44          this.name = name;
45          this.setType(type);
46          this.originType = type;
47          this.hasDefaultValue = false;
48      }
49      
50      public Parameter(ClassNode type, String name, Expression defaultValue) {
51          this(type,name);
52          this.defaultValue = defaultValue;
53          this.hasDefaultValue = defaultValue != null;
54      }
55  
56      public String toString() {
57          return super.toString() + "[name:" + name + ((type == null) ? "" : " type: " + type.getName()) + ", hasDefaultValue: " + this.hasInitialExpression() + "]";
58      }
59  
60      public String getName() {
61          return name;
62      }
63  
64      public ClassNode getType() {
65          return type;
66      }
67  
68      public void setType(ClassNode type) {
69          this.type = type;
70          dynamicTyped |= type==ClassHelper.DYNAMIC_TYPE;
71      }
72      
73      public boolean hasInitialExpression() {
74          return this.hasDefaultValue;
75      }
76      
77      /**
78       * @return the default value expression for this parameter or null if
79       * no default value is specified
80       */
81      public Expression getInitialExpression() {
82          return defaultValue;
83      }
84      
85      public void setInitialExpression(Expression init) {
86          defaultValue = init;
87          hasDefaultValue = defaultValue != null;
88      }
89      
90      public boolean isInStaticContext() {
91          return inStaticContext;
92      }
93      
94      public void setInStaticContext(boolean inStaticContext) {
95          this.inStaticContext = inStaticContext;
96      }
97  
98      public boolean isDynamicTyped() {
99          return dynamicTyped;
100     }
101 
102     public boolean isClosureSharedVariable() {
103         return closureShare;
104     }
105 
106     public void setClosureSharedVariable(boolean inClosure) {
107         closureShare = inClosure;        
108     }
109 
110     public int getModifiers() {
111         return modifiers;
112     }
113 
114     public ClassNode getOriginType() {
115         return originType;
116     }
117     
118     public void setOriginType(ClassNode cn) {
119         originType = cn;
120     }
121 
122     public void setModifiers(int modifiers) {
123         this.modifiers = modifiers;
124     }
125 }