BaseClassesNotReRead.java revision 3294:9adfb22ff08f
1/* 2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 8038455 27 * @summary Check that classfiles are read only once in common cases despite several rounds of 28 * annotation processing. 29 * @modules jdk.compiler 30 * @clean * 31 * @run main BaseClassesNotReRead 32 */ 33 34import java.io.*; 35import java.util.*; 36import javax.annotation.processing.*; 37import javax.lang.model.*; 38import javax.lang.model.element.*; 39import javax.tools.*; 40import javax.tools.JavaFileObject.Kind; 41import com.sun.source.util.JavacTask; 42 43 44@SupportedAnnotationTypes("*") 45public class BaseClassesNotReRead extends AbstractProcessor { 46 public static void main(String... args) throws IOException { 47 new BaseClassesNotReRead().run(); 48 } 49 50 void run() throws IOException { 51 File sources = new File(System.getProperty("test.src")); 52 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 53 try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) { 54 Iterable<? extends JavaFileObject> files = 55 fm.getJavaFileObjects(new File(sources, "BaseClassesNotReReadSource.java")); 56 DiagnosticListener<JavaFileObject> noErrors = new DiagnosticListener<JavaFileObject>() { 57 @Override 58 public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 59 throw new IllegalStateException(diagnostic.toString()); 60 } 61 }; 62 JavaFileManager manager = new OnlyOneReadFileManager(fm); 63 Iterable<String> options = Arrays.asList("-processor", "BaseClassesNotReRead"); 64 JavacTask task = (JavacTask) compiler.getTask(null, manager, noErrors, options, null, files); 65 task.analyze(); 66 } 67 } 68 69 int round = 1; 70 public boolean process(Set<? extends TypeElement> annotations, 71 RoundEnvironment roundEnv) { 72 if (round++ == 1) { 73 for (int c = 1; c <= 6; c++) { 74 generateSource("GenClass" + c, 75 "public class GenClass" + c + " { public void test() { } }"); 76 } 77 for (int c = 1; c <= 3; c++) { 78 generateSource("GenIntf" + c, 79 "public interface GenIntf" + c + " { public void test(); }"); 80 } 81 generateSource("GenAnnotation", 82 "public @interface GenAnnotation { }"); 83 generateSource("GenException", 84 "public class GenException extends Exception { }"); 85 } 86 87 return false; 88 } 89 90 private void generateSource(String name, String code) { 91 Filer filer = processingEnv.getFiler(); 92 try (Writer out = filer.createSourceFile(name).openWriter()) { 93 out.write(code); 94 out.close(); 95 } catch (IOException e) { 96 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.toString()); 97 } 98 } 99 100 @Override 101 public SourceVersion getSupportedSourceVersion() { 102 return SourceVersion.latest(); 103 } 104 105 final class OnlyOneReadFileManager extends ForwardingJavaFileManager<JavaFileManager> { 106 107 public OnlyOneReadFileManager(JavaFileManager fileManager) { 108 super(fileManager); 109 } 110 111 @Override 112 public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) 113 throws IOException { 114 JavaFileObject fo = super.getJavaFileForInput(location, className, kind); 115 return (fo == null) ? null : new OnlyOneReadJavaFileObject(fo); 116 } 117 118 @Override 119 public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, 120 boolean recurse) throws IOException { 121 List<JavaFileObject> result = new ArrayList<>(); 122 for (JavaFileObject jfo : super.list(location, packageName, kinds, recurse)) { 123 result.add(new OnlyOneReadJavaFileObject(jfo)); 124 } 125 return result; 126 } 127 128 @Override 129 public String inferBinaryName(Location location, JavaFileObject file) { 130 return super.inferBinaryName(location, 131 ((OnlyOneReadJavaFileObject) file).getFileObject()); 132 } 133 134 } 135 136 final class OnlyOneReadJavaFileObject extends ForwardingJavaFileObject<JavaFileObject> { 137 138 public OnlyOneReadJavaFileObject(JavaFileObject fileObject) { 139 super(fileObject); 140 } 141 142 boolean used; 143 144 @Override 145 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { 146 if (used) throw new IllegalStateException("Already read."); 147 used = true; 148 return super.getCharContent(ignoreEncodingErrors); 149 } 150 151 @Override 152 public InputStream openInputStream() throws IOException { 153 if (used) throw new IllegalStateException("Already read."); 154 used = true; 155 return super.openInputStream(); 156 } 157 158 @Override 159 public Reader openReader(boolean ignoreEncodingErrors) throws IOException { 160 if (used) throw new IllegalStateException("Already read."); 161 used = true; 162 return super.openReader(ignoreEncodingErrors); 163 } 164 165 public JavaFileObject getFileObject() { 166 return fileObject; 167 } 168 } 169} 170