KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.jdt.apt.core.internal.declaration;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jdt.apt.core.internal.declaration.EclipseMirrorObject.MirrorKind;
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.core.dom.IMethodBinding;
22 import org.eclipse.jdt.core.dom.ITypeBinding;
23 import org.eclipse.jdt.core.dom.Name;
24 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
25 import org.eclipse.jdt.core.dom.TypeParameter;
26
27 import com.sun.mirror.declaration.ParameterDeclaration;
28 import com.sun.mirror.declaration.TypeDeclaration;
29 import com.sun.mirror.declaration.TypeParameterDeclaration;
30 import com.sun.mirror.type.ReferenceType;
31
32 class ExecutableUtil {
33
34     /**
35      * @param executable must be a constructor, method or annotation element.
36      * @return the formal type parameters of the executable.
37      */

38     static Collection JavaDoc<TypeParameterDeclaration> getFormalTypeParameters(
39             EclipseDeclarationImpl executable,
40             BaseProcessorEnv env)
41     {
42         // the dom ast does not provide type parameter list for annotation element
43
// that incorrectly includes them in the text
44
if(executable == null || executable.kind() == MirrorKind.ANNOTATION_ELEMENT)
45             return Collections.emptyList();
46         if( executable.kind() != MirrorKind.METHOD && executable.kind() != MirrorKind.CONSTRUCTOR)
47             throw new IllegalArgumentException JavaDoc("Executable is not a method " + //$NON-NLS-1$
48
executable.getClass().getName());
49         
50         if( executable.isFromSource() ){
51             final org.eclipse.jdt.core.dom.MethodDeclaration methodAstNode =
52                 (org.eclipse.jdt.core.dom.MethodDeclaration)executable.getAstNode();
53             
54             // Synthetic methods will have no ast node
55
if (methodAstNode == null)
56                 return Collections.emptyList();
57             @SuppressWarnings JavaDoc("unchecked")
58             final List JavaDoc<TypeParameter> typeParams = methodAstNode.typeParameters();
59             final List JavaDoc<TypeParameterDeclaration> result = new ArrayList JavaDoc<TypeParameterDeclaration>();
60             for(TypeParameter typeParam : typeParams){
61                 final ITypeBinding typeBinding = typeParam.resolveBinding();
62                 if( typeBinding == null ){
63                     throw new UnsupportedOperationException JavaDoc("cannot create a type parameter declaration without a binding"); //$NON-NLS-1$
64
}
65                 else{
66                     final TypeParameterDeclaration typeParamDecl =
67                         (TypeParameterDeclaration)Factory.createDeclaration(typeBinding, env);
68                     if( typeParamDecl != null )
69                         result.add(typeParamDecl);
70                 }
71             }
72             return result;
73         }
74         else{ // binary
75
if( !executable.isBindingBased() )
76                 throw new IllegalStateException JavaDoc("binary executable without binding."); //$NON-NLS-1$
77
final IMethodBinding methodBinding = ((ExecutableDeclarationImpl)executable).getDeclarationBinding();
78                 final ITypeBinding[] typeParams = methodBinding.getTypeParameters();
79                 if( typeParams == null || typeParams.length == 0 )
80                     return Collections.emptyList();
81                 final List JavaDoc<TypeParameterDeclaration> result = new ArrayList JavaDoc<TypeParameterDeclaration>();
82                 for( ITypeBinding typeVar : typeParams ){
83                     final TypeParameterDeclaration typeParamDecl =
84                         (TypeParameterDeclaration)Factory.createDeclaration(typeVar, env);
85                     if( typeParamDecl != null )
86                         result.add(typeParamDecl);
87                 }
88                 return result;
89             
90         }
91     }
92     
93     /**
94      * @param executable must be a constructor, method or annotation element.
95      * @return the list formal parameters of the executable.
96      */

97     static Collection JavaDoc<ParameterDeclaration> getParameters(
98             final EclipseDeclarationImpl executable,
99             final BaseProcessorEnv env)
100     {
101         // the dom ast does not provide parameter list for annotation element
102
// that incorrectly includes them in the text
103
if(executable == null || executable.kind() == MirrorKind.ANNOTATION_ELEMENT)
104             return Collections.emptyList();
105         if( executable.kind() != MirrorKind.METHOD && executable.kind() != MirrorKind.CONSTRUCTOR)
106             throw new IllegalArgumentException JavaDoc("Executable is not a method " + //$NON-NLS-1$
107
executable.getClass().getName());
108         
109         if( executable.isFromSource() ){
110             // We always need to look into the ast to make sure the complete list of
111
// parameters are returned since parameters with unresolved type will not
112
// show up in the method binding
113
final org.eclipse.jdt.core.dom.MethodDeclaration methodAstNode =
114                 (org.eclipse.jdt.core.dom.MethodDeclaration)executable.getAstNode();
115             
116             // Synthetic methods will have no ast node
117
if (methodAstNode == null)
118                 return Collections.emptyList();
119             
120             @SuppressWarnings JavaDoc("unchecked")
121             final List JavaDoc<SingleVariableDeclaration> params = methodAstNode.parameters();
122             if( params == null || params.size() == 0 )
123                 return Collections.emptyList();
124             final List JavaDoc<ParameterDeclaration> result = new ArrayList JavaDoc<ParameterDeclaration>(params.size());
125             for( int i=0, size=params.size(); i<size; i++ ){
126                 final SingleVariableDeclaration varDecl = params.get(i);
127                 final ParameterDeclaration param =
128                     Factory.createParameterDeclaration(varDecl, executable.getResource(), env);
129                 result.add(param);
130             }
131             return result;
132         }
133         else{
134             if( !executable.isBindingBased() )
135                 throw new IllegalStateException JavaDoc("binary executable without binding."); //$NON-NLS-1$
136
// it is binary, since we don't support the class file format, will rely on the
137
// binding and hope that it's complete.
138
final ExecutableDeclarationImpl impl = (ExecutableDeclarationImpl)executable;
139             final IMethodBinding methodBinding = impl.getDeclarationBinding();
140             final ITypeBinding[] paramTypes = methodBinding.getParameterTypes();
141             if( paramTypes == null || paramTypes.length == 0 )
142                 return Collections.emptyList();
143             final List JavaDoc<ParameterDeclaration> result = new ArrayList JavaDoc<ParameterDeclaration>(paramTypes.length);
144             
145             for( int i=0; i<paramTypes.length; i++ ){
146                 final ITypeBinding type = paramTypes[i];
147                 final ParameterDeclaration param = Factory.createParameterDeclaration(impl, i, type, env);
148                 result.add(param);
149             }
150
151             return result;
152             
153         }
154     }
155     
156     /**
157      * @param executable must be a constructor, method or annotation element.
158      * @return the list thrown types of the executable.
159      */

160     static Collection JavaDoc<ReferenceType> getThrownTypes(
161             final EclipseDeclarationImpl executable,
162             final BaseProcessorEnv env)
163     {
164         if(executable == null || executable.kind() == MirrorKind.ANNOTATION_ELEMENT)
165             return Collections.emptyList();
166         if( executable.kind() != MirrorKind.METHOD && executable.kind() != MirrorKind.CONSTRUCTOR)
167             throw new IllegalArgumentException JavaDoc("Executable is not a method " + //$NON-NLS-1$
168
executable.getClass().getName());
169         if( executable.isFromSource()){
170             // We always need to look into the ast to make sure the complete list of
171
// parameters are returned since parameters with unresolved type will not
172
// show up in the method binding
173
final org.eclipse.jdt.core.dom.MethodDeclaration methodAstNode =
174                 (org.eclipse.jdt.core.dom.MethodDeclaration)executable.getAstNode();
175             
176             // If this method is synthetic, there will be no AST node
177
if (methodAstNode == null)
178                 return Collections.emptyList();
179             
180             @SuppressWarnings JavaDoc("unchecked")
181             final List JavaDoc<Name> exceptions = methodAstNode.thrownExceptions();
182             if(exceptions == null || exceptions.size() == 0 )
183                 return Collections.emptyList();
184             final List JavaDoc<ReferenceType> results = new ArrayList JavaDoc<ReferenceType>(4);
185             for(Name exception : exceptions ){
186                 final ITypeBinding eType = exception.resolveTypeBinding();
187                 final ReferenceType refType;
188                 if( eType == null )
189                     refType = Factory.createErrorClassType(exception.toString());
190                 else
191                     refType = Factory.createReferenceType(eType, env);
192                 results.add(refType);
193             }
194             
195             return results;
196         }
197         else{
198             if( !executable.isBindingBased() )
199                 throw new IllegalStateException JavaDoc("binary executable without binding."); //$NON-NLS-1$
200
final ExecutableDeclarationImpl impl = (ExecutableDeclarationImpl)executable;
201             final IMethodBinding methodBinding = impl.getDeclarationBinding();
202             final ITypeBinding[] exceptions = methodBinding.getExceptionTypes();
203             final List JavaDoc<ReferenceType> results = new ArrayList JavaDoc<ReferenceType>(4);
204             for( ITypeBinding exception : exceptions ){
205                 final TypeDeclaration mirrorDecl = Factory.createReferenceType(exception, env);
206                 if( mirrorDecl != null)
207                     results.add((ReferenceType)mirrorDecl);
208             }
209             return results;
210         }
211     }
212 }
213
Popular Tags