Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 214756 Details for
Bug 377990
Support package level annotations
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
Proposed patch
patch.txt (text/plain), 15.43 KB, created by
Satyam Kandula
on 2012-04-28 01:22:47 EDT
(
hide
)
Description:
Proposed patch
Filename:
MIME Type:
Creator:
Satyam Kandula
Created:
2012-04-28 01:22:47 EDT
Size:
15.43 KB
patch
obsolete
>diff --git a/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/AnnotationMirrorImpl.java b/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/AnnotationMirrorImpl.java >index 0f71084..6591271 100644 >--- a/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/AnnotationMirrorImpl.java >+++ b/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/AnnotationMirrorImpl.java >@@ -235,6 +235,7 @@ > { > if( isFromSource() ){ > final CompilationUnit unit = _annotated.getCompilationUnit(); >+ if (unit == null) return null; > final ASTNode node = unit.findDeclaringNode(_domAnnotation); > if( node instanceof org.eclipse.jdt.core.dom.Annotation ) > return (org.eclipse.jdt.core.dom.Annotation)node; >diff --git a/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/MemberDeclarationImpl.java b/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/MemberDeclarationImpl.java >index b019f5b..30100e9 100644 >--- a/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/MemberDeclarationImpl.java >+++ b/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/MemberDeclarationImpl.java >@@ -1,5 +1,5 @@ > /******************************************************************************* >- * Copyright (c) 2005, 2007 BEA Systems, Inc. >+ * Copyright (c) 2005, 2012 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 >@@ -62,8 +62,7 @@ > instances = ((IVariableBinding)binding).getAnnotations(); > break; > case IBinding.PACKAGE: >- // TODO: support package annotation >- return null; >+ instances = ((IPackageBinding)binding).getAnnotations(); > default: > throw new IllegalStateException(); > } >@@ -92,7 +91,7 @@ > return null; > } > >- /** >+ /** > * @return the source position of this declaration. > * Return null if this declaration did not come from source or > * if the declaration is (or is part of) a secondary type that is defined >diff --git a/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/PackageDeclarationImpl.java b/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/PackageDeclarationImpl.java >index 399e355..dcc9320 100644 >--- a/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/PackageDeclarationImpl.java >+++ b/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/declaration/PackageDeclarationImpl.java >@@ -1,5 +1,5 @@ > /******************************************************************************* >- * Copyright (c) 2005, 2008 BEA Systems, Inc. >+ * Copyright (c) 2005, 2012 BEA Systems, Inc. and others > * 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 >@@ -54,6 +54,9 @@ > // Lazily initialized unless specified in constructor. > private IPackageFragment[] _pkgFragments = null; > >+ // boolean to indicate if this is getting created for a package-info. >+ private final boolean _isPkgInfo; >+ > public PackageDeclarationImpl( > final IPackageBinding binding, > final TypeDeclarationImpl typeDecl, >@@ -64,7 +67,8 @@ > typeDecl, > env, > hideSourcePosition, >- null); >+ null, >+ false); > } > > public PackageDeclarationImpl( >@@ -74,10 +78,27 @@ > final boolean hideSourcePosition, > final IPackageFragment[] pkgFragments) > { >+ this(binding, >+ typeDecl, >+ env, >+ hideSourcePosition, >+ pkgFragments, >+ false); >+ } >+ >+ public PackageDeclarationImpl( >+ final IPackageBinding binding, >+ final TypeDeclarationImpl typeDecl, >+ final BaseProcessorEnv env, >+ final boolean hideSourcePosition, >+ final IPackageFragment[] pkgFragments, >+ final boolean isPkgInfo) >+ { > super(binding, env); > _typeDecl = typeDecl; > _hideSourcePosition = hideSourcePosition; > _pkgFragments = pkgFragments; >+ _isPkgInfo = isPkgInfo; > } > > public IPackageBinding getPackageBinding(){ return (IPackageBinding)_binding; } >@@ -171,7 +192,8 @@ > if (_hideSourcePosition) > return null; > if (isFromSource()){ >- final CompilationUnit unit = _typeDecl.getCompilationUnit(); >+ final CompilationUnit unit = _isPkgInfo ? getCompilationUnit() : _typeDecl.getCompilationUnit(); >+ if (unit == null) return null; > final ASTNode node = unit.findDeclaringNode(getDeclarationBinding()); > if( node == null ) return null; > final int start = node.getStartPosition(); >@@ -204,7 +226,7 @@ > > public IPackageBinding getDeclarationBinding(){ return (IPackageBinding)_binding; } > >- public boolean isFromSource(){ return _typeDecl != null && _typeDecl.isFromSource(); } >+ public boolean isFromSource(){ return _isPkgInfo == true || (_typeDecl != null && _typeDecl.isFromSource()); } > > /** > * Make sure to call this before attempting to access _pkgFragments. >diff --git a/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/env/BaseProcessorEnv.java b/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/env/BaseProcessorEnv.java >index 8aab9ac..94f8762 100644 >--- a/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/env/BaseProcessorEnv.java >+++ b/org.eclipse.jdt.apt.core/src/org/eclipse/jdt/apt/core/internal/env/BaseProcessorEnv.java >@@ -1,5 +1,5 @@ > /******************************************************************************* >- * Copyright (c) 2005, 2009 BEA Systems Inc. and others >+ * Copyright (c) 2005, 2012 BEA Systems Inc. and others > * 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 >@@ -533,6 +533,7 @@ > { > if (name == null) > throw new IllegalArgumentException("name cannot be null"); //$NON-NLS-1$ >+ > IPackageFragment[] pkgFrags = PackageUtil.getPackageFragments(name, this); > > // No packages found, null expected >@@ -552,6 +553,15 @@ > } > if (containsNoJavaResources) > return new PackageDeclarationImplNoBinding(pkgFrags); >+ >+ if (_astRoot != null && _astRoot.types().size() == 0) { >+ if (_astRoot.getPackage().getName().getFullyQualifiedName().equals(name)) { >+ if (_file != null && "package-info.java".equalsIgnoreCase(_file.getName())) { //$NON-NLS-1$ >+ IPackageBinding binding = _astRoot.getPackage().resolveBinding(); >+ return new PackageDeclarationImpl(binding, null, this, true, pkgFrags, true); >+ } >+ } >+ } > > // We should be able to create a class or > // source file from one of the packages. >@@ -676,10 +686,14 @@ > { > assert binding.getKind() == IBinding.TYPE || > binding.getKind() == IBinding.METHOD || >- binding.getKind() == IBinding.VARIABLE ; >+ binding.getKind() == IBinding.VARIABLE || >+ binding.getKind() == IBinding.PACKAGE; > CompilationUnit domUnit = searchLocallyForBinding(binding); > if( domUnit != null ) > return domUnit; >+ if (binding.getKind() == IBinding.PACKAGE) { >+ return null; >+ } > else{ > final IMember member = (IMember)binding.getJavaElement(); > final ICompilationUnit unit; >diff --git a/org.eclipse.jdt.apt.tests/src/org/eclipse/jdt/apt/tests/MirrorDeclarationTests.java b/org.eclipse.jdt.apt.tests/src/org/eclipse/jdt/apt/tests/MirrorDeclarationTests.java >index a4841d0..2fe0f3b 100644 >--- a/org.eclipse.jdt.apt.tests/src/org/eclipse/jdt/apt/tests/MirrorDeclarationTests.java >+++ b/org.eclipse.jdt.apt.tests/src/org/eclipse/jdt/apt/tests/MirrorDeclarationTests.java >@@ -1,5 +1,5 @@ > /******************************************************************************* >- * Copyright (c) 2005, 2012 BEA Systems, Inc. and others >+ * Copyright (c) 2005, 2008 BEA Systems, Inc. and others > * 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 >@@ -7,7 +7,7 @@ > * > * Contributors: > * sbandow@bea.com - initial API and implementation >- * IBM Corporation - Added test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=352949 >+ * > *******************************************************************************/ > > package org.eclipse.jdt.apt.tests; >@@ -37,6 +37,7 @@ > import com.sun.mirror.declaration.AnnotationTypeElementDeclaration; > import com.sun.mirror.declaration.AnnotationValue; > import com.sun.mirror.declaration.Declaration; >+import com.sun.mirror.declaration.PackageDeclaration; > import com.sun.mirror.declaration.TypeDeclaration; > import com.sun.mirror.util.SourcePosition; > >@@ -166,6 +167,7 @@ > " public class Inner{} \n" + > "}"; > >+ > final IPath fooPath = env.addClass(srcRoot, "test", "Foo", codeFoo); > fullBuild( project.getFullPath() ); > >@@ -264,7 +266,7 @@ > } > > public void testPackageInfo() { >- PackageInfoProc p = new PackageInfoProc(); >+ PackageInfoProc p = new PackageInfoProc(true); > GenericFactory.setProcessor(p); > > IProject project = env.getProject( getProjectName() ); >@@ -281,7 +283,35 @@ > "@interface PkgAnnotation {\n" + > " String value() default \"def\";\n" + > "}\n"; >- env.addClass(srcRoot, "pkg", "pkgAnnotation", annContents); >+ env.addClass(srcRoot, "pkg", "PkgAnnotation", annContents); >+ fullBuild( project.getFullPath() ); >+ expectingNoProblems(); >+ >+ assertTrue("Processor not invoked", p.called); >+ } >+ >+ public void testPackageInfoOfOtherClass() { >+ PackageInfoProc p = new PackageInfoProc(false); >+ GenericFactory.setProcessor(p); >+ >+ IProject project = env.getProject( getProjectName() ); >+ IPath srcRoot = getSourcePath(); >+ IPath pkg = env.addPackage(srcRoot, "pkg"); >+ String contents = "@PkgAnnotation\n" + >+ "package pkg;"; >+ env.addFile(pkg, "package-info.java", contents); >+ String annContents = >+ "package pkg;\n" + >+ "\n" + >+ "@interface PkgAnnotation {\n" + >+ " String value() default \"def\";\n" + >+ "}\n"; >+ env.addClass(srcRoot, "pkg", "PkgAnnotation", annContents); >+ contents = "package otherpkg;\n"+ >+ "import org.eclipse.jdt.apt.tests.annotations.generic.*;\n"+ >+ "@GenericAnnotation\n" + >+ "class Test{}"; >+ env.addClass(srcRoot, "otherpkg", "Test", contents); > fullBuild( project.getFullPath() ); > expectingNoProblems(); > >@@ -329,7 +359,10 @@ > static class PackageInfoProc extends AbstractGenericProcessor { > > boolean called; >- >+ boolean annotInSamePkg; >+ public PackageInfoProc(boolean annotInSamePkg) { >+ this.annotInSamePkg = annotInSamePkg; >+ } > public void _process() { > called = true; > AnnotationTypeDeclaration annoDecl = (AnnotationTypeDeclaration)env.getTypeDeclaration("pkg.PkgAnnotation"); >@@ -339,20 +372,25 @@ > // don't return the package declaration - well, apt is doing that.. > assertTrue(annotatedDecls == null || annotatedDecls.size() == 0); > >- //TODO: test to support package annotation >- // PackageDeclaration pdCode = env.getPackage("pkg"); >- // for (AnnotationMirror am : pdCode.getAnnotationMirrors()) { >- // if ("GenericAnnotation".equals(am.getAnnotationType().getDeclaration().getSimpleName())) { >- // continue; >- // } >- // assertTrue(null != am.getPosition()); >- // AnnotationTypeDeclaration atd = am.getAnnotationType().getDeclaration(); >- // assertTrue(null != atd.getPosition()); >- // for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> entry : am.getElementValues().entrySet()) { >- // assertNotNull(entry.getKey().getPosition()); >- // assertNotNull(entry.getKey().getDefaultValue().getPosition()); >- // } >- // } >+ PackageDeclaration pdCode = env.getPackage("pkg"); >+ for (AnnotationMirror am : pdCode.getAnnotationMirrors()) { >+ if ("GenericAnnotation".equals(am.getAnnotationType().getDeclaration().getSimpleName())) { >+ continue; >+ } >+ if (this.annotInSamePkg) { >+ assertTrue(null != am.getPosition()); >+ }else { >+ // package-info.java in not getting processed >+ // and hence this is OK.. >+ assertTrue(null == am.getPosition()); >+ } >+ AnnotationTypeDeclaration atd = am.getAnnotationType().getDeclaration(); >+ assertTrue(null != atd.getPosition()); >+ for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> entry : am.getElementValues().entrySet()) { >+ assertNotNull(entry.getKey().getPosition()); >+ assertNotNull(entry.getKey().getDefaultValue().getPosition()); >+ } >+ } > } > } > >diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/PackageBinding.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/PackageBinding.java >index 148584e..bcb4851 100644 >--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/PackageBinding.java >+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/PackageBinding.java >@@ -55,6 +55,19 @@ > > public IAnnotationBinding[] getAnnotations() { > try { >+ ASTNode node = this.resolver.findDeclaringNode(this); >+ if (node != null) { >+ ASTNode parent = node.getParent(); >+ if (parent instanceof CompilationUnit && ((CompilationUnit)parent).types().size() == 0) { >+ org.eclipse.jdt.internal.compiler.ast.ASTNode compNode = this.resolver.getCorrespondingNode(parent); >+ if (compNode != null) { >+ org.eclipse.jdt.internal.compiler.ast.TypeDeclaration[] typeDecl = ((org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)compNode).types; >+ if (typeDecl != null && typeDecl.length == 1 && CharOperation.equals(typeDecl[0].name, TypeConstants.PACKAGE_INFO_NAME)) { >+ return getAnnotations((PackageDeclaration)node); >+ } >+ } >+ } >+ } > INameEnvironment nameEnvironment = this.binding.environment.nameEnvironment; > if (!(nameEnvironment instanceof SearchableEnvironment)) > return AnnotationBinding.NoAnnotations; >@@ -82,18 +95,7 @@ > CompilationUnit domUnit = (CompilationUnit) p.createAST(null); > PackageDeclaration pkgDecl = domUnit.getPackage(); > if (pkgDecl != null) { >- List annos = pkgDecl.annotations(); >- if (annos == null || annos.isEmpty()) >- return AnnotationBinding.NoAnnotations; >- IAnnotationBinding[] result = new IAnnotationBinding[annos.size()]; >- int index=0; >- for (Iterator it = annos.iterator(); it.hasNext(); index++) { >- result[index] = ((Annotation) it.next()).resolveAnnotationBinding(); >- // not resolving bindings >- if (result[index] == null) >- return AnnotationBinding.NoAnnotations; >- } >- return result; >+ return getAnnotations(pkgDecl); > } > } > break; >@@ -127,6 +129,21 @@ > return AnnotationBinding.NoAnnotations; > } > >+ private IAnnotationBinding[] getAnnotations(PackageDeclaration pkgDecl) { >+ List annos = pkgDecl.annotations(); >+ if (annos == null || annos.isEmpty()) >+ return AnnotationBinding.NoAnnotations; >+ IAnnotationBinding[] result = new IAnnotationBinding[annos.size()]; >+ int index=0; >+ for (Iterator it = annos.iterator(); it.hasNext(); index++) { >+ result[index] = ((Annotation) it.next()).resolveAnnotationBinding(); >+ // not resolving bindings >+ if (result[index] == null) >+ return AnnotationBinding.NoAnnotations; >+ } >+ return result; >+ } >+ > /* > * @see IBinding#getName() > */
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 377990
: 214756