001 /*
002 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.management;
027
028 import java.lang.annotation.*;
029
030 /**
031 * <p>Meta-annotation that describes how an annotation element relates
032 * to a field in a {@link Descriptor}. This can be the Descriptor for
033 * an MBean, or for an attribute, operation, or constructor in an
034 * MBean, or for a parameter of an operation or constructor.</p>
035 *
036 * <p>Consider this annotation for example:</p>
037 *
038 * <pre>
039 * @Documented
040 * @Target(ElementType.METHOD)
041 * @Retention(RetentionPolicy.RUNTIME)
042 * public @interface Units {
043 * <b>@DescriptorKey("units")</b>
044 * String value();
045 * }
046 * </pre>
047 *
048 * <p>and this use of the annotation:</p>
049 *
050 * <pre>
051 * public interface CacheControlMBean {
052 * <b>@Units("bytes")</b>
053 * public long getCacheSize();
054 * }
055 * </pre>
056 *
057 * <p>When a Standard MBean is made from the {@code CacheControlMBean},
058 * the usual rules mean that it will have an attribute called
059 * {@code CacheSize} of type {@code long}. The {@code @Units}
060 * attribute, given the above definition, will ensure that the
061 * {@link MBeanAttributeInfo} for this attribute will have a
062 * {@code Descriptor} that has a field called {@code units} with
063 * corresponding value {@code bytes}.</p>
064 *
065 * <p>Similarly, if the annotation looks like this:</p>
066 *
067 * <pre>
068 * @Documented
069 * @Target(ElementType.METHOD)
070 * @Retention(RetentionPolicy.RUNTIME)
071 * public @interface Units {
072 * <b>@DescriptorKey("units")</b>
073 * String value();
074 *
075 * <b>@DescriptorKey("descriptionResourceKey")</b>
076 * String resourceKey() default "";
077 *
078 * <b>@DescriptorKey("descriptionResourceBundleBaseName")</b>
079 * String resourceBundleBaseName() default "";
080 * }
081 * </pre>
082 *
083 * <p>and it is used like this:</p>
084 *
085 * <pre>
086 * public interface CacheControlMBean {
087 * <b>@Units("bytes",
088 * resourceKey="bytes.key",
089 * resourceBundleBaseName="com.example.foo.MBeanResources")</b>
090 * public long getCacheSize();
091 * }
092 * </pre>
093 *
094 * <p>then the resulting {@code Descriptor} will contain the following
095 * fields:</p>
096 *
097 * <table border="2">
098 * <tr><th>Name</th><th>Value</th></tr>
099 * <tr><td>units</td><td>"bytes"</td></tr>
100 * <tr><td>descriptionResourceKey</td><td>"bytes.key"</td></tr>
101 * <tr><td>descriptionResourceBundleBaseName</td>
102 * <td>"com.example.foo.MBeanResources"</td></tr>
103 * </table>
104 *
105 * <p>An annotation such as {@code @Units} can be applied to:</p>
106 *
107 * <ul>
108 * <li>a Standard MBean or MXBean interface;
109 * <li>a method in such an interface;
110 * <li>a parameter of a method in a Standard MBean or MXBean interface
111 * when that method is an operation (not a getter or setter for an attribute);
112 * <li>a public constructor in the class that implements a Standard MBean
113 * or MXBean;
114 * <li>a parameter in such a constructor.
115 * </ul>
116 *
117 * <p>Other uses of the annotation are ignored.</p>
118 *
119 * <p>Interface annotations are checked only on the exact interface
120 * that defines the management interface of a Standard MBean or an
121 * MXBean, not on its parent interfaces. Method annotations are
122 * checked only in the most specific interface in which the method
123 * appears; in other words, if a child interface overrides a method
124 * from a parent interface, only {@code @DescriptorKey} annotations in
125 * the method in the child interface are considered.
126 *
127 * <p>The Descriptor fields contributed in this way by different
128 * annotations on the same program element must be consistent. That
129 * is, two different annotations, or two members of the same
130 * annotation, must not define a different value for the same
131 * Descriptor field. Fields from annotations on a getter method must
132 * also be consistent with fields from annotations on the
133 * corresponding setter method.</p>
134 *
135 * <p>The Descriptor resulting from these annotations will be merged
136 * with any Descriptor fields provided by the implementation, such as
137 * the <a href="Descriptor.html#immutableInfo">{@code
138 * immutableInfo}</a> field for an MBean. The fields from the annotations
139 * must be consistent with these fields provided by the implementation.</p>
140 *
141 * <p>An annotation element to be converted into a descriptor field
142 * can be of any type allowed by the Java language, except an annotation
143 * or an array of annotations. The value of the field is derived from
144 * the value of the annotation element as follows:</p>
145 *
146 * <table border="2">
147 * <tr><th>Annotation element</th><th>Descriptor field</th></tr>
148 * <tr><td>Primitive value ({@code 5}, {@code false}, etc)</td>
149 * <td>Wrapped value ({@code Integer.valueOf(5)},
150 * {@code Boolean.FALSE}, etc)</td></tr>
151 * <tr><td>Class constant (e.g. {@code Thread.class})</td>
152 * <td>Class name from {@link Class#getName()}
153 * (e.g. {@code "java.lang.Thread"})</td></tr>
154 * <tr><td>Enum constant (e.g. {@link ElementType#FIELD})</td>
155 * <td>Constant name from {@link Enum#name()}
156 * (e.g. {@code "FIELD"})</td></tr>
157 * <tr><td>Array of class constants or enum constants</td>
158 * <td>String array derived by applying these rules to each
159 * element</td></tr>
160 * <tr><td>Value of any other type<br>
161 * ({@code String}, {@code String[]}, {@code int[]}, etc)</td>
162 * <td>The same value</td></tr>
163 * </table>
164 *
165 * @since 1.6
166 */
167 @Documented
168 @Retention(RetentionPolicy.RUNTIME)
169 @Target(ElementType.METHOD)
170 public @interface DescriptorKey {
171 String value();
172 }
|