1 /*
   2  * Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /*
  26  * @test
  27  * @bug 4853450 5014539 5034991
  28  * @summary Tests AnnotationValue methods.
  29  * @library ../../lib
  30  * @compile -source 1.5 AnnoVal.java
  31  * @run main/othervm AnnoVal
  32  */
  33 
  34 
  35 import java.util.*;
  36 import com.sun.mirror.declaration.*;
  37 import com.sun.mirror.type.*;
  38 
  39 
  40 public class AnnoVal extends Tester {
  41 
  42     public static void main(String[] args) {
  43         (new AnnoVal()).run();
  44     }
  45 
  46     @Test(result={
  47         "i Integer 2",
  48         "l Long 4294967296",
  49         "d Double 3.14",
  50         "b Boolean true",
  51         "c Character @",
  52         "s String sigh",
  53         // The following results reflect some implementation details.
  54         "k ClassTypeImpl java.lang.Boolean",
  55         "kb PrimitiveTypeImpl boolean",
  56         "ka ArrayTypeImpl java.lang.Boolean[]",
  57         "kab ArrayTypeImpl int[][]",
  58         "w ClassTypeImpl java.lang.Long",
  59         "e EnumConstantDeclarationImpl TYPE",
  60         "sa ArrayList [\"up\", \"down\"]",
  61         "a AnnotationMirrorImpl @AT1"})
  62     @AT2(i = 1 + 1,
  63          l = 1024 * 1024 * 1024 * 4L,
  64          d = 3.14,
  65          b = true,
  66          c = '@',
  67          s = "sigh",
  68          k = Boolean.class,
  69          kb = boolean.class,
  70          ka = Boolean[].class,          // bugid 5020899
  71          kab = int[][].class,           //      "
  72          w = Long.class,
  73          e = java.lang.annotation.ElementType.TYPE,
  74          sa = {"up", "down"},
  75          a = @AT1)
  76     Collection<String> getValue() {
  77         Collection<String> res = new ArrayList<String>();
  78         AnnotationMirror anno = getAnno("getValue", "AT2");
  79 
  80         for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> e :
  81                  anno.getElementValues().entrySet()) {
  82             Object val = e.getValue().getValue();
  83             res.add(String.format("%s %s %s",
  84                                   e.getKey().getSimpleName(),
  85                                   simpleClassName(val),
  86                                   val));
  87         }
  88         return res;
  89     }
  90 
  91     @Test(result={
  92         "int i 2",
  93         "long l 4294967296L",
  94         "double d 3.14",
  95         "boolean b true",
  96         "char c '@'",
  97         "java.lang.String s \"sigh\"",
  98         "java.lang.Class k java.lang.Boolean.class",
  99         "java.lang.Class kb boolean.class",
 100         "java.lang.Class ka java.lang.Boolean[].class",
 101         "java.lang.Class kab int[][].class",
 102         "java.lang.Class<? extends java.lang.Number> w java.lang.Long.class",
 103         "java.lang.annotation.ElementType e java.lang.annotation.ElementType.TYPE",
 104         "java.lang.String[] sa {\"up\", \"down\"}",
 105         "AT1 a @AT1"})
 106     Collection<String> toStringTests() {
 107         Collection<String> res = new ArrayList<String>();
 108         AnnotationMirror anno = getAnno("getValue", "AT2");
 109 
 110         for (Map.Entry<AnnotationTypeElementDeclaration,AnnotationValue> e :
 111                  anno.getElementValues().entrySet()) {
 112             res.add(String.format("%s %s %s",
 113                                   e.getKey().getReturnType(),
 114                                   e.getKey().getSimpleName(),
 115                                   e.getValue().toString()));
 116         }
 117         return res;
 118     }
 119 
 120     @Test(result={
 121         "byte b 0x0b",
 122         "float f 3.0f",
 123         "double nan 0.0/0.0",
 124         "double hi 1.0/0.0",
 125         "float lo -1.0f/0.0f",
 126         "char newline '\\n'",
 127         "char ff '\\u00ff'",
 128         "java.lang.String s \"\\\"high\\tlow\\\"\"",
 129         "java.lang.String smiley \"\\u263a\""})
 130     @AT3(b = 11,
 131          f = 3,
 132          nan = 0.0/0.0,
 133          hi = 1.0/0.0,
 134          lo = -1.0f/0.0f,
 135          newline = '\n',
 136          ff = '\u00FF',
 137          s = "\"high\tlow\"",
 138          smiley = "\u263A")
 139     Collection<String> toStringFancy() {
 140         Collection<String> res = new ArrayList<String>();
 141         AnnotationMirror anno = getAnno("toStringFancy", "AT3");
 142 
 143         for (Map.Entry<AnnotationTypeElementDeclaration,AnnotationValue> e :
 144                  anno.getElementValues().entrySet()) {
 145             res.add(String.format("%s %s %s",
 146                                   e.getKey().getReturnType(),
 147                                   e.getKey().getSimpleName(),
 148                                   e.getValue().toString()));
 149         }
 150         return res;
 151     }
 152 
 153 
 154     /**
 155      * Returns the simple name of an object's class.
 156      */
 157     private String simpleClassName(Object o) {
 158         return (o == null)
 159             ? "null"
 160             : o.getClass().getName().replaceFirst(".*\\.", "");
 161     }
 162 }
 163 
 164 
 165 /*
 166  * Annotations used for testing.
 167  */
 168 
 169 @interface AT1 {
 170     String value() default "";
 171 }
 172 
 173 @interface AT2 {
 174     int i();
 175     long l();
 176     double d();
 177     boolean b();
 178     char c();
 179     String s();
 180     Class k();
 181     Class kb();
 182     Class ka();
 183     Class kab();
 184     Class<? extends Number> w();
 185     java.lang.annotation.ElementType e();
 186     String[] sa();
 187     AT1 a();
 188 }
 189 
 190 @interface AT3 {
 191     byte b();
 192     float f();
 193     double nan();
 194     double hi();
 195     float lo();
 196     char newline();
 197     char ff();
 198     String s();
 199     String smiley();
 200 }