0N/A/*
698N/A * Copyright (c) 2006, 2010, 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 *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6439826 6411930 6380018 6392177
0N/A * @summary Exception issuing Diagnostic while processing generated errant code
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport javax.annotation.processing.*;
0N/Aimport javax.lang.model.*;
0N/Aimport javax.lang.model.element.*;
0N/Aimport javax.tools.*;
0N/Aimport com.sun.source.util.*;
0N/Aimport com.sun.tools.javac.api.*;
0N/Aimport static javax.lang.model.util.ElementFilter.*;
0N/A
0N/A
0N/A@SupportedAnnotationTypes("*")
0N/Apublic class T6439826 extends AbstractProcessor {
0N/A public static void main(String... args) {
0N/A String testSrc = System.getProperty("test.src", ".");
0N/A String testClasses = System.getProperty("test.classes");
0N/A JavacTool tool = JavacTool.create();
0N/A MyDiagListener dl = new MyDiagListener();
0N/A StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
0N/A Iterable<? extends JavaFileObject> files =
0N/A fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6439826.class.getName()+".java")));
698N/A Iterable<String> opts = Arrays.asList("-proc:only",
0N/A "-processor", "T6439826",
0N/A "-processorpath", testClasses);
0N/A StringWriter out = new StringWriter();
0N/A JavacTask task = tool.getTask(out, fm, dl, opts, null, files);
0N/A task.call();
0N/A String s = out.toString();
0N/A System.err.print(s);
0N/A // Expect the following 2 diagnostics, and no output to log
0N/A // Foo.java:1: illegal character: \35
0N/A // Foo.java:1: reached end of file while parsing
0N/A System.err.println(dl.count + " diagnostics; " + s.length() + " characters");
0N/A if (dl.count != 2 || s.length() != 0)
0N/A throw new AssertionError("unexpected output from compiler");
0N/A }
0N/A
0N/A public boolean process(Set<? extends TypeElement> annotations,
0N/A RoundEnvironment roundEnv) {
0N/A Set<? extends TypeElement> elems = typesIn(roundEnv.getRootElements());
0N/A for (TypeElement e: elems) {
0N/A if (e.getSimpleName().toString().equals(T6439826.class.getName()))
0N/A writeBadFile();
0N/A }
0N/A return false;
0N/A }
0N/A
494N/A @Override
494N/A public SourceVersion getSupportedSourceVersion() {
494N/A return SourceVersion.latest();
494N/A }
494N/A
0N/A private void writeBadFile() {
0N/A Filer filer = processingEnv.getFiler();
0N/A Messager messager = processingEnv.getMessager();
0N/A try {
0N/A Writer out = filer.createSourceFile("Foo").openWriter();
0N/A out.write("class Foo #"); // write a file that generates a scanner error
0N/A out.close();
0N/A } catch (IOException e) {
0N/A messager.printMessage(Diagnostic.Kind.ERROR, e.toString());
0N/A }
0N/A }
0N/A
0N/A static class MyDiagListener implements DiagnosticListener {
0N/A public void report(Diagnostic d) {
0N/A System.err.println(d);
0N/A count++;
0N/A }
0N/A
0N/A public int count;
0N/A }
0N/A}