Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1d20860dc59979f790b32cd8a55ecbb4d8d8c693 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*******************************************************************************
 * Copyright (c) 2005, 2017 BEA Systems, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    tyeung@bea.com - initial API and implementation
 *******************************************************************************/

package org.eclipse.jdt.apt.core.internal.declaration;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
import org.eclipse.jdt.apt.core.internal.util.PackageUtil;
import org.eclipse.jdt.apt.core.internal.util.SourcePositionImpl;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IOrdinaryClassFile;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IPackageBinding;

import com.sun.mirror.declaration.AnnotationMirror;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
import com.sun.mirror.declaration.ClassDeclaration;
import com.sun.mirror.declaration.EnumDeclaration;
import com.sun.mirror.declaration.InterfaceDeclaration;
import com.sun.mirror.declaration.Modifier;
import com.sun.mirror.declaration.PackageDeclaration;
import com.sun.mirror.declaration.TypeDeclaration;
import com.sun.mirror.util.DeclarationVisitor;
import com.sun.mirror.util.SourcePosition;

public class PackageDeclarationImpl extends DeclarationImpl implements PackageDeclaration
{   
	// If this package came from directly requesting it via the environment,
	// need to hide the source position, as this is an artifact of our implementation
	private final boolean _hideSourcePosition;
	
	/** The back-pointer to the type declaration that created this package declaration
	 * @see TypeDeclarationImpl#getPackage()
	 */
	private final TypeDeclarationImpl _typeDecl;
	
	// Lazily initialized unless specified in constructor.
	private IPackageFragment[] _pkgFragments = null;
	
    public PackageDeclarationImpl(
			final IPackageBinding binding, 
			final TypeDeclarationImpl typeDecl, 
			final BaseProcessorEnv env,
			final boolean hideSourcePosition)
    {
        this(binding, 
        	 typeDecl, 
        	 env, 
        	 hideSourcePosition, 
        	 null);
    }
    
    public PackageDeclarationImpl(
			final IPackageBinding binding, 
			final TypeDeclarationImpl typeDecl, 
			final BaseProcessorEnv env,
			final boolean hideSourcePosition,
			final IPackageFragment[] pkgFragments)
    {
        super(binding, env);   
		_typeDecl = typeDecl;
		_hideSourcePosition = hideSourcePosition;
		_pkgFragments = pkgFragments;
    }

    public IPackageBinding getPackageBinding(){ return (IPackageBinding)_binding; }

    @Override
	public void accept(DeclarationVisitor visitor)
    {
        visitor.visitPackageDeclaration(this);
    }
    
    @Override
	public <A extends Annotation> A getAnnotation(Class<A> anno)
    {
		return _getAnnotation(anno, getPackageBinding().getAnnotations());
    }

    @Override
	public Collection<AnnotationMirror> getAnnotationMirrors()
    {	
		return _getAnnotationMirrors(getPackageBinding().getAnnotations());
    }

    @Override
	public Collection<AnnotationTypeDeclaration> getAnnotationTypes()
    {
		// jdt currently have no support for package declaration.
		return Collections.emptyList();	
    }

    @Override
	public Collection<ClassDeclaration> getClasses() {
    	initFragments();
    	List<IType> types = getTypesInPackage(_pkgFragments);
		List<ClassDeclaration> classes = new ArrayList<>();
		for (IType type : types) {
			try {
				// isClass() will return true if TypeDeclaration is an InterfaceDeclaration
				if (type.isClass()) {
					TypeDeclaration td = _env.getTypeDeclaration( type );
					if ( td instanceof ClassDeclaration ) {				
						classes.add((ClassDeclaration)td);
					}
				}
			}
			catch (JavaModelException ex) {} // No longer exists, don't return it
		}
		
		return classes;
    }

    @Override
	public Collection<EnumDeclaration> getEnums() {
    	initFragments();
    	List<IType> types = getTypesInPackage(_pkgFragments);
		List<EnumDeclaration> enums = new ArrayList<>();
		for (IType type : types) {
			try {
				if (type.isEnum()) {
					enums.add((EnumDeclaration)_env.getTypeDeclaration(type));
				}
			}
			catch (JavaModelException ex) {} // No longer exists, don't return it
		}
		
		return enums;
    }

    @Override
	public Collection<InterfaceDeclaration> getInterfaces() {
    	initFragments();
    	List<IType> types = getTypesInPackage(_pkgFragments);
		List<InterfaceDeclaration> interfaces = new ArrayList<>();
		for (IType type : types) {
			try {
				if (type.isInterface()) {
					interfaces.add((InterfaceDeclaration)_env.getTypeDeclaration(type));
				}
			}
			catch (JavaModelException ex) {} // No longer exists, don't return it
		}
		
		return interfaces;
    }

    @Override
	public String getDocComment()
    {
        return null;
    }

    @Override
	public Collection<Modifier> getModifiers()
    {
        // package doesn't have modifiers.
        return Collections.emptyList();
    }

    @Override
	public SourcePosition getPosition()
    {
		if (_hideSourcePosition)
			return null;
		if (isFromSource()){
			final CompilationUnit unit = _typeDecl.getCompilationUnit();
			final ASTNode node = unit.findDeclaringNode(getDeclarationBinding());
			if( node == null ) return null;
			final int start = node.getStartPosition();			
	        return new SourcePositionImpl(start,
										  node.getLength(),
	                                      unit.getLineNumber(start),
	                                      unit.getColumnNumber(start),
	                                      this);			
		}
		return null;
        
    }

    @Override
	public String getQualifiedName()
    {
		return getPackageBinding().getName();
    }   

    @Override
	public String getSimpleName()
    {
        IPackageBinding pkg = getPackageBinding();
        final String[] components = pkg.getNameComponents();
        if( components == null || components.length == 0 ) return ""; //$NON-NLS-1$
        return components[components.length - 1];
    }

    @Override
	public MirrorKind kind(){ return MirrorKind.PACKAGE; }

    @Override
	public String toString(){ return getQualifiedName(); }
	
	@Override
	public IPackageBinding getDeclarationBinding(){ return (IPackageBinding)_binding; }

	@Override
	public boolean isFromSource(){ return _typeDecl != null && _typeDecl.isFromSource(); }
	
	/**
	 * Make sure to call this before attempting to access _pkgFragments.
	 * We initialize this field lazily, because it is very expensive to compute and
	 * there are some common questions such as getQualifiedName() that can be
	 * answered without initializing it at all.
	 */
	private void initFragments() {
		if (null == _pkgFragments) {
			_pkgFragments = PackageUtil.getPackageFragments(_binding.getName(), _env);
		}
	}
	
	private static List<IType> getTypesInPackage(final IPackageFragment[] fragments) {
		List<IType> types = new ArrayList<>();
		try {
			// Get all top-level classes -- ignore local, member, and anonymous classes
			for (IPackageFragment fragment : fragments) {
				for (IOrdinaryClassFile classFile : fragment.getOrdinaryClassFiles()) {
					IType type = classFile.getType();
					if (! (type.isLocal() || type.isMember() || type.isAnonymous()) ) {
						types.add(type);
					}
				}
				for (ICompilationUnit compUnit : fragment.getCompilationUnits()) {
					for (IType type : compUnit.getTypes()) {
						if (! (type.isLocal() || type.isMember() || type.isAnonymous()) ) {
							types.add(type);
						}
					}
				}
			}
		}
		catch (JavaModelException jme) {
			// Ignore -- project is in a bad state. This will get recalled if necessary
		}
		return types;
	}
   
}

Back to the top