1 11 12 package org.eclipse.jdt.apt.core.internal.declaration; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IFile; 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.ASTNode; 22 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 23 import org.eclipse.jdt.core.dom.BodyDeclaration; 24 import org.eclipse.jdt.core.dom.IMethodBinding; 25 import org.eclipse.jdt.core.dom.ITypeBinding; 26 27 import com.sun.mirror.declaration.ClassDeclaration; 28 import com.sun.mirror.declaration.ConstructorDeclaration; 29 import com.sun.mirror.declaration.Declaration; 30 import com.sun.mirror.declaration.MethodDeclaration; 31 import com.sun.mirror.type.ClassType; 32 import com.sun.mirror.util.DeclarationVisitor; 33 import com.sun.mirror.util.TypeVisitor; 34 35 public class ClassDeclarationImpl extends TypeDeclarationImpl implements ClassDeclaration, ClassType 36 { 37 public ClassDeclarationImpl(final ITypeBinding binding, final BaseProcessorEnv env) 38 { 39 super(binding, env); 40 assert !binding.isInterface(); 42 } 43 44 public void accept(DeclarationVisitor visitor) 45 { 46 visitor.visitClassDeclaration(this); 47 } 48 49 @SuppressWarnings ("unchecked") 50 private void getASTConstructor( 51 final AbstractTypeDeclaration typeDecl, 52 final List <ConstructorDeclaration> results){ 53 54 final List bodyDecls = typeDecl.bodyDeclarations(); 55 IFile file = null; 56 for( int i=0, len=bodyDecls.size(); i<len; i++ ){ 57 final BodyDeclaration bodyDecl = (BodyDeclaration)bodyDecls.get(i); 58 if( bodyDecl.getNodeType() == ASTNode.METHOD_DECLARATION ){ 59 final org.eclipse.jdt.core.dom.MethodDeclaration methodDecl = 60 (org.eclipse.jdt.core.dom.MethodDeclaration)bodyDecl; 61 62 if( methodDecl.isConstructor() ){ 63 final IMethodBinding methodBinding = methodDecl.resolveBinding(); 64 if( methodBinding == null ){ 66 if( file == null ) 67 file = getResource(); 68 ConstructorDeclaration mirrorDecl = 69 (ConstructorDeclaration)Factory.createDeclaration(methodDecl, file, _env); 70 if( mirrorDecl != null ) 71 results.add(mirrorDecl); 72 } 73 } 74 } 75 } 76 } 77 78 public Collection <ConstructorDeclaration> getConstructors() 79 { 80 final List <ConstructorDeclaration> results = new ArrayList <ConstructorDeclaration>(); 81 if( isFromSource() ){ 82 final ITypeBinding typeBinding = getDeclarationBinding(); 85 final ASTNode node = 86 _env.getASTNodeForBinding(typeBinding); 87 if( node != null ){ 88 switch( node.getNodeType() ) 89 { 90 case ASTNode.TYPE_DECLARATION: 91 case ASTNode.ANNOTATION_TYPE_DECLARATION: 92 case ASTNode.ENUM_DECLARATION: 93 AbstractTypeDeclaration typeDecl = 94 (AbstractTypeDeclaration)node; 95 getASTConstructor(typeDecl, results); 97 break; 98 default: 99 throw new IllegalStateException ("expecting a AbstractTypeDeclaration but got " + node.getClass().getName() ); 102 } 103 } 104 } 105 108 final IMethodBinding[] methods = getDeclarationBinding().getDeclaredMethods(); 109 for( IMethodBinding method : methods ){ 110 if( method.isSynthetic() ) continue; 111 if( method.isConstructor() ){ 112 Declaration mirrorDecl = Factory.createDeclaration(method, _env); 113 if( mirrorDecl != null) 114 results.add((ConstructorDeclaration)mirrorDecl); 115 } 116 } 117 return results; 118 119 } 120 121 @SuppressWarnings ("unchecked") 122 public Collection <MethodDeclaration> getMethods() 123 { 124 return (Collection <MethodDeclaration>)_getMethods(); 125 } 126 127 public void accept(TypeVisitor visitor) 129 { 130 visitor.visitClassType(this); 131 } 132 133 public ClassType getSuperclass() 134 { 135 final ITypeBinding superClass = getDeclarationBinding().getSuperclass(); 136 if ( superClass == null ) 137 return null; 138 else if( superClass.isClass() ) 139 return (ClassType)Factory.createReferenceType(superClass, _env); 140 else return Factory.createErrorClassType(superClass); 142 } 143 144 public ClassDeclaration getDeclaration() 145 { 146 return (ClassDeclaration)super.getDeclaration(); 147 } 148 150 public MirrorKind kind(){ return MirrorKind.TYPE_CLASS; } 151 } 152 | Popular Tags |