KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > core > ext > typeinfo > JAbstractMethod


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.core.ext.typeinfo;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  * Common superclass for {@link JMethod} and {@link JConstructor}.
24  */

25 public abstract class JAbstractMethod implements HasMetaData {
26
27   private int bodyEnd;
28
29   private int bodyStart;
30
31   private final int declEnd;
32
33   private final int declStart;
34
35   private final HasMetaData metaData = new MetaData();
36
37   private int modifierBits;
38
39   private final String JavaDoc name;
40
41   private final List JavaDoc params = new ArrayList JavaDoc();
42
43   private final List JavaDoc thrownTypes = new ArrayList JavaDoc();
44
45   // Only the builder can construct
46
JAbstractMethod(String JavaDoc name, int declStart, int declEnd, int bodyStart,
47       int bodyEnd) {
48     this.name = name;
49     this.declStart = declStart;
50     this.declEnd = declEnd;
51     this.bodyStart = bodyStart;
52     this.bodyEnd = bodyEnd;
53   }
54
55   public void addMetaData(String JavaDoc tagName, String JavaDoc[] values) {
56     metaData.addMetaData(tagName, values);
57   }
58
59   public void addModifierBits(int bits) {
60     modifierBits |= bits;
61   }
62
63   public void addThrows(JType type) {
64     thrownTypes.add(type);
65   }
66
67   public JParameter findParameter(String JavaDoc name) {
68     for (Iterator JavaDoc iter = params.iterator(); iter.hasNext();) {
69       JParameter param = (JParameter) iter.next();
70       if (param.getName().equals(name)) {
71         return param;
72       }
73     }
74     return null;
75   }
76
77   public int getBodyEnd() {
78     return bodyEnd;
79   }
80
81   public int getBodyStart() {
82     return bodyStart;
83   }
84
85   public int getDeclEnd() {
86     return declEnd;
87   }
88
89   public int getDeclStart() {
90     return declStart;
91   }
92
93   /**
94    * Gets the type in which this method or constructor was declared.
95    */

96   public abstract JClassType getEnclosingType();
97
98   public String JavaDoc[][] getMetaData(String JavaDoc tagName) {
99     return metaData.getMetaData(tagName);
100   }
101
102   public String JavaDoc[] getMetaDataTags() {
103     return metaData.getMetaDataTags();
104   }
105
106   public String JavaDoc getName() {
107     return name;
108   }
109
110   public JParameter[] getParameters() {
111     return (JParameter[]) params.toArray(TypeOracle.NO_JPARAMS);
112   }
113
114   public abstract String JavaDoc getReadableDeclaration();
115
116   public JType[] getThrows() {
117     return (JType[]) thrownTypes.toArray(TypeOracle.NO_JTYPES);
118   }
119
120   public abstract JConstructor isConstructor();
121
122   public boolean isDefaultAccess() {
123     return 0 == (modifierBits & (TypeOracle.MOD_PUBLIC | TypeOracle.MOD_PRIVATE | TypeOracle.MOD_PROTECTED));
124   }
125   public abstract JMethod isMethod();
126   public boolean isPrivate() {
127     return 0 != (modifierBits & TypeOracle.MOD_PRIVATE);
128   }
129   public boolean isProtected() {
130     return 0 != (modifierBits & TypeOracle.MOD_PROTECTED);
131   }
132   public boolean isPublic() {
133     return 0 != (modifierBits & TypeOracle.MOD_PUBLIC);
134   }
135   protected int getModifierBits() {
136     return modifierBits;
137   }
138   protected void toStringParamsAndThrows(StringBuffer JavaDoc sb) {
139     sb.append("(");
140     boolean needComma = false;
141     for (Iterator JavaDoc iter = params.iterator(); iter.hasNext();) {
142       JParameter param = (JParameter) iter.next();
143       if (needComma) {
144         sb.append(", ");
145       } else {
146         needComma = true;
147       }
148       sb.append(param.getType().getParameterizedQualifiedSourceName());
149       sb.append(" ");
150       sb.append(param.getName());
151     }
152     sb.append(")");
153
154     if (!thrownTypes.isEmpty()) {
155       sb.append(" throws ");
156       needComma = false;
157       for (Iterator JavaDoc iter = thrownTypes.iterator(); iter.hasNext();) {
158         JClassType thrownType = (JClassType) iter.next();
159         if (needComma) {
160           sb.append(", ");
161         } else {
162           needComma = true;
163         }
164         sb.append(thrownType.getParameterizedQualifiedSourceName());
165       }
166     }
167   }
168   void addParameter(JParameter param) {
169     params.add(param);
170   }
171   boolean hasParamTypes(JType[] paramTypes) {
172     if (params.size() != paramTypes.length) {
173       return false;
174     }
175
176     for (int i = 0; i < paramTypes.length; i++) {
177       JParameter candidate = (JParameter) params.get(i);
178       // Identity tests are ok since identity is durable within an oracle.
179
//
180
if (candidate.getType() != paramTypes[i]) {
181         return false;
182       }
183     }
184     return true;
185   }
186 }
187
Popular Tags