| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2008, 2009 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.pde.api.tools.internal.model; |
| 12 | |
| 13 | import org.eclipse.core.runtime.CoreException; |
| 14 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
| 15 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; |
| 16 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiMember; |
| 17 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiType; |
| 18 | |
| 19 | /** |
| 20 | * Base implementation of {@link IApiMember} |
| 21 | * |
| 22 | * @since 1.0.0 |
| 23 | * @noextend This class is not intended to be subclassed by clients. |
| 24 | */ |
| 25 | public abstract class ApiMember extends ApiElement implements IApiMember { |
| 26 | |
| 27 | private int fFlags = -1; |
| 28 | private String fSignature; |
| 29 | private String fGenericSignature; |
| 30 | |
| 31 | /** |
| 32 | * Constructs a member enclosed by the given type with the specified |
| 33 | * name, type constant, and flags (modifiers). |
| 34 | * |
| 35 | * @param parent the parent {@link IApiElement} for this type |
| 36 | * @param name member name |
| 37 | * @param signature signature (type or method) |
| 38 | * @param type see element type constants in {@link IApiMember} |
| 39 | * @param flags modifier flags |
| 40 | */ |
| 41 | protected ApiMember(IApiElement parent, String name, String signature, String genericSig, int type, int flags) { |
| 42 | super(parent, type, name); |
| 43 | fFlags = flags; |
| 44 | fSignature = signature; |
| 45 | fGenericSignature = genericSig; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns this member's signature. |
| 50 | * |
| 51 | * @return signature |
| 52 | */ |
| 53 | public String getSignature() { |
| 54 | return fSignature; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns generic signature or <code>null</code> if none. |
| 59 | * |
| 60 | * @return |
| 61 | */ |
| 62 | public String getGenericSignature() { |
| 63 | return fGenericSignature; |
| 64 | } |
| 65 | |
| 66 | /* (non-Javadoc) |
| 67 | * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiMember#getModifiers() |
| 68 | */ |
| 69 | public int getModifiers() { |
| 70 | return fFlags; |
| 71 | } |
| 72 | |
| 73 | /* (non-Javadoc) |
| 74 | * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiMember#getEnclosingType() |
| 75 | */ |
| 76 | public IApiType getEnclosingType() throws CoreException { |
| 77 | return (IApiType) getAncestor(IApiElement.TYPE); |
| 78 | } |
| 79 | |
| 80 | /* (non-Javadoc) |
| 81 | * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiMember#getApiComponent() |
| 82 | */ |
| 83 | public IApiComponent getApiComponent() { |
| 84 | return (IApiComponent) getAncestor(IApiElement.COMPONENT); |
| 85 | } |
| 86 | |
| 87 | /* (non-Javadoc) |
| 88 | * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiMember#getPackageName() |
| 89 | */ |
| 90 | public String getPackageName() { |
| 91 | try { |
| 92 | IApiType type = getEnclosingType(); |
| 93 | if(type != null) { |
| 94 | return type.getPackageName(); |
| 95 | } |
| 96 | } |
| 97 | catch(CoreException ce) {} |
| 98 | return ""; //$NON-NLS-1$ |
| 99 | } |
| 100 | |
| 101 | /* (non-Javadoc) |
| 102 | * @see java.lang.Object#equals(java.lang.Object) |
| 103 | */ |
| 104 | public boolean equals(Object obj) { |
| 105 | if (obj instanceof IApiElement) { |
| 106 | IApiElement element = (IApiElement) obj; |
| 107 | if(element.getType() == this.getType()) { |
| 108 | return enclosingTypesEqual(this, element) |
| 109 | && getName().equals(element.getName()); |
| 110 | } |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns if the immediate enclosing {@link IApiType}s of the two members |
| 117 | * are equal: where both enclosing type being <code>null</code> is considered equal |
| 118 | * @param e1 |
| 119 | * @param e2 |
| 120 | * @return true if both immediate enclosing type are equal |
| 121 | */ |
| 122 | protected boolean enclosingTypesEqual(IApiElement e1, IApiElement e2) { |
| 123 | IApiType t1 = (IApiType) e1.getAncestor(IApiElement.TYPE); |
| 124 | IApiType t2 = (IApiType) e2.getAncestor(IApiElement.TYPE); |
| 125 | if(t1 == null) { |
| 126 | return t2 == null; |
| 127 | } |
| 128 | return t1.equals(t2); |
| 129 | } |
| 130 | |
| 131 | /* (non-Javadoc) |
| 132 | * @see java.lang.Object#hashCode() |
| 133 | */ |
| 134 | public int hashCode() { |
| 135 | IApiType enclosing = (IApiType) getAncestor(IApiElement.TYPE); |
| 136 | return getType() + getName().hashCode() + (enclosing == null ? 0 : enclosing.hashCode()); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Used when building a member type. |
| 141 | * @param access modifiers |
| 142 | */ |
| 143 | public void setModifiers(int access) { |
| 144 | fFlags = access; |
| 145 | } |
| 146 | } |