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 5037165
  28  * @summary Test the Declarations.overrides method
  29  * @library ../../lib
  30  * @run main/othervm Overrides
  31  */
  32 
  33 
  34 import java.util.*;
  35 import com.sun.mirror.declaration.*;
  36 import com.sun.mirror.type.*;
  37 import com.sun.mirror.util.*;
  38 
  39 
  40 public class Overrides extends Tester {
  41 
  42     public static void main(String[] args) {
  43         (new Overrides()).run();
  44     }
  45 
  46 
  47     // Declarations used by tests
  48 
  49     static class A {
  50         void m1(int i) {};              // does not override itself
  51         void m2(int i) {};
  52         static void m3(int i) {};
  53     }
  54 
  55         static class B extends A {
  56             void m1(int j) {};          // overrides A.m1
  57             void m1(String i) {};       // does not override A.m1
  58             void m4(int i) {};          // does not override A.m1
  59         }
  60 
  61             static class C extends B {
  62                 void m1(int i) {};      // overrides A.m1 and B.m1
  63                 void m2(int i) {};      // overrides A.m2
  64             }
  65 
  66         static class D extends A {
  67             static void m3(int i) {};   // does not override A.m3
  68         }
  69 
  70     static class E {
  71         void m1(int i) {};              // does not override A.m1
  72     }
  73 
  74 
  75 
  76     private Declarations decls;
  77 
  78     private TypeDeclaration A;
  79     private TypeDeclaration B;
  80     private TypeDeclaration C;
  81     private TypeDeclaration D;
  82     private TypeDeclaration E;
  83     private MethodDeclaration Am1;
  84     private MethodDeclaration Am2;
  85     private MethodDeclaration Am3;
  86     private MethodDeclaration Bm1;
  87     private MethodDeclaration Bm1b;
  88     private MethodDeclaration Bm4;
  89     private MethodDeclaration Cm1;
  90     private MethodDeclaration Cm2;
  91     private MethodDeclaration Dm3;
  92     private MethodDeclaration Em1;
  93 
  94     protected void init() {
  95         decls = env.getDeclarationUtils();
  96 
  97         A = env.getTypeDeclaration("Overrides.A");
  98         B = env.getTypeDeclaration("Overrides.B");
  99         C = env.getTypeDeclaration("Overrides.C");
 100         D = env.getTypeDeclaration("Overrides.D");
 101         E = env.getTypeDeclaration("Overrides.E");
 102 
 103         Am1  = getMethod(A, "m1", "i");
 104         Am2  = getMethod(A, "m2", "i");
 105         Am3  = getMethod(A, "m3", "i");
 106         Bm1  = getMethod(B, "m1", "j");
 107         Bm1b = getMethod(B, "m1", "i");
 108         Bm4  = getMethod(B, "m4", "i");
 109         Cm1  = getMethod(C, "m1", "i");
 110         Cm2  = getMethod(C, "m2", "i");
 111         Dm3  = getMethod(D, "m3", "i");
 112         Em1  = getMethod(E, "m1", "i");
 113     }
 114 
 115     private MethodDeclaration getMethod(TypeDeclaration t,
 116                                         String methodName, String paramName) {
 117         for (MethodDeclaration m : t.getMethods()) {
 118             if (methodName.equals(m.getSimpleName()) &&
 119                     paramName.equals(m.getParameters().iterator().next()
 120                                                         .getSimpleName())) {
 121                 return m;
 122             }
 123         }
 124         throw new AssertionError();
 125     }
 126 
 127 
 128     // Declarations methods
 129 
 130     @Test(result={"false",
 131                   "true",
 132                   "false",
 133                   "false",
 134                   "true",
 135                   "true",
 136                   "true",
 137                   "false",
 138                   "false"},
 139           ordered=true)
 140     List<Boolean> overrides() {
 141         return Arrays.asList(
 142                 decls.overrides(Am1, Am1),
 143                 decls.overrides(Bm1, Am1),
 144                 decls.overrides(Bm1b,Am1),
 145                 decls.overrides(Bm4, Am1),
 146                 decls.overrides(Cm1, Am1),
 147                 decls.overrides(Cm1, Bm1),
 148                 decls.overrides(Cm2, Am2),
 149                 decls.overrides(Dm3, Am3),
 150                 decls.overrides(Em1, Am1));
 151     }
 152 }