1 11 12 package org.eclipse.jdt.apt.core.internal.type; 13 14 import com.sun.mirror.type.ArrayType; 15 import com.sun.mirror.util.TypeVisitor; 16 17 import org.eclipse.jdt.apt.core.internal.declaration.EclipseMirrorType; 18 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv; 19 import org.eclipse.jdt.apt.core.internal.util.Factory; 20 import org.eclipse.jdt.core.BindingKey; 21 import org.eclipse.jdt.core.dom.ITypeBinding; 22 23 public class ArrayTypeImpl implements ArrayType, EclipseMirrorType 24 { 25 private final ITypeBinding _arrayBinding; 26 private final BaseProcessorEnv _env; 27 public ArrayTypeImpl(final ITypeBinding binding, BaseProcessorEnv env) 28 { 29 _arrayBinding = binding; 30 _env = env; 31 assert _arrayBinding != null && _arrayBinding.isArray(); 32 assert env != null : "missing environment"; } 34 35 public void accept(TypeVisitor visitor) 36 { 37 visitor.visitArrayType(this); 38 } 39 40 public EclipseMirrorType getComponentType() 41 { 42 final ITypeBinding elementType = _arrayBinding.getElementType(); 43 final int dimension = _arrayBinding.getDimensions(); 44 if( dimension == 0 ) return null; 46 final ITypeBinding result; 47 if( dimension == 1 ) result = elementType; 49 else{ 50 final String componentKey = BindingKey.createArrayTypeBindingKey(elementType.getKey(), dimension - 1); 51 result = _env.getTypeBindingFromKey(componentKey); 52 if( result == null ) 53 throw new IllegalStateException ("unknown component type for " + _arrayBinding); } 55 56 final EclipseMirrorType mirror = Factory.createTypeMirror(result, _env); 57 if( mirror == null ) 58 return (EclipseMirrorType)Factory.createErrorClassType(result); 59 return mirror; 60 } 61 62 public String toString(){ 63 final ITypeBinding elementType = _arrayBinding.getElementType(); 64 final StringBuilder buffer = new StringBuilder (); 65 String name = elementType.getQualifiedName(); 66 buffer.append(name); 67 for( int i=0, dim = _arrayBinding.getDimensions(); i<dim; i++ ) 68 buffer.append("[]"); 70 return buffer.toString(); 71 } 72 73 public boolean equals(Object obj) 74 { 75 if( obj instanceof ArrayTypeImpl ) 76 return _arrayBinding == ((ArrayTypeImpl)obj)._arrayBinding; 77 return false; 78 } 79 80 public ITypeBinding getTypeBinding(){ return _arrayBinding; } 81 82 public int hashCode(){ return _arrayBinding.hashCode(); } 83 84 public MirrorKind kind(){ return MirrorKind.TYPE_ARRAY; } 85 86 public BaseProcessorEnv getEnvironment(){ return _env; } 87 88 public boolean isAssignmentCompatible(EclipseMirrorType left) { 89 return isSubTypeCompatible(left); 90 } 91 92 public boolean isSubTypeCompatible(EclipseMirrorType type) { 93 if (type.kind() == MirrorKind.TYPE_CLASS) 94 return "java.lang.Object".equals(type.getTypeBinding().getQualifiedName()); if (type.kind() == MirrorKind.TYPE_INTERFACE) 96 return "java.lang.Cloneable".equals(type.getTypeBinding().getQualifiedName()) || "java.io.Serializable".equals(type.getTypeBinding().getQualifiedName()); if (type.kind() == MirrorKind.TYPE_ARRAY) { 99 EclipseMirrorType element1 = getComponentType(); 100 EclipseMirrorType element2 = ((ArrayTypeImpl)type).getComponentType(); 101 return element1.isSubTypeCompatible(element2); 102 } 103 return false; 104 } 105 106 } 107 | Popular Tags |