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