KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > declaration > TypeParameterDeclarationImpl


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.declaration;
13
14 import java.lang.annotation.Annotation JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.Collections JavaDoc;
18
19 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
20 import org.eclipse.jdt.apt.core.internal.util.Factory;
21 import org.eclipse.jdt.apt.core.internal.util.SourcePositionImpl;
22 import org.eclipse.jdt.core.dom.ASTNode;
23 import org.eclipse.jdt.core.dom.CompilationUnit;
24 import org.eclipse.jdt.core.dom.IBinding;
25 import org.eclipse.jdt.core.dom.ITypeBinding;
26
27 import com.sun.mirror.declaration.AnnotationMirror;
28 import com.sun.mirror.declaration.Declaration;
29 import com.sun.mirror.declaration.Modifier;
30 import com.sun.mirror.declaration.TypeParameterDeclaration;
31 import com.sun.mirror.type.ReferenceType;
32 import com.sun.mirror.type.TypeVariable;
33 import com.sun.mirror.util.DeclarationVisitor;
34 import com.sun.mirror.util.SourcePosition;
35 import com.sun.mirror.util.TypeVisitor;
36
37 public class TypeParameterDeclarationImpl extends DeclarationImpl implements
38     TypeParameterDeclaration, TypeVariable, EclipseMirrorType
39 {
40     public TypeParameterDeclarationImpl(final ITypeBinding binding,
41                                         final BaseProcessorEnv env)
42     {
43         super(binding, env);
44         assert binding.isTypeVariable();
45     }
46
47     public void accept(DeclarationVisitor visitor)
48     {
49         visitor.visitTypeParameterDeclaration(this);
50     }
51
52     public <A extends Annotation JavaDoc> A getAnnotation(Class JavaDoc<A> annotationClass)
53     {
54         return null;
55     }
56
57     public Collection JavaDoc<AnnotationMirror> getAnnotationMirrors()
58     {
59         return Collections.emptyList();
60     }
61
62     public Collection JavaDoc<ReferenceType> getBounds()
63     {
64         final ITypeBinding[] bounds = getDeclarationBinding().getTypeBounds();
65         if( bounds == null || bounds.length == 0 )
66             return Collections.emptyList();
67
68         final Collection JavaDoc<ReferenceType> result = new ArrayList JavaDoc<ReferenceType>(4);
69         for( ITypeBinding bound : bounds ){
70             final ReferenceType type = Factory.createReferenceType(bound, _env);
71              if( type != null )
72                 result.add(type);
73         }
74
75         return result;
76     }
77
78     public String JavaDoc getDocComment()
79     {
80         return null;
81     }
82
83     public Collection JavaDoc<Modifier> getModifiers()
84     {
85         return Collections.emptyList();
86     }
87
88     public Declaration getOwner()
89     {
90         return Factory.createDeclaration(getOwnerBinding(), _env);
91     }
92
93     private IBinding getOwnerBinding() {
94         final ITypeBinding binding = getDeclarationBinding();
95         // declared on a class
96
IBinding owner = binding.getDeclaringClass();
97         if( owner == null )
98             // declared on the method
99
owner = binding.getDeclaringMethod();
100         return owner;
101     }
102     
103     public SourcePosition getPosition()
104     {
105         if( isFromSource() )
106         {
107             final ASTNode node = getAstNode();
108             if( node == null ) return null;
109             final CompilationUnit unit = getCompilationUnit();
110             final int offset = node.getStartPosition();
111             return new SourcePositionImpl(offset,
112                                           node.getLength(),
113                                           unit.getLineNumber(offset),
114                                           unit.getColumnNumber(offset),
115                                           this);
116         }
117         else
118             return null;
119     }
120
121     public String JavaDoc getSimpleName()
122     {
123         final ITypeBinding typeVar = getDeclarationBinding();
124         return typeVar.getName();
125     }
126
127     // Start of implementation of TypeVariable API
128
public void accept(TypeVisitor visitor)
129     {
130         visitor.visitTypeVariable(this);
131     }
132
133     public TypeParameterDeclaration getDeclaration()
134     {
135         return this;
136     }
137     // End of implementation of TypeVariable API
138

139     public String JavaDoc toString()
140     {
141         return getSimpleName();
142     }
143
144     public MirrorKind kind(){ return MirrorKind.TYPE_PARAMETER_VARIABLE; }
145     
146     public ITypeBinding getDeclarationBinding(){ return (ITypeBinding) _binding; }
147     public ITypeBinding getTypeBinding() { return (ITypeBinding)_binding;}
148
149     public boolean isFromSource(){ return getDeclarationBinding().isFromSource(); }
150
151     public boolean isAssignmentCompatible(EclipseMirrorType left) {
152         return isSubTypeCompatible(left);
153     }
154
155     public boolean isSubTypeCompatible(EclipseMirrorType type) {
156         if (type.kind() == MirrorKind.TYPE_PARAMETER_VARIABLE) {
157             TypeParameterDeclarationImpl other = (TypeParameterDeclarationImpl) type;
158             return getOwnerBinding() == other.getOwnerBinding() &&
159                 getSimpleName().equals(other.getSimpleName());
160         }
161         
162         for (ReferenceType bound : getBounds()) {
163             if (((EclipseMirrorType)bound).isSubTypeCompatible(type))
164                 return true;
165         }
166         
167         return false;
168     }
169 }
170
Popular Tags