KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > Member


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 import org.eclipse.jdt.core.*;
17 import org.eclipse.jdt.core.compiler.CharOperation;
18 import org.eclipse.jdt.core.compiler.IScanner;
19 import org.eclipse.jdt.core.compiler.ITerminalSymbols;
20 import org.eclipse.jdt.core.compiler.InvalidInputException;
21 import org.eclipse.jdt.internal.compiler.impl.Constant;
22 import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
23 import org.eclipse.jdt.internal.core.util.MementoTokenizer;
24
25 /**
26  * @see IMember
27  */

28
29 public abstract class Member extends SourceRefElement implements IMember {
30     
31 protected Member(JavaElement parent) {
32     super(parent);
33 }
34 protected static boolean areSimilarMethods(
35     String JavaDoc name1, String JavaDoc[] params1,
36     String JavaDoc name2, String JavaDoc[] params2,
37     String JavaDoc[] simpleNames1) {
38         
39     if (name1.equals(name2)) {
40         int params1Length = params1.length;
41         if (params1Length == params2.length) {
42             for (int i = 0; i < params1Length; i++) {
43                 String JavaDoc simpleName1 =
44                     simpleNames1 == null ?
45                         Signature.getSimpleName(Signature.toString(Signature.getTypeErasure(params1[i]))) :
46                         simpleNames1[i];
47                 String JavaDoc simpleName2 = Signature.getSimpleName(Signature.toString(Signature.getTypeErasure(params2[i])));
48                 if (!simpleName1.equals(simpleName2)) {
49                     return false;
50                 }
51             }
52             return true;
53         }
54     }
55     return false;
56 }
57 /**
58  * Converts a field constant from the compiler's representation
59  * to the Java Model constant representation (Number or String).
60  */

61 protected static Object JavaDoc convertConstant(Constant constant) {
62     if (constant == null)
63         return null;
64     if (constant == Constant.NotAConstant) {
65         return null;
66     }
67     switch (constant.typeID()) {
68         case TypeIds.T_boolean :
69             return constant.booleanValue() ? Boolean.TRUE : Boolean.FALSE;
70         case TypeIds.T_byte :
71             return new Byte JavaDoc(constant.byteValue());
72         case TypeIds.T_char :
73             return new Character JavaDoc(constant.charValue());
74         case TypeIds.T_double :
75             return new Double JavaDoc(constant.doubleValue());
76         case TypeIds.T_float :
77             return new Float JavaDoc(constant.floatValue());
78         case TypeIds.T_int :
79             return new Integer JavaDoc(constant.intValue());
80         case TypeIds.T_long :
81             return new Long JavaDoc(constant.longValue());
82         case TypeIds.T_short :
83             return new Short JavaDoc(constant.shortValue());
84         case TypeIds.T_JavaLangString :
85             return constant.stringValue();
86         default :
87             return null;
88     }
89 }
90 /*
91  * Helper method for SourceType.findMethods and BinaryType.findMethods
92  */

93 public static IMethod[] findMethods(IMethod method, IMethod[] methods) {
94     String JavaDoc elementName = method.getElementName();
95     String JavaDoc[] parameters = method.getParameterTypes();
96     int paramLength = parameters.length;
97     String JavaDoc[] simpleNames = new String JavaDoc[paramLength];
98     for (int i = 0; i < paramLength; i++) {
99         String JavaDoc erasure = Signature.getTypeErasure(parameters[i]);
100         simpleNames[i] = Signature.getSimpleName(Signature.toString(erasure));
101     }
102     ArrayList JavaDoc list = new ArrayList JavaDoc();
103     for (int i = 0, length = methods.length; i < length; i++) {
104         IMethod existingMethod = methods[i];
105         if (areSimilarMethods(
106                 elementName,
107                 parameters,
108                 existingMethod.getElementName(),
109                 existingMethod.getParameterTypes(),
110                 simpleNames)) {
111             list.add(existingMethod);
112         }
113     }
114     int size = list.size();
115     if (size == 0) {
116         return null;
117     } else {
118         IMethod[] result = new IMethod[size];
119         list.toArray(result);
120         return result;
121     }
122 }
123 public String JavaDoc[] getCategories() throws JavaModelException {
124     IType type = (IType) getAncestor(IJavaElement.TYPE);
125     if (type == null) return CharOperation.NO_STRINGS;
126     if (type.isBinary()) {
127         return CharOperation.NO_STRINGS;
128     } else {
129         SourceTypeElementInfo info = (SourceTypeElementInfo) ((SourceType) type).getElementInfo();
130         HashMap JavaDoc map = info.getCategories();
131         if (map == null) return CharOperation.NO_STRINGS;
132         String JavaDoc[] categories = (String JavaDoc[]) map.get(this);
133         if (categories == null) return CharOperation.NO_STRINGS;
134         return categories;
135     }
136 }
137 /**
138  * @see IMember
139  */

140 public IClassFile getClassFile() {
141     IJavaElement element = getParent();
142     while (element instanceof IMember) {
143         element= element.getParent();
144     }
145     if (element instanceof IClassFile) {
146         return (IClassFile) element;
147     }
148     return null;
149 }
150 /**
151  * @see IMember
152  */

153 public IType getDeclaringType() {
154     JavaElement parentElement = (JavaElement)getParent();
155     if (parentElement.getElementType() == TYPE) {
156         return (IType) parentElement;
157     }
158     return null;
159 }
160 /**
161  * @see IMember
162  */

163 public int getFlags() throws JavaModelException {
164     MemberElementInfo info = (MemberElementInfo) getElementInfo();
165     return info.getModifiers();
166 }
167 /*
168  * @see JavaElement
169  */

170 public IJavaElement getHandleFromMemento(String JavaDoc token, MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) {
171     switch (token.charAt(0)) {
172         case JEM_COUNT:
173             return getHandleUpdatingCountFromMemento(memento, workingCopyOwner);
174         case JEM_TYPE:
175             String JavaDoc typeName;
176             if (memento.hasMoreTokens()) {
177                 typeName = memento.nextToken();
178                 char firstChar = typeName.charAt(0);
179                 if (firstChar == JEM_FIELD || firstChar == JEM_INITIALIZER || firstChar == JEM_METHOD || firstChar == JEM_TYPE || firstChar == JEM_COUNT) {
180                     token = typeName;
181                     typeName = ""; //$NON-NLS-1$
182
} else {
183                     token = null;
184                 }
185             } else {
186                 typeName = ""; //$NON-NLS-1$
187
token = null;
188             }
189             JavaElement type = (JavaElement)getType(typeName, 1);
190             if (token == null) {
191                 return type.getHandleFromMemento(memento, workingCopyOwner);
192             } else {
193                 return type.getHandleFromMemento(token, memento, workingCopyOwner);
194             }
195         case JEM_LOCALVARIABLE:
196             if (!memento.hasMoreTokens()) return this;
197             String JavaDoc varName = memento.nextToken();
198             if (!memento.hasMoreTokens()) return this;
199             memento.nextToken(); // JEM_COUNT
200
if (!memento.hasMoreTokens()) return this;
201             int declarationStart = Integer.parseInt(memento.nextToken());
202             if (!memento.hasMoreTokens()) return this;
203             memento.nextToken(); // JEM_COUNT
204
if (!memento.hasMoreTokens()) return this;
205             int declarationEnd = Integer.parseInt(memento.nextToken());
206             if (!memento.hasMoreTokens()) return this;
207             memento.nextToken(); // JEM_COUNT
208
if (!memento.hasMoreTokens()) return this;
209             int nameStart = Integer.parseInt(memento.nextToken());
210             if (!memento.hasMoreTokens()) return this;
211             memento.nextToken(); // JEM_COUNT
212
if (!memento.hasMoreTokens()) return this;
213             int nameEnd = Integer.parseInt(memento.nextToken());
214             if (!memento.hasMoreTokens()) return this;
215             memento.nextToken(); // JEM_COUNT
216
if (!memento.hasMoreTokens()) return this;
217             String JavaDoc typeSignature = memento.nextToken();
218             return new LocalVariable(this, varName, declarationStart, declarationEnd, nameStart, nameEnd, typeSignature);
219         case JEM_TYPE_PARAMETER:
220             if (!memento.hasMoreTokens()) return this;
221             String JavaDoc typeParameterName = memento.nextToken();
222             JavaElement typeParameter = new TypeParameter(this, typeParameterName);
223             return typeParameter.getHandleFromMemento(memento, workingCopyOwner);
224     }
225     return null;
226 }
227 /**
228  * @see JavaElement#getHandleMemento()
229  */

230 protected char getHandleMementoDelimiter() {
231     return JavaElement.JEM_TYPE;
232 }
233 /*
234  * Returns the outermost context defining a local element. Per construction, it can only be a
235  * method/field/initializarer member; thus, returns null if this member is already a top-level type or member type.
236  * e.g for X.java/X/Y/foo()/Z/bar()/T, it will return X.java/X/Y/foo()
237  */

238 public Member getOuterMostLocalContext() {
239     IJavaElement current = this;
240     Member lastLocalContext = null;
241     parentLoop: while (true) {
242         switch (current.getElementType()) {
243             case CLASS_FILE:
244             case COMPILATION_UNIT:
245                 break parentLoop; // done recursing
246
case TYPE:
247                 // cannot be a local context
248
break;
249             case INITIALIZER:
250             case FIELD:
251             case METHOD:
252                  // these elements can define local members
253
lastLocalContext = (Member) current;
254                 break;
255         }
256         current = current.getParent();
257     }
258     return lastLocalContext;
259 }
260 public ISourceRange getJavadocRange() throws JavaModelException {
261     ISourceRange range= this.getSourceRange();
262     if (range == null) return null;
263     IBuffer buf= null;
264     if (this.isBinary()) {
265         buf = this.getClassFile().getBuffer();
266     } else {
267         ICompilationUnit compilationUnit = this.getCompilationUnit();
268         if (!compilationUnit.isConsistent()) {
269             return null;
270         }
271         buf = compilationUnit.getBuffer();
272     }
273     final int start= range.getOffset();
274     final int length= range.getLength();
275     if (length > 0 && buf.getChar(start) == '/') {
276         IScanner scanner= ToolFactory.createScanner(true, false, false, false);
277         scanner.setSource(buf.getText(start, length).toCharArray());
278         try {
279             int docOffset= -1;
280             int docEnd= -1;
281             
282             int terminal= scanner.getNextToken();
283             loop: while (true) {
284                 switch(terminal) {
285                     case ITerminalSymbols.TokenNameCOMMENT_JAVADOC :
286                         docOffset= scanner.getCurrentTokenStartPosition();
287                         docEnd= scanner.getCurrentTokenEndPosition() + 1;
288                         terminal= scanner.getNextToken();
289                         break;
290                     case ITerminalSymbols.TokenNameCOMMENT_LINE :
291                     case ITerminalSymbols.TokenNameCOMMENT_BLOCK :
292                         terminal= scanner.getNextToken();
293                         continue loop;
294                     default :
295                         break loop;
296                 }
297             }
298             if (docOffset != -1) {
299                 return new SourceRange(docOffset + start, docEnd - docOffset + 1);
300             }
301         } catch (InvalidInputException ex) {
302             // try if there is inherited Javadoc
303
}
304     }
305     return null;
306 }
307 /**
308  * @see IMember
309  */

310 public ISourceRange getNameRange() throws JavaModelException {
311     MemberElementInfo info= (MemberElementInfo)getElementInfo();
312     return new SourceRange(info.getNameSourceStart(), info.getNameSourceEnd() - info.getNameSourceStart() + 1);
313 }
314 /**
315  * @see IMember
316  */

317 public IType getType(String JavaDoc typeName, int count) {
318     if (isBinary()) {
319         throw new IllegalArgumentException JavaDoc("Not a source member " + toStringWithAncestors()); //$NON-NLS-1$
320
} else {
321         SourceType type = new SourceType(this, typeName);
322         type.occurrenceCount = count;
323         return type;
324     }
325 }
326 /**
327  * @see IMember#getTypeRoot()
328  */

329 public ITypeRoot getTypeRoot() {
330     IJavaElement element = getParent();
331     while (element instanceof IMember) {
332         element= element.getParent();
333     }
334     return (ITypeRoot) element;
335 }
336 /**
337  * @see IMember
338  */

339 public boolean isBinary() {
340     return false;
341 }
342 protected boolean isMainMethod(IMethod method) throws JavaModelException {
343     if ("main".equals(method.getElementName()) && Signature.SIG_VOID.equals(method.getReturnType())) { //$NON-NLS-1$
344
int flags= method.getFlags();
345         if (Flags.isStatic(flags) && Flags.isPublic(flags)) {
346             String JavaDoc[] paramTypes= method.getParameterTypes();
347             if (paramTypes.length == 1) {
348                 String JavaDoc typeSignature= Signature.toString(paramTypes[0]);
349                 return "String[]".equals(Signature.getSimpleName(typeSignature)); //$NON-NLS-1$
350
}
351         }
352     }
353     return false;
354 }
355 /**
356  * @see IJavaElement
357  */

358 public boolean isReadOnly() {
359     return getClassFile() != null;
360 }
361 /**
362  */

363 public String JavaDoc readableName() {
364
365     IJavaElement declaringType = getDeclaringType();
366     if (declaringType != null) {
367         String JavaDoc declaringName = ((JavaElement) getDeclaringType()).readableName();
368         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(declaringName);
369         buffer.append('.');
370         buffer.append(this.getElementName());
371         return buffer.toString();
372     } else {
373         return super.readableName();
374     }
375 }
376 /**
377  * Updates the name range for this element.
378  */

379 protected void updateNameRange(int nameStart, int nameEnd) {
380     try {
381         MemberElementInfo info = (MemberElementInfo) getElementInfo();
382         info.setNameSourceStart(nameStart);
383         info.setNameSourceEnd(nameEnd);
384     } catch (JavaModelException npe) {
385         return;
386     }
387 }
388 }
389
Popular Tags