| 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.descriptors.IMemberDescriptor; | 
| 15 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IMethodDescriptor; | 
| 16 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IReferenceTypeDescriptor; | 
| 17 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; | 
| 18 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiMethod; | 
| 19 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiType; | 
| 20 | import org.eclipse.pde.api.tools.internal.util.Util; | 
| 21 | import org.objectweb.asm.Opcodes; | 
| 22 |   | 
| 23 | /** | 
| 24 |  * Base implementation of {@link IApiMethod} | 
| 25 |  *  | 
| 26 |  * @since 1.0.0 | 
| 27 |  * @noextend This class is not intended to be subclassed by clients. | 
| 28 |  * @noinstantiate This class is not intended to be instantiated by clients. | 
| 29 |  */ | 
| 30 | public class ApiMethod extends ApiMember implements IApiMethod { | 
| 31 |          | 
| 32 |         private static final String INIT = "<init>"; //$NON-NLS-1$ | 
| 33 |         private static final String CLINIT = "<clinit>"; //$NON-NLS-1$         | 
| 34 |          | 
| 35 |         private String[] fExceptions; | 
| 36 |         private String fDefaultValue; | 
| 37 |          | 
| 38 |         private IMethodDescriptor fHandle; | 
| 39 |          | 
| 40 |         /** | 
| 41 |          * Constructor | 
| 42 |          * @param enclosing enclosing type | 
| 43 |          * @param name method name | 
| 44 |          * @param signature method signature | 
| 45 |          * @param genericSig  | 
| 46 |          * @param flags | 
| 47 |          */ | 
| 48 |         protected ApiMethod(IApiType enclosing, String name, String signature, String genericSig, int flags, String[] exceptions) { | 
| 49 |                 super(enclosing, name, signature, genericSig, IApiElement.METHOD, flags); | 
| 50 |                 fExceptions = exceptions; | 
| 51 |         } | 
| 52 |   | 
| 53 |         /** | 
| 54 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiMethod#isConstructor() | 
| 55 |          */ | 
| 56 |         public boolean isConstructor() { | 
| 57 |                 return getName().equals(INIT); | 
| 58 |         } | 
| 59 |          | 
| 60 |         /* (non-Javadoc) | 
| 61 |          * @see org.eclipse.pde.api.tools.internal.model.ApiMember#equals(java.lang.Object) | 
| 62 |          */ | 
| 63 |         public boolean equals(Object obj) { | 
| 64 |                 if (obj instanceof IApiMethod) { | 
| 65 |                         return super.equals(obj) && ((IApiMethod)obj).getSignature().equals(getSignature()); | 
| 66 |                 } | 
| 67 |                 return false; | 
| 68 |         } | 
| 69 |          | 
| 70 |         /* (non-Javadoc) | 
| 71 |          * @see org.eclipse.pde.api.tools.internal.model.ApiMember#hashCode() | 
| 72 |          */ | 
| 73 |         public int hashCode() { | 
| 74 |                 return super.hashCode() + getSignature().hashCode(); | 
| 75 |         } | 
| 76 |          | 
| 77 |         /* (non-Javadoc) | 
| 78 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiMethod#getExceptionNames() | 
| 79 |          */ | 
| 80 |         public String[] getExceptionNames() { | 
| 81 |                 return fExceptions; | 
| 82 |         } | 
| 83 |   | 
| 84 |         /* (non-Javadoc) | 
| 85 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiMethod#isClassInitializer() | 
| 86 |          */ | 
| 87 |         public boolean isClassInitializer() { | 
| 88 |                 return getName().equals(CLINIT); | 
| 89 |         } | 
| 90 |   | 
| 91 |         /* (non-Javadoc) | 
| 92 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiMethod#getDefaultValue() | 
| 93 |          */ | 
| 94 |         public String getDefaultValue() { | 
| 95 |                 return fDefaultValue; | 
| 96 |         } | 
| 97 |   | 
| 98 |         /** | 
| 99 |          * Used when building a type structure. | 
| 100 |          *  | 
| 101 |          * @param value default value | 
| 102 |          */ | 
| 103 |         public void setDefaultValue(String value) { | 
| 104 |                 fDefaultValue = value; | 
| 105 |         } | 
| 106 |   | 
| 107 |         /* (non-Javadoc) | 
| 108 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiMethod#isSynthetic() | 
| 109 |          */ | 
| 110 |         public boolean isSynthetic() { | 
| 111 |                 return (getModifiers() & Opcodes.ACC_SYNTHETIC) != 0; | 
| 112 |         } | 
| 113 |          | 
| 114 |         /** | 
| 115 |          * @see java.lang.Object#toString() | 
| 116 |          */ | 
| 117 |         public String toString() { | 
| 118 |                 StringBuffer buffer = new StringBuffer(); | 
| 119 |                 buffer | 
| 120 |                         .append("Method : access(") //$NON-NLS-1$ | 
| 121 |                         .append(getModifiers()) | 
| 122 |                         .append(") ") //$NON-NLS-1$ | 
| 123 |                         .append(getSignature()) | 
| 124 |                         .append(' ') | 
| 125 |                         .append(getName()); | 
| 126 |                 if (getExceptionNames() != null) { | 
| 127 |                         buffer.append(" throws "); //$NON-NLS-1$ | 
| 128 |                         for (int i = 0; i < getExceptionNames().length; i++) { | 
| 129 |                                 if (i > 0) buffer.append(','); | 
| 130 |                                 buffer.append(getExceptionNames()[i]); | 
| 131 |                         } | 
| 132 |                 } | 
| 133 |                 buffer.append(';').append(Util.LINE_DELIMITER); | 
| 134 |                 if (getGenericSignature() != null) { | 
| 135 |                         buffer | 
| 136 |                                 .append(" Generic signature : ") //$NON-NLS-1$ | 
| 137 |                                 .append(getGenericSignature()).append(Util.LINE_DELIMITER); | 
| 138 |                 } | 
| 139 |                 return String.valueOf(buffer); | 
| 140 |         }         | 
| 141 |          | 
| 142 |         /* (non-Javadoc) | 
| 143 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiMember#getHandle() | 
| 144 |          */ | 
| 145 |         public IMemberDescriptor getHandle() { | 
| 146 |                 if (fHandle == null) { | 
| 147 |                         try { | 
| 148 |                                 IApiType type = getEnclosingType(); | 
| 149 |                                 fHandle = ((IReferenceTypeDescriptor)type.getHandle()).getMethod(getName(), getSignature()); | 
| 150 |                         } catch (CoreException e) { | 
| 151 |                                 // should not happen for field or method - enclosing type is cached | 
| 152 |                         } | 
| 153 |                 } | 
| 154 |                 return fHandle; | 
| 155 |         } | 
| 156 |   | 
| 157 | } |