| 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.jdt.core.Flags; | 
| 15 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IFieldDescriptor; | 
| 16 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IMemberDescriptor; | 
| 17 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IReferenceTypeDescriptor; | 
| 18 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; | 
| 19 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiField; | 
| 20 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiType; | 
| 21 | import org.eclipse.pde.api.tools.internal.util.Util; | 
| 22 |   | 
| 23 | /** | 
| 24 |  * Base implementation of {@link IApiField} | 
| 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 ApiField extends ApiMember implements IApiField { | 
| 31 |          | 
| 32 |         /** | 
| 33 |          * Constant value | 
| 34 |          */ | 
| 35 |         private Object fValue; | 
| 36 |          | 
| 37 |         private IFieldDescriptor fHandle; | 
| 38 |          | 
| 39 |         /** | 
| 40 |          * Constructor | 
| 41 |          * @param parent the enclosing type of the field | 
| 42 |          * @param name the name of the field | 
| 43 |          * @param signature the signature for the field | 
| 44 |          * @param genericSig the generic signature of the field | 
| 45 |          * @param flags the flags for the field | 
| 46 |          * @param value the value assigned to the field | 
| 47 |          * @param value constant value or <code>null</code> if none | 
| 48 |          */ | 
| 49 |         protected ApiField(IApiType enclosing, String name, String signature, String genericSig, int flags, Object value) { | 
| 50 |                 super(enclosing, name, signature, genericSig, IApiElement.FIELD, flags); | 
| 51 |                 fValue = value; | 
| 52 |         } | 
| 53 |          | 
| 54 |         /** | 
| 55 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiField#isEnumConstant() | 
| 56 |          */ | 
| 57 |         public boolean isEnumConstant() { | 
| 58 |                 return (Flags.isEnum(getModifiers())); | 
| 59 |         } | 
| 60 |          | 
| 61 |         /* (non-Javadoc) | 
| 62 |          * @see org.eclipse.pde.api.tools.internal.model.ApiMember#equals(java.lang.Object) | 
| 63 |          */ | 
| 64 |         public boolean equals(Object obj) { | 
| 65 |                 if (obj instanceof IApiField) { | 
| 66 |                         return super.equals(obj); | 
| 67 |                 } | 
| 68 |                 return false; | 
| 69 |         } | 
| 70 |   | 
| 71 |         public int hashCode() { | 
| 72 |                 return super.hashCode() + (this.fValue != null ? this.fValue.hashCode() : 0); | 
| 73 |         } | 
| 74 |   | 
| 75 |         /* (non-Javadoc) | 
| 76 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiField#getConstantValue() | 
| 77 |          */ | 
| 78 |         public Object getConstantValue() { | 
| 79 |                 return fValue; | 
| 80 |         } | 
| 81 |          | 
| 82 |         public String toString() { | 
| 83 |                 StringBuffer buffer = new StringBuffer(); | 
| 84 |                 buffer | 
| 85 |                         .append("Field : access(") //$NON-NLS-1$ | 
| 86 |                         .append(getModifiers()) | 
| 87 |                         .append(") ") //$NON-NLS-1$ | 
| 88 |                         .append(getSignature()) | 
| 89 |                         .append(' ') | 
| 90 |                         .append(getName()) | 
| 91 |                         .append(" isEnum constant ") //$NON-NLS-1$ | 
| 92 |                         .append(isEnumConstant()); | 
| 93 |                 if (getConstantValue() != null) { | 
| 94 |                         buffer.append(" = ").append(getConstantValue()); //$NON-NLS-1$ | 
| 95 |                 } | 
| 96 |                 buffer.append(';').append(Util.LINE_DELIMITER); | 
| 97 |                 if (getGenericSignature() != null) { | 
| 98 |                         buffer | 
| 99 |                                 .append(" Signature : ") //$NON-NLS-1$ | 
| 100 |                                 .append(getGenericSignature()).append(Util.LINE_DELIMITER); | 
| 101 |                 } | 
| 102 |                 return String.valueOf(buffer); | 
| 103 |         }         | 
| 104 |          | 
| 105 |         /* (non-Javadoc) | 
| 106 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiMember#getHandle() | 
| 107 |          */ | 
| 108 |         public IMemberDescriptor getHandle() { | 
| 109 |                 if (fHandle == null) { | 
| 110 |                         try { | 
| 111 |                                 IApiType type = getEnclosingType(); | 
| 112 |                                 fHandle = ((IReferenceTypeDescriptor)type.getHandle()).getField(getName()); | 
| 113 |                         } catch (CoreException e) { | 
| 114 |                                 // should not happen for field or method - enclosing type is cached | 
| 115 |                         } | 
| 116 |                 } | 
| 117 |                 return fHandle; | 
| 118 |         } | 
| 119 | } |