langtools/test/tools/javac/processing/6499119/ClassProcessor.java
author darcy
Fri, 21 Dec 2012 08:45:43 -0800
changeset 14963 974d4423c999
parent 6720 f16f91662ad8
child 30730 d3ce7619db2c
permissions -rw-r--r--
8005282: Use @library tag with non-relative path for javac tests Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4871
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     4
 *
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     8
 *
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    13
 * accompanied this code).
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    14
 *
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4871
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4871
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4871
diff changeset
    21
 * questions.
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    22
 */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    23
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    24
import java.io.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    25
import java.util.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    26
import javax.annotation.processing.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    27
import javax.lang.model.element.*;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    28
import javax.lang.model.SourceVersion;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    29
import javax.tools.Diagnostic.Kind;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    30
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    31
/*
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    32
 * @test
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    33
 * @bug 6499119
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    34
 * @summary Created package-info class file modeled improperly
14963
974d4423c999 8005282: Use @library tag with non-relative path for javac tests
darcy
parents: 6720
diff changeset
    35
 * @library /tools/javac/lib
6720
f16f91662ad8 6983738: Use a JavacTestingAbstractProcessor
darcy
parents: 5520
diff changeset
    36
 * @build   JavacTestingAbstractProcessor
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    37
 * @compile ClassProcessor.java package-info.java
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    38
 * @compile/process -cp . -processor ClassProcessor -Akind=java  java.lang.Object
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    39
 * @compile/process -cp . -processor ClassProcessor -Akind=class java.lang.Object
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    40
 */
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    41
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    42
@SupportedOptions({ "gen", "expect" })
6720
f16f91662ad8 6983738: Use a JavacTestingAbstractProcessor
darcy
parents: 5520
diff changeset
    43
public class ClassProcessor extends JavacTestingAbstractProcessor {
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    44
    int round = 1;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    45
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    46
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    47
        if (round == 1) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    48
            System.out.println("-- Round 1 --");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    49
            createPackageFile();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    50
        } else if (round == 2) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    51
            boolean found_foo_A = false;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    52
            System.out.println("-- Round 2 --");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    53
            for(Element e: roundEnv.getRootElements()) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    54
                System.out.println("ElementKind: " + e.getKind());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    55
                System.out.println("Modifiers:   " + e.getModifiers());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    56
                System.out.println("Annotations: " + e.getAnnotationMirrors());
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    57
                if (e.getAnnotationMirrors().toString().equals("@foo.A")) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    58
                    found_foo_A = true;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    59
                    checkEqual("ElementKind", e.getKind().toString(), "PACKAGE");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    60
                    checkEqual("Modifiers",   e.getModifiers().toString(), "[]");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    61
                }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    62
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    63
            if (!found_foo_A)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    64
                error("did not find @foo.A");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    65
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    66
        round++;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    67
        return true;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    68
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    69
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    70
    private void createPackageFile() {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    71
        String kind = processingEnv.getOptions().get("kind");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    72
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    73
        File pkgInfo;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    74
        if (kind.equals("java"))
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    75
            pkgInfo = new File(System.getProperty("test.src"),     "package-info.java");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    76
        else
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    77
            pkgInfo = new File(System.getProperty("test.classes"), "foo/package-info.class");
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    78
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    79
        byte[] bytes = new byte[(int) pkgInfo.length()];
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    80
        DataInputStream in = null;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    81
        try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    82
            in = new DataInputStream(new FileInputStream(pkgInfo));
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    83
            in.readFully(bytes);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    84
        } catch (IOException ioe) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    85
            error("Couldn't read package info file: " + ioe);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    86
        } finally {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    87
            if(in != null) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    88
                try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    89
                    in.close();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    90
                } catch (IOException e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    91
                    error("InputStream closing failed: " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    92
                }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    93
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    94
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    95
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    96
        OutputStream out = null;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    97
        try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    98
            if (kind.equals("java"))
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
    99
                out = filer.createSourceFile("foo.package-info").openOutputStream();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   100
            else
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   101
                out = filer.createClassFile("foo.package-info").openOutputStream();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   102
            out.write(bytes, 0, bytes.length);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   103
        } catch (IOException ioe) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   104
            error("Couldn't create package info file: " + ioe);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   105
        } finally {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   106
            if(out != null) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   107
                try {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   108
                    out.close();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   109
                } catch (IOException e) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   110
                    error("OutputStream closing failed: " + e);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   111
                }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   112
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   113
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   114
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   115
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   116
    private void checkEqual(String label, String actual, String expect) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   117
        if (!actual.equals(expect)) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   118
            error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   119
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   120
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   121
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   122
    private void error(String msg) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   123
        messager.printMessage(Kind.ERROR, msg);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   124
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   125
}
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents:
diff changeset
   126