Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8200166

Repeating annotations refering to to-be-generated classes don't work.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 11
    • 10, 11
    • tools
    • b17
    • Verified

        Consider this jtreg test case:
        ---GeneratedInRepeating.java
        /**
         * @test
         * @compile Processor.java
         * @compile -processor Processor GeneratedInRepeating.java
         */

        import java.lang.annotation.Repeatable;

        @Annot(Gen.class)
        @Annot(Gen.class)
        public class GeneratedInRepeating {
        }

        @Repeatable(AnnotContainer.class)
        @interface Annot {
            public Class<?> value();
        }

        @interface AnnotContainer {
            public Annot[] value();
        }
        ---Processor.java
        /*
         * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
         *
         * This code is free software; you can redistribute it and/or modify it
         * under the terms of the GNU General Public License version 2 only, as
         * published by the Free Software Foundation.
         *
         * This code is distributed in the hope that it will be useful, but WITHOUT
         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
         * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
         * version 2 for more details (a copy is included in the LICENSE file that
         * accompanied this code).
         *
         * You should have received a copy of the GNU General Public License version
         * 2 along with this work; if not, write to the Free Software Foundation,
         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
         *
         * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
         * or visit www.oracle.com if you need additional information or have any
         * questions.
         */

        import java.io.IOException;
        import java.io.Writer;
        import java.util.Set;

        import javax.annotation.processing.AbstractProcessor;
        import javax.annotation.processing.RoundEnvironment;
        import javax.annotation.processing.SupportedAnnotationTypes;
        import javax.lang.model.SourceVersion;
        import javax.lang.model.element.TypeElement;

        @SupportedAnnotationTypes("*")
        public class Processor extends AbstractProcessor {

            int round;

            @Override
            public SourceVersion getSupportedSourceVersion() {
                return SourceVersion.latestSupported();
            }

            @Override
            public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
                if (round == 0) {
                    try (Writer w = processingEnv.getFiler().createSourceFile("Gen").openWriter()) {
                        w.write("class Gen {}");
                    } catch (IOException ex) {
                        throw new IllegalStateException(ex);
                    }
                }
                round++;
                return false;
            }
        }
        ---

        Running this yields:
        .../test/langtools/tools/javac/annotations/repeatingAnnotations/generatedInRepeating/GeneratedInRepeating.java:9: error: cannot find symbol
        @Annot(Gen.class)
               ^
          symbol: class Gen
        .../test/langtools/tools/javac/annotations/repeatingAnnotations/generatedInRepeating/GeneratedInRepeating.java:10: error: cannot find symbol
        @Annot(Gen.class)
               ^
          symbol: class Gen
        .../test/langtools/tools/javac/annotations/repeatingAnnotations/generatedInRepeating/GeneratedInRepeating.java:9: error: expression not allowed as annotation value
        @Annot(Gen.class)
        ^
        3 errors

        But the compilation should pass, as the processor (if it would run), would generate the "Gen" class.

              jlahoda Jan Lahoda
              jlahoda Jan Lahoda
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: