annotate truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/processor/TruffleProcessorTest.java @ 22157:dc83cc1f94f2

Using fully qualified imports
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 16 Sep 2015 11:33:22 +0200
parents 9c8c0937da41
children 5309cc9668e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21420
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
1 /*
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
4 *
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
8 *
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
13 * accompanied this code).
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
14 *
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
18 *
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
21 * questions.
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
22 */
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
23 package com.oracle.truffle.api.dsl.test.processor;
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
24
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
25 import com.oracle.truffle.api.dsl.test.ExpectError;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
26 import com.oracle.truffle.api.nodes.Node;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
27 import com.oracle.truffle.dsl.processor.verify.VerifyTruffleProcessor;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
28 import java.util.Locale;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
29 import java.util.ServiceLoader;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
30 import javax.annotation.processing.Processor;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
31 import javax.tools.Diagnostic;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
32 import javax.tools.JavaFileObject;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
33 import static org.junit.Assert.assertNotNull;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
34 import static org.junit.Assert.fail;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
35 import org.junit.Test;
21420
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
36
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
37 /**
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
38 * Verify errors emitted by the processor.
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
39 */
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
40 public class TruffleProcessorTest {
21480
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
41 //
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
42 // AnnotationProcessor test using the NetBeans style
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
43 //
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
44
21420
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
45 @Test
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
46 public void childCannotBeFinal() throws Exception {
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
47 // @formatter:off
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
48 String code = "package x.y.z;\n" +
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
49 "import com.oracle.truffle.api.nodes.Node;\n" +
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
50 "abstract class MyNode extends Node {\n" +
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
51 " @Child final MyNode first;\n" +
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
52 " MyNode(MyNode n) {\n" +
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
53 " this.first = n;\n" +
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
54 " };\n" +
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
55 "}\n";
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
56 // @formatter:on
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
57
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
58 Compile c = Compile.create(VerifyTruffleProcessor.class, code);
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
59 c.assertErrors();
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
60 boolean ok = false;
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
61 StringBuilder msgs = new StringBuilder();
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
62 for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
63 String msg = e.getMessage(Locale.ENGLISH);
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
64 if (msg.contains("cannot be final")) {
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
65 ok = true;
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
66 }
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
67 msgs.append("\n").append(msg);
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
68 }
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
69 if (!ok) {
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
70 fail("Should contain warning about final:" + msgs);
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
71 }
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
72 }
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
73
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
74 @Test
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
75 public void workAroundCannonicalDependency() throws Exception {
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
76 Class<?> myProc = VerifyTruffleProcessor.class;
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
77 assertNotNull(myProc);
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
78 StringBuilder sb = new StringBuilder();
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
79 sb.append("Cannot find ").append(myProc);
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
80 for (Processor load : ServiceLoader.load(Processor.class)) {
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
81 sb.append("Found ").append(load);
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
82 if (myProc.isInstance(load)) {
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
83 return;
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
84 }
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
85 }
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
86 fail(sb.toString());
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
87 }
21480
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
88
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
89 //
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
90 // and now the Truffle traditional way
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
91 //
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
92
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
93 abstract class MyNode extends Node {
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
94 @ExpectError("@Child field cannot be final") @Child final MyNode first;
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
95
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
96 MyNode(MyNode n) {
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
97 this.first = n;
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
98 }
c2b006c5e15f Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21466
diff changeset
99 }
21420
fb17e716b03c The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
100 }