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

Fri, 29 Jan 2010 16:54:52 -0800

author
jjg
date
Fri, 29 Jan 2010 16:54:52 -0800
changeset 483
8e638442522a
child 554
9d9f26857129
permissions
-rw-r--r--

6499119: Created package-info class file modeled improperly
6920317: package-info.java file has to be specified on the javac cmdline, else it will not be avail.
Reviewed-by: darcy

jjg@483 1 /*
jjg@483 2 * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved.
jjg@483 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@483 4 *
jjg@483 5 * This code is free software; you can redistribute it and/or modify it
jjg@483 6 * under the terms of the GNU General Public License version 2 only, as
jjg@483 7 * published by the Free Software Foundation.
jjg@483 8 *
jjg@483 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@483 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@483 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@483 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@483 13 * accompanied this code).
jjg@483 14 *
jjg@483 15 * You should have received a copy of the GNU General Public License version
jjg@483 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@483 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@483 18 *
jjg@483 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@483 20 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@483 21 * have any questions.
jjg@483 22 */
jjg@483 23
jjg@483 24 import java.io.*;
jjg@483 25 import java.util.*;
jjg@483 26 import javax.annotation.processing.*;
jjg@483 27 import javax.lang.model.element.*;
jjg@483 28 import javax.lang.model.SourceVersion;
jjg@483 29 import javax.tools.Diagnostic.Kind;
jjg@483 30
jjg@483 31 /*
jjg@483 32 * @test
jjg@483 33 * @bug 6499119
jjg@483 34 * @summary Created package-info class file modeled improperly
jjg@483 35 * @compile ClassProcessor.java package-info.java
jjg@483 36 * @compile/process -cp . -processor ClassProcessor -Akind=java java.lang.Object
jjg@483 37 * @compile/process -cp . -processor ClassProcessor -Akind=class java.lang.Object
jjg@483 38 */
jjg@483 39
jjg@483 40 @SupportedOptions({ "gen", "expect" })
jjg@483 41 @SupportedAnnotationTypes({"*"})
jjg@483 42 public class ClassProcessor extends AbstractProcessor {
jjg@483 43 int round = 1;
jjg@483 44
jjg@483 45 public SourceVersion getSupportedSourceVersion() {
jjg@483 46 return SourceVersion.latest();
jjg@483 47 }
jjg@483 48
jjg@483 49 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
jjg@483 50 if (round == 1) {
jjg@483 51 System.out.println("-- Round 1 --");
jjg@483 52 createPackageFile();
jjg@483 53 } else if (round == 2) {
jjg@483 54 boolean found_foo_A = false;
jjg@483 55 System.out.println("-- Round 2 --");
jjg@483 56 for(Element e: roundEnv.getRootElements()) {
jjg@483 57 System.out.println("ElementKind: " + e.getKind());
jjg@483 58 System.out.println("Modifiers: " + e.getModifiers());
jjg@483 59 System.out.println("Annotations: " + e.getAnnotationMirrors());
jjg@483 60 if (e.getAnnotationMirrors().toString().equals("@foo.A")) {
jjg@483 61 found_foo_A = true;
jjg@483 62 checkEqual("ElementKind", e.getKind().toString(), "PACKAGE");
jjg@483 63 checkEqual("Modifiers", e.getModifiers().toString(), "[]");
jjg@483 64 }
jjg@483 65 }
jjg@483 66 if (!found_foo_A)
jjg@483 67 error("did not find @foo.A");
jjg@483 68 }
jjg@483 69 round++;
jjg@483 70 return true;
jjg@483 71 }
jjg@483 72
jjg@483 73 private void createPackageFile() {
jjg@483 74 Filer filer = processingEnv.getFiler();
jjg@483 75
jjg@483 76 String kind = processingEnv.getOptions().get("kind");
jjg@483 77
jjg@483 78 File pkgInfo;
jjg@483 79 if (kind.equals("java"))
jjg@483 80 pkgInfo = new File(System.getProperty("test.src"), "package-info.java");
jjg@483 81 else
jjg@483 82 pkgInfo = new File(System.getProperty("test.classes"), "foo/package-info.class");
jjg@483 83
jjg@483 84 byte[] bytes = new byte[(int) pkgInfo.length()];
jjg@483 85 DataInputStream in = null;
jjg@483 86 try {
jjg@483 87 in = new DataInputStream(new FileInputStream(pkgInfo));
jjg@483 88 in.readFully(bytes);
jjg@483 89 } catch (IOException ioe) {
jjg@483 90 error("Couldn't read package info file: " + ioe);
jjg@483 91 } finally {
jjg@483 92 if(in != null) {
jjg@483 93 try {
jjg@483 94 in.close();
jjg@483 95 } catch (IOException e) {
jjg@483 96 error("InputStream closing failed: " + e);
jjg@483 97 }
jjg@483 98 }
jjg@483 99 }
jjg@483 100
jjg@483 101 OutputStream out = null;
jjg@483 102 try {
jjg@483 103 if (kind.equals("java"))
jjg@483 104 out = filer.createSourceFile("foo.package-info").openOutputStream();
jjg@483 105 else
jjg@483 106 out = filer.createClassFile("foo.package-info").openOutputStream();
jjg@483 107 out.write(bytes, 0, bytes.length);
jjg@483 108 } catch (IOException ioe) {
jjg@483 109 error("Couldn't create package info file: " + ioe);
jjg@483 110 } finally {
jjg@483 111 if(out != null) {
jjg@483 112 try {
jjg@483 113 out.close();
jjg@483 114 } catch (IOException e) {
jjg@483 115 error("OutputStream closing failed: " + e);
jjg@483 116 }
jjg@483 117 }
jjg@483 118 }
jjg@483 119 }
jjg@483 120
jjg@483 121 private void checkEqual(String label, String actual, String expect) {
jjg@483 122 if (!actual.equals(expect)) {
jjg@483 123 error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect);
jjg@483 124 }
jjg@483 125 }
jjg@483 126
jjg@483 127 private void error(String msg) {
jjg@483 128 Messager messager = processingEnv.getMessager();
jjg@483 129 messager.printMessage(Kind.ERROR, msg);
jjg@483 130 }
jjg@483 131 }
jjg@483 132

mercurial