1 /*
  2  * Copyright (c) 2026, 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  * @summary Check presence/absence of code model in annotated elements
 27  * @library /tools/javac/lib
 28  * @modules java.compiler
 29  *          jdk.compiler
 30  *          jdk.incubator.code
 31  * @build   JavacTestingAbstractProcessor TestElementReflection
 32  * @compile -processor TestElementReflection -proc:only TestElementReflection.java
 33  */
 34 
 35 import jdk.incubator.code.Op;
 36 import jdk.incubator.code.Reflect;
 37 import jdk.incubator.code.dialect.core.CoreOp.FuncOp;
 38 
 39 import java.util.Optional;
 40 import java.util.Set;
 41 import javax.annotation.processing.RoundEnvironment;
 42 import javax.lang.model.element.*;
 43 
 44 public class TestElementReflection extends JavacTestingAbstractProcessor {
 45 
 46     @interface HasModel {
 47         boolean value();
 48     }
 49 
 50     static class Test {
 51         @HasModel(false)
 52         Test() { } // constructor
 53 
 54         @Reflect
 55         @HasModel(true)
 56         void member_reflectable() { } // reflectable class member
 57 
 58         @HasModel(false)
 59         void member() { } // class member
 60 
 61         class Inner {
 62             @Reflect
 63             @HasModel(false)
 64             void inner_member_reflectable() { } // reflectable inner class member
 65 
 66             @HasModel(false)
 67             void inner_member() { } // inner class member
 68         }
 69 
 70         static class Nested {
 71             @Reflect
 72             @HasModel(true)
 73             void static_member_reflectable() { } // reflectable nested class member
 74 
 75             @HasModel(false)
 76             void static_member() { } // nested class member
 77         }
 78     }
 79 
 80     public boolean process(Set<? extends TypeElement> annotations,
 81                            RoundEnvironment roundEnvironment) {
 82         class Scan extends ElementScanner<Void,Void> {
 83             @Override
 84             public Void visitExecutable(ExecutableElement e, Void p) {
 85                 HasModel hasModel = e.getAnnotation(HasModel.class);
 86                 if (hasModel == null) {
 87                     return null; // skip
 88                 }
 89                 Optional<FuncOp> body = Op.ofElement(processingEnv, e);
 90                 if (body.isPresent() && !hasModel.value()) {
 91                     throw new AssertionError(String.format("Unexpected model found for unsupported element %s",
 92                             toExecutableElementString(e)));
 93                 } else if (body.isEmpty() && hasModel.value()) {
 94                     throw new AssertionError(String.format("Model not found for supported element %s",
 95                             toExecutableElementString(e)));
 96                 }
 97                 return null;
 98             }
 99         }
100         Scan scan = new Scan();
101         for (Element e : roundEnvironment.getRootElements()) {
102             scan.scan(e);
103         }
104         return true;
105     }
106 
107     static String toExecutableElementString(ExecutableElement e) {
108         return e.getEnclosingElement() + "." + e;
109     }
110 }