T7021650.java revision 892
0N/A/*
2265N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A */
0N/A
0N/A/**
1879N/A * @test
1879N/A * @bug 7021650
1879N/A * @summary Fix Context issues
1879N/A * @library ../../lib
1879N/A * @build JavacTestingAbstractProcessor T7021650
1879N/A * @run main T7021650
1879N/A */
1879N/A
1879N/Aimport java.io.*;
1879N/Aimport java.net.*;
1879N/Aimport java.util.*;
1879N/Aimport javax.annotation.processing.*;
1879N/Aimport javax.lang.model.element.*;
1879N/Aimport javax.tools.*;
1879N/A
1879N/Aimport com.sun.tools.javac.comp.Attr;
1879N/Aimport com.sun.tools.javac.file.JavacFileManager;
0N/Aimport com.sun.tools.javac.main.Main;
0N/Aimport com.sun.tools.javac.processing.JavacProcessingEnvironment;
0N/Aimport com.sun.tools.javac.util.Context;
0N/A
0N/Apublic class T7021650 extends JavacTestingAbstractProcessor {
0N/A public static void main(String... args) throws Exception {
0N/A new T7021650().run();
0N/A }
0N/A
0N/A static File testSrc = new File(System.getProperty("test.src"));
0N/A static final int MAX_ROUNDS = 3;
0N/A
0N/A /**
0N/A * Perform a compilation with custom factories registered in the context,
0N/A * and verify that corresponding objects are created in each round.
0N/A */
0N/A void run() throws Exception {
0N/A Counter demoCounter = new Counter();
0N/A Counter myAttrCounter = new Counter();
0N/A
0N/A Context context = new Context();
0N/A // Use a custom file manager which creates classloaders for annotation
0N/A // processors with a sensible delegation parent, so that all instances
0N/A // of test classes come from the same class loader. This is important
0N/A // because the test performs class checks on the instances of classes
0N/A // found in the context for each round or processing.
0N/A context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() {
0N/A public JavaFileManager make(Context c) {
0N/A return new JavacFileManager(c, true, null) {
0N/A @Override
0N/A protected ClassLoader getClassLoader(URL[] urls) {
0N/A return new URLClassLoader(urls, T7021650.class.getClassLoader());
0N/A }
0N/A };
0N/A }
0N/A });
113N/A
113N/A Demo.preRegister(context, demoCounter);
113N/A MyAttr.preRegister(context, myAttrCounter);
1142N/A
0N/A String[] args = {
2265N/A "-d", ".",
0N/A "-processor", T7021650.class.getName(),
0N/A "-XprintRounds",
0N/A new File(testSrc, T7021650.class.getName() + ".java").getPath()
0N/A };
113N/A
0N/A compile(context, args);
0N/A
0N/A // Expect to create Demo for initial round, then MAX_ROUNDS in which
0N/A // GenX files are generated, then standard final round of processing.
0N/A checkEqual("demoCounter", demoCounter.count, MAX_ROUNDS + 2);
0N/A
0N/A // Expect to create MyAttr for same processing rounds as for Demo,
0N/A // plus additional context for final compilation.
0N/A checkEqual("myAttrCounter", myAttrCounter.count, MAX_ROUNDS + 3);
0N/A }
0N/A
0N/A void compile(Context context, String... args) throws Exception {
0N/A StringWriter sw = new StringWriter();
0N/A PrintWriter pw = new PrintWriter(sw);
0N/A Main m = new Main("javac", pw);
0N/A int rc = m.compile(args, context);
0N/A pw.close();
0N/A String out = sw.toString();
0N/A if (!out.isEmpty())
0N/A System.err.println(out);
0N/A if (rc != 0)
0N/A throw new Exception("compilation failed unexpectedly: rc=" + rc);
0N/A }
0N/A
0N/A void checkEqual(String label, int found, int expect) throws Exception {
0N/A if (found != expect)
0N/A throw new Exception("unexpected value for " + label
0N/A + ": expected " + expect
0N/A + ": found " + found);
0N/A }
0N/A
0N/A //---------------
0N/A
0N/A /*
0N/A * A custom class unknown to javac but nonetheless registered in the context.
0N/A */
0N/A static class Demo {
0N/A static void preRegister(Context context, final Counter counter) {
2062N/A context.put(Demo.class, new Context.Factory<Demo>() {
2062N/A public Demo make(Context c) {
2062N/A counter.count++;
2062N/A return new Demo(c);
2062N/A }
2062N/A });
2062N/A }
2062N/A
2062N/A Demo(Context c) {
2062N/A c.put(Demo.class, this);
2062N/A }
2062N/A
2062N/A static Demo instance(Context context) {
2062N/A return context.get(Demo.class);
2062N/A }
2062N/A }
2062N/A
2062N/A /**
2062N/A * A custom version of a standard javac component.
2062N/A */
2062N/A static class MyAttr extends Attr {
2062N/A static void preRegister(Context context, final Counter counter) {
2062N/A context.put(attrKey, new Context.Factory<Attr>() {
2062N/A public Attr make(Context c) {
2062N/A counter.count++;
2062N/A return new MyAttr(c);
2062N/A }
2062N/A });
2062N/A }
2062N/A
2062N/A MyAttr(Context c) {
2062N/A super(c);
2062N/A }
2062N/A }
2062N/A
2062N/A static class Counter {
2062N/A int count;
2062N/A }
2062N/A
2062N/A //---------------
2062N/A
2062N/A int round = 0;
2062N/A
2062N/A @Override
2062N/A public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
2062N/A round++;
2062N/A
2062N/A Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
2062N/A
2062N/A // verify items in context as expected
2062N/A check("Demo", Demo.instance(context), Demo.class);
2062N/A check("Attr", Attr.instance(context), MyAttr.class);
2062N/A
2062N/A // For a few rounds, generate new source files, so that we can check whether
2062N/A // values in the context are correctly handled in subsequent processing rounds
2062N/A if (round <= MAX_ROUNDS) {
2062N/A String pkg = "p";
2062N/A String currClass = "Gen" + round;
2062N/A String curr = pkg + "." + currClass;
2062N/A String next = (pkg + ".Gen" + (round + 1));
2062N/A StringBuilder text = new StringBuilder();
2062N/A text.append("package ").append(pkg).append(";\n");
2062N/A text.append("public class ").append(currClass).append(" {\n");
0N/A if (round < MAX_ROUNDS)
0N/A text.append(" ").append(next).append(" x;\n");
0N/A text.append("}\n");
0N/A
113N/A try {
113N/A JavaFileObject fo = filer.createSourceFile(curr);
0N/A Writer out = fo.openWriter();
0N/A try {
0N/A out.write(text.toString());
0N/A } finally {
0N/A out.close();
0N/A }
0N/A } catch (IOException e) {
0N/A throw new Error(e);
0N/A }
0N/A }
0N/A
0N/A return true;
113N/A }
0N/A
0N/A void check(String label, Object o, Class<?> clazz) {
0N/A if (o == null)
0N/A throw new IllegalStateException(label + ": no item found");
0N/A if (!clazz.isAssignableFrom(o.getClass()))
0N/A throw new IllegalStateException(label + ": unexpected class: " + o.getClass());
0N/A }
0N/A}
0N/A