Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 377990
Collapse All | Expand All

(-)a/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/AnnotationMirrorImpl.java (+1 lines)
Lines 235-240 Link Here
235
	{ 
235
	{ 
236
		if( isFromSource() ){
236
		if( isFromSource() ){
237
			final CompilationUnit unit = _annotated.getCompilationUnit();
237
			final CompilationUnit unit = _annotated.getCompilationUnit();
238
			if (unit == null) return null;
238
			final ASTNode node = unit.findDeclaringNode(_domAnnotation);
239
			final ASTNode node = unit.findDeclaringNode(_domAnnotation);
239
			if( node instanceof org.eclipse.jdt.core.dom.Annotation )
240
			if( node instanceof org.eclipse.jdt.core.dom.Annotation )
240
				return (org.eclipse.jdt.core.dom.Annotation)node;			
241
				return (org.eclipse.jdt.core.dom.Annotation)node;			
(-)a/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/MemberDeclarationImpl.java (-4 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 BEA Systems, Inc.
2
 * Copyright (c) 2005, 2012 BEA Systems, Inc.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 62-69 Link Here
62
			instances = ((IVariableBinding)binding).getAnnotations();
62
			instances = ((IVariableBinding)binding).getAnnotations();
63
			break;
63
			break;
64
		case IBinding.PACKAGE:
64
		case IBinding.PACKAGE:
65
			// TODO: support package annotation
65
			instances = ((IPackageBinding)binding).getAnnotations();
66
			return null;
67
		default:			
66
		default:			
68
			throw new IllegalStateException();
67
			throw new IllegalStateException();
69
		}
68
		}
Lines 92-98 Link Here
92
        return null;
91
        return null;
93
    }    
92
    }    
94
93
95
	/**
94
    /**
96
	 * @return the source position of this declaration. 
95
	 * @return the source position of this declaration. 
97
	 *         Return null if this declaration did not come from source or 
96
	 *         Return null if this declaration did not come from source or 
98
	 *         if the declaration is (or is part of) a secondary type that is defined 
97
	 *         if the declaration is (or is part of) a secondary type that is defined 
(-)a/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/PackageDeclarationImpl.java (-4 / +26 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 BEA Systems, Inc.
2
 * Copyright (c) 2005, 2012 BEA Systems, Inc. and others
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 54-59 Link Here
54
	// Lazily initialized unless specified in constructor.
54
	// Lazily initialized unless specified in constructor.
55
	private IPackageFragment[] _pkgFragments = null;
55
	private IPackageFragment[] _pkgFragments = null;
56
	
56
	
57
	// boolean to indicate if this is getting created for a package-info.
58
	private final boolean _isPkgInfo; 
59
	
57
    public PackageDeclarationImpl(
60
    public PackageDeclarationImpl(
58
			final IPackageBinding binding, 
61
			final IPackageBinding binding, 
59
			final TypeDeclarationImpl typeDecl, 
62
			final TypeDeclarationImpl typeDecl, 
Lines 64-70 Link Here
64
        	 typeDecl, 
67
        	 typeDecl, 
65
        	 env, 
68
        	 env, 
66
        	 hideSourcePosition, 
69
        	 hideSourcePosition, 
67
        	 null);
70
        	 null,
71
        	 false);
68
    }
72
    }
69
    
73
    
70
    public PackageDeclarationImpl(
74
    public PackageDeclarationImpl(
Lines 74-83 Link Here
74
			final boolean hideSourcePosition,
78
			final boolean hideSourcePosition,
75
			final IPackageFragment[] pkgFragments)
79
			final IPackageFragment[] pkgFragments)
76
    {
80
    {
81
    	this(binding, 
82
           	 typeDecl, 
83
           	 env, 
84
           	 hideSourcePosition, 
85
           	 pkgFragments,
86
           	 false);
87
    }
88
    
89
    public PackageDeclarationImpl(
90
			final IPackageBinding binding, 
91
			final TypeDeclarationImpl typeDecl, 
92
			final BaseProcessorEnv env,
93
			final boolean hideSourcePosition,
94
			final IPackageFragment[] pkgFragments,
95
			final boolean isPkgInfo)
96
    {
77
        super(binding, env);   
97
        super(binding, env);   
78
		_typeDecl = typeDecl;
98
		_typeDecl = typeDecl;
79
		_hideSourcePosition = hideSourcePosition;
99
		_hideSourcePosition = hideSourcePosition;
80
		_pkgFragments = pkgFragments;
100
		_pkgFragments = pkgFragments;
101
		_isPkgInfo = isPkgInfo;
81
    }
102
    }
82
103
83
    public IPackageBinding getPackageBinding(){ return (IPackageBinding)_binding; }
104
    public IPackageBinding getPackageBinding(){ return (IPackageBinding)_binding; }
Lines 171-177 Link Here
171
		if (_hideSourcePosition)
192
		if (_hideSourcePosition)
172
			return null;
193
			return null;
173
		if (isFromSource()){
194
		if (isFromSource()){
174
			final CompilationUnit unit = _typeDecl.getCompilationUnit();
195
			final CompilationUnit unit = _isPkgInfo ? getCompilationUnit() : _typeDecl.getCompilationUnit();
196
			if (unit == null) return null;
175
			final ASTNode node = unit.findDeclaringNode(getDeclarationBinding());
197
			final ASTNode node = unit.findDeclaringNode(getDeclarationBinding());
176
			if( node == null ) return null;
198
			if( node == null ) return null;
177
			final int start = node.getStartPosition();			
199
			final int start = node.getStartPosition();			
Lines 204-210 Link Here
204
	
226
	
205
	public IPackageBinding getDeclarationBinding(){ return (IPackageBinding)_binding; }
227
	public IPackageBinding getDeclarationBinding(){ return (IPackageBinding)_binding; }
206
228
207
	public boolean isFromSource(){ return _typeDecl != null && _typeDecl.isFromSource(); }
229
	public boolean isFromSource(){ return _isPkgInfo == true || (_typeDecl != null && _typeDecl.isFromSource()); }
208
	
230
	
209
	/**
231
	/**
210
	 * Make sure to call this before attempting to access _pkgFragments.
232
	 * Make sure to call this before attempting to access _pkgFragments.
(-)a/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/env/BaseProcessorEnv.java (-2 / +16 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2009 BEA Systems Inc. and others
2
 * Copyright (c) 2005, 2012 BEA Systems Inc. and others
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 533-538 Link Here
533
	{
533
	{
534
		if (name == null)
534
		if (name == null)
535
			throw new IllegalArgumentException("name cannot be null"); //$NON-NLS-1$		
535
			throw new IllegalArgumentException("name cannot be null"); //$NON-NLS-1$		
536
				
536
		IPackageFragment[] pkgFrags = PackageUtil.getPackageFragments(name, this);
537
		IPackageFragment[] pkgFrags = PackageUtil.getPackageFragments(name, this);
537
538
538
		// No packages found, null expected
539
		// No packages found, null expected
Lines 552-557 Link Here
552
			}
553
			}
553
			if (containsNoJavaResources)
554
			if (containsNoJavaResources)
554
				return new PackageDeclarationImplNoBinding(pkgFrags);
555
				return new PackageDeclarationImplNoBinding(pkgFrags);
556
			
557
			if (_astRoot != null && _astRoot.types().size() == 0) {
558
				if (_astRoot.getPackage().getName().getFullyQualifiedName().equals(name)) {
559
					if (_file != null && "package-info.java".equalsIgnoreCase(_file.getName())) { //$NON-NLS-1$
560
						IPackageBinding binding = _astRoot.getPackage().resolveBinding();
561
						return new PackageDeclarationImpl(binding, null, this, true, pkgFrags, true);
562
					}
563
				}
564
			}
555
565
556
			// We should be able to create a class or
566
			// We should be able to create a class or
557
			// source file from one of the packages.
567
			// source file from one of the packages.
Lines 676-685 Link Here
676
    {
686
    {
677
        assert binding.getKind() == IBinding.TYPE ||
687
        assert binding.getKind() == IBinding.TYPE ||
678
               binding.getKind() == IBinding.METHOD ||
688
               binding.getKind() == IBinding.METHOD ||
679
               binding.getKind() == IBinding.VARIABLE ;
689
               binding.getKind() == IBinding.VARIABLE ||
690
               binding.getKind() == IBinding.PACKAGE;
680
        CompilationUnit domUnit = searchLocallyForBinding(binding);        
691
        CompilationUnit domUnit = searchLocallyForBinding(binding);        
681
        if( domUnit != null ) 
692
        if( domUnit != null ) 
682
        	return domUnit;
693
        	return domUnit;
694
        if (binding.getKind() == IBinding.PACKAGE) {
695
        	return null;
696
        }
683
        else{
697
        else{
684
			final IMember member = (IMember)binding.getJavaElement();
698
			final IMember member = (IMember)binding.getJavaElement();
685
			final ICompilationUnit unit;
699
			final ICompilationUnit unit;
(-)a/org.eclipse.jdt.apt.tests/src/org/eclipse/jdt/apt/tests/MirrorDeclarationTests.java (-19 / +57 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2012 BEA Systems, Inc. and others
2
 * Copyright (c) 2005, 2008 BEA Systems, Inc. and others
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *    sbandow@bea.com - initial API and implementation
9
 *    sbandow@bea.com - initial API and implementation
10
 *    IBM Corporation - Added test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=352949
10
 *    
11
 *******************************************************************************/
11
 *******************************************************************************/
12
12
13
package org.eclipse.jdt.apt.tests;
13
package org.eclipse.jdt.apt.tests;
Lines 37-42 Link Here
37
import com.sun.mirror.declaration.AnnotationTypeElementDeclaration;
37
import com.sun.mirror.declaration.AnnotationTypeElementDeclaration;
38
import com.sun.mirror.declaration.AnnotationValue;
38
import com.sun.mirror.declaration.AnnotationValue;
39
import com.sun.mirror.declaration.Declaration;
39
import com.sun.mirror.declaration.Declaration;
40
import com.sun.mirror.declaration.PackageDeclaration;
40
import com.sun.mirror.declaration.TypeDeclaration;
41
import com.sun.mirror.declaration.TypeDeclaration;
41
import com.sun.mirror.util.SourcePosition;
42
import com.sun.mirror.util.SourcePosition;
42
43
Lines 166-171 Link Here
166
			"    public class Inner{} \n" +
167
			"    public class Inner{} \n" +
167
			"}";
168
			"}";
168
		
169
		
170
		
169
		final IPath fooPath = env.addClass(srcRoot, "test", "Foo", codeFoo);
171
		final IPath fooPath = env.addClass(srcRoot, "test", "Foo", codeFoo);
170
		fullBuild( project.getFullPath() );
172
		fullBuild( project.getFullPath() );
171
		
173
		
Lines 264-270 Link Here
264
	}
266
	}
265
	
267
	
266
	public void testPackageInfo() {
268
	public void testPackageInfo() {
267
		PackageInfoProc p = new PackageInfoProc(); 
269
		PackageInfoProc p = new PackageInfoProc(true); 
268
		GenericFactory.setProcessor(p);
270
		GenericFactory.setProcessor(p);
269
		
271
		
270
		IProject project = env.getProject( getProjectName() );
272
		IProject project = env.getProject( getProjectName() );
Lines 281-287 Link Here
281
				"@interface PkgAnnotation {\n" +
283
				"@interface PkgAnnotation {\n" +
282
				"    String value() default \"def\";\n" +
284
				"    String value() default \"def\";\n" +
283
				"}\n";
285
				"}\n";
284
		env.addClass(srcRoot, "pkg", "pkgAnnotation", annContents);
286
		env.addClass(srcRoot, "pkg", "PkgAnnotation", annContents);
287
		fullBuild( project.getFullPath() );
288
		expectingNoProblems();
289
		
290
		assertTrue("Processor not invoked", p.called);
291
	}
292
	
293
	public void testPackageInfoOfOtherClass() {
294
		PackageInfoProc p = new PackageInfoProc(false); 
295
		GenericFactory.setProcessor(p);
296
		
297
		IProject project = env.getProject( getProjectName() );
298
		IPath srcRoot = getSourcePath();
299
		IPath pkg = env.addPackage(srcRoot, "pkg");
300
		String contents = "@PkgAnnotation\n" +
301
						  "package pkg;";
302
		env.addFile(pkg, "package-info.java", contents);
303
		String annContents =
304
				"package pkg;\n" +
305
				"\n" +
306
				"@interface PkgAnnotation {\n" +
307
				"    String value() default \"def\";\n" +
308
				"}\n";
309
		env.addClass(srcRoot, "pkg", "PkgAnnotation", annContents);
310
		contents = "package otherpkg;\n"+
311
		           "import org.eclipse.jdt.apt.tests.annotations.generic.*;\n"+
312
		           "@GenericAnnotation\n" +
313
		           "class Test{}";
314
		env.addClass(srcRoot, "otherpkg", "Test", contents);
285
		fullBuild( project.getFullPath() );
315
		fullBuild( project.getFullPath() );
286
		expectingNoProblems();
316
		expectingNoProblems();
287
		
317
		
Lines 329-335 Link Here
329
	static class PackageInfoProc extends AbstractGenericProcessor {
359
	static class PackageInfoProc extends AbstractGenericProcessor {
330
		
360
		
331
		boolean called;
361
		boolean called;
332
		
362
		boolean annotInSamePkg;
363
		public PackageInfoProc(boolean annotInSamePkg) {
364
			this.annotInSamePkg = annotInSamePkg;
365
		}
333
		public void _process() {
366
		public void _process() {
334
			called = true;
367
			called = true;
335
			AnnotationTypeDeclaration annoDecl = (AnnotationTypeDeclaration)env.getTypeDeclaration("pkg.PkgAnnotation");
368
			AnnotationTypeDeclaration annoDecl = (AnnotationTypeDeclaration)env.getTypeDeclaration("pkg.PkgAnnotation");
Lines 339-358 Link Here
339
			// don't return the package declaration - well, apt is doing that.. 
372
			// don't return the package declaration - well, apt is doing that.. 
340
			assertTrue(annotatedDecls == null  || annotatedDecls.size() == 0); 
373
			assertTrue(annotatedDecls == null  || annotatedDecls.size() == 0); 
341
			
374
			
342
			//TODO: test to support package annotation
375
			PackageDeclaration pdCode = env.getPackage("pkg");
343
			// PackageDeclaration pdCode = env.getPackage("pkg");
376
			for (AnnotationMirror am : pdCode.getAnnotationMirrors()) {
344
			// for (AnnotationMirror am : pdCode.getAnnotationMirrors()) {
377
				if ("GenericAnnotation".equals(am.getAnnotationType().getDeclaration().getSimpleName())) {
345
			//	if ("GenericAnnotation".equals(am.getAnnotationType().getDeclaration().getSimpleName())) {
378
					continue;
346
			//		continue;
379
				}
347
			//	}
380
				if (this.annotInSamePkg) {
348
			//	assertTrue(null != am.getPosition());
381
					assertTrue(null != am.getPosition());
349
			//	AnnotationTypeDeclaration atd = am.getAnnotationType().getDeclaration();
382
				}else {
350
			//	assertTrue(null != atd.getPosition());
383
					// package-info.java in not getting processed
351
			//	for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> entry : am.getElementValues().entrySet()) {
384
					// and hence this is OK..
352
			//		assertNotNull(entry.getKey().getPosition());
385
					assertTrue(null == am.getPosition());
353
			//		assertNotNull(entry.getKey().getDefaultValue().getPosition());
386
				}
354
			//	}
387
				AnnotationTypeDeclaration atd = am.getAnnotationType().getDeclaration();
355
			// }
388
				assertTrue(null != atd.getPosition());
389
				for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> entry : am.getElementValues().entrySet()) {
390
					assertNotNull(entry.getKey().getPosition());
391
					assertNotNull(entry.getKey().getDefaultValue().getPosition());
392
				}
393
			 }
356
		}
394
		}
357
	}
395
	}
358
	
396
	
(-)a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/PackageBinding.java (-12 / +29 lines)
Lines 55-60 Link Here
55
55
56
	public IAnnotationBinding[] getAnnotations() {
56
	public IAnnotationBinding[] getAnnotations() {
57
		try {
57
		try {
58
			ASTNode node = this.resolver.findDeclaringNode(this);
59
			if (node != null) {
60
				ASTNode parent = node.getParent();
61
				if (parent instanceof CompilationUnit && ((CompilationUnit)parent).types().size() == 0) {
62
					org.eclipse.jdt.internal.compiler.ast.ASTNode compNode = this.resolver.getCorrespondingNode(parent);
63
					if (compNode != null) {
64
						org.eclipse.jdt.internal.compiler.ast.TypeDeclaration[] typeDecl = ((org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)compNode).types;
65
						if (typeDecl != null && typeDecl.length == 1 && CharOperation.equals(typeDecl[0].name, TypeConstants.PACKAGE_INFO_NAME)) {
66
							return getAnnotations((PackageDeclaration)node);
67
						}
68
					}
69
				}
70
			}
58
			INameEnvironment nameEnvironment = this.binding.environment.nameEnvironment;
71
			INameEnvironment nameEnvironment = this.binding.environment.nameEnvironment;
59
			if (!(nameEnvironment instanceof SearchableEnvironment))
72
			if (!(nameEnvironment instanceof SearchableEnvironment))
60
				return AnnotationBinding.NoAnnotations;
73
				return AnnotationBinding.NoAnnotations;
Lines 82-99 Link Here
82
							CompilationUnit domUnit = (CompilationUnit) p.createAST(null);
95
							CompilationUnit domUnit = (CompilationUnit) p.createAST(null);
83
							PackageDeclaration pkgDecl = domUnit.getPackage();
96
							PackageDeclaration pkgDecl = domUnit.getPackage();
84
							if (pkgDecl != null) {
97
							if (pkgDecl != null) {
85
								List annos = pkgDecl.annotations();
98
								return getAnnotations(pkgDecl);
86
								if (annos == null || annos.isEmpty())
87
									return AnnotationBinding.NoAnnotations;
88
								IAnnotationBinding[] result = new IAnnotationBinding[annos.size()];
89
								int index=0;
90
		 						for (Iterator it = annos.iterator(); it.hasNext(); index++) {
91
									result[index] = ((Annotation) it.next()).resolveAnnotationBinding();
92
									// not resolving bindings
93
									if (result[index] == null)
94
										return AnnotationBinding.NoAnnotations;
95
								}
96
								return result;
97
							}
99
							}
98
						}
100
						}
99
						break;
101
						break;
Lines 127-132 Link Here
127
		return AnnotationBinding.NoAnnotations;
129
		return AnnotationBinding.NoAnnotations;
128
	}
130
	}
129
131
132
	private IAnnotationBinding[] getAnnotations(PackageDeclaration pkgDecl) {
133
		List annos = pkgDecl.annotations();
134
		if (annos == null || annos.isEmpty())
135
			return AnnotationBinding.NoAnnotations;
136
		IAnnotationBinding[] result = new IAnnotationBinding[annos.size()];
137
		int index=0;
138
		for (Iterator it = annos.iterator(); it.hasNext(); index++) {
139
			result[index] = ((Annotation) it.next()).resolveAnnotationBinding();
140
			// not resolving bindings
141
			if (result[index] == null)
142
				return AnnotationBinding.NoAnnotations;
143
		}
144
		return result;
145
	}
146
130
	/*
147
	/*
131
	 * @see IBinding#getName()
148
	 * @see IBinding#getName()
132
	 */
149
	 */

Return to bug 377990