1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import java.util.ArrayList;
15 import net.sourceforge.phpdt.internal.core.SourceType;
17 import net.sourceforge.phpdt.core.Flags;
18 import net.sourceforge.phpdt.core.IJavaElement;
19 import net.sourceforge.phpdt.core.IMember;
20 import net.sourceforge.phpdt.core.IMethod;
21 import net.sourceforge.phpdt.core.ISourceRange;
22 import net.sourceforge.phpdt.core.IType;
23 import net.sourceforge.phpdt.core.JavaModelException;
24 import net.sourceforge.phpdt.core.Signature;
25 import net.sourceforge.phpdt.core.jdom.IDOMNode;
32 public abstract class Member extends SourceRefElement implements IMember {
33 protected Member(JavaElement parent, String name) {
36 protected boolean areSimilarMethods(
37 String name1, String[] params1,
38 String name2, String[] params2,
39 String[] simpleNames1) {
41 if (name1.equals(name2)) {
42 int params1Length = params1.length;
43 if (params1Length == params2.length) {
44 for (int i = 0; i < params1Length; i++) {
46 simpleNames1 == null ?
47 Signature.getSimpleName(Signature.toString(params1[i])) :
49 String simpleName2 = Signature.getSimpleName(Signature.toString(params2[i]));
50 if (!simpleName1.equals(simpleName2)) {
60 * Converts a field constant from the compiler's representation
61 * to the Java Model constant representation (Number or String).
63 //protected static Object convertConstant(Constant constant) {
64 // if (constant == null)
66 // if (constant == Constant.NotAConstant) {
69 // switch (constant.typeID()) {
70 // case TypeIds.T_boolean :
71 // return constant.booleanValue() ? Boolean.TRUE : Boolean.FALSE;
72 // case TypeIds.T_byte :
73 // return new Byte(constant.byteValue());
74 // case TypeIds.T_char :
75 // return new Character(constant.charValue());
76 // case TypeIds.T_double :
77 // return new Double(constant.doubleValue());
78 // case TypeIds.T_float :
79 // return new Float(constant.floatValue());
80 // case TypeIds.T_int :
81 // return new Integer(constant.intValue());
82 // case TypeIds.T_long :
83 // return new Long(constant.longValue());
84 // case TypeIds.T_short :
85 // return new Short(constant.shortValue());
86 // case TypeIds.T_String :
87 // return constant.stringValue();
93 * @see JavaElement#equalsDOMNode
95 protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
96 return getElementName().equals(node.getName());
99 * Helper method for SourceType.findMethods and BinaryType.findMethods
101 protected IMethod[] findMethods(IMethod method, IMethod[] methods) {
102 String elementName = method.getElementName();
103 String[] parameters = method.getParameterTypes();
104 int paramLength = parameters.length;
105 String[] simpleNames = new String[paramLength];
106 for (int i = 0; i < paramLength; i++) {
107 simpleNames[i] = Signature.getSimpleName(Signature.toString(parameters[i]));
109 ArrayList list = new ArrayList();
110 next: for (int i = 0, length = methods.length; i < length; i++) {
111 IMethod existingMethod = methods[i];
112 if (this.areSimilarMethods(
115 existingMethod.getElementName(),
116 existingMethod.getParameterTypes(),
118 list.add(existingMethod);
121 int size = list.size();
125 IMethod[] result = new IMethod[size];
126 list.toArray(result);
133 //public IClassFile getClassFile() {
134 // return ((JavaElement)getParent()).getClassFile();
139 public IType getDeclaringType() {
140 JavaElement parentElement = (JavaElement)getParent();
141 if (parentElement.getElementType() == TYPE) {
142 return (IType) parentElement;
149 public int getFlags() throws JavaModelException {
150 MemberElementInfo info = (MemberElementInfo) getElementInfo();
151 return info.getModifiers();
154 * @see JavaElement#getHandleMemento()
156 protected char getHandleMementoDelimiter() {
157 return JavaElement.JEM_TYPE;
160 * Returns the outermost context defining a local element. Per construction, it can only be a
161 * method/field/initializarer member; thus, returns null if this member is already a top-level type or member type.
162 * e.g for X.java/X/Y/foo()/Z/bar()/T, it will return X.java/X/Y/foo()
164 public Member getOuterMostLocalContext() {
165 IJavaElement current = this;
166 Member lastLocalContext = null;
167 parentLoop: while (true) {
168 switch (current.getElementType()) {
170 case COMPILATION_UNIT:
171 break parentLoop; // done recursing
173 // cannot be a local context
178 // these elements can define local members
179 lastLocalContext = (Member) current;
182 current = current.getParent();
184 return lastLocalContext;
189 public ISourceRange getNameRange() throws JavaModelException {
190 MemberElementInfo info= (MemberElementInfo)getElementInfo();
191 return new SourceRange(info.getNameSourceStart(), info.getNameSourceEnd() - info.getNameSourceStart() + 1);
196 public IType getType(String typeName, int count) {
198 throw new IllegalArgumentException("Not a source member " + toStringWithAncestors()); //$NON-NLS-1$
200 SourceType type = new SourceType(this, typeName);
201 type.occurrenceCount = count;
208 public boolean isBinary() {
211 protected boolean isMainMethod(IMethod method) throws JavaModelException {
212 if ("main".equals(method.getElementName()) && Signature.SIG_VOID.equals(method.getReturnType())) { //$NON-NLS-1$
213 int flags= method.getFlags();
214 if (Flags.isStatic(flags) && Flags.isPublic(flags)) {
215 String[] paramTypes= method.getParameterTypes();
216 if (paramTypes.length == 1) {
217 String name= Signature.toString(paramTypes[0]);
218 return "String[]".equals(Signature.getSimpleName(name)); //$NON-NLS-1$
227 public boolean isReadOnly() {
228 return false; //getClassFile() != null;
232 public String readableName() {
234 IJavaElement declaringType = getDeclaringType();
235 if (declaringType != null) {
236 String declaringName = ((JavaElement) getDeclaringType()).readableName();
237 StringBuffer buffer = new StringBuffer(declaringName);
239 buffer.append(this.getElementName());
240 return buffer.toString();
242 return super.readableName();
246 * Updates the name range for this element.
248 protected void updateNameRange(int nameStart, int nameEnd) {
250 MemberElementInfo info = (MemberElementInfo) getElementInfo();
251 info.setNameSourceStart(nameStart);
252 info.setNameSourceEnd(nameEnd);
253 } catch (JavaModelException npe) {