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