View Javadoc
Minimize
Table

1   /*
2    * Copyright 2003-2011 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  
17  package org.codehaus.groovy.ast;
18  
19  /**
20   * This class is used to describe generic type signatures for ClassNodes.
21   *
22   * @author Jochen Theodorou
23   * @see ClassNode
24   */
25  public class GenericsType extends ASTNode {
26      private final ClassNode[] upperBounds;
27      private final ClassNode lowerBound;
28      private ClassNode type;
29      private String name;
30      private boolean placeholder;
31      private boolean resolved;
32      private boolean wildcard;
33  
34      public GenericsType(ClassNode type, ClassNode[] upperBounds, ClassNode lowerBound) {
35          this.type = type;
36          this.name = type.isGenericsPlaceHolder() ? type.getUnresolvedName() : type.getName();
37          this.upperBounds = upperBounds;
38          this.lowerBound = lowerBound;
39          placeholder = type.isGenericsPlaceHolder();
40          resolved = false;
41      }
42  
43      public GenericsType(ClassNode basicType) {
44          this(basicType, null, null);
45      }
46  
47      public ClassNode getType() {
48          return type;
49      }
50  
51      public void setType(ClassNode type) {
52          this.type = type;
53      }
54  
55      public String toString() {
56          String ret = (type == null || placeholder || wildcard) ? name : genericsBounds(type);
57          if (upperBounds != null) {
58              ret += " extends ";
59              for (int i = 0; i < upperBounds.length; i++) {
60                  ret += genericsBounds(upperBounds[i]);
61                  if (i + 1 < upperBounds.length) ret += " & ";
62              }
63          } else if (lowerBound != null) {
64              ret += " super " + genericsBounds(lowerBound);
65          }
66          return ret;
67      }
68  
69      private String genericsBounds(ClassNode theType) {
70          String ret = theType.getName();
71          GenericsType[] genericsTypes = theType.getGenericsTypes();
72          if (genericsTypes == null || genericsTypes.length == 0) return ret;
73          ret += "<";
74          for (int i = 0; i < genericsTypes.length; i++) {
75              if (i != 0) ret += ", ";
76              ret += genericsTypes[i].toString();
77          }
78          ret += ">";
79          return ret;
80      }
81  
82      public ClassNode[] getUpperBounds() {
83          return upperBounds;
84      }
85  
86      public String getName() {
87          return name;
88      }
89  
90      public boolean isPlaceholder() {
91          return placeholder;
92      }
93  
94      public void setPlaceholder(boolean placeholder) {
95          this.placeholder = placeholder;
96          type.setGenericsPlaceHolder(placeholder);
97      }
98  
99      public boolean isResolved() {
100         return resolved || placeholder;
101     }
102 
103     public void setResolved(boolean res) {
104         resolved = res;
105     }
106 
107     public void setName(String name) {
108         this.name = name;
109     }
110 
111     public boolean isWildcard() {
112         return wildcard;
113     }
114 
115     public void setWildcard(boolean wildcard) {
116         this.wildcard = wildcard;
117     }
118 
119     public ClassNode getLowerBound() {
120         return lowerBound;
121     }
122 }