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 4993303 5004618 5010746
  28  * @summary InterfaceDeclaration tests
  29  * @library ../../lib
  30  * @compile -source 1.5 InterfaceDecl.java
  31  * @run main/othervm InterfaceDecl
  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 /**
  42  * Sed Quis custodiet ipsos custodes?
  43  */
  44 @AT1
  45 @AT2
  46 public class InterfaceDecl extends Tester {
  47 
  48     public static void main(String[] args) {
  49         (new InterfaceDecl()).run();
  50     }
  51 
  52 
  53     private InterfaceDeclaration iDecl = null;          // an interface
  54     private InterfaceDeclaration nested = null;         // a nested interface
  55 
  56     protected void init() {
  57         iDecl = (InterfaceDeclaration) env.getTypeDeclaration("I");
  58         nested = (InterfaceDeclaration)
  59             iDecl.getNestedTypes().iterator().next();
  60     }
  61 
  62 
  63     // Declaration methods
  64 
  65     @Test(result="interface")
  66     Collection<String> accept() {
  67         final Collection<String> res = new ArrayList<String>();
  68 
  69         iDecl.accept(new SimpleDeclarationVisitor() {
  70             public void visitTypeDeclaration(TypeDeclaration t) {
  71                 res.add("type");
  72             }
  73             public void visitClassDeclaration(ClassDeclaration c) {
  74                 res.add("class");
  75             }
  76             public void visitInterfaceDeclaration(InterfaceDeclaration e) {
  77                 res.add("interface");
  78             }
  79             public void visitAnnotationTypeDeclaration(
  80                                         AnnotationTypeDeclaration e) {
  81                 res.add("annotation type");
  82             }
  83         });
  84         return res;
  85     }
  86 
  87     @Test(result="true")
  88     boolean equals1() {
  89         return iDecl.equals(iDecl);
  90     }
  91 
  92     @Test(result="false")
  93     boolean equals2() {
  94         return iDecl.equals(nested);
  95     }
  96 
  97     @Test(result="true")
  98     boolean equals3() {
  99         return iDecl.equals(env.getTypeDeclaration("I"));
 100     }
 101 
 102 
 103     @Test(result={"@AT1", "@AT2"})
 104     Collection<AnnotationMirror> getAnnotationMirrors() {
 105         return iDecl.getAnnotationMirrors();
 106     }
 107 
 108     @Test(result=" Sed Quis custodiet ipsos custodes?\n")
 109     String getDocComment() {
 110         return iDecl.getDocComment();
 111     }
 112 
 113     // Check that interface has "abstract" modifier, even though it's implict
 114     // in the source code.
 115     @Test(result={"abstract"})
 116     Collection<Modifier> getModifiers1() {
 117         return iDecl.getModifiers();
 118     }
 119 
 120     // Check that nested interface has "static" modifier, even though
 121     // it's implicit in the source code and the VM doesn't set that bit.
 122     @Test(result={"public", "abstract", "static"})
 123     Collection<Modifier> getModifiers2() {
 124         return nested.getModifiers();
 125     }
 126 
 127     @Test(result="InterfaceDecl.java")
 128     String getPosition() {
 129         return iDecl.getPosition().file().getName();
 130     }
 131 
 132     @Test(result="I")
 133     String getSimpleName1() {
 134         return iDecl.getSimpleName();
 135     }
 136 
 137     @Test(result="Nested")
 138     String getSimpleName2() {
 139         return nested.getSimpleName();
 140     }
 141 
 142 
 143     // MemberDeclaration method
 144 
 145     @Test(result="null")
 146     TypeDeclaration getDeclaringType1() {
 147         return iDecl.getDeclaringType();
 148     }
 149 
 150     @Test(result="I<T extends java.lang.Number>")
 151     TypeDeclaration getDeclaringType2() {
 152         return nested.getDeclaringType();
 153     }
 154 
 155 
 156     // TypeDeclaration methods
 157 
 158     @Test(result={"i"})
 159     Collection<FieldDeclaration> getFields() {
 160         return iDecl.getFields();
 161     }
 162 
 163     @Test(result={"T extends java.lang.Number"})
 164     Collection<TypeParameterDeclaration> getFormalTypeParameters1() {
 165         return iDecl.getFormalTypeParameters();
 166     }
 167 
 168     @Test(result={})
 169     Collection<TypeParameterDeclaration> getFormalTypeParameters2() {
 170         return nested.getFormalTypeParameters();
 171     }
 172 
 173     // 4993303: verify policy on Object methods being visible
 174     @Test(result={"m()", "toString()"})
 175     Collection<? extends MethodDeclaration> getMethods() {
 176         return nested.getMethods();
 177     }
 178 
 179     @Test(result="I.Nested")
 180     Collection<TypeDeclaration> getNestedTypes() {
 181         return iDecl.getNestedTypes();
 182     }
 183 
 184     @Test(result="")
 185     PackageDeclaration getPackage1() {
 186         return iDecl.getPackage();
 187     }
 188 
 189     @Test(result="java.util")
 190     PackageDeclaration getPackage2() {
 191         InterfaceDeclaration set =
 192             (InterfaceDeclaration) env.getTypeDeclaration("java.util.Set");
 193         return set.getPackage();
 194     }
 195 
 196     @Test(result="I")
 197     String getQualifiedName1() {
 198         return iDecl.getQualifiedName();
 199     }
 200 
 201     @Test(result="I.Nested")
 202     String getQualifiedName2() {
 203         return nested.getQualifiedName();
 204     }
 205 
 206     @Test(result="java.util.Set")
 207     String getQualifiedName3() {
 208         InterfaceDeclaration set =
 209             (InterfaceDeclaration) env.getTypeDeclaration("java.util.Set");
 210         return set.getQualifiedName();
 211     }
 212 
 213     @Test(result="java.lang.Runnable")
 214     Collection<InterfaceType> getSuperinterfaces() {
 215         return iDecl.getSuperinterfaces();
 216     }
 217 }
 218 
 219 
 220 // Interfaces used for testing.
 221 
 222 /**
 223  * Sed Quis custodiet ipsos custodes?
 224  */
 225 @AT1
 226 @AT2
 227 interface I<T extends Number> extends Runnable {
 228     int i = 6;
 229     void m1();
 230     void m2();
 231     void m2(int j);
 232 
 233     interface Nested {
 234         void m();
 235         String toString();
 236     }
 237 }
 238 
 239 @interface AT1 {
 240 }
 241 
 242 @interface AT2 {
 243 }