test/langtools/tools/javac/processing/PackageInfo/Overwrite/Processor.java
author cushon
Thu, 13 Sep 2018 15:29:44 -0700
changeset 51734 e6b524cdcc34
permissions -rw-r--r--
8193037: package-info annotations are not reported when annotation processing is enabled Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51734
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
     1
/*
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
     2
 * Copyright (c) 2018, Google Inc. All rights reserved.
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
     4
 *
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
     8
 *
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    13
 * accompanied this code).
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    14
 *
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    18
 */
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    19
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    20
import static java.nio.charset.StandardCharsets.UTF_8;
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    21
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    22
import java.io.IOError;
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    23
import java.io.IOException;
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    24
import java.io.OutputStream;
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    25
import java.util.Set;
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    26
import javax.annotation.processing.RoundEnvironment;
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    27
import javax.annotation.processing.SupportedAnnotationTypes;
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    28
import javax.lang.model.element.PackageElement;
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    29
import javax.lang.model.element.TypeElement;
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    30
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    31
@SupportedAnnotationTypes("*")
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    32
public class Processor extends JavacTestingAbstractProcessor {
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    33
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    34
    boolean first = true;
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    35
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    36
    @Override
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    37
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    38
        if (first) {
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    39
            // Annotations are present on the initial package-info loaded from the classpath.
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    40
            PackageElement p = processingEnv.getElementUtils().getPackageElement("p");
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    41
            if (p.getAnnotationMirrors().isEmpty()) {
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    42
                throw new AssertionError(
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    43
                        "expected package annotations: " + p.getAnnotationMirrors());
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    44
            }
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    45
            // Overwrite the package-info with a new unannotated package-info.
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    46
            try (OutputStream os =
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    47
                    processingEnv
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    48
                            .getFiler()
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    49
                            .createSourceFile("p.package-info")
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    50
                            .openOutputStream()) {
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    51
                os.write("package p;".getBytes(UTF_8));
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    52
            } catch (IOException e) {
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    53
                throw new IOError(e);
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    54
            }
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    55
            first = false;
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    56
        }
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    57
        // The package-info's symbol should be reset between rounds, and when annotation
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    58
        // processing is over the package-info should be unannotated.
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    59
        PackageElement p = processingEnv.getElementUtils().getPackageElement("p");
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    60
        if (roundEnv.processingOver()) {
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    61
            if (!p.getAnnotationMirrors().isEmpty()) {
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    62
                throw new AssertionError(
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    63
                        "expected no package annotations: " + p.getAnnotationMirrors());
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    64
            }
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    65
        }
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    66
        return false;
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    67
    }
e6b524cdcc34 8193037: package-info annotations are not reported when annotation processing is enabled
cushon
parents:
diff changeset
    68
}