KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.resources.IFile;
14 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
15 import org.eclipse.jdt.apt.core.internal.util.Factory;
16 import org.eclipse.jdt.core.dom.ASTNode;
17 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
18 import org.eclipse.jdt.core.dom.BodyDeclaration;
19 import org.eclipse.jdt.core.dom.ITypeBinding;
20 import com.sun.mirror.declaration.MemberDeclaration;
21 import com.sun.mirror.declaration.TypeDeclaration;
22 import com.sun.mirror.util.DeclarationVisitor;
23
24 public abstract class ASTBasedMemberDeclarationImpl
25     extends ASTBasedDeclarationImpl implements MemberDeclaration{
26     
27     public ASTBasedMemberDeclarationImpl(
28             ASTNode astNode,
29             IFile file,
30             BaseProcessorEnv env)
31     {
32         super(astNode, file, env);
33     }
34     
35     public void accept(DeclarationVisitor visitor) {
36         visitor.visitMemberDeclaration(this);
37     }
38     
39     public TypeDeclaration getDeclaringType()
40     {
41         final AbstractTypeDeclaration parentType = getContainingTypeAstNode();
42         // most likely a mal-formed text.
43
if( parentType == null )
44             return null;
45         
46         final ITypeBinding parentTypeBinding = parentType.resolveBinding();
47         if( parentTypeBinding == null )
48             throw new UnsupportedOperationException JavaDoc("Type declaration that doesn't have binding"); //$NON-NLS-1$
49
return Factory.createReferenceType(parentTypeBinding, _env);
50     }
51     
52     public String JavaDoc getDocComment()
53     {
54         final ASTNode node = getAstNode();
55         
56         if( node instanceof BodyDeclaration )
57             return getDocComment((BodyDeclaration)node);
58
59         else if( node.getNodeType() == ASTNode.VARIABLE_DECLARATION_FRAGMENT ){
60             final ASTNode parent = node.getParent();
61             // a field declaration
62
if( parent instanceof BodyDeclaration )
63                 return getDocComment((BodyDeclaration)parent);
64             
65         }
66         return EMPTY_STRING;
67     }
68     
69     /**
70      * @return the closest ancestor to the ast node in this instance that
71      * is a type declaration node or <code>null</code> if none is found.
72      */

73     protected AbstractTypeDeclaration getContainingTypeAstNode()
74     {
75         ASTNode cur = _astNode;
76         while(cur != null){
77             switch(cur.getNodeType()){
78             case ASTNode.ANNOTATION_TYPE_DECLARATION:
79             case ASTNode.ENUM_DECLARATION:
80             case ASTNode.TYPE_DECLARATION:
81                 return (AbstractTypeDeclaration)cur;
82             }
83             cur = cur.getParent();
84         }
85         return null;
86     }
87 }
88
Popular Tags