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.core.Flags;
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.core.IMember;
18 import net.sourceforge.phpdt.core.IMethod;
19 import net.sourceforge.phpdt.core.ISourceRange;
20 import net.sourceforge.phpdt.core.IType;
21 import net.sourceforge.phpdt.core.JavaModelException;
22 import net.sourceforge.phpdt.core.Signature;
23 import net.sourceforge.phpdt.core.jdom.IDOMNode;
25 import com.sun.corba.se.internal.core.Constant;
32 /* package */ abstract class Member extends SourceRefElement implements IMember {
33 protected Member(int type, IJavaElement parent, String name) {
34 super(type, parent, 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 parent = (JavaElement)getParent();
141 if (parent.fLEType == TYPE) {
142 return (IType) parent;
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;
162 public ISourceRange getNameRange() throws JavaModelException {
163 MemberElementInfo info= (MemberElementInfo)getElementInfo();
164 return new SourceRange(info.getNameSourceStart(), info.getNameSourceEnd() - info.getNameSourceStart() + 1);
169 public boolean isBinary() {
172 protected boolean isMainMethod(IMethod method) throws JavaModelException {
173 if ("main".equals(method.getElementName()) && Signature.SIG_VOID.equals(method.getReturnType())) { //$NON-NLS-1$
174 int flags= method.getFlags();
175 if (Flags.isStatic(flags) && Flags.isPublic(flags)) {
176 String[] paramTypes= method.getParameterTypes();
177 if (paramTypes.length == 1) {
178 String name= Signature.toString(paramTypes[0]);
179 return "String[]".equals(Signature.getSimpleName(name)); //$NON-NLS-1$
188 public boolean isReadOnly() {
189 return false; //getClassFile() != null;
193 public String readableName() {
195 IJavaElement declaringType = getDeclaringType();
196 if (declaringType != null) {
197 String declaringName = ((JavaElement) getDeclaringType()).readableName();
198 StringBuffer buffer = new StringBuffer(declaringName);
200 buffer.append(this.getElementName());
201 return buffer.toString();
203 return super.readableName();
207 * Updates the name range for this element.
209 protected void updateNameRange(int nameStart, int nameEnd) {
211 MemberElementInfo info = (MemberElementInfo) getElementInfo();
212 info.setNameSourceStart(nameStart);
213 info.setNameSourceEnd(nameEnd);
214 } catch (JavaModelException npe) {