Path: blob/master/src/java.management/share/classes/javax/management/DescriptorKey.java
41155 views
/*1* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.management;2627import java.lang.annotation.*;2829/**30* <p>Meta-annotation that describes how an annotation element relates31* to a field in a {@link Descriptor}. This can be the Descriptor for32* an MBean, or for an attribute, operation, or constructor in an33* MBean, or for a parameter of an operation or constructor.</p>34*35* <p>Consider this annotation for example:</p>36*37* <pre>38* @Documented39* @Target(ElementType.METHOD)40* @Retention(RetentionPolicy.RUNTIME)41* public @interface Units {42* <b>@DescriptorKey("units")</b>43* String value();44* }45* </pre>46*47* <p>and this use of the annotation:</p>48*49* <pre>50* public interface CacheControlMBean {51* <b>@Units("bytes")</b>52* public long getCacheSize();53* }54* </pre>55*56* <p>When a Standard MBean is made from the {@code CacheControlMBean},57* the usual rules mean that it will have an attribute called58* {@code CacheSize} of type {@code long}. The {@code @Units}59* annotation, given the above definition, will ensure that the60* {@link MBeanAttributeInfo} for this attribute will have a61* {@code Descriptor} that has a field called {@code units} with62* corresponding value {@code bytes}.</p>63*64* <p>Similarly, if the annotation looks like this:</p>65*66* <pre>67* @Documented68* @Target(ElementType.METHOD)69* @Retention(RetentionPolicy.RUNTIME)70* public @interface Units {71* <b>@DescriptorKey("units")</b>72* String value();73*74* <b>@DescriptorKey("descriptionResourceKey")</b>75* String resourceKey() default "";76*77* <b>@DescriptorKey("descriptionResourceBundleBaseName")</b>78* String resourceBundleBaseName() default "";79* }80* </pre>81*82* <p>and it is used like this:</p>83*84* <pre>85* public interface CacheControlMBean {86* <b>@Units("bytes",87* resourceKey="bytes.key",88* resourceBundleBaseName="com.example.foo.MBeanResources")</b>89* public long getCacheSize();90* }91* </pre>92*93* <p>then the resulting {@code Descriptor} will contain the following94* fields:</p>95*96* <table class="striped">97* <caption style="display:none">Descriptor Fields</caption>98* <thead>99* <tr><th scope="col">Name</th><th scope="col">Value</th></tr>100* </thead>101* <tbody style="text-align:left">102* <tr><th scope="row">units</th><td>"bytes"</td></tr>103* <tr><th scope="row">descriptionResourceKey</th><td>"bytes.key"</td></tr>104* <tr><th scope="row">descriptionResourceBundleBaseName</th>105* <td>"com.example.foo.MBeanResources"</td></tr>106* </tbody>107* </table>108*109* <p>An annotation such as {@code @Units} can be applied to:</p>110*111* <ul>112* <li>a Standard MBean or MXBean interface;113* <li>a method in such an interface;114* <li>a parameter of a method in a Standard MBean or MXBean interface115* when that method is an operation (not a getter or setter for an attribute);116* <li>a public constructor in the class that implements a Standard MBean117* or MXBean;118* <li>a parameter in such a constructor.119* </ul>120*121* <p>Other uses of the annotation are ignored.</p>122*123* <p>Interface annotations are checked only on the exact interface124* that defines the management interface of a Standard MBean or an125* MXBean, not on its parent interfaces. Method annotations are126* checked only in the most specific interface in which the method127* appears; in other words, if a child interface overrides a method128* from a parent interface, only {@code @DescriptorKey} annotations in129* the method in the child interface are considered.130*131* <p>The Descriptor fields contributed in this way by different132* annotations on the same program element must be consistent. That133* is, two different annotations, or two members of the same134* annotation, must not define a different value for the same135* Descriptor field. Fields from annotations on a getter method must136* also be consistent with fields from annotations on the137* corresponding setter method.</p>138*139* <p>The Descriptor resulting from these annotations will be merged140* with any Descriptor fields provided by the implementation, such as141* the <a href="Descriptor.html#immutableInfo">{@code142* immutableInfo}</a> field for an MBean. The fields from the annotations143* must be consistent with these fields provided by the implementation.</p>144*145* <p>An annotation element to be converted into a descriptor field146* can be of any type allowed by the Java language, except an annotation147* or an array of annotations. The value of the field is derived from148* the value of the annotation element as follows:</p>149*150* <table class="striped">151* <caption style="display:none">Descriptor Field Types</caption>152* <thead>153* <tr><th scope="col">Annotation element</th><th scope="col">Descriptor field</th></tr>154* </thead>155* <tbody style="text-align:left">156* <tr><th scope="row">Primitive value ({@code 5}, {@code false}, etc)</th>157* <td>Wrapped value ({@code Integer.valueOf(5)},158* {@code Boolean.FALSE}, etc)</td></tr>159* <tr><th scope="row">Class constant (e.g. {@code Thread.class})</th>160* <td>Class name from {@link Class#getName()}161* (e.g. {@code "java.lang.Thread"})</td></tr>162* <tr><th scope="row">Enum constant (e.g. {@link ElementType#FIELD})</th>163* <td>Constant name from {@link Enum#name()}164* (e.g. {@code "FIELD"})</td></tr>165* <tr><th scope="row">Array of class constants or enum constants</th>166* <td>String array derived by applying these rules to each167* element</td></tr>168* <tr><th scope="row">Value of any other type<br>169* ({@code String}, {@code String[]}, {@code int[]}, etc)</th>170* <td>The same value</td></tr>171* </tbody>172* </table>173*174* @since 1.6175*/176@Documented177@Retention(RetentionPolicy.RUNTIME)178@Target(ElementType.METHOD)179public @interface DescriptorKey {180/**181* Returns the descriptor key.182* @return the descriptor key183*/184String value();185}186187188