KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > apt > model > TypeParameterElementImpl


1 /*******************************************************************************
2  * Copyright (c) 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  * wharley@bea.com - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.compiler.apt.model;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.List JavaDoc;
17
18 import javax.lang.model.element.Element;
19 import javax.lang.model.element.ElementKind;
20 import javax.lang.model.element.ElementVisitor;
21 import javax.lang.model.element.PackageElement;
22 import javax.lang.model.element.TypeParameterElement;
23 import javax.lang.model.type.TypeMirror;
24
25 import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl;
26 import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding;
27 import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment;
28 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
29 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
30 import org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;
31
32 /**
33  *
34  */

35 public class TypeParameterElementImpl extends ElementImpl implements TypeParameterElement
36 {
37     private final Element _declaringElement;
38     
39     // Cache the bounds, because they're expensive to compute
40
private List JavaDoc<? extends TypeMirror> _bounds = null;
41     
42     /* package */ TypeParameterElementImpl(BaseProcessingEnvImpl env, TypeVariableBinding binding, Element declaringElement) {
43         super(env, binding);
44         _declaringElement = declaringElement;
45     }
46
47     /* package */ TypeParameterElementImpl(BaseProcessingEnvImpl env, TypeVariableBinding binding) {
48         super(env, binding);
49         _declaringElement = _env.getFactory().newElement(binding.declaringElement);
50     }
51
52     @Override JavaDoc
53     public List JavaDoc<? extends TypeMirror> getBounds()
54     {
55         if (null == _bounds) {
56             _bounds = calculateBounds();
57         }
58         return _bounds;
59     }
60     
61     // This code is drawn from org.eclipse.jdt.core.dom.TypeBinding.getTypeBounds()
62
private List JavaDoc<? extends TypeMirror> calculateBounds() {
63         TypeVariableBinding typeVariableBinding = (TypeVariableBinding)_binding;
64         ReferenceBinding varSuperclass = typeVariableBinding.superclass();
65         TypeBinding firstClassOrArrayBound = typeVariableBinding.firstBound;
66         int boundsLength = 0;
67         if (firstClassOrArrayBound != null) {
68             if (firstClassOrArrayBound == varSuperclass) {
69                 boundsLength++;
70             } else if (firstClassOrArrayBound.isArrayType()) { // capture of ? extends/super arrayType
71
boundsLength++;
72             } else {
73                 firstClassOrArrayBound = null;
74             }
75         }
76         ReferenceBinding[] superinterfaces = typeVariableBinding.superInterfaces();
77         int superinterfacesLength = 0;
78         if (superinterfaces != null) {
79             superinterfacesLength = superinterfaces.length;
80             boundsLength += superinterfacesLength;
81         }
82         List JavaDoc<TypeMirror> typeBounds = new ArrayList JavaDoc<TypeMirror>(boundsLength);
83         if (boundsLength != 0) {
84             if (firstClassOrArrayBound != null) {
85                 TypeMirror typeBinding = _env.getFactory().newTypeMirror(firstClassOrArrayBound);
86                 if (typeBinding == null) {
87                     return Collections.emptyList();
88                 }
89                 typeBounds.add(typeBinding);
90             }
91             if (superinterfaces != null) {
92                 for (int i = 0; i < superinterfacesLength; i++) {
93                     TypeMirror typeBinding = _env.getFactory().newTypeMirror(superinterfaces[i]);
94                     if (typeBinding == null) {
95                         return Collections.emptyList();
96                     }
97                     typeBounds.add(typeBinding);
98                 }
99             }
100         } else {
101             // at least we must add java.lang.Object
102
typeBounds.add(_env.getFactory().newTypeMirror(_env.getLookupEnvironment().getType(LookupEnvironment.JAVA_LANG_OBJECT)));
103         }
104         return Collections.unmodifiableList(typeBounds);
105     }
106
107     @Override JavaDoc
108     public Element getGenericElement()
109     {
110         return _declaringElement;
111     }
112
113     @Override JavaDoc
114     public <R, P> R accept(ElementVisitor<R, P> v, P p)
115     {
116         return v.visitTypeParameter(this, p);
117     }
118
119     /*
120      * (non-Javadoc)
121      * Java does not currently support annotations on type parameters.
122      * @see javax.lang.model.element.Element#getAnnotationMirrors()
123      */

124     @Override JavaDoc
125     protected AnnotationBinding[] getAnnotationBindings()
126     {
127         return null;
128     }
129
130     /*
131      * (non-Javadoc)
132      * Always return an empty list; type parameters do not enclose other elements.
133      * @see javax.lang.model.element.Element#getEnclosedElements()
134      */

135     @Override JavaDoc
136     public List JavaDoc<? extends Element> getEnclosedElements()
137     {
138         return Collections.emptyList();
139     }
140
141     /*
142      * (non-Javadoc)
143      * Always return null.
144      * @see javax.lang.model.element.Element#getEnclosingElement()
145      */

146     @Override JavaDoc
147     public Element getEnclosingElement()
148     {
149         return null;
150     }
151
152     @Override JavaDoc
153     public ElementKind getKind()
154     {
155         return ElementKind.TYPE_PARAMETER;
156     }
157
158     @Override JavaDoc
159     PackageElement getPackage()
160     {
161         // TODO what is the package of a type parameter?
162
return null;
163     }
164     
165     @Override JavaDoc
166     public String JavaDoc toString() {
167         return new String JavaDoc(_binding.readableName());
168     }
169 }
170
Popular Tags