|
1 /* |
|
2 * Copyright (c) 2006, 2010, 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 * @test |
|
26 * @bug 6453386 |
|
27 * @summary Verify that example code in Elements.overrides works as spec'ed. |
|
28 * @author Scott Seligman |
|
29 * @library /tools/javac/lib |
|
30 * @build JavacTestingAbstractProcessor |
|
31 * @compile -g OverridesSpecEx.java |
|
32 * @compile -processor OverridesSpecEx -proc:only OverridesSpecEx.java |
|
33 */ |
|
34 |
|
35 import java.util.Set; |
|
36 import javax.annotation.processing.*; |
|
37 import javax.lang.model.SourceVersion; |
|
38 import javax.lang.model.element.*; |
|
39 import javax.lang.model.type.*; |
|
40 import javax.lang.model.util.*; |
|
41 |
|
42 import static javax.lang.model.util.ElementFilter.*; |
|
43 |
|
44 public class OverridesSpecEx extends JavacTestingAbstractProcessor { |
|
45 public boolean process(Set<? extends TypeElement> annoTypes, |
|
46 RoundEnvironment round) { |
|
47 if (!round.processingOver()) |
|
48 doit(annoTypes, round); |
|
49 return true; |
|
50 } |
|
51 |
|
52 private void doit(Set<? extends TypeElement> annoTypes, |
|
53 RoundEnvironment round) { |
|
54 TypeElement string = elements.getTypeElement("java.lang.String"); |
|
55 TypeElement object = elements.getTypeElement("java.lang.Object"); |
|
56 |
|
57 ExecutableElement m1 = null; |
|
58 ExecutableElement m2 = null; |
|
59 for (ExecutableElement m : methodsIn(string.getEnclosedElements())) { |
|
60 if (m.getSimpleName().contentEquals("hashCode")) { |
|
61 m1 = m; |
|
62 break; |
|
63 } |
|
64 } |
|
65 for (ExecutableElement m : methodsIn(object.getEnclosedElements())) { |
|
66 if (m.getSimpleName().contentEquals("hashCode")) { |
|
67 m2 = m; |
|
68 break; |
|
69 } |
|
70 } |
|
71 |
|
72 boolean res = |
|
73 elements.overrides(m1, m2, (TypeElement) m1.getEnclosingElement()); |
|
74 System.out.println("String.hashCode overrides Object.hashCode? " + res); |
|
75 checkResult(res); |
|
76 |
|
77 TypeElement a = elements.getTypeElement("OverridesSpecEx.A"); |
|
78 TypeElement b = elements.getTypeElement("OverridesSpecEx.B"); |
|
79 TypeElement c = elements.getTypeElement("OverridesSpecEx.C"); |
|
80 |
|
81 m1 = null; |
|
82 m2 = null; |
|
83 for (ExecutableElement m : methodsIn(a.getEnclosedElements())) |
|
84 m1 = m; |
|
85 for (ExecutableElement m : methodsIn(b.getEnclosedElements())) |
|
86 m2 = m; |
|
87 |
|
88 res = elements.overrides(m1, m2, a); |
|
89 System.out.println("A.m overrides B.m in B? " + res); |
|
90 checkResult(!res); |
|
91 res = elements.overrides(m1, m2, c); |
|
92 System.out.println("A.m overrides B.m in C? " + res); |
|
93 checkResult(res); |
|
94 } |
|
95 |
|
96 private static void checkResult(boolean truthiness) { |
|
97 if (!truthiness) |
|
98 throw new AssertionError("Bogus result"); |
|
99 } |
|
100 |
|
101 // Fodder for the processor |
|
102 class A { |
|
103 public void m() {} |
|
104 } |
|
105 interface B { |
|
106 void m(); |
|
107 } |
|
108 class C extends A implements B { |
|
109 } |
|
110 } |