KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.List JavaDoc;
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         // Enum types return false for isClass().
41
assert !binding.isInterface();
42     }
43
44     public void accept(DeclarationVisitor visitor)
45     {
46         visitor.visitClassDeclaration(this);
47     }
48     
49     @SuppressWarnings JavaDoc("unchecked")
50     private void getASTConstructor(
51             final AbstractTypeDeclaration typeDecl,
52             final List JavaDoc<ConstructorDeclaration> results){
53         
54         final List JavaDoc 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                     // built an ast based representation.
65
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 JavaDoc<ConstructorDeclaration> getConstructors()
79     {
80         final List JavaDoc<ConstructorDeclaration> results = new ArrayList JavaDoc<ConstructorDeclaration>();
81         if( isFromSource() ){
82             // need to consult the ast since methods with broken signature
83
// do not appear in bindings.
84
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                     // built the ast based methods first.
96
getASTConstructor(typeDecl, results);
97                     break;
98                 default:
99                     // the ast node for a type binding should be a AbstractTypeDeclaration.
100
throw new IllegalStateException JavaDoc("expecting a AbstractTypeDeclaration but got " //$NON-NLS-1$
101
+ node.getClass().getName() );
102                 }
103             }
104         }
105         // build methods for binding type or
106
// build the binding based method for source type.
107

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 JavaDoc("unchecked")
122     public Collection JavaDoc<MethodDeclaration> getMethods()
123     {
124         return (Collection JavaDoc<MethodDeclaration>)_getMethods();
125     }
126
127     // Start of implementation of ClassType API
128
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 // catch error case where user extends some interface instead of a class.
141
return Factory.createErrorClassType(superClass);
142     }
143
144     public ClassDeclaration getDeclaration()
145     {
146         return (ClassDeclaration)super.getDeclaration();
147     }
148     // End of implementation of ClassType API
149

150     public MirrorKind kind(){ return MirrorKind.TYPE_CLASS; }
151 }
152
Popular Tags