KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > generic > GenericObjectType


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2004, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.ba.generic;
21
22 import java.util.List JavaDoc;
23
24 import org.apache.bcel.generic.ObjectType;
25 import org.apache.bcel.generic.Type;
26
27 import edu.umd.cs.findbugs.annotations.NonNull;
28
29 /**
30  * Extension to ObjectType that includes additional information
31  * about the generic signature. <p>
32  *
33  * A GenericObjectType is either a parameterized type e.g.
34  * <code>List&lt;String&gt;</code>, or a type variable e.g.
35  * <code>T</code>. <p>
36  *
37  * This class cannot be initialized directly. Instead, create a GenericObjectType
38  * by calling GenericUtilities.getType(String) and passing in the bytecode
39  * signature for the type.
40  *
41  * @author Nat Ayewah
42  */

43 public class GenericObjectType extends ObjectType {
44
45     final List JavaDoc<Type> parameters;
46     
47     final String JavaDoc variable;
48     
49     final Type extension;
50
51     public Type getUpperBound() {
52         if (variable.equals("+")) return extension;
53         return this;
54     }
55     
56     /**
57      * @return Returns the extension.
58      */

59     public Type getExtension() {
60         return extension;
61     }
62
63     /**
64      * @return Returns the variable.
65      */

66     public String JavaDoc getVariable() {
67         return variable;
68     }
69
70     /**
71      * Get the TypeCategory that represents this Object
72      * @see GenericUtilities.TypeCategory
73      */

74     public GenericUtilities.TypeCategory getTypeCategory() {
75         if (hasParameters() && variable == null && extension == null) {
76             return GenericUtilities.TypeCategory.PARAMETERS;
77             
78         } else if(!hasParameters() && variable != null && extension == null){
79             if (variable.equals("*")) return GenericUtilities.TypeCategory.WILDCARD;
80             else return GenericUtilities.TypeCategory.TYPE_VARIABLE;
81             
82         } else if(!hasParameters() && variable != null && extension != null){
83             if (variable.equals("+")) return GenericUtilities.TypeCategory.WILDCARD_EXTENDS;
84             else if (variable.equals("-")) return GenericUtilities.TypeCategory.WILDCARD_SUPER;
85             
86         }
87         // this should never happen
88
throw new IllegalStateException JavaDoc("The Generic Object Type is badly initialized");
89     }
90     
91     /**
92      * @return true if this GenericObjectType represents a parameterized type e.g.
93      * <code>List&lt;String&gt;</code>. This implies that isVariable() is falses
94      */

95     public boolean hasParameters() {
96         return parameters != null && parameters.size() > 0;
97     }
98     
99     /**
100      * @return the number of parameters if this is a parameterized class, 0 otherwise
101      */

102     public int getNumParameters() {
103         return parameters != null ? parameters.size() : 0;
104     }
105     
106     /**
107      * @param index should be less than getNumParameters()
108      * @return the type parameter at index
109      */

110     public Type getParameterAt(int index) {
111         if (index < getNumParameters())
112             return parameters.get(index);
113         else
114             throw new IndexOutOfBoundsException JavaDoc("The index " + index + " is too large");
115     }
116     
117     // Package Level constructors
118

119     /**
120      * Create a GenericObjectType that represents a Simple Type Variable
121      * or a simple wildcard with no extensions
122      * @param variable the type variable e.g. <code>T</code>
123      */

124     GenericObjectType(@NonNull String JavaDoc variable) {
125         this(variable, (Type) null);
126     }
127     
128     /**
129      * Create a GenericObjectType that represents a Wildcard
130      * with extensions
131      * @param variable the type variable e.g. <code>T</code>
132      */

133     GenericObjectType(@NonNull String JavaDoc wildcard, Type extension) {
134         super(Type.OBJECT.getClassName());
135         this.variable = wildcard;
136         this.extension = extension;
137         parameters = null;
138     }
139     
140     /**
141      * Create a GenericObjectType that represents a parameterized class
142      * @param class_name the class that is parameterized. e.g. <code>java.util.List</code>
143      * @param parameters the parameters of this class, must be at least 1 parameter
144      */

145     GenericObjectType(String JavaDoc class_name, List JavaDoc<Type> parameters) {
146         super(class_name);
147         variable = null;
148         extension = null;
149         if (parameters == null || parameters.size() == 0)
150             throw new IllegalStateException JavaDoc("argument 'parameters' must contain at least 1 parameter");
151         this.parameters = parameters;
152     }
153     
154     /**
155      * @return the underlying ObjectType for this Generic Object
156      */

157     public ObjectType getObjectType() {
158         return (ObjectType) Type.getType(getSignature());
159     }
160         
161     /**
162      * Return a string representation of this object.
163      * (I do not override <code>toString()</code> in case
164      * any existing code assumes that this object is an
165      * ObjectType and expects similar string representation.
166      * i.e. <code>toString()</code> is equivalent to
167      * <code>toString(false)</code>)
168      *
169      * @param includeGenerics if true then the string includes generic information
170      * in this object. Otherwise this returns the same value as ObjectType.toString()
171      */

172     public String JavaDoc toString(boolean includeGenerics) {
173         if (!includeGenerics) return super.toString();
174         
175         return getTypeCategory().asString(this);
176     }
177         
178 }
179
Popular Tags