001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 BEA Systems, Inc.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * tyeung@bea.com - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.apt.core.internal.declaration;
011:
012: import java.lang.annotation.Annotation;
013: import java.util.ArrayList;
014: import java.util.Collection;
015: import java.util.Collections;
016:
017: import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
018: import org.eclipse.jdt.apt.core.internal.util.Factory;
019: import org.eclipse.jdt.apt.core.internal.util.SourcePositionImpl;
020: import org.eclipse.jdt.core.dom.ASTNode;
021: import org.eclipse.jdt.core.dom.CompilationUnit;
022: import org.eclipse.jdt.core.dom.IBinding;
023: import org.eclipse.jdt.core.dom.ITypeBinding;
024:
025: import com.sun.mirror.declaration.AnnotationMirror;
026: import com.sun.mirror.declaration.Declaration;
027: import com.sun.mirror.declaration.Modifier;
028: import com.sun.mirror.declaration.TypeParameterDeclaration;
029: import com.sun.mirror.type.ReferenceType;
030: import com.sun.mirror.type.TypeVariable;
031: import com.sun.mirror.util.DeclarationVisitor;
032: import com.sun.mirror.util.SourcePosition;
033: import com.sun.mirror.util.TypeVisitor;
034:
035: public class TypeParameterDeclarationImpl extends DeclarationImpl
036: implements TypeParameterDeclaration, TypeVariable,
037: EclipseMirrorType {
038: public TypeParameterDeclarationImpl(final ITypeBinding binding,
039: final BaseProcessorEnv env) {
040: super (binding, env);
041: assert binding.isTypeVariable();
042: }
043:
044: public void accept(DeclarationVisitor visitor) {
045: visitor.visitTypeParameterDeclaration(this );
046: }
047:
048: public <A extends Annotation> A getAnnotation(
049: Class<A> annotationClass) {
050: return null;
051: }
052:
053: public Collection<AnnotationMirror> getAnnotationMirrors() {
054: return Collections.emptyList();
055: }
056:
057: public Collection<ReferenceType> getBounds() {
058: final ITypeBinding[] bounds = getDeclarationBinding()
059: .getTypeBounds();
060: if (bounds == null || bounds.length == 0)
061: return Collections.emptyList();
062:
063: final Collection<ReferenceType> result = new ArrayList<ReferenceType>(
064: 4);
065: for (ITypeBinding bound : bounds) {
066: final ReferenceType type = Factory.createReferenceType(
067: bound, _env);
068: if (type != null)
069: result.add(type);
070: }
071:
072: return result;
073: }
074:
075: public String getDocComment() {
076: return null;
077: }
078:
079: public Collection<Modifier> getModifiers() {
080: return Collections.emptyList();
081: }
082:
083: public Declaration getOwner() {
084: return Factory.createDeclaration(getOwnerBinding(), _env);
085: }
086:
087: private IBinding getOwnerBinding() {
088: final ITypeBinding binding = getDeclarationBinding();
089: // declared on a class
090: IBinding owner = binding.getDeclaringClass();
091: if (owner == null)
092: // declared on the method
093: owner = binding.getDeclaringMethod();
094: return owner;
095: }
096:
097: public SourcePosition getPosition() {
098: if (isFromSource()) {
099: final ASTNode node = getAstNode();
100: if (node == null)
101: return null;
102: final CompilationUnit unit = getCompilationUnit();
103: final int offset = node.getStartPosition();
104: return new SourcePositionImpl(offset, node.getLength(),
105: unit.getLineNumber(offset), unit
106: .getColumnNumber(offset), this );
107: } else
108: return null;
109: }
110:
111: public String getSimpleName() {
112: final ITypeBinding typeVar = getDeclarationBinding();
113: return typeVar.getName();
114: }
115:
116: // Start of implementation of TypeVariable API
117: public void accept(TypeVisitor visitor) {
118: visitor.visitTypeVariable(this );
119: }
120:
121: public TypeParameterDeclaration getDeclaration() {
122: return this ;
123: }
124:
125: // End of implementation of TypeVariable API
126:
127: public String toString() {
128: return getSimpleName();
129: }
130:
131: public MirrorKind kind() {
132: return MirrorKind.TYPE_PARAMETER_VARIABLE;
133: }
134:
135: public ITypeBinding getDeclarationBinding() {
136: return (ITypeBinding) _binding;
137: }
138:
139: public ITypeBinding getTypeBinding() {
140: return (ITypeBinding) _binding;
141: }
142:
143: public boolean isFromSource() {
144: return getDeclarationBinding().isFromSource();
145: }
146:
147: public boolean isAssignmentCompatible(EclipseMirrorType left) {
148: return isSubTypeCompatible(left);
149: }
150:
151: public boolean isSubTypeCompatible(EclipseMirrorType type) {
152: if (type.kind() == MirrorKind.TYPE_PARAMETER_VARIABLE) {
153: TypeParameterDeclarationImpl other = (TypeParameterDeclarationImpl) type;
154: return getOwnerBinding() == other.getOwnerBinding()
155: && getSimpleName().equals(other.getSimpleName());
156: }
157:
158: for (ReferenceType bound : getBounds()) {
159: if (((EclipseMirrorType) bound).isSubTypeCompatible(type))
160: return true;
161: }
162:
163: return false;
164: }
165: }
|