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 5010746
  28  * @summary MethodDeclaration tests
  29  * @library ../../lib
  30  * @compile -source 1.5 MethodDecl.java
  31  * @run main/othervm MethodDecl
  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 MethodDecl extends Tester {
  42 
  43     public static void main(String[] args) {
  44         (new MethodDecl()).run();
  45     }
  46 
  47 
  48     private MethodDeclaration meth1 = null;             // a method
  49     private MethodDeclaration meth2 = null;             // another method
  50 
  51     protected void init() {
  52         meth1 = getMethod("m1");
  53         meth2 = getMethod("m2");
  54     }
  55 
  56 
  57     // Declaration methods
  58 
  59     @Test(result="method")
  60     Collection<String> accept() {
  61         final Collection<String> res = new ArrayList<String>();
  62 
  63         meth1.accept(new SimpleDeclarationVisitor() {
  64             public void visitTypeDeclaration(TypeDeclaration t) {
  65                 res.add("type");
  66             }
  67             public void visitExecutableDeclaration(ExecutableDeclaration e) {
  68                 res.add("executable");
  69             }
  70             public void visitMethodDeclaration(MethodDeclaration m) {
  71                 res.add("method");
  72             }
  73             public void visitAnnotationTypeElementDeclaration(
  74                                         AnnotationTypeElementDeclaration a) {
  75                 res.add("anno type element");
  76             }
  77         });
  78         return res;
  79     }
  80 
  81     @Test(result={"@AT1"})
  82     Collection<AnnotationMirror> getAnnotationMirrors() {
  83         return meth1.getAnnotationMirrors();
  84     }
  85 
  86     @Test(result=" Sed Quis custodiet ipsos custodes?\n")
  87     String getDocComment() {
  88         return meth1.getDocComment();
  89     }
  90 
  91     @Test(result={"private", "static", "strictfp"})
  92     Collection<Modifier> getModifiers() {
  93         return meth1.getModifiers();
  94     }
  95 
  96     // Interface methods are implicitly public and abstract.
  97     @Test(result={"public", "abstract"})
  98     Collection<Modifier> getModifiersInterface() {
  99         for (TypeDeclaration t : thisClassDecl.getNestedTypes()) {
 100             for (MethodDeclaration m : t.getMethods()) {
 101                 return m.getModifiers();
 102             }
 103         }
 104         throw new AssertionError();
 105     }
 106 
 107     @Test(result="MethodDecl.java")
 108     String getPosition() {
 109         return meth1.getPosition().file().getName();
 110     }
 111 
 112     @Test(result="m2")
 113     String getSimpleName() {
 114         return meth2.getSimpleName();
 115     }
 116 
 117 
 118     // MemberDeclaration method
 119 
 120     @Test(result="MethodDecl")
 121     TypeDeclaration getDeclaringType() {
 122         return meth1.getDeclaringType();
 123     }
 124 
 125 
 126     // ExecutableDeclaration methods
 127 
 128     @Test(result={})
 129     Collection<TypeParameterDeclaration> getFormalTypeParameters1() {
 130         return meth1.getFormalTypeParameters();
 131     }
 132 
 133     @Test(result={"T", "N extends java.lang.Number"},
 134           ordered=true)
 135     Collection<TypeParameterDeclaration> getFormalTypeParameters2() {
 136         return meth2.getFormalTypeParameters();
 137     }
 138 
 139     @Test(result={})
 140     Collection<ParameterDeclaration> getParameters1() {
 141         return meth1.getParameters();
 142     }
 143 
 144     @Test(result={"N n", "java.lang.String[] ss"},
 145           ordered=true)
 146     Collection<ParameterDeclaration> getParameters2() {
 147         return meth2.getParameters();
 148     }
 149 
 150     @Test(result="true")
 151     boolean parameterEquals1() {
 152         ParameterDeclaration p1 =
 153             getMethod("m3").getParameters().iterator().next();
 154         ParameterDeclaration p2 =
 155             getMethod("m3").getParameters().iterator().next();
 156         return p1.equals(p2);
 157     }
 158 
 159     @Test(result="false")
 160     boolean parameterEquals2() {
 161         ParameterDeclaration p1 =
 162             getMethod("m3").getParameters().iterator().next();
 163         ParameterDeclaration p2 =
 164             getMethod("m4").getParameters().iterator().next();
 165         return p1.equals(p2);
 166     }
 167 
 168     @Test(result="true")
 169     boolean parameterHashCode() {
 170         ParameterDeclaration p1 =
 171             getMethod("m3").getParameters().iterator().next();
 172         ParameterDeclaration p2 =
 173             getMethod("m3").getParameters().iterator().next();
 174         return p1.hashCode() == p2.hashCode();
 175     }
 176 
 177     @Test(result={"java.lang.Throwable"})
 178     Collection<ReferenceType> getThrownTypes() {
 179         return meth2.getThrownTypes();
 180     }
 181 
 182     @Test(result="false")
 183     Boolean isVarArgs1() {
 184         return meth1.isVarArgs();
 185     }
 186 
 187     @Test(result="true")
 188     Boolean isVarArgs2() {
 189         return meth2.isVarArgs();
 190     }
 191 
 192 
 193     // MethodDeclaration methods
 194 
 195     @Test(result="void")
 196     TypeMirror getReturnType1() {
 197         return meth1.getReturnType();
 198     }
 199 
 200     @Test(result="N")
 201     TypeMirror getReturnType2() {
 202         return meth2.getReturnType();
 203     }
 204 
 205 
 206     // toString
 207 
 208     @Test(result="<T, N extends java.lang.Number> m2(N, java.lang.String...)")
 209     @Ignore("This is what it would be nice to see.")
 210     String toStringTest() {
 211         return meth2.toString();
 212     }
 213 
 214 
 215     // Declarations used by tests.
 216 
 217     /**
 218      * Sed Quis custodiet ipsos custodes?
 219      */
 220     @AT1
 221     private static strictfp void m1() {
 222     }
 223 
 224     private <T, N extends Number> N m2(N n, String... ss) throws Throwable {
 225         return null;
 226     }
 227 
 228     private void m3(String s) {
 229     }
 230 
 231     private void m4(String s) {
 232     }
 233 
 234     // A nested interface
 235     interface I {
 236         void m();
 237     }
 238 }
 239 
 240 
 241 // Annotation type used by tests.
 242 
 243 @interface AT1 {
 244 }