|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 2025, 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 8174840 |
| 27 | + * @library /tools/javac/lib |
| 28 | + * @build JavacTestingAbstractProcessor TestOverrides |
| 29 | + * @compile -processor TestOverrides -proc:only S.java |
| 30 | + */ |
| 31 | + |
| 32 | +import java.util.Set; |
| 33 | +import javax.annotation.processing.RoundEnvironment; |
| 34 | +import javax.lang.model.element.ExecutableElement; |
| 35 | +import javax.lang.model.element.TypeElement; |
| 36 | + |
| 37 | +import static javax.lang.model.element.ElementKind.METHOD; |
| 38 | + |
| 39 | +public class TestOverrides extends JavacTestingAbstractProcessor { |
| 40 | + |
| 41 | + @Override |
| 42 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment round) { |
| 43 | + if (!round.processingOver()) { |
| 44 | + var sm = mIn(elements.getTypeElement("S")); |
| 45 | + for (var subtypeName : new String[]{"T1", "T2", "T3", "T4", "T5"}) { |
| 46 | + var t = elements.getTypeElement(subtypeName); |
| 47 | + var tm = mIn(t); |
| 48 | + if (!elements.overrides(tm, sm, t)) |
| 49 | + messager.printError(String.format( |
| 50 | + "%s does not override from %s method %s", tm, t.getQualifiedName(), sm)); |
| 51 | + } |
| 52 | + } |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + private ExecutableElement mIn(TypeElement t) { |
| 57 | + return t.getEnclosedElements().stream() |
| 58 | + .filter(e -> e.getKind() == METHOD) |
| 59 | + .filter(e -> e.getSimpleName().toString().equals("m")) |
| 60 | + .map(e -> (ExecutableElement) e) |
| 61 | + .findAny() |
| 62 | + .get(); |
| 63 | + } |
| 64 | +} |
0 commit comments