3m9 compatible;
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / Member.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import java.util.ArrayList;
14
15 import net.sourceforge.phpdt.internal.core.JavaElement;
16
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;
26
27 import com.sun.corba.se.internal.core.Constant;
28
29
30 /**
31  * @see IMember
32  */
33
34 /* package */ abstract class Member extends SourceRefElement implements IMember {
35         protected Member(JavaElement parent, String name) {
36                 super(parent, name);
37         }
38 protected boolean areSimilarMethods(
39         String name1, String[] params1, 
40         String name2, String[] params2,
41         String[] simpleNames1) {
42                 
43         if (name1.equals(name2)) {
44                 int params1Length = params1.length;
45                 if (params1Length == params2.length) {
46                         for (int i = 0; i < params1Length; i++) {
47                                 String simpleName1 = 
48                                         simpleNames1 == null ? 
49                                                 Signature.getSimpleName(Signature.toString(params1[i])) :
50                                                 simpleNames1[i];
51                                 String simpleName2 = Signature.getSimpleName(Signature.toString(params2[i]));
52                                 if (!simpleName1.equals(simpleName2)) {
53                                         return false;
54                                 }
55                         }
56                         return true;
57                 }
58         }
59         return false;
60 }
61 /**
62  * Converts a field constant from the compiler's representation
63  * to the Java Model constant representation (Number or String).
64  */
65 //protected static Object convertConstant(Constant constant) {
66 //      if (constant == null)
67 //              return null;
68 //      if (constant == Constant.NotAConstant) {
69 //              return null;
70 //      }
71 //      switch (constant.typeID()) {
72 //              case TypeIds.T_boolean :
73 //                      return constant.booleanValue() ? Boolean.TRUE : Boolean.FALSE;
74 //              case TypeIds.T_byte :
75 //                      return new Byte(constant.byteValue());
76 //              case TypeIds.T_char :
77 //                      return new Character(constant.charValue());
78 //              case TypeIds.T_double :
79 //                      return new Double(constant.doubleValue());
80 //              case TypeIds.T_float :
81 //                      return new Float(constant.floatValue());
82 //              case TypeIds.T_int :
83 //                      return new Integer(constant.intValue());
84 //              case TypeIds.T_long :
85 //                      return new Long(constant.longValue());
86 //              case TypeIds.T_short :
87 //                      return new Short(constant.shortValue());
88 //              case TypeIds.T_String :
89 //                      return constant.stringValue();
90 //              default :
91 //                      return null;
92 //      }
93 //}
94 /**
95  * @see JavaElement#equalsDOMNode
96  */
97 protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
98         return getElementName().equals(node.getName());
99 }
100 /*
101  * Helper method for SourceType.findMethods and BinaryType.findMethods
102  */
103 protected IMethod[] findMethods(IMethod method, IMethod[] methods) {
104         String elementName = method.getElementName();
105         String[] parameters = method.getParameterTypes();
106         int paramLength = parameters.length;
107         String[] simpleNames = new String[paramLength];
108         for (int i = 0; i < paramLength; i++) {
109                 simpleNames[i] = Signature.getSimpleName(Signature.toString(parameters[i]));
110         }
111         ArrayList list = new ArrayList();
112         next: for (int i = 0, length = methods.length; i < length; i++) {
113                 IMethod existingMethod = methods[i];
114                 if (this.areSimilarMethods(
115                                 elementName,
116                                 parameters,
117                                 existingMethod.getElementName(),
118                                 existingMethod.getParameterTypes(),
119                                 simpleNames)) {
120                         list.add(existingMethod);
121                 }
122         }
123         int size = list.size();
124         if (size == 0) {
125                 return null;
126         } else {
127                 IMethod[] result = new IMethod[size];
128                 list.toArray(result);
129                 return result;
130         }
131 }
132 /**
133  * @see IMember
134  */
135 //public IClassFile getClassFile() {
136 //      return ((JavaElement)getParent()).getClassFile();
137 //}
138 /**
139  * @see IMember
140  */
141 public IType getDeclaringType() {
142         JavaElement parentElement = (JavaElement)getParent();
143         if (parentElement.getElementType() == TYPE) {
144                 return (IType) parentElement;
145         }
146         return null;
147 }
148 /**
149  * @see IMember
150  */
151 public int getFlags() throws JavaModelException {
152         MemberElementInfo info = (MemberElementInfo) getElementInfo();
153         return info.getModifiers();
154 }
155 /**
156  * @see JavaElement#getHandleMemento()
157  */
158 protected char getHandleMementoDelimiter() {
159         return JavaElement.JEM_TYPE;
160 }
161 /**
162  * @see IMember
163  */
164 public ISourceRange getNameRange() throws JavaModelException {
165         MemberElementInfo info= (MemberElementInfo)getElementInfo();
166         return new SourceRange(info.getNameSourceStart(), info.getNameSourceEnd() - info.getNameSourceStart() + 1);
167 }
168 /**
169  * @see IMember
170  */
171 public boolean isBinary() {
172         return false;
173 }
174 protected boolean isMainMethod(IMethod method) throws JavaModelException {
175         if ("main".equals(method.getElementName()) && Signature.SIG_VOID.equals(method.getReturnType())) { //$NON-NLS-1$
176                 int flags= method.getFlags();
177                 if (Flags.isStatic(flags) && Flags.isPublic(flags)) {
178                         String[] paramTypes= method.getParameterTypes();
179                         if (paramTypes.length == 1) {
180                                 String name=  Signature.toString(paramTypes[0]);
181                                 return "String[]".equals(Signature.getSimpleName(name)); //$NON-NLS-1$
182                         }
183                 }
184         }
185         return false;
186 }
187 /**
188  * @see IJavaElement
189  */
190 public boolean isReadOnly() {
191         return false; //getClassFile() != null;
192 }
193 /**
194  */
195 public String readableName() {
196
197         IJavaElement declaringType = getDeclaringType();
198         if (declaringType != null) {
199                 String declaringName = ((JavaElement) getDeclaringType()).readableName();
200                 StringBuffer buffer = new StringBuffer(declaringName);
201                 buffer.append('.');
202                 buffer.append(this.getElementName());
203                 return buffer.toString();
204         } else {
205                 return super.readableName();
206         }
207 }
208 /**
209  * Updates the name range for this element.
210  */
211 protected void updateNameRange(int nameStart, int nameEnd) {
212         try {
213                 MemberElementInfo info = (MemberElementInfo) getElementInfo();
214                 info.setNameSourceStart(nameStart);
215                 info.setNameSourceEnd(nameEnd);
216         } catch (JavaModelException npe) {
217                 return;
218         }
219 }
220 }