test/tools/javac/processing/6499119/ClassProcessor.java

Wed, 13 Aug 2014 14:50:00 -0700

author
katleman
date
Wed, 13 Aug 2014 14:50:00 -0700
changeset 2549
0b6cc4ea670f
parent 1466
b52a38d4536c
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u40-b01 for changeset bf89a471779d

     1 /*
     2  * Copyright (c) 2010, 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  */
    24 import java.io.*;
    25 import java.util.*;
    26 import javax.annotation.processing.*;
    27 import javax.lang.model.element.*;
    28 import javax.lang.model.SourceVersion;
    29 import javax.tools.Diagnostic.Kind;
    31 /*
    32  * @test
    33  * @bug 6499119
    34  * @summary Created package-info class file modeled improperly
    35  * @library /tools/javac/lib
    36  * @build   JavacTestingAbstractProcessor
    37  * @compile ClassProcessor.java package-info.java
    38  * @compile/process -cp . -processor ClassProcessor -Akind=java  java.lang.Object
    39  * @compile/process -cp . -processor ClassProcessor -Akind=class java.lang.Object
    40  */
    42 @SupportedOptions({ "gen", "expect" })
    43 public class ClassProcessor extends JavacTestingAbstractProcessor {
    44     int round = 1;
    46     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    47         if (round == 1) {
    48             System.out.println("-- Round 1 --");
    49             createPackageFile();
    50         } else if (round == 2) {
    51             boolean found_foo_A = false;
    52             System.out.println("-- Round 2 --");
    53             for(Element e: roundEnv.getRootElements()) {
    54                 System.out.println("ElementKind: " + e.getKind());
    55                 System.out.println("Modifiers:   " + e.getModifiers());
    56                 System.out.println("Annotations: " + e.getAnnotationMirrors());
    57                 if (e.getAnnotationMirrors().toString().equals("@foo.A")) {
    58                     found_foo_A = true;
    59                     checkEqual("ElementKind", e.getKind().toString(), "PACKAGE");
    60                     checkEqual("Modifiers",   e.getModifiers().toString(), "[]");
    61                 }
    62             }
    63             if (!found_foo_A)
    64                 error("did not find @foo.A");
    65         }
    66         round++;
    67         return true;
    68     }
    70     private void createPackageFile() {
    71         String kind = processingEnv.getOptions().get("kind");
    73         File pkgInfo;
    74         if (kind.equals("java"))
    75             pkgInfo = new File(System.getProperty("test.src"),     "package-info.java");
    76         else
    77             pkgInfo = new File(System.getProperty("test.classes"), "foo/package-info.class");
    79         byte[] bytes = new byte[(int) pkgInfo.length()];
    80         DataInputStream in = null;
    81         try {
    82             in = new DataInputStream(new FileInputStream(pkgInfo));
    83             in.readFully(bytes);
    84         } catch (IOException ioe) {
    85             error("Couldn't read package info file: " + ioe);
    86         } finally {
    87             if(in != null) {
    88                 try {
    89                     in.close();
    90                 } catch (IOException e) {
    91                     error("InputStream closing failed: " + e);
    92                 }
    93             }
    94         }
    96         OutputStream out = null;
    97         try {
    98             if (kind.equals("java"))
    99                 out = filer.createSourceFile("foo.package-info").openOutputStream();
   100             else
   101                 out = filer.createClassFile("foo.package-info").openOutputStream();
   102             out.write(bytes, 0, bytes.length);
   103         } catch (IOException ioe) {
   104             error("Couldn't create package info file: " + ioe);
   105         } finally {
   106             if(out != null) {
   107                 try {
   108                     out.close();
   109                 } catch (IOException e) {
   110                     error("OutputStream closing failed: " + e);
   111                 }
   112             }
   113         }
   114     }
   116     private void checkEqual(String label, String actual, String expect) {
   117         if (!actual.equals(expect)) {
   118             error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect);
   119         }
   120     }
   122     private void error(String msg) {
   123         messager.printMessage(Kind.ERROR, msg);
   124     }
   125 }

mercurial