1 11 12 package org.eclipse.jdt.apt.core.internal.declaration; 13 14 import java.lang.annotation.Annotation ; 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 import java.util.Collections ; 18 import java.util.List ; 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 private final boolean _hideSourcePosition; 48 49 52 private final TypeDeclarationImpl _typeDecl; 53 54 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 > A getAnnotation(Class <A> anno) 91 { 92 return _getAnnotation(anno, getPackageBinding().getAnnotations()); 93 } 94 95 public Collection <AnnotationMirror> getAnnotationMirrors() 96 { 97 return _getAnnotationMirrors(getPackageBinding().getAnnotations()); 98 } 99 100 public Collection <AnnotationTypeDeclaration> getAnnotationTypes() 101 { 102 return Collections.emptyList(); 104 } 105 106 public Collection <ClassDeclaration> getClasses() { 107 initFragments(); 108 List <IType> types = getTypesInPackage(_pkgFragments); 109 List <ClassDeclaration> classes = new ArrayList <ClassDeclaration>(); 110 for (IType type : types) { 111 try { 112 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) {} } 122 123 return classes; 124 } 125 126 public Collection <EnumDeclaration> getEnums() { 127 initFragments(); 128 List <IType> types = getTypesInPackage(_pkgFragments); 129 List <EnumDeclaration> enums = new ArrayList <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) {} } 138 139 return enums; 140 } 141 142 public Collection <InterfaceDeclaration> getInterfaces() { 143 initFragments(); 144 List <IType> types = getTypesInPackage(_pkgFragments); 145 List <InterfaceDeclaration> interfaces = new ArrayList <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) {} } 154 155 return interfaces; 156 } 157 158 public String getDocComment() 159 { 160 return null; 161 } 162 163 public Collection <Modifier> getModifiers() 164 { 165 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 getQualifiedName() 189 { 190 return getPackageBinding().getName(); 191 } 192 193 public String getSimpleName() 194 { 195 IPackageBinding pkg = getPackageBinding(); 196 final String [] components = pkg.getNameComponents(); 197 if( components == null || components.length == 0 ) return ""; return components[components.length - 1]; 199 } 200 201 public MirrorKind kind(){ return MirrorKind.PACKAGE; } 202 203 public String toString(){ return getQualifiedName(); } 204 205 public IPackageBinding getDeclarationBinding(){ return (IPackageBinding)_binding; } 206 207 public boolean isFromSource(){ return _typeDecl.isFromSource(); } 208 209 215 private void initFragments() { 216 if (null == _pkgFragments) { 217 _pkgFragments = PackageUtil.getPackageFragments(_binding.getName(), _env); 218 } 219 } 220 221 private static List <IType> getTypesInPackage(final IPackageFragment[] fragments) { 222 List <IType> types = new ArrayList <IType>(); 223 try { 224 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 } 244 return types; 245 } 246 247 } 248 | Popular Tags |