1 /* 2 * Copyright (c) 2017, 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 8034933 27 * @summary Elements hides does not work correctly with interface methods 28 * @library /tools/javac/lib 29 * @modules java.compiler 30 * jdk.compiler 31 * @build JavacTestingAbstractProcessor Hides 32 * @compile -processor Hides -proc:only C.java I.java 33 */ 34 35 import java.util.Set; 36 import java.util.List; 37 import javax.annotation.processing.*; 38 import javax.lang.model.element.*; 39 40 import static javax.lang.model.util.ElementFilter.*; 41 42 public class Hides extends JavacTestingAbstractProcessor { 43 44 VariableElement getField(TypeElement te) { 45 List<VariableElement> fields = fieldsIn(te.getEnclosedElements()); 46 if (fields.size() != 1) { 47 throw new AssertionError("Expected only one field in: " + te); 48 } 49 return fields.get(0); 50 } 51 52 ExecutableElement getMethod(TypeElement te) { 53 List<ExecutableElement> methods = methodsIn(te.getEnclosedElements()); 54 if (methods.size() != 1) { 55 throw new AssertionError("Expected only one method in: " + te); 56 } 57 return methods.get(0); 58 } 59 60 TypeElement getIC(TypeElement te) { 61 List<TypeElement> ics = typesIn(te.getEnclosedElements()); 62 if (ics.size() != 1) { 63 throw new AssertionError("Expected only one inner class in: " + te); 64 } 65 return ics.get(0); 66 } 67 68 public boolean process(Set<? extends TypeElement> tes, 69 RoundEnvironment round) { 70 if (round.processingOver()) 71 return true; 72 73 TypeElement klass = null; 74 TypeElement intfc = null; 75 76 for (TypeElement te : typesIn(round.getRootElements())) { 77 switch (te.getKind()) { 78 case INTERFACE: 79 intfc = te; 80 break; 81 case CLASS: 82 klass = te; 83 break; 84 default: 85 throw new AssertionError("don't know what this is: " + te); 86 } 87 } 88 89 for (Element e : klass.getEnclosedElements()) { 90 switch (e.getKind()) { 91 case FIELD: 92 check(e, getField(intfc)); 93 break; 94 case METHOD: 95 check(e, getMethod(intfc)); 96 break; 97 case CLASS: 98 check(e, getIC(intfc)); 99 default: 100 break; 101 } 102 } 103 104 if (!status) 105 throw new Error("Test fails"); 106 return true; 107 } 108 boolean status = true; 109 110 String getFQN(Element e) { 111 return e.getEnclosingElement() + "." + e; 112 } 113 114 void check(Element e1, Element e2) { 115 if (eltUtils.hides(e1, e2)) { 116 System.err.println("Pass: " + getFQN(e1) + " hides: " + getFQN(e2)); 117 } else { 118 System.err.println("Fail: Expected: " + getFQN(e1) + " to hide: " + getFQN(e2)); 119 status = false; 120 } 121 } 122 }