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