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

Wed, 29 Sep 2010 23:27:57 -0700

author
darcy
date
Wed, 29 Sep 2010 23:27:57 -0700
changeset 699
d2aaaec153e8
parent 554
9d9f26857129
child 1466
b52a38d4536c
permissions
-rw-r--r--

6983738: Use a JavacTestingAbstractProcessor
Reviewed-by: jjg

jjg@483 1 /*
ohair@554 2 * Copyright (c) 2010, Oracle and/or its affiliates. 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 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * 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
darcy@699 35 * @library ../../lib
darcy@699 36 * @build JavacTestingAbstractProcessor
jjg@483 37 * @compile ClassProcessor.java package-info.java
jjg@483 38 * @compile/process -cp . -processor ClassProcessor -Akind=java java.lang.Object
jjg@483 39 * @compile/process -cp . -processor ClassProcessor -Akind=class java.lang.Object
jjg@483 40 */
jjg@483 41
jjg@483 42 @SupportedOptions({ "gen", "expect" })
darcy@699 43 public class ClassProcessor extends JavacTestingAbstractProcessor {
jjg@483 44 int round = 1;
jjg@483 45
jjg@483 46 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
jjg@483 47 if (round == 1) {
jjg@483 48 System.out.println("-- Round 1 --");
jjg@483 49 createPackageFile();
jjg@483 50 } else if (round == 2) {
jjg@483 51 boolean found_foo_A = false;
jjg@483 52 System.out.println("-- Round 2 --");
jjg@483 53 for(Element e: roundEnv.getRootElements()) {
jjg@483 54 System.out.println("ElementKind: " + e.getKind());
jjg@483 55 System.out.println("Modifiers: " + e.getModifiers());
jjg@483 56 System.out.println("Annotations: " + e.getAnnotationMirrors());
jjg@483 57 if (e.getAnnotationMirrors().toString().equals("@foo.A")) {
jjg@483 58 found_foo_A = true;
jjg@483 59 checkEqual("ElementKind", e.getKind().toString(), "PACKAGE");
jjg@483 60 checkEqual("Modifiers", e.getModifiers().toString(), "[]");
jjg@483 61 }
jjg@483 62 }
jjg@483 63 if (!found_foo_A)
jjg@483 64 error("did not find @foo.A");
jjg@483 65 }
jjg@483 66 round++;
jjg@483 67 return true;
jjg@483 68 }
jjg@483 69
jjg@483 70 private void createPackageFile() {
jjg@483 71 String kind = processingEnv.getOptions().get("kind");
jjg@483 72
jjg@483 73 File pkgInfo;
jjg@483 74 if (kind.equals("java"))
jjg@483 75 pkgInfo = new File(System.getProperty("test.src"), "package-info.java");
jjg@483 76 else
jjg@483 77 pkgInfo = new File(System.getProperty("test.classes"), "foo/package-info.class");
jjg@483 78
jjg@483 79 byte[] bytes = new byte[(int) pkgInfo.length()];
jjg@483 80 DataInputStream in = null;
jjg@483 81 try {
jjg@483 82 in = new DataInputStream(new FileInputStream(pkgInfo));
jjg@483 83 in.readFully(bytes);
jjg@483 84 } catch (IOException ioe) {
jjg@483 85 error("Couldn't read package info file: " + ioe);
jjg@483 86 } finally {
jjg@483 87 if(in != null) {
jjg@483 88 try {
jjg@483 89 in.close();
jjg@483 90 } catch (IOException e) {
jjg@483 91 error("InputStream closing failed: " + e);
jjg@483 92 }
jjg@483 93 }
jjg@483 94 }
jjg@483 95
jjg@483 96 OutputStream out = null;
jjg@483 97 try {
jjg@483 98 if (kind.equals("java"))
jjg@483 99 out = filer.createSourceFile("foo.package-info").openOutputStream();
jjg@483 100 else
jjg@483 101 out = filer.createClassFile("foo.package-info").openOutputStream();
jjg@483 102 out.write(bytes, 0, bytes.length);
jjg@483 103 } catch (IOException ioe) {
jjg@483 104 error("Couldn't create package info file: " + ioe);
jjg@483 105 } finally {
jjg@483 106 if(out != null) {
jjg@483 107 try {
jjg@483 108 out.close();
jjg@483 109 } catch (IOException e) {
jjg@483 110 error("OutputStream closing failed: " + e);
jjg@483 111 }
jjg@483 112 }
jjg@483 113 }
jjg@483 114 }
jjg@483 115
jjg@483 116 private void checkEqual(String label, String actual, String expect) {
jjg@483 117 if (!actual.equals(expect)) {
jjg@483 118 error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect);
jjg@483 119 }
jjg@483 120 }
jjg@483 121
jjg@483 122 private void error(String msg) {
jjg@483 123 messager.printMessage(Kind.ERROR, msg);
jjg@483 124 }
jjg@483 125 }
jjg@483 126

mercurial