# HG changeset patch # Parent c5a5a55bdad364eb5b4cd43c040b9078b64ea8df # User Jesse Glick #206543: permit @NbBundle.Messages on fields. diff --git a/apisupport.refactoring/src/org/netbeans/modules/apisupport/hints/UseNbBundleMessages.java b/apisupport.refactoring/src/org/netbeans/modules/apisupport/hints/UseNbBundleMessages.java --- a/apisupport.refactoring/src/org/netbeans/modules/apisupport/hints/UseNbBundleMessages.java +++ b/apisupport.refactoring/src/org/netbeans/modules/apisupport/hints/UseNbBundleMessages.java @@ -87,9 +87,11 @@ import com.sun.source.tree.NewArrayTree; import com.sun.source.tree.Tree; import com.sun.source.tree.Tree.Kind; +import com.sun.source.tree.VariableTree; import com.sun.source.util.SourcePositions; import com.sun.source.util.TreePath; import org.netbeans.api.actions.Savable; +import org.netbeans.api.java.source.TreeUtilities; public class UseNbBundleMessages extends AbstractHint { @@ -264,11 +266,17 @@ if (!isAlreadyRegistered) { Tree enclosing = findEnclosingElement(wc, treePath); Tree modifiers; - if (enclosing.getKind() == Kind.METHOD) { + switch (enclosing.getKind()) { + case METHOD: modifiers = ((MethodTree) enclosing).getModifiers(); - } else if (enclosing.getKind() == Kind.COMPILATION_UNIT) { + break; + case VARIABLE: + modifiers = ((VariableTree) enclosing).getModifiers(); + break; + case COMPILATION_UNIT: modifiers = enclosing; - } else { + break; + default: modifiers = ((ClassTree) enclosing).getModifiers(); } List lines = new ArrayList(); @@ -354,9 +362,10 @@ case INTERFACE: case ANNOTATION_TYPE: case METHOD: // (or constructor) + case VARIABLE: Element e = wc.getTrees().getElement(treePath); if (e != null) { - TypeElement type = kind == Kind.METHOD ? wc.getElementUtilities().enclosingTypeElement(e) : (TypeElement) e; + TypeElement type = TreeUtilities.CLASS_TREE_KINDS.contains(kind) ? (TypeElement) e : wc.getElementUtilities().enclosingTypeElement(e); if (type == null || !wc.getElementUtilities().isLocal(type)) { return leaf; } // else part of an inner class @@ -420,6 +429,9 @@ case METHOD: modifiers = ((MethodTree) tree).getModifiers(); break; + case VARIABLE: + modifiers = ((VariableTree) tree).getModifiers(); + break; case CLASS: case ENUM: case INTERFACE: diff --git a/maven.apisupport/src/org/netbeans/modules/maven/apisupport/NbmWizardPanelVisual.java b/maven.apisupport/src/org/netbeans/modules/maven/apisupport/NbmWizardPanelVisual.java --- a/maven.apisupport/src/org/netbeans/modules/maven/apisupport/NbmWizardPanelVisual.java +++ b/maven.apisupport/src/org/netbeans/modules/maven/apisupport/NbmWizardPanelVisual.java @@ -68,11 +68,11 @@ * * @author mkleint */ -@Messages("NbmWizardPanelVisual.wait=Searching...") public class NbmWizardPanelVisual extends javax.swing.JPanel { private static final RequestProcessor RP = new RequestProcessor(NbmWizardPanelVisual.class); + @Messages("NbmWizardPanelVisual.wait=Searching...") private static final String SEARCHING = NbmWizardPanelVisual_wait(); private final NbmWizardPanel panel; private ValidationGroup vg = ValidationGroup.create(); diff --git a/maven/src/org/netbeans/modules/maven/nodes/MavenProjectNode.java b/maven/src/org/netbeans/modules/maven/nodes/MavenProjectNode.java --- a/maven/src/org/netbeans/modules/maven/nodes/MavenProjectNode.java +++ b/maven/src/org/netbeans/modules/maven/nodes/MavenProjectNode.java @@ -73,9 +73,9 @@ * * @author Milos Kleint */ -@Messages("ICON_BrokenProjectBadge=Project loading failed or was not complete") public class MavenProjectNode extends AbstractNode { static final String BADGE_ICON = "org/netbeans/modules/maven/brokenProjectBadge.png";//NOI18N + @Messages("ICON_BrokenProjectBadge=Project loading failed or was not complete") private static final String toolTipBroken = " " + ICON_BrokenProjectBadge(); private NbMavenProjectImpl project; diff --git a/openide.util/apichanges.xml b/openide.util/apichanges.xml --- a/openide.util/apichanges.xml +++ b/openide.util/apichanges.xml @@ -51,6 +51,30 @@ Actions API + + + @NbBundle.Messages available on fields + + + + +

+ Existing annotations made on classes but only used from a field + within that class will continue to work, but for clarity should + be moved onto the field. +

+
+ +

+ @NbBundle.Messages may now be used on fields, useful + in case the field initializer involves some complex construction + requiring a localized message. (Merely storing a localized message + in a String constant is poor practice; inline the field instead.) +

+
+ + +
HelpCtx.display added diff --git a/openide.util/manifest.mf b/openide.util/manifest.mf --- a/openide.util/manifest.mf +++ b/openide.util/manifest.mf @@ -1,5 +1,5 @@ Manifest-Version: 1.0 OpenIDE-Module: org.openide.util OpenIDE-Module-Localizing-Bundle: org/openide/util/Bundle.properties -OpenIDE-Module-Specification-Version: 8.21 +OpenIDE-Module-Specification-Version: 8.22 diff --git a/openide.util/src/org/openide/util/NbBundle.java b/openide.util/src/org/openide/util/NbBundle.java --- a/openide.util/src/org/openide/util/NbBundle.java +++ b/openide.util/src/org/openide/util/NbBundle.java @@ -820,10 +820,10 @@ * # {0} - file path * dialog.message=The file {0} was invalid. * - * @since org.openide.util 8.10 + * @since org.openide.util 8.10 (available also on fields since 8.22) */ @Retention(RetentionPolicy.SOURCE) - @Target({ElementType.PACKAGE, ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR}) + @Target({ElementType.PACKAGE, ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD}) public @interface Messages { /** * List of key/value pairs. diff --git a/openide.util/test/unit/src/org/netbeans/modules/openide/util/NbBundleProcessorTest.java b/openide.util/test/unit/src/org/netbeans/modules/openide/util/NbBundleProcessorTest.java --- a/openide.util/test/unit/src/org/netbeans/modules/openide/util/NbBundleProcessorTest.java +++ b/openide.util/test/unit/src/org/netbeans/modules/openide/util/NbBundleProcessorTest.java @@ -96,6 +96,17 @@ assertEquals("&Build 2 Projects", LBL_BuildMainProjectAction_Name(2, "whatever")); } + public void testFieldUsage() throws Exception { + AnnotationProcessorTestUtils.makeSource(src, "p.C", + "public class C {", + "@org.openide.util.NbBundle.Messages(\"k=v\")", + "public static final Object X = new Object() {public String toString() {return Bundle.k();}};", + "}"); + assertTrue(AnnotationProcessorTestUtils.runJavac(src, null, dest, null, null)); + ClassLoader l = new URLClassLoader(new URL[] {dest.toURI().toURL()}); + assertEquals("v", l.loadClass("p.C").getField("X").get(null).toString()); + } + @Messages({ "s1=Don't worry", "s2=Don''t worry about {0}", diff --git a/projectui/src/org/netbeans/modules/project/ui/groups/GroupOptionProcessor.java b/projectui/src/org/netbeans/modules/project/ui/groups/GroupOptionProcessor.java --- a/projectui/src/org/netbeans/modules/project/ui/groups/GroupOptionProcessor.java +++ b/projectui/src/org/netbeans/modules/project/ui/groups/GroupOptionProcessor.java @@ -56,24 +56,24 @@ import org.openide.util.lookup.ServiceProvider; @ServiceProvider(service=OptionProcessor.class) -@Messages({ - "GroupOptionProcessor.open.name=--open-group NAME", - "GroupOptionProcessor.open.desc=open a project group by name", - "GroupOptionProcessor.close.desc=close any open project group", - "GroupOptionProcessor.list.desc=list available project groups" -}) public class GroupOptionProcessor extends OptionProcessor { + @Messages({ + "GroupOptionProcessor.open.name=--open-group NAME", + "GroupOptionProcessor.open.desc=open a project group by name", + }) private static final Option OPEN_OPTION = Option.shortDescription( Option.displayName( Option.requiredArgument(Option.NO_SHORT_NAME, "open-group"), Bundle.class.getName(), "GroupOptionProcessor.open.name"), Bundle.class.getName(), "GroupOptionProcessor.open.desc"); + @Messages("GroupOptionProcessor.close.desc=close any open project group") private static final Option CLOSE_OPTION = Option.shortDescription( Option.withoutArgument(Option.NO_SHORT_NAME, "close-group"), Bundle.class.getName(), "GroupOptionProcessor.close.desc"); + @Messages("GroupOptionProcessor.list.desc=list available project groups") private static final Option LIST_OPTION = Option.shortDescription( Option.withoutArgument(Option.NO_SHORT_NAME, "list-groups"),