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