KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.mirror.declaration.AnnotationMirror;
15 import com.sun.mirror.declaration.Modifier;
16 import com.sun.mirror.declaration.ParameterDeclaration;
17 import com.sun.mirror.type.TypeMirror;
18 import com.sun.mirror.util.DeclarationVisitor;
19 import com.sun.mirror.util.SourcePosition;
20
21 import java.lang.annotation.Annotation JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
27 import org.eclipse.jdt.apt.core.internal.util.Factory;
28 import org.eclipse.jdt.core.dom.ASTNode;
29 import org.eclipse.jdt.core.dom.IBinding;
30 import org.eclipse.jdt.core.dom.IMethodBinding;
31 import org.eclipse.jdt.core.dom.IAnnotationBinding;
32 import org.eclipse.jdt.core.dom.ITypeBinding;
33 import org.eclipse.jdt.core.dom.SimpleName;
34 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
35
36 /**
37  * Represents a formal parameter that came from binary.
38  */

39 public class BinaryParameterDeclarationImpl extends DeclarationImpl implements ParameterDeclaration
40 {
41     static final String JavaDoc ARG = "arg"; //$NON-NLS-1$
42
private final ITypeBinding _type;
43     private final ExecutableDeclarationImpl _executable;
44     private final int _paramIndex;
45     /**
46      * Parameter declaration from binary
47      */

48     public BinaryParameterDeclarationImpl(ExecutableDeclarationImpl executable,
49                                           ITypeBinding typeBinding,
50                                           int index,
51                                           BaseProcessorEnv env)
52     {
53         super(typeBinding, env);
54         assert( typeBinding != null ) : "missing type binding"; //$NON-NLS-1$
55
assert( index >= 0 ) : "invalid index " + index; //$NON-NLS-1$
56
assert executable != null : "missing executable"; //$NON-NLS-1$
57
_type = typeBinding;
58         _paramIndex = index;
59         _executable = executable;
60     }
61     
62     public void accept(DeclarationVisitor visitor)
63     {
64         visitor.visitParameterDeclaration(this);
65     }
66  
67     public Collection JavaDoc<Modifier> getModifiers()
68     {
69         // TODO
70
// we don't store this information. so simply return nothing for now.
71
return Collections.emptyList();
72     }
73     
74     public String JavaDoc getDocComment()
75     {
76         return null;
77     }
78     
79     public String JavaDoc getSimpleName()
80     {
81         final SingleVariableDeclaration decl = (SingleVariableDeclaration)getAstNode();
82         if( decl == null ) return ARG + _paramIndex;
83         final SimpleName name = decl.getName();
84         return name == null ? ARG : name.toString();
85     }
86
87     public SourcePosition getPosition()
88     {
89         return null;
90     }
91     
92     public TypeMirror getType()
93     {
94         final TypeMirror mirrorType = Factory.createTypeMirror(getTypeBinding(), _env);
95         if( mirrorType == null )
96             return Factory.createErrorClassType(getTypeBinding());
97         return mirrorType;
98     }
99     
100     public <A extends Annotation JavaDoc> A getAnnotation(Class JavaDoc<A> annotationClass)
101     {
102         final IMethodBinding methodBinding = _executable.getDeclarationBinding();
103         final IAnnotationBinding[] paramAnnos = methodBinding.getParameterAnnotations(_paramIndex);
104         return _getAnnotation(annotationClass, paramAnnos);
105     }
106
107     public Collection JavaDoc<AnnotationMirror> getAnnotationMirrors()
108     {
109         final IMethodBinding methodBinding = _executable.getDeclarationBinding();
110         final IAnnotationBinding[] paramAnnos = methodBinding.getParameterAnnotations(_paramIndex);
111         return _getAnnotationMirrors(paramAnnos);
112     }
113     
114     public boolean isBindingBased(){ return true; }
115     
116     public boolean isFromSource(){ return false; }
117
118     ASTNode getAstNode(){ return null; }
119
120     public IFile getResource(){ return null; }
121     
122     private ITypeBinding getTypeBinding(){ return _type; }
123     
124     public MirrorKind kind(){ return MirrorKind.FORMAL_PARAMETER; }
125     
126     public IBinding getDeclarationBinding(){
127         throw new UnsupportedOperationException JavaDoc("should never be invoked on a BinaryParameterDeclaration"); //$NON-NLS-1$
128
}
129     
130     public boolean equals(Object JavaDoc obj){
131         if( obj instanceof BinaryParameterDeclarationImpl ){
132             final BinaryParameterDeclarationImpl otherParam = (BinaryParameterDeclarationImpl)obj;
133             return otherParam._paramIndex == _paramIndex &&
134                    otherParam._executable.getDeclarationBinding().isEqualTo(_executable.getDeclarationBinding()) ;
135         }
136         return false;
137     }
138     
139     public int hashCode(){
140         final String JavaDoc methodKey = _executable.getDeclarationBinding().getKey();
141         int hashcode = 0;
142         if( methodKey != null )
143             hashcode = methodKey.hashCode();
144         return hashcode + _paramIndex;
145     }
146     
147     public String JavaDoc toString(){
148         final StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
149         builder.append(getTypeBinding().getName());
150         builder.append(' ');
151         builder.append(getSimpleName());
152         return builder.toString();
153     }
154 }
155
Popular Tags