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 4993299 5010385 5014539
  28  * @summary AnnotationTypeElementDeclaration tests
  29  * @library ../../lib
  30  * @compile -source 1.5 AnnoTypeElemDecl.java
  31  * @run main/othervm AnnoTypeElemDecl
  32  */
  33 
  34 
  35 import java.util.*;
  36 import com.sun.mirror.declaration.*;
  37 import com.sun.mirror.type.*;
  38 import com.sun.mirror.util.*;
  39 
  40 
  41 public class AnnoTypeElemDecl extends Tester {
  42 
  43     public static void main(String[] args) {
  44         (new AnnoTypeElemDecl()).run();
  45     }
  46 
  47 
  48     // Some annotation type elements to use.
  49     private AnnotationTypeElementDeclaration elem1 = null;      // s()
  50     private AnnotationTypeElementDeclaration elem2 = null;      // i()
  51     private AnnotationTypeElementDeclaration elem3 = null;      // b()
  52 
  53     protected void init() {
  54         for (TypeDeclaration at : thisClassDecl.getNestedTypes()) {
  55             for (MethodDeclaration meth : at.getMethods()) {
  56                 AnnotationTypeElementDeclaration elem =
  57                     (AnnotationTypeElementDeclaration) meth;
  58                 if (elem.getSimpleName().equals("s")) {
  59                     elem1 = elem;       // s()
  60                 } else if (elem.getSimpleName().equals("i")) {
  61                     elem2 = elem;       // i()
  62                 } else {
  63                     elem3 = elem;       // b()
  64                 }
  65             }
  66         }
  67     }
  68 
  69 
  70     // Declaration methods
  71 
  72     @Test(result="anno type element")
  73     Collection<String> accept() {
  74         final Collection<String> res = new ArrayList<String>();
  75 
  76         elem1.accept(new SimpleDeclarationVisitor() {
  77             public void visitTypeDeclaration(TypeDeclaration t) {
  78                 res.add("type");
  79             }
  80             public void visitExecutableDeclaration(ExecutableDeclaration e) {
  81                 res.add("executable");
  82             }
  83             public void visitMethodDeclaration(MethodDeclaration m) {
  84                 res.add("method");
  85             }
  86             public void visitAnnotationTypeElementDeclaration(
  87                                         AnnotationTypeElementDeclaration a) {
  88                 res.add("anno type element");
  89             }
  90         });
  91         return res;
  92     }
  93 
  94     @Test(result={"@AnnoTypeElemDecl.AT2"})
  95     Collection<AnnotationMirror> getAnnotationMirrors() {
  96         return elem1.getAnnotationMirrors();
  97     }
  98 
  99     @Test(result=" Sed Quis custodiet ipsos custodes?\n")
 100     String getDocComment() {
 101         return elem1.getDocComment();
 102     }
 103 
 104     @Test(result={"public", "abstract"})
 105     Collection<Modifier> getModifiers() {
 106         return elem1.getModifiers();
 107     }
 108 
 109     @Test(result="AnnoTypeElemDecl.java")
 110     String getPosition() {
 111         return elem1.getPosition().file().getName();
 112     }
 113 
 114     @Test(result="s")
 115     String getSimpleName() {
 116         return elem1.getSimpleName();
 117     }
 118 
 119 
 120     // MemberDeclaration method
 121 
 122     @Test(result="AnnoTypeElemDecl.AT1")
 123     TypeDeclaration getDeclaringType() {
 124         return elem1.getDeclaringType();
 125     }
 126 
 127 
 128     // ExecutableDeclaration methods
 129 
 130     @Test(result={})
 131     Collection<TypeParameterDeclaration> getFormalTypeParameters() {
 132         return elem1.getFormalTypeParameters();
 133     }
 134 
 135     @Test(result={})
 136     Collection<ParameterDeclaration> getParameters() {
 137         return elem1.getParameters();
 138     }
 139 
 140     @Test(result={})
 141     Collection<ReferenceType> getThrownTypes() {
 142         return elem1.getThrownTypes();
 143     }
 144 
 145     @Test(result="false")
 146     Boolean isVarArgs() {
 147         return elem1.isVarArgs();
 148     }
 149 
 150 
 151     // AnnotationTypeElementDeclaration method
 152 
 153     @Test(result="\"default\"")
 154     AnnotationValue getDefaultValue1() {
 155         return elem1.getDefaultValue();
 156     }
 157 
 158     @Test(result="null")
 159     AnnotationValue getDefaultValue2() {
 160         return elem2.getDefaultValue();
 161     }
 162 
 163     // 5010385: getValue() returns null for boolean type elements
 164     @Test(result="false")
 165     Boolean getDefaultValue3() {
 166         return (Boolean) elem3.getDefaultValue().getValue();
 167     }
 168 
 169 
 170     // toString
 171 
 172     @Test(result="s()")
 173     String toStringTest() {
 174         return elem1.toString();
 175     }
 176 
 177 
 178     // Declarations used by tests.
 179 
 180     @interface AT1 {
 181         /**
 182          * Sed Quis custodiet ipsos custodes?
 183          */
 184         @AT2
 185         String s() default "default";
 186 
 187         int i();
 188 
 189         boolean b() default false;
 190     }
 191 
 192     @interface AT2 {
 193     }
 194 }