KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 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
13 package org.eclipse.jdt.internal.compiler.apt.model;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.List JavaDoc;
18
19 import javax.lang.model.element.Element;
20 import javax.lang.model.element.TypeElement;
21 import javax.lang.model.type.DeclaredType;
22 import javax.lang.model.type.TypeKind;
23 import javax.lang.model.type.TypeMirror;
24 import javax.lang.model.type.TypeVisitor;
25
26 import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl;
27 import org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
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  * Implementation of DeclaredType, which refers to a particular usage or instance of a type.
34  * Contrast with {@link TypeElement}, which is an element that potentially defines a family
35  * of DeclaredTypes.
36  */

37 public class DeclaredTypeImpl extends TypeMirrorImpl implements DeclaredType {
38     
39     /* package */ DeclaredTypeImpl(BaseProcessingEnvImpl env, ReferenceBinding binding) {
40         super(env, binding);
41     }
42
43     @Override JavaDoc
44     public Element asElement() {
45         // The JDT compiler does not distinguish between type elements and declared types
46
return _env.getFactory().newElement((ReferenceBinding)_binding);
47     }
48
49     @Override JavaDoc
50     public TypeMirror getEnclosingType() {
51         ReferenceBinding binding = (ReferenceBinding)_binding;
52         ReferenceBinding enclosingType = binding.enclosingType();
53         if (enclosingType != null) return _env.getFactory().newDeclaredType(enclosingType);
54         return _env.getFactory().getNoType(TypeKind.NONE);
55     }
56
57     /*
58      * (non-Javadoc)
59      * @see javax.lang.model.type.DeclaredType#getTypeArguments()
60      * @see javax.lang.model.element.TypeElement#getTypeParameters().
61      */

62     @Override JavaDoc
63     public List JavaDoc<? extends TypeMirror> getTypeArguments() {
64         ReferenceBinding binding = (ReferenceBinding)_binding;
65         if (binding.isParameterizedType()) {
66             ParameterizedTypeBinding ptb = (ParameterizedTypeBinding)_binding;
67             TypeBinding[] arguments = ptb.arguments;
68             int length = arguments == null ? 0 : arguments.length;
69             if (length == 0) return Collections.emptyList();
70             List JavaDoc<TypeMirror> args = new ArrayList JavaDoc<TypeMirror>(length);
71             for (TypeBinding arg : arguments) {
72                 args.add(_env.getFactory().newTypeMirror(arg));
73             }
74             return Collections.unmodifiableList(args);
75         }
76         if (binding.isGenericType()) {
77             TypeVariableBinding[] typeVariables = binding.typeVariables();
78             List JavaDoc<TypeMirror> args = new ArrayList JavaDoc<TypeMirror>(typeVariables.length);
79             for (TypeBinding arg : typeVariables) {
80                 args.add(_env.getFactory().newTypeMirror(arg));
81             }
82             return Collections.unmodifiableList(args);
83         }
84         return Collections.emptyList();
85     }
86
87     /* (non-Javadoc)
88      * @see javax.lang.model.type.TypeMirror#accept(javax.lang.model.type.TypeVisitor, java.lang.Object)
89      */

90     @Override JavaDoc
91     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
92         return v.visitDeclared(this, p);
93     }
94
95     @Override JavaDoc
96     public TypeKind getKind() {
97         return TypeKind.DECLARED;
98     }
99
100     @Override JavaDoc
101     public String JavaDoc toString() {
102         return new String JavaDoc(_binding.readableName());
103     }
104
105 }
106
Popular Tags