1 /*******************************************************************************
2  * Copyright (c) 2008, 2009 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.pde.api.tools.internal.model;
15 
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.jdt.core.Flags;
18 import org.eclipse.pde.api.tools.internal.provisional.descriptors.IFieldDescriptor;
19 import org.eclipse.pde.api.tools.internal.provisional.descriptors.IMemberDescriptor;
20 import org.eclipse.pde.api.tools.internal.provisional.descriptors.IReferenceTypeDescriptor;
21 import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement;
22 import org.eclipse.pde.api.tools.internal.provisional.model.IApiField;
23 import org.eclipse.pde.api.tools.internal.provisional.model.IApiType;
24 import org.eclipse.pde.api.tools.internal.util.Util;
25 
26 /**
27  * Base implementation of {@link IApiField}
28  *
29  * @since 1.0.0
30  * @noextend This class is not intended to be subclassed by clients.
31  * @noinstantiate This class is not intended to be instantiated by clients.
32  */
33 public class ApiField extends ApiMember implements IApiField {
34 
35 	/**
36 	 * Constant value
37 	 */
38 	private Object fValue;
39 
40 	private IFieldDescriptor fHandle;
41 
42 	/**
43 	 * Constructor
44 	 *
45 	 * @param parent the enclosing type of the field
46 	 * @param name the name of the field
47 	 * @param signature the signature for the field
48 	 * @param genericSig the generic signature of the field
49 	 * @param flags the flags for the field
50 	 * @param value the value assigned to the field
51 	 * @param value constant value or <code>null</code> if none
52 	 */
ApiField(IApiType enclosing, String name, String signature, String genericSig, int flags, Object value)53 	protected ApiField(IApiType enclosing, String name, String signature, String genericSig, int flags, Object value) {
54 		super(enclosing, name, signature, genericSig, IApiElement.FIELD, flags);
55 		fValue = value;
56 	}
57 
58 	/**
59 	 * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiField#isEnumConstant()
60 	 */
61 	@Override
isEnumConstant()62 	public boolean isEnumConstant() {
63 		return (Flags.isEnum(getModifiers()));
64 	}
65 
66 	@Override
equals(Object obj)67 	public boolean equals(Object obj) {
68 		if (obj instanceof IApiField) {
69 			return super.equals(obj);
70 		}
71 		return false;
72 	}
73 
74 	@Override
hashCode()75 	public int hashCode() {
76 		return super.hashCode() + (this.fValue != null ? this.fValue.hashCode() : 0);
77 	}
78 
79 	@Override
getConstantValue()80 	public Object getConstantValue() {
81 		return fValue;
82 	}
83 
84 	@Override
toString()85 	public String toString() {
86 		StringBuilder buffer = new StringBuilder();
87 		buffer.append("Field : access(") //$NON-NLS-1$
88 		.append(getModifiers()).append(") ") //$NON-NLS-1$
89 		.append(getSignature()).append(' ').append(getName()).append(" isEnum constant ") //$NON-NLS-1$
90 		.append(isEnumConstant());
91 		if (getConstantValue() != null) {
92 			buffer.append(" = ").append(getConstantValue()); //$NON-NLS-1$
93 		}
94 		buffer.append(';').append(Util.LINE_DELIMITER);
95 		if (getGenericSignature() != null) {
96 			buffer.append(" Signature : ") //$NON-NLS-1$
97 			.append(getGenericSignature()).append(Util.LINE_DELIMITER);
98 		}
99 		return String.valueOf(buffer);
100 	}
101 
102 	@Override
getHandle()103 	public IMemberDescriptor getHandle() {
104 		if (fHandle == null) {
105 			try {
106 				IApiType type = getEnclosingType();
107 				fHandle = ((IReferenceTypeDescriptor) type.getHandle()).getField(getName());
108 			} catch (CoreException e) {
109 				// should not happen for field or method - enclosing type is
110 				// cached
111 			}
112 		}
113 		return fHandle;
114 	}
115 }
116