1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/processing/errors/TestReturnCode.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,106 @@ 1.4 +/* 1.5 + * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6403468 1.30 + * @summary Test that an erroneous return code results from raising an error. 1.31 + * @author Joseph D. Darcy 1.32 + * @library /tools/javac/lib 1.33 + * @build JavacTestingAbstractProcessor CompileFail 1.34 + * @compile TestReturnCode.java 1.35 + * 1.36 + * @compile -processor TestReturnCode -proc:only Foo.java 1.37 + * @run main CompileFail ERROR -processor TestReturnCode -proc:only -AErrorOnFirst Foo.java 1.38 + * @run main CompileFail ERROR -processor TestReturnCode -proc:only -AErrorOnLast Foo.java 1.39 + * @run main CompileFail ERROR -processor TestReturnCode -proc:only -AErrorOnLast -AErrorOnFirst Foo.java 1.40 + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnFirst Foo.java 1.41 + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnFirst -AErrorOnFirst Foo.java 1.42 + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnFirst -AErrorOnLast Foo.java 1.43 + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnFirst -AErrorOnLast -AErrorOnFirst Foo.java 1.44 + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast Foo.java 1.45 + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AErrorOnFirst Foo.java 1.46 + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AErrorOnLast Foo.java 1.47 + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AErrorOnLast -AErrorOnFirst Foo.java 1.48 + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AExceptionOnFirst Foo.java 1.49 + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AExceptionOnFirst -AErrorOnFirst Foo.java 1.50 + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AExceptionOnFirst -AErrorOnLast Foo.java 1.51 + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AExceptionOnFirst -AErrorOnLast -AErrorOnFirst Foo.java 1.52 + */ 1.53 + 1.54 +import java.util.Set; 1.55 +import java.util.HashSet; 1.56 +import java.util.Arrays; 1.57 +import javax.annotation.processing.*; 1.58 +import javax.lang.model.SourceVersion; 1.59 +import javax.lang.model.element.*; 1.60 +import javax.lang.model.util.*; 1.61 +import static javax.tools.Diagnostic.Kind.*; 1.62 + 1.63 + 1.64 +/** 1.65 + * This processor raises errors or throws exceptions on different 1.66 + * rounds to allow the return code to be test. 1.67 + */ 1.68 +@SupportedOptions({"ErrorOnFirst", 1.69 + "ErrorOnLast", 1.70 + "ExceptionOnFirst", 1.71 + "ExceptionOnLast"}) 1.72 +public class TestReturnCode extends JavacTestingAbstractProcessor { 1.73 + 1.74 + private boolean errorOnFirst; 1.75 + private boolean errorOnLast; 1.76 + private boolean exceptionOnFirst; 1.77 + private boolean exceptionOnLast; 1.78 + 1.79 + public boolean process(Set<? extends TypeElement> annotations, 1.80 + RoundEnvironment roundEnv) { 1.81 + if (!roundEnv.processingOver()) { 1.82 + System.out.format("Variables: %b\t%b\t%b\t%b%n", 1.83 + errorOnFirst, 1.84 + errorOnLast, 1.85 + exceptionOnFirst, 1.86 + exceptionOnLast); 1.87 + if (errorOnFirst) 1.88 + messager.printMessage(ERROR, "Error on first round."); 1.89 + if (exceptionOnFirst) 1.90 + throw new RuntimeException("Exception on first round."); 1.91 + } else { 1.92 + if (errorOnLast) 1.93 + messager.printMessage(ERROR, "Error on last round."); 1.94 + if (exceptionOnLast) 1.95 + throw new RuntimeException("Exception on last round."); 1.96 + } 1.97 + return true; 1.98 + } 1.99 + 1.100 + @Override 1.101 + public void init(ProcessingEnvironment processingEnv) { 1.102 + super.init(processingEnv); 1.103 + Set<String> keySet = processingEnv.getOptions().keySet(); 1.104 + errorOnFirst = keySet.contains("ErrorOnFirst"); 1.105 + errorOnLast = keySet.contains("ErrorOnLast"); 1.106 + exceptionOnFirst = keySet.contains("ExceptionOnFirst"); 1.107 + exceptionOnLast = keySet.contains("ExceptionOnLast"); 1.108 + } 1.109 +}