test/tools/javac/util/context/T7021650.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1466
b52a38d4536c
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /**
aoqi@0 25 * @test
aoqi@0 26 * @bug 7021650
aoqi@0 27 * @summary Fix Context issues
aoqi@0 28 * @library /tools/javac/lib
aoqi@0 29 * @build JavacTestingAbstractProcessor T7021650
aoqi@0 30 * @run main T7021650
aoqi@0 31 */
aoqi@0 32
aoqi@0 33 import java.io.*;
aoqi@0 34 import java.net.*;
aoqi@0 35 import java.util.*;
aoqi@0 36 import javax.annotation.processing.*;
aoqi@0 37 import javax.lang.model.element.*;
aoqi@0 38 import javax.tools.*;
aoqi@0 39
aoqi@0 40 import com.sun.tools.javac.comp.Attr;
aoqi@0 41 import com.sun.tools.javac.file.JavacFileManager;
aoqi@0 42 import com.sun.tools.javac.main.Main;
aoqi@0 43 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
aoqi@0 44 import com.sun.tools.javac.util.Context;
aoqi@0 45
aoqi@0 46 public class T7021650 extends JavacTestingAbstractProcessor {
aoqi@0 47 public static void main(String... args) throws Exception {
aoqi@0 48 new T7021650().run();
aoqi@0 49 }
aoqi@0 50
aoqi@0 51 static File testSrc = new File(System.getProperty("test.src"));
aoqi@0 52 static final int MAX_ROUNDS = 3;
aoqi@0 53
aoqi@0 54 /**
aoqi@0 55 * Perform a compilation with custom factories registered in the context,
aoqi@0 56 * and verify that corresponding objects are created in each round.
aoqi@0 57 */
aoqi@0 58 void run() throws Exception {
aoqi@0 59 Counter demoCounter = new Counter();
aoqi@0 60 Counter myAttrCounter = new Counter();
aoqi@0 61
aoqi@0 62 Context context = new Context();
aoqi@0 63 // Use a custom file manager which creates classloaders for annotation
aoqi@0 64 // processors with a sensible delegation parent, so that all instances
aoqi@0 65 // of test classes come from the same class loader. This is important
aoqi@0 66 // because the test performs class checks on the instances of classes
aoqi@0 67 // found in the context for each round or processing.
aoqi@0 68 context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() {
aoqi@0 69 public JavaFileManager make(Context c) {
aoqi@0 70 return new JavacFileManager(c, true, null) {
aoqi@0 71 @Override
aoqi@0 72 protected ClassLoader getClassLoader(URL[] urls) {
aoqi@0 73 return new URLClassLoader(urls, T7021650.class.getClassLoader());
aoqi@0 74 }
aoqi@0 75 };
aoqi@0 76 }
aoqi@0 77 });
aoqi@0 78
aoqi@0 79 Demo.preRegister(context, demoCounter);
aoqi@0 80 MyAttr.preRegister(context, myAttrCounter);
aoqi@0 81
aoqi@0 82 String[] args = {
aoqi@0 83 "-d", ".",
aoqi@0 84 "-processor", T7021650.class.getName(),
aoqi@0 85 "-XprintRounds",
aoqi@0 86 new File(testSrc, T7021650.class.getName() + ".java").getPath()
aoqi@0 87 };
aoqi@0 88
aoqi@0 89 compile(context, args);
aoqi@0 90
aoqi@0 91 // Expect to create Demo for initial round, then MAX_ROUNDS in which
aoqi@0 92 // GenX files are generated, then standard final round of processing.
aoqi@0 93 checkEqual("demoCounter", demoCounter.count, MAX_ROUNDS + 2);
aoqi@0 94
aoqi@0 95 // Expect to create MyAttr for same processing rounds as for Demo,
aoqi@0 96 // plus additional context for final compilation.
aoqi@0 97 checkEqual("myAttrCounter", myAttrCounter.count, MAX_ROUNDS + 3);
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 void compile(Context context, String... args) throws Exception {
aoqi@0 101 StringWriter sw = new StringWriter();
aoqi@0 102 PrintWriter pw = new PrintWriter(sw);
aoqi@0 103 Main m = new Main("javac", pw);
aoqi@0 104 Main.Result res = m.compile(args, context);
aoqi@0 105 pw.close();
aoqi@0 106 String out = sw.toString();
aoqi@0 107 if (!out.isEmpty())
aoqi@0 108 System.err.println(out);
aoqi@0 109 if (!res.isOK())
aoqi@0 110 throw new Exception("compilation failed unexpectedly: result=" + res);
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 void checkEqual(String label, int found, int expect) throws Exception {
aoqi@0 114 if (found != expect)
aoqi@0 115 throw new Exception("unexpected value for " + label
aoqi@0 116 + ": expected " + expect
aoqi@0 117 + ": found " + found);
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 //---------------
aoqi@0 121
aoqi@0 122 /*
aoqi@0 123 * A custom class unknown to javac but nonetheless registered in the context.
aoqi@0 124 */
aoqi@0 125 static class Demo {
aoqi@0 126 static void preRegister(Context context, final Counter counter) {
aoqi@0 127 context.put(Demo.class, new Context.Factory<Demo>() {
aoqi@0 128 public Demo make(Context c) {
aoqi@0 129 counter.count++;
aoqi@0 130 return new Demo(c);
aoqi@0 131 }
aoqi@0 132 });
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 Demo(Context c) {
aoqi@0 136 c.put(Demo.class, this);
aoqi@0 137 }
aoqi@0 138
aoqi@0 139 static Demo instance(Context context) {
aoqi@0 140 return context.get(Demo.class);
aoqi@0 141 }
aoqi@0 142 }
aoqi@0 143
aoqi@0 144 /**
aoqi@0 145 * A custom version of a standard javac component.
aoqi@0 146 */
aoqi@0 147 static class MyAttr extends Attr {
aoqi@0 148 static void preRegister(Context context, final Counter counter) {
aoqi@0 149 context.put(attrKey, new Context.Factory<Attr>() {
aoqi@0 150 public Attr make(Context c) {
aoqi@0 151 counter.count++;
aoqi@0 152 return new MyAttr(c);
aoqi@0 153 }
aoqi@0 154 });
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 MyAttr(Context c) {
aoqi@0 158 super(c);
aoqi@0 159 }
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 static class Counter {
aoqi@0 163 int count;
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 //---------------
aoqi@0 167
aoqi@0 168 int round = 0;
aoqi@0 169
aoqi@0 170 @Override
aoqi@0 171 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
aoqi@0 172 round++;
aoqi@0 173
aoqi@0 174 Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
aoqi@0 175
aoqi@0 176 // verify items in context as expected
aoqi@0 177 check("Demo", Demo.instance(context), Demo.class);
aoqi@0 178 check("Attr", Attr.instance(context), MyAttr.class);
aoqi@0 179
aoqi@0 180 // For a few rounds, generate new source files, so that we can check whether
aoqi@0 181 // values in the context are correctly handled in subsequent processing rounds
aoqi@0 182 if (round <= MAX_ROUNDS) {
aoqi@0 183 String pkg = "p";
aoqi@0 184 String currClass = "Gen" + round;
aoqi@0 185 String curr = pkg + "." + currClass;
aoqi@0 186 String next = (pkg + ".Gen" + (round + 1));
aoqi@0 187 StringBuilder text = new StringBuilder();
aoqi@0 188 text.append("package ").append(pkg).append(";\n");
aoqi@0 189 text.append("public class ").append(currClass).append(" {\n");
aoqi@0 190 if (round < MAX_ROUNDS)
aoqi@0 191 text.append(" ").append(next).append(" x;\n");
aoqi@0 192 text.append("}\n");
aoqi@0 193
aoqi@0 194 try {
aoqi@0 195 JavaFileObject fo = filer.createSourceFile(curr);
aoqi@0 196 Writer out = fo.openWriter();
aoqi@0 197 try {
aoqi@0 198 out.write(text.toString());
aoqi@0 199 } finally {
aoqi@0 200 out.close();
aoqi@0 201 }
aoqi@0 202 } catch (IOException e) {
aoqi@0 203 throw new Error(e);
aoqi@0 204 }
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 return true;
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 void check(String label, Object o, Class<?> clazz) {
aoqi@0 211 if (o == null)
aoqi@0 212 throw new IllegalStateException(label + ": no item found");
aoqi@0 213 if (!clazz.isAssignableFrom(o.getClass()))
aoqi@0 214 throw new IllegalStateException(label + ": unexpected class: " + o.getClass());
aoqi@0 215 }
aoqi@0 216 }

mercurial