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.JavaElement;
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;
27 import com.sun.corba.se.internal.core.Constant;
34 /* package */ abstract class Member extends SourceRefElement implements IMember {
35 protected Member(JavaElement parent, String name) {
38 protected boolean areSimilarMethods(
39 String name1, String[] params1,
40 String name2, String[] params2,
41 String[] simpleNames1) {
43 if (name1.equals(name2)) {
44 int params1Length = params1.length;
45 if (params1Length == params2.length) {
46 for (int i = 0; i < params1Length; i++) {
48 simpleNames1 == null ?
49 Signature.getSimpleName(Signature.toString(params1[i])) :
51 String simpleName2 = Signature.getSimpleName(Signature.toString(params2[i]));
52 if (!simpleName1.equals(simpleName2)) {
62 * Converts a field constant from the compiler's representation
63 * to the Java Model constant representation (Number or String).
65 //protected static Object convertConstant(Constant constant) {
66 // if (constant == null)
68 // if (constant == Constant.NotAConstant) {
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();
95 * @see JavaElement#equalsDOMNode
97 protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
98 return getElementName().equals(node.getName());
101 * Helper method for SourceType.findMethods and BinaryType.findMethods
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]));
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(
117 existingMethod.getElementName(),
118 existingMethod.getParameterTypes(),
120 list.add(existingMethod);
123 int size = list.size();
127 IMethod[] result = new IMethod[size];
128 list.toArray(result);
135 //public IClassFile getClassFile() {
136 // return ((JavaElement)getParent()).getClassFile();
141 public IType getDeclaringType() {
142 JavaElement parentElement = (JavaElement)getParent();
143 if (parentElement.getElementType() == TYPE) {
144 return (IType) parentElement;
151 public int getFlags() throws JavaModelException {
152 MemberElementInfo info = (MemberElementInfo) getElementInfo();
153 return info.getModifiers();
156 * @see JavaElement#getHandleMemento()
158 protected char getHandleMementoDelimiter() {
159 return JavaElement.JEM_TYPE;
164 public ISourceRange getNameRange() throws JavaModelException {
165 MemberElementInfo info= (MemberElementInfo)getElementInfo();
166 return new SourceRange(info.getNameSourceStart(), info.getNameSourceEnd() - info.getNameSourceStart() + 1);
171 public boolean isBinary() {
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$
190 public boolean isReadOnly() {
191 return false; //getClassFile() != null;
195 public String readableName() {
197 IJavaElement declaringType = getDeclaringType();
198 if (declaringType != null) {
199 String declaringName = ((JavaElement) getDeclaringType()).readableName();
200 StringBuffer buffer = new StringBuffer(declaringName);
202 buffer.append(this.getElementName());
203 return buffer.toString();
205 return super.readableName();
209 * Updates the name range for this element.
211 protected void updateNameRange(int nameStart, int nameEnd) {
213 MemberElementInfo info = (MemberElementInfo) getElementInfo();
214 info.setNameSourceStart(nameStart);
215 info.setNameSourceEnd(nameEnd);
216 } catch (JavaModelException npe) {