src/share/jaxws_classes/com/sun/codemodel/internal/JAnnotationUse.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.codemodel.internal;
aoqi@0 27
aoqi@0 28
aoqi@0 29 import java.lang.annotation.Annotation;
aoqi@0 30 import java.util.Collections;
aoqi@0 31 import java.util.LinkedHashMap;
aoqi@0 32 import java.util.Map;
aoqi@0 33
aoqi@0 34 /**
aoqi@0 35 * Represents an annotation on a program element.
aoqi@0 36 *
aoqi@0 37 * TODO
aoqi@0 38 * How to add enums to the annotations
aoqi@0 39 * @author
aoqi@0 40 * Bhakti Mehta (bhakti.mehta@sun.com)
aoqi@0 41 */
aoqi@0 42 public final class JAnnotationUse extends JAnnotationValue {
aoqi@0 43
aoqi@0 44 /**
aoqi@0 45 * The {@link Annotation} class
aoqi@0 46 */
aoqi@0 47 private final JClass clazz;
aoqi@0 48
aoqi@0 49 /**
aoqi@0 50 * Map of member values.
aoqi@0 51 */
aoqi@0 52 private Map<String,JAnnotationValue> memberValues;
aoqi@0 53
aoqi@0 54 JAnnotationUse(JClass clazz){
aoqi@0 55 this.clazz = clazz;
aoqi@0 56 }
aoqi@0 57
aoqi@0 58 public JClass getAnnotationClass() {
aoqi@0 59 return clazz;
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 public Map<String, JAnnotationValue> getAnnotationMembers() {
aoqi@0 63 return Collections.unmodifiableMap(memberValues);
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 private JCodeModel owner() {
aoqi@0 67 return clazz.owner();
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 private void addValue(String name, JAnnotationValue annotationValue) {
aoqi@0 71 // Use ordered map to keep the code generation the same on any JVM.
aoqi@0 72 // Lazily created.
aoqi@0 73 if(memberValues==null)
aoqi@0 74 memberValues = new LinkedHashMap<String, JAnnotationValue>();
aoqi@0 75 memberValues.put(name,annotationValue);
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 /**
aoqi@0 79 * Adds a member value pair to this annotation
aoqi@0 80 *
aoqi@0 81 * @param name
aoqi@0 82 * The simple name for this annotation
aoqi@0 83 *
aoqi@0 84 * @param value
aoqi@0 85 * The boolean value for this annotation
aoqi@0 86 * @return
aoqi@0 87 * The JAnnotationUse. More member value pairs can
aoqi@0 88 * be added to it using the same or the overloaded methods.
aoqi@0 89 *
aoqi@0 90 */
aoqi@0 91 public JAnnotationUse param(String name, boolean value){
aoqi@0 92 addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
aoqi@0 93 return this;
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 /**
aoqi@0 97 * Adds a member value pair to this annotation
aoqi@0 98 * @param name
aoqi@0 99 * The simple name for this annotation
aoqi@0 100 *
aoqi@0 101 * @param value
aoqi@0 102 * The byte member value for this annotation
aoqi@0 103 * @return
aoqi@0 104 * The JAnnotationUse. More member value pairs can
aoqi@0 105 * be added to it using the same or the overloaded methods.
aoqi@0 106 *
aoqi@0 107 */
aoqi@0 108 public JAnnotationUse param(String name, byte value){
aoqi@0 109 addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
aoqi@0 110 return this;
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 /**
aoqi@0 114 * Adds a member value pair to this annotation
aoqi@0 115 * @param name
aoqi@0 116 * The simple name for this annotation
aoqi@0 117 *
aoqi@0 118 * @param value
aoqi@0 119 * The char member value for this annotation
aoqi@0 120 * @return
aoqi@0 121 * The JAnnotationUse. More member value pairs can
aoqi@0 122 * be added to it using the same or the overloaded methods.
aoqi@0 123 *
aoqi@0 124 */
aoqi@0 125 public JAnnotationUse param(String name, char value){
aoqi@0 126 addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
aoqi@0 127 return this;
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 /**
aoqi@0 131 * Adds a member value pair to this annotation
aoqi@0 132 * @param name
aoqi@0 133 * The simple name for this annotation
aoqi@0 134 *
aoqi@0 135 * @param value
aoqi@0 136 * The double member value for this annotation
aoqi@0 137 * @return
aoqi@0 138 * The JAnnotationUse. More member value pairs can
aoqi@0 139 * be added to it using the same or the overloaded methods.
aoqi@0 140 *
aoqi@0 141 */
aoqi@0 142 public JAnnotationUse param(String name, double value){
aoqi@0 143 addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
aoqi@0 144 return this;
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 /**
aoqi@0 148 * Adds a member value pair to this annotation
aoqi@0 149 * @param name
aoqi@0 150 * The simple name for this annotation
aoqi@0 151 *
aoqi@0 152 * @param value
aoqi@0 153 * The float member value for this annotation
aoqi@0 154 * @return
aoqi@0 155 * The JAnnotationUse. More member value pairs can
aoqi@0 156 * be added to it using the same or the overloaded methods.
aoqi@0 157 *
aoqi@0 158 */
aoqi@0 159 public JAnnotationUse param(String name, float value){
aoqi@0 160 addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
aoqi@0 161 return this;
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 /**
aoqi@0 165 * Adds a member value pair to this annotation
aoqi@0 166 * @param name
aoqi@0 167 * The simple name for this annotation
aoqi@0 168 *
aoqi@0 169 * @param value
aoqi@0 170 * The long member value for this annotation
aoqi@0 171 * @return
aoqi@0 172 * The JAnnotationUse. More member value pairs can
aoqi@0 173 * be added to it using the same or the overloaded methods.
aoqi@0 174 *
aoqi@0 175 */
aoqi@0 176 public JAnnotationUse param(String name, long value){
aoqi@0 177 addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
aoqi@0 178 return this;
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 /**
aoqi@0 182 * Adds a member value pair to this annotation
aoqi@0 183 * @param name
aoqi@0 184 * The simple name for this annotation
aoqi@0 185 *
aoqi@0 186 * @param value
aoqi@0 187 * The short member value for this annotation
aoqi@0 188 * @return
aoqi@0 189 * The JAnnotationUse. More member value pairs can
aoqi@0 190 * be added to it using the same or the overloaded methods.
aoqi@0 191 *
aoqi@0 192 */
aoqi@0 193 public JAnnotationUse param(String name, short value){
aoqi@0 194 addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
aoqi@0 195 return this;
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 /**
aoqi@0 199 * Adds a member value pair to this annotation
aoqi@0 200 * @param name
aoqi@0 201 * The simple name for this annotation
aoqi@0 202 *
aoqi@0 203 * @param value
aoqi@0 204 * The int member value for this annotation
aoqi@0 205 * @return
aoqi@0 206 * The JAnnotationUse. More member value pairs can
aoqi@0 207 * be added to it using the same or the overloaded methods.
aoqi@0 208 *
aoqi@0 209 */
aoqi@0 210 public JAnnotationUse param(String name, int value){
aoqi@0 211 addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
aoqi@0 212 return this;
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 /**
aoqi@0 216 * Adds a member value pair to this annotation
aoqi@0 217 * @param name
aoqi@0 218 * The simple name for this annotation
aoqi@0 219 *
aoqi@0 220 * @param value
aoqi@0 221 * The String member value for this annotation
aoqi@0 222 * @return
aoqi@0 223 * The JAnnotationUse. More member value pairs can
aoqi@0 224 * be added to it using the same or the overloaded methods.
aoqi@0 225 *
aoqi@0 226 */
aoqi@0 227 public JAnnotationUse param(String name, String value){
aoqi@0 228 //Escape string values with quotes so that they can
aoqi@0 229 //be generated accordingly
aoqi@0 230 addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
aoqi@0 231 return this;
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 /**
aoqi@0 235 * Adds a member value pair to this annotation
aoqi@0 236 * For adding class values as param
aoqi@0 237 * @see #param(String, Class)
aoqi@0 238 * @param name
aoqi@0 239 * The simple name for this annotation
aoqi@0 240 *
aoqi@0 241 * @param value
aoqi@0 242 * The annotation class which is member value for this annotation
aoqi@0 243 * @return
aoqi@0 244 * The JAnnotationUse. More member value pairs can
aoqi@0 245 * be added to it using the same or the overloaded methods.
aoqi@0 246 *
aoqi@0 247 */
aoqi@0 248 public JAnnotationUse annotationParam(String name, Class<? extends Annotation> value) {
aoqi@0 249 JAnnotationUse annotationUse = new JAnnotationUse(owner().ref(value));
aoqi@0 250 addValue(name, annotationUse);
aoqi@0 251 return annotationUse;
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 /**
aoqi@0 255 * Adds a member value pair to this annotation
aoqi@0 256 * @param name
aoqi@0 257 * The simple name for this annotation
aoqi@0 258 *
aoqi@0 259 * @param value
aoqi@0 260 * The enum class which is member value for this annotation
aoqi@0 261 * @return
aoqi@0 262 * The JAnnotationUse. More member value pairs can
aoqi@0 263 * be added to it using the same or the overloaded methods.
aoqi@0 264 *
aoqi@0 265 */
aoqi@0 266 public JAnnotationUse param(String name, final Enum<?> value) {
aoqi@0 267 addValue(name, new JAnnotationValue() {
aoqi@0 268 public void generate(JFormatter f) {
aoqi@0 269 f.t(owner().ref(value.getDeclaringClass())).p('.').p(value.name());
aoqi@0 270 }
aoqi@0 271 });
aoqi@0 272 return this;
aoqi@0 273 }
aoqi@0 274
aoqi@0 275 /**
aoqi@0 276 * Adds a member value pair to this annotation
aoqi@0 277 * @param name
aoqi@0 278 * The simple name for this annotation
aoqi@0 279 *
aoqi@0 280 * @param value
aoqi@0 281 * The JEnumConstant which is member value for this annotation
aoqi@0 282 * @return
aoqi@0 283 * The JAnnotationUse. More member value pairs can
aoqi@0 284 * be added to it using the same or the overloaded methods.
aoqi@0 285 *
aoqi@0 286 */
aoqi@0 287 public JAnnotationUse param(String name, JEnumConstant value){
aoqi@0 288 addValue(name, new JAnnotationStringValue(value));
aoqi@0 289 return this;
aoqi@0 290 }
aoqi@0 291
aoqi@0 292 /**
aoqi@0 293 * Adds a member value pair to this annotation
aoqi@0 294 * This can be used for e.g to specify
aoqi@0 295 * <pre>
aoqi@0 296 * &#64;XmlCollectionItem(type=Integer.class);
aoqi@0 297 * <pre>
aoqi@0 298 * For adding a value of Class<? extends Annotation>
aoqi@0 299 * @link
aoqi@0 300 * #annotationParam(java.lang.String, java.lang.Class<? extends java.lang.annotation.Annotation>)
aoqi@0 301 * @param name
aoqi@0 302 * The simple name for this annotation param
aoqi@0 303 *
aoqi@0 304 * @param value
aoqi@0 305 * The class type of the param
aoqi@0 306 * @return
aoqi@0 307 * The JAnnotationUse. More member value pairs can
aoqi@0 308 * be added to it using the same or the overloaded methods.
aoqi@0 309 *
aoqi@0 310 *
aoqi@0 311 *
aoqi@0 312 */
aoqi@0 313 public JAnnotationUse param(String name, final Class<?> value){
aoqi@0 314 addValue(name, new JAnnotationStringValue(
aoqi@0 315 new JExpressionImpl() {
aoqi@0 316 public void generate(JFormatter f) {
aoqi@0 317 f.p(value.getName().replace('$', '.'));
aoqi@0 318 f.p(".class");
aoqi@0 319 }
aoqi@0 320 }));
aoqi@0 321 return this;
aoqi@0 322 }
aoqi@0 323
aoqi@0 324 /**
aoqi@0 325 * Adds a member value pair to this annotation based on the
aoqi@0 326 * type represented by the given JType
aoqi@0 327 *
aoqi@0 328 * @param name The simple name for this annotation param
aoqi@0 329 * @param type the JType representing the actual type
aoqi@0 330 * @return The JAnnotationUse. More member value pairs can
aoqi@0 331 * be added to it using the same or the overloaded methods.
aoqi@0 332 */
aoqi@0 333 public JAnnotationUse param(String name, JType type){
aoqi@0 334 JClass c = type.boxify();
aoqi@0 335 addValue(name, new JAnnotationStringValue ( c.dotclass() ));
aoqi@0 336 return this;
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 /**
aoqi@0 340 * Adds a member value pair to this annotation.
aoqi@0 341 * @param name
aoqi@0 342 * The simple name for this annotation
aoqi@0 343 *
aoqi@0 344 * @param value
aoqi@0 345 * The JExpression which provides the contant value for this annotation
aoqi@0 346 * @return
aoqi@0 347 * The JAnnotationUse. More member value pairs can
aoqi@0 348 * be added to it using the same or the overloaded methods.
aoqi@0 349 *
aoqi@0 350 */
aoqi@0 351 public JAnnotationUse param(String name, JExpression value){
aoqi@0 352 addValue(name, new JAnnotationStringValue(value));
aoqi@0 353 return this;
aoqi@0 354 }
aoqi@0 355
aoqi@0 356 /**
aoqi@0 357 * Adds a member value pair which is of type array to this annotation
aoqi@0 358 * @param name
aoqi@0 359 * The simple name for this annotation
aoqi@0 360 *
aoqi@0 361 * @return
aoqi@0 362 * The JAnnotationArrayMember. For adding array values
aoqi@0 363 * @see JAnnotationArrayMember
aoqi@0 364 *
aoqi@0 365 */
aoqi@0 366 public JAnnotationArrayMember paramArray(String name){
aoqi@0 367 JAnnotationArrayMember arrayMember = new JAnnotationArrayMember(owner());
aoqi@0 368 addValue(name, arrayMember);
aoqi@0 369 return arrayMember;
aoqi@0 370 }
aoqi@0 371
aoqi@0 372
aoqi@0 373 // /**
aoqi@0 374 // * This can be used to add annotations inside annotations
aoqi@0 375 // * for e.g &#64;XmlCollection(values= &#64;XmlCollectionItem(type=Foo.class))
aoqi@0 376 // * @param className
aoqi@0 377 // * The classname of the annotation to be included
aoqi@0 378 // * @return
aoqi@0 379 // * The JAnnotationUse that can be used as a member within this JAnnotationUse
aoqi@0 380 // * @deprecated
aoqi@0 381 // * use {@link JAnnotationArrayMember#annotate}
aoqi@0 382 // */
aoqi@0 383 // public JAnnotationUse annotate(String className) {
aoqi@0 384 // JAnnotationUse annotationUse = new JAnnotationUse(owner().ref(className));
aoqi@0 385 // return annotationUse;
aoqi@0 386 // }
aoqi@0 387
aoqi@0 388 /**
aoqi@0 389 * This can be used to add annotations inside annotations
aoqi@0 390 * for e.g &#64;XmlCollection(values= &#64;XmlCollectionItem(type=Foo.class))
aoqi@0 391 * @param clazz
aoqi@0 392 * The annotation class to be included
aoqi@0 393 * @return
aoqi@0 394 * The JAnnotationUse that can be used as a member within this JAnnotationUse
aoqi@0 395 * @deprecated
aoqi@0 396 * use {@link JAnnotationArrayMember#annotate}
aoqi@0 397 */
aoqi@0 398 public JAnnotationUse annotate(Class <? extends Annotation> clazz) {
aoqi@0 399 JAnnotationUse annotationUse = new JAnnotationUse(owner().ref(clazz));
aoqi@0 400 return annotationUse;
aoqi@0 401 }
aoqi@0 402
aoqi@0 403 public void generate(JFormatter f) {
aoqi@0 404 f.p('@').g(clazz);
aoqi@0 405 if(memberValues!=null) {
aoqi@0 406 f.p('(');
aoqi@0 407 boolean first = true;
aoqi@0 408
aoqi@0 409 if(isOptimizable()) {
aoqi@0 410 // short form
aoqi@0 411 f.g(memberValues.get("value"));
aoqi@0 412 } else {
aoqi@0 413 for (Map.Entry<String, JAnnotationValue> mapEntry : memberValues.entrySet()) {
aoqi@0 414 if (!first) f.p(',');
aoqi@0 415 f.p(mapEntry.getKey()).p('=').g(mapEntry.getValue());
aoqi@0 416 first = false;
aoqi@0 417 }
aoqi@0 418 }
aoqi@0 419 f.p(')');
aoqi@0 420 }
aoqi@0 421 }
aoqi@0 422
aoqi@0 423 private boolean isOptimizable() {
aoqi@0 424 return memberValues.size()==1 && memberValues.containsKey("value");
aoqi@0 425 }
aoqi@0 426 }

mercurial