KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.annotation.Annotation JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
21 import org.eclipse.jdt.apt.core.internal.util.PackageUtil;
22 import org.eclipse.jdt.apt.core.internal.util.SourcePositionImpl;
23 import org.eclipse.jdt.core.IClassFile;
24 import org.eclipse.jdt.core.ICompilationUnit;
25 import org.eclipse.jdt.core.IPackageFragment;
26 import org.eclipse.jdt.core.IType;
27 import org.eclipse.jdt.core.JavaModelException;
28 import org.eclipse.jdt.core.dom.ASTNode;
29 import org.eclipse.jdt.core.dom.CompilationUnit;
30 import org.eclipse.jdt.core.dom.IPackageBinding;
31
32 import com.sun.mirror.declaration.AnnotationMirror;
33 import com.sun.mirror.declaration.AnnotationTypeDeclaration;
34 import com.sun.mirror.declaration.ClassDeclaration;
35 import com.sun.mirror.declaration.EnumDeclaration;
36 import com.sun.mirror.declaration.InterfaceDeclaration;
37 import com.sun.mirror.declaration.Modifier;
38 import com.sun.mirror.declaration.PackageDeclaration;
39 import com.sun.mirror.declaration.TypeDeclaration;
40 import com.sun.mirror.util.DeclarationVisitor;
41 import com.sun.mirror.util.SourcePosition;
42
43 public class PackageDeclarationImpl extends DeclarationImpl implements PackageDeclaration
44 {
45     // If this package came from directly requesting it via the environment,
46
// need to hide the source position, as this is an artifact of our implementation
47
private final boolean _hideSourcePosition;
48     
49     /** The back-pointer to the type declaration that created this package declaration
50      * @see TypeDeclarationImpl#getPackage()
51      */

52     private final TypeDeclarationImpl _typeDecl;
53     
54     // Lazily initialized unless specified in constructor.
55
private IPackageFragment[] _pkgFragments = null;
56     
57     public PackageDeclarationImpl(
58             final IPackageBinding binding,
59             final TypeDeclarationImpl typeDecl,
60             final BaseProcessorEnv env,
61             final boolean hideSourcePosition)
62     {
63         this(binding,
64              typeDecl,
65              env,
66              hideSourcePosition,
67              null);
68     }
69     
70     public PackageDeclarationImpl(
71             final IPackageBinding binding,
72             final TypeDeclarationImpl typeDecl,
73             final BaseProcessorEnv env,
74             final boolean hideSourcePosition,
75             final IPackageFragment[] pkgFragments)
76     {
77         super(binding, env);
78         _typeDecl = typeDecl;
79         _hideSourcePosition = hideSourcePosition;
80         _pkgFragments = pkgFragments;
81     }
82
83     public IPackageBinding getPackageBinding(){ return (IPackageBinding)_binding; }
84
85     public void accept(DeclarationVisitor visitor)
86     {
87         visitor.visitPackageDeclaration(this);
88     }
89     
90     public <A extends Annotation JavaDoc> A getAnnotation(Class JavaDoc<A> anno)
91     {
92         return _getAnnotation(anno, getPackageBinding().getAnnotations());
93     }
94
95     public Collection JavaDoc<AnnotationMirror> getAnnotationMirrors()
96     {
97         return _getAnnotationMirrors(getPackageBinding().getAnnotations());
98     }
99
100     public Collection JavaDoc<AnnotationTypeDeclaration> getAnnotationTypes()
101     {
102         // jdt currently have no support for package declaration.
103
return Collections.emptyList();
104     }
105
106     public Collection JavaDoc<ClassDeclaration> getClasses() {
107         initFragments();
108         List JavaDoc<IType> types = getTypesInPackage(_pkgFragments);
109         List JavaDoc<ClassDeclaration> classes = new ArrayList JavaDoc<ClassDeclaration>();
110         for (IType type : types) {
111             try {
112                 // isClass() will return true if TypeDeclaration is an InterfaceDeclaration
113
if (type.isClass()) {
114                     TypeDeclaration td = _env.getTypeDeclaration( type );
115                     if ( td instanceof ClassDeclaration ) {
116                         classes.add((ClassDeclaration)td);
117                     }
118                 }
119             }
120             catch (JavaModelException ex) {} // No longer exists, don't return it
121
}
122         
123         return classes;
124     }
125
126     public Collection JavaDoc<EnumDeclaration> getEnums() {
127         initFragments();
128         List JavaDoc<IType> types = getTypesInPackage(_pkgFragments);
129         List JavaDoc<EnumDeclaration> enums = new ArrayList JavaDoc<EnumDeclaration>();
130         for (IType type : types) {
131             try {
132                 if (type.isEnum()) {
133                     enums.add((EnumDeclaration)_env.getTypeDeclaration(type));
134                 }
135             }
136             catch (JavaModelException ex) {} // No longer exists, don't return it
137
}
138         
139         return enums;
140     }
141
142     public Collection JavaDoc<InterfaceDeclaration> getInterfaces() {
143         initFragments();
144         List JavaDoc<IType> types = getTypesInPackage(_pkgFragments);
145         List JavaDoc<InterfaceDeclaration> interfaces = new ArrayList JavaDoc<InterfaceDeclaration>();
146         for (IType type : types) {
147             try {
148                 if (type.isInterface()) {
149                     interfaces.add((InterfaceDeclaration)_env.getTypeDeclaration(type));
150                 }
151             }
152             catch (JavaModelException ex) {} // No longer exists, don't return it
153
}
154         
155         return interfaces;
156     }
157
158     public String JavaDoc getDocComment()
159     {
160         return null;
161     }
162
163     public Collection JavaDoc<Modifier> getModifiers()
164     {
165         // package doesn't have modifiers.
166
return Collections.emptyList();
167     }
168
169     public SourcePosition getPosition()
170     {
171         if (_hideSourcePosition)
172             return null;
173         if(_typeDecl.isFromSource()){
174             final CompilationUnit unit = _typeDecl.getCompilationUnit();
175             final ASTNode node = unit.findDeclaringNode(getDeclarationBinding());
176             if( node == null ) return null;
177             final int start = node.getStartPosition();
178             return new SourcePositionImpl(start,
179                                           node.getLength(),
180                                           unit.getLineNumber(start),
181                                           unit.getColumnNumber(start),
182                                           this);
183         }
184         return null;
185         
186     }
187
188     public String JavaDoc getQualifiedName()
189     {
190         return getPackageBinding().getName();
191     }
192
193     public String JavaDoc getSimpleName()
194     {
195         IPackageBinding pkg = getPackageBinding();
196         final String JavaDoc[] components = pkg.getNameComponents();
197         if( components == null || components.length == 0 ) return ""; //$NON-NLS-1$
198
return components[components.length - 1];
199     }
200
201     public MirrorKind kind(){ return MirrorKind.PACKAGE; }
202
203     public String JavaDoc toString(){ return getQualifiedName(); }
204     
205     public IPackageBinding getDeclarationBinding(){ return (IPackageBinding)_binding; }
206
207     public boolean isFromSource(){ return _typeDecl.isFromSource(); }
208     
209     /**
210      * Make sure to call this before attempting to access _pkgFragments.
211      * We initialize this field lazily, because it is very expensive to compute and
212      * there are some common questions such as getQualifiedName() that can be
213      * answered without initializing it at all.
214      */

215     private void initFragments() {
216         if (null == _pkgFragments) {
217             _pkgFragments = PackageUtil.getPackageFragments(_binding.getName(), _env);
218         }
219     }
220     
221     private static List JavaDoc<IType> getTypesInPackage(final IPackageFragment[] fragments) {
222         List JavaDoc<IType> types = new ArrayList JavaDoc<IType>();
223         try {
224             // Get all top-level classes -- ignore local, member, and anonymous classes
225
for (IPackageFragment fragment : fragments) {
226                 for (IClassFile classFile : fragment.getClassFiles()) {
227                     IType type = classFile.getType();
228                     if (! (type.isLocal() || type.isMember() || type.isAnonymous()) ) {
229                         types.add(type);
230                     }
231                 }
232                 for (ICompilationUnit compUnit : fragment.getCompilationUnits()) {
233                     for (IType type : compUnit.getTypes()) {
234                         if (! (type.isLocal() || type.isMember() || type.isAnonymous()) ) {
235                             types.add(type);
236                         }
237                     }
238                 }
239             }
240         }
241         catch (JavaModelException jme) {
242             // Ignore -- project is in a bad state. This will get recalled if necessary
243
}
244         return types;
245     }
246    
247 }
248
Popular Tags