1 /*
   2  * Copyright (c) 2019, 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 that anno processors wont see annotations before being trimmed from records
  27  * @modules jdk.compiler/com.sun.tools.javac.code
  28  *          jdk.compiler/com.sun.tools.javac.util
  29  * @library /tools/javac/lib
  30  * @build AnnoProcessorOnRecordsTest JavacTestingAbstractProcessor
  31  * @compile -XDaccessInternalAPI -processor AnnoProcessorOnRecordsTest -proc:only AnnoProcessorOnRecordsTest.java
  32  */
  33 
  34 import java.lang.annotation.ElementType;
  35 import java.lang.annotation.Retention;
  36 import java.lang.annotation.RetentionPolicy;
  37 import java.lang.annotation.Target;
  38 import java.util.Set;
  39 
  40 import javax.annotation.processing.AbstractProcessor;
  41 import javax.annotation.processing.RoundEnvironment;
  42 import javax.annotation.processing.SupportedAnnotationTypes;
  43 import javax.annotation.processing.SupportedSourceVersion;
  44 import javax.lang.model.SourceVersion;
  45 import javax.lang.model.element.Element;
  46 import javax.lang.model.element.ElementKind;
  47 import javax.lang.model.element.TypeElement;
  48 import javax.tools.Diagnostic.Kind;
  49 
  50 import com.sun.tools.javac.code.Symbol;
  51 import com.sun.tools.javac.code.Symbol.VarSymbol;
  52 
  53 import com.sun.tools.javac.util.Assert;
  54 
  55 @SupportedAnnotationTypes("*")
  56 public class AnnoProcessorOnRecordsTest extends JavacTestingAbstractProcessor {
  57 
  58     @Retention(RetentionPolicy.RUNTIME)
  59     @Target({ ElementType.PARAMETER })
  60     @interface Parameter {}
  61 
  62     @Retention(RetentionPolicy.RUNTIME)
  63     @Target({ ElementType.METHOD })
  64     @interface Method {}
  65 
  66     @Retention(RetentionPolicy.RUNTIME)
  67     @Target({ ElementType.FIELD })
  68     @interface Field {}
  69 
  70     @Retention(RetentionPolicy.RUNTIME)
  71     @interface All {}
  72 
  73     record R1(@Parameter int i) {}
  74 
  75     record R2(@Method int i) {}
  76 
  77     record R3(@Field int i) {}
  78 
  79     record R4(@All int i) {}
  80 
  81     public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv) {
  82         for (TypeElement te : tes) {
  83             for (Element e : renv.getElementsAnnotatedWith(te)) {
  84                 Symbol s = (Symbol)e;
  85                 if (s.isRecord() || s.owner.isRecord()) {
  86                     // debug
  87                     // System.out.println(te.toString());
  88                     switch (te.toString()) {
  89                         case "AnnoProcessorOnRecordsTest.Parameter" :
  90                             // debug
  91                             // System.out.println(s.getKind());
  92                             Assert.check(s.getKind() == ElementKind.PARAMETER);
  93                             break;
  94                         case "AnnoProcessorOnRecordsTest.Method":
  95                             // debug
  96                             // System.out.println(s.getKind());
  97                             Assert.check(s.getKind() == ElementKind.METHOD);
  98                             break;
  99                         case "AnnoProcessorOnRecordsTest.Field":
 100                             // debug
 101                             // System.out.println(s.getKind());
 102                             Assert.check(s.getKind() == ElementKind.FIELD);
 103                             break;
 104                         case "AnnoProcessorOnRecordsTest.All":
 105                             // debug
 106                             // System.out.println(s.getKind());
 107                             Assert.check(s.getKind() == ElementKind.FIELD || s.getKind() == ElementKind.METHOD || s.getKind() == ElementKind.PARAMETER);
 108                             break;
 109                         default:
 110                     }
 111                 }
 112             }
 113         }
 114         return true;
 115     }
 116 }