1 11 package org.eclipse.jdt.apt.core.internal.declaration; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Collections ; 16 import java.util.List ; 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 38 static Collection <TypeParameterDeclaration> getFormalTypeParameters( 39 EclipseDeclarationImpl executable, 40 BaseProcessorEnv env) 41 { 42 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 ("Executable is not a method " + 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 if (methodAstNode == null) 56 return Collections.emptyList(); 57 @SuppressWarnings ("unchecked") 58 final List <TypeParameter> typeParams = methodAstNode.typeParameters(); 59 final List <TypeParameterDeclaration> result = new ArrayList <TypeParameterDeclaration>(); 60 for(TypeParameter typeParam : typeParams){ 61 final ITypeBinding typeBinding = typeParam.resolveBinding(); 62 if( typeBinding == null ){ 63 throw new UnsupportedOperationException ("cannot create a type parameter declaration without a binding"); } 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{ if( !executable.isBindingBased() ) 76 throw new IllegalStateException ("binary executable without binding."); 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 <TypeParameterDeclaration> result = new ArrayList <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 97 static Collection <ParameterDeclaration> getParameters( 98 final EclipseDeclarationImpl executable, 99 final BaseProcessorEnv env) 100 { 101 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 ("Executable is not a method " + executable.getClass().getName()); 108 109 if( executable.isFromSource() ){ 110 final org.eclipse.jdt.core.dom.MethodDeclaration methodAstNode = 114 (org.eclipse.jdt.core.dom.MethodDeclaration)executable.getAstNode(); 115 116 if (methodAstNode == null) 118 return Collections.emptyList(); 119 120 @SuppressWarnings ("unchecked") 121 final List <SingleVariableDeclaration> params = methodAstNode.parameters(); 122 if( params == null || params.size() == 0 ) 123 return Collections.emptyList(); 124 final List <ParameterDeclaration> result = new ArrayList <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 ("binary executable without binding."); 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 <ParameterDeclaration> result = new ArrayList <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 160 static Collection <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 ("Executable is not a method " + executable.getClass().getName()); 169 if( executable.isFromSource()){ 170 final org.eclipse.jdt.core.dom.MethodDeclaration methodAstNode = 174 (org.eclipse.jdt.core.dom.MethodDeclaration)executable.getAstNode(); 175 176 if (methodAstNode == null) 178 return Collections.emptyList(); 179 180 @SuppressWarnings ("unchecked") 181 final List <Name> exceptions = methodAstNode.thrownExceptions(); 182 if(exceptions == null || exceptions.size() == 0 ) 183 return Collections.emptyList(); 184 final List <ReferenceType> results = new ArrayList <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 ("binary executable without binding."); final ExecutableDeclarationImpl impl = (ExecutableDeclarationImpl)executable; 201 final IMethodBinding methodBinding = impl.getDeclarationBinding(); 202 final ITypeBinding[] exceptions = methodBinding.getExceptionTypes(); 203 final List <ReferenceType> results = new ArrayList <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 |