Thu, 21 Feb 2013 14:43:51 -0800
8008405: Now that metafactory is in place, add javac lambda serialization tests
Summary: Tests part of original langtools serialization review.
Reviewed-by: mcimadamore
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2012, 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. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | /* |
aoqi@0 | 27 | * @test |
aoqi@0 | 28 | * @bug 7166010 |
aoqi@0 | 29 | * @summary warnings printed by annotation processors uses incorrect source |
aoqi@0 | 30 | */ |
aoqi@0 | 31 | import com.sun.source.util.JavacTask; |
aoqi@0 | 32 | import java.io.IOException; |
aoqi@0 | 33 | import java.net.URI; |
aoqi@0 | 34 | import java.util.ArrayList; |
aoqi@0 | 35 | import java.util.Arrays; |
aoqi@0 | 36 | import java.util.LinkedList; |
aoqi@0 | 37 | import java.util.List; |
aoqi@0 | 38 | import java.util.Set; |
aoqi@0 | 39 | import javax.annotation.processing.AbstractProcessor; |
aoqi@0 | 40 | import javax.annotation.processing.Messager; |
aoqi@0 | 41 | import javax.annotation.processing.RoundEnvironment; |
aoqi@0 | 42 | import javax.annotation.processing.SupportedAnnotationTypes; |
aoqi@0 | 43 | import javax.annotation.processing.SupportedSourceVersion; |
aoqi@0 | 44 | import javax.lang.model.SourceVersion; |
aoqi@0 | 45 | import javax.lang.model.element.Element; |
aoqi@0 | 46 | import javax.lang.model.element.TypeElement; |
aoqi@0 | 47 | import javax.tools.Diagnostic; |
aoqi@0 | 48 | import javax.tools.DiagnosticCollector; |
aoqi@0 | 49 | import javax.tools.JavaCompiler; |
aoqi@0 | 50 | import javax.tools.JavaFileObject; |
aoqi@0 | 51 | import javax.tools.SimpleJavaFileObject; |
aoqi@0 | 52 | import javax.tools.ToolProvider; |
aoqi@0 | 53 | |
aoqi@0 | 54 | import static javax.tools.Diagnostic.Kind.*; |
aoqi@0 | 55 | import static javax.tools.JavaFileObject.Kind.*; |
aoqi@0 | 56 | |
aoqi@0 | 57 | @SupportedSourceVersion(SourceVersion.RELEASE_6) |
aoqi@0 | 58 | @SupportedAnnotationTypes("*") |
aoqi@0 | 59 | public class MessagerDiags extends AbstractProcessor { |
aoqi@0 | 60 | static final String CNAME = "Test"; |
aoqi@0 | 61 | static final String TEST_JAVA = CNAME + ".java"; |
aoqi@0 | 62 | static final String TEST_JAVA_URI_NAME = "myfo:/" + TEST_JAVA; |
aoqi@0 | 63 | static final String WRN_NO_SOURCE = "warning without source"; |
aoqi@0 | 64 | static final String WRN_WITH_SOURCE = "warning with source"; |
aoqi@0 | 65 | static final String NONE = "<none>"; |
aoqi@0 | 66 | static final String[] EXPECTED = { NONE + ":-1--1:" + WRN_NO_SOURCE, |
aoqi@0 | 67 | TEST_JAVA + ":0-13:" + WRN_WITH_SOURCE, |
aoqi@0 | 68 | NONE + ":-1--1:" + WRN_NO_SOURCE |
aoqi@0 | 69 | }; |
aoqi@0 | 70 | |
aoqi@0 | 71 | @Override |
aoqi@0 | 72 | public boolean process(Set<? extends TypeElement> annotations, |
aoqi@0 | 73 | RoundEnvironment roundEnv) { |
aoqi@0 | 74 | Messager messager = processingEnv.getMessager(); |
aoqi@0 | 75 | for (Element e : roundEnv.getRootElements()) { |
aoqi@0 | 76 | messager.printMessage(WARNING, WRN_NO_SOURCE); |
aoqi@0 | 77 | messager.printMessage(WARNING, WRN_WITH_SOURCE, e); |
aoqi@0 | 78 | messager.printMessage(WARNING, WRN_NO_SOURCE); |
aoqi@0 | 79 | } |
aoqi@0 | 80 | return false; |
aoqi@0 | 81 | } |
aoqi@0 | 82 | |
aoqi@0 | 83 | public static void main(String... args) throws IOException { |
aoqi@0 | 84 | final String bootPath = System.getProperty("sun.boot.class.path"); |
aoqi@0 | 85 | final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
aoqi@0 | 86 | assert tool != null; |
aoqi@0 | 87 | |
aoqi@0 | 88 | DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<>(); |
aoqi@0 | 89 | List<String> options = new LinkedList<>(); |
aoqi@0 | 90 | options.addAll(Arrays.asList("-bootclasspath", bootPath, |
aoqi@0 | 91 | "-source", "1.6", "-classpath", |
aoqi@0 | 92 | System.getProperty("java.class.path"))); |
aoqi@0 | 93 | options.addAll(Arrays.asList("-processor", |
aoqi@0 | 94 | MessagerDiags.class.getName())); |
aoqi@0 | 95 | JavacTask ct = (JavacTask)tool.getTask(null, null, dc, options, null, |
aoqi@0 | 96 | Arrays.asList(new MyFileObject("class " + CNAME + " {}"))); |
aoqi@0 | 97 | ct.analyze(); |
aoqi@0 | 98 | |
aoqi@0 | 99 | List<String> obtainedErrors = new ArrayList<>(); |
aoqi@0 | 100 | |
aoqi@0 | 101 | for (Diagnostic<? extends JavaFileObject> d : dc.getDiagnostics()) { |
aoqi@0 | 102 | String dSource; |
aoqi@0 | 103 | if (d.getSource() != null) { |
aoqi@0 | 104 | dSource = d.getSource().toUri().getPath(); |
aoqi@0 | 105 | dSource = dSource.substring(dSource.lastIndexOf('/') + 1); |
aoqi@0 | 106 | } else { |
aoqi@0 | 107 | dSource = NONE; |
aoqi@0 | 108 | } |
aoqi@0 | 109 | obtainedErrors.add(dSource + ":" + d.getStartPosition() + "-" + |
aoqi@0 | 110 | d.getEndPosition() + ":" + d.getMessage(null)); |
aoqi@0 | 111 | } |
aoqi@0 | 112 | List<String> expectedErrors = Arrays.asList(EXPECTED); |
aoqi@0 | 113 | if (!expectedErrors.equals(obtainedErrors)) { |
aoqi@0 | 114 | System.err.println("Expected: " + expectedErrors); |
aoqi@0 | 115 | System.err.println("Obtained: " + obtainedErrors); |
aoqi@0 | 116 | throw new AssertionError("Messages don't match"); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | static class MyFileObject extends SimpleJavaFileObject { |
aoqi@0 | 121 | private String text; |
aoqi@0 | 122 | public MyFileObject(String text) { |
aoqi@0 | 123 | super(URI.create(TEST_JAVA_URI_NAME), SOURCE); |
aoqi@0 | 124 | this.text = text; |
aoqi@0 | 125 | } |
aoqi@0 | 126 | @Override |
aoqi@0 | 127 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
aoqi@0 | 128 | return text; |
aoqi@0 | 129 | } |
aoqi@0 | 130 | } |
aoqi@0 | 131 | } |