| package org.eclipse.jst.jsf.core.internal.types; |
| |
| import org.eclipse.jdt.core.Signature; |
| |
| /** |
| * Value object representing a value binding type by it's type and assignability |
| * information |
| * |
| * @author cbateman |
| * |
| */ |
| public class ValueType implements SignatureBasedType, IAssignable |
| { |
| private final static String[] EMPTY_STRING_ARRAY = new String[0]; |
| |
| private final String _signature; |
| private final int _assignmentMask; |
| private final String[] _superTypes; |
| private final String[] _interfaceTypes; |
| |
| private String[] _allTypes; // lazy creation on getAllTypes |
| |
| /** |
| * Construct a new ValueType object with the given |
| * signature |
| * |
| * @param signature |
| * @param superTypes |
| * @param interfaceTypes |
| * @param assignmentMask |
| */ |
| public ValueType(final String signature, |
| final String[] superTypes, |
| final String[] interfaceTypes, |
| final int assignmentMask) |
| { |
| if (signature == null) |
| { |
| throw new AssertionError("signature can never be null"); |
| } |
| |
| _signature = signature; |
| _assignmentMask = assignmentMask; |
| _superTypes = superTypes != null ? superTypes : EMPTY_STRING_ARRAY; |
| _interfaceTypes = interfaceTypes != null ? interfaceTypes : EMPTY_STRING_ARRAY; |
| } |
| |
| /** |
| * Copy constructor equivilent to |
| * ValueType(template.getSignature(), template.getSuperTypes(), template.getInterfaceTypes(),assingmentMask) |
| * |
| * @param template |
| * @param assignmentMask |
| */ |
| public ValueType(final ValueType template, final int assignmentMask) |
| { |
| this(template._signature, template._superTypes, |
| template._interfaceTypes, assignmentMask); |
| } |
| |
| /** |
| * Convienence constructor for creating ValueType's with no supertype |
| * or interface info. Equivilent to: |
| * ValueType(signature, new String[0], new String[0], assignmentMask) |
| * |
| * @param signature |
| * @param assignmentMask |
| */ |
| public ValueType(final String signature, final int assignmentMask) |
| { |
| this(signature, EMPTY_STRING_ARRAY, EMPTY_STRING_ARRAY, assignmentMask); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jst.jsf.core.internal.types.SignatureBasedType#getSignature() |
| */ |
| public String getSignature() |
| { |
| return _signature; |
| } |
| |
| public int getAssignability() { |
| return _assignmentMask; |
| } |
| |
| public boolean isLHS() { |
| return TypeUtil.matchesLHS(_assignmentMask); |
| } |
| |
| public boolean isRHS() { |
| return TypeUtil.matchesRHS(_assignmentMask); |
| } |
| |
| public String toString() |
| { |
| return Signature.getSignatureSimpleName |
| (TypeTransformer.transformBoxPrimitives(_signature)); |
| } |
| |
| /** |
| * @return an array of all signatures of all super types or empty |
| * array if there are no super types for this type |
| */ |
| public String[] getSuperTypes() |
| { |
| return _superTypes; |
| } |
| |
| /** |
| * @return an array of all interfaces implemented or empty array |
| * if none |
| */ |
| public String[] getInterfaceTypes() |
| { |
| return _interfaceTypes; |
| } |
| |
| /** |
| * @return all types including the base type, super types and interface |
| * types. |
| */ |
| public String[] getAllTypes() |
| { |
| if (_allTypes == null) |
| { |
| int numberOfTypes = 1 + _superTypes.length + _interfaceTypes.length; |
| _allTypes = new String[numberOfTypes]; |
| _allTypes[0] = _signature; |
| System.arraycopy(_superTypes, 0, _allTypes, 1, _superTypes.length); |
| System.arraycopy(_interfaceTypes, 0, _allTypes, 1+_superTypes.length, _interfaceTypes.length); |
| } |
| return _allTypes; |
| } |
| |
| |
| |
| public CompositeType toCompositeType() |
| { |
| return new CompositeType(getAllTypes(), getAssignability()); |
| } |
| |
| /** |
| * @param signature |
| * @return true if an instance of this type would satisfy instanceof signature |
| * |
| */ |
| public boolean isInstanceOf(final String signature) |
| { |
| final String[] allTypes = getAllTypes(); |
| |
| for (int i = 0; i < allTypes.length; i++) |
| { |
| if (allTypes[i].equals(signature)) |
| { |
| return true; |
| } |
| } |
| |
| return false; |
| } |
| } |