KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > type > ArrayTypeImpl


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
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  * tyeung@bea.com - initial API and implementation
10  *******************************************************************************/

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"; //$NON-NLS-1$
33
}
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         // guarding around error cases.
45
if( dimension == 0 ) return null;
46         final ITypeBinding result;
47         if( dimension == 1 ) // the element type is the component type.
48
result = elementType;
49         else{
50             final String JavaDoc componentKey = BindingKey.createArrayTypeBindingKey(elementType.getKey(), dimension - 1);
51             result = _env.getTypeBindingFromKey(componentKey);
52             if( result == null )
53                 throw new IllegalStateException JavaDoc("unknown component type for " + _arrayBinding); //$NON-NLS-1$
54
}
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 JavaDoc toString(){
63         final ITypeBinding elementType = _arrayBinding.getElementType();
64         final StringBuilder JavaDoc buffer = new StringBuilder JavaDoc();
65         String JavaDoc name = elementType.getQualifiedName();
66         buffer.append(name);
67         for( int i=0, dim = _arrayBinding.getDimensions(); i<dim; i++ )
68             buffer.append("[]"); //$NON-NLS-1$
69

70         return buffer.toString();
71     }
72
73     public boolean equals(Object JavaDoc 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()); //$NON-NLS-1$
95
if (type.kind() == MirrorKind.TYPE_INTERFACE)
96             return "java.lang.Cloneable".equals(type.getTypeBinding().getQualifiedName()) || //$NON-NLS-1$
97
"java.io.Serializable".equals(type.getTypeBinding().getQualifiedName()); //$NON-NLS-1$
98
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