Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/processor/TruffleProcessorTest.java @ 21480:c2b006c5e15f
Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Mon, 25 May 2015 10:36:30 +0200 |
parents | 24d12404e70a |
children | f5b49d881909 b1530a6cce8c |
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 |
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
|
25 import com.oracle.truffle.api.dsl.ExpectError; |
c2b006c5e15f
Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21466
diff
changeset
|
26 import com.oracle.truffle.api.nodes.Node; |
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
|
27 import com.oracle.truffle.dsl.processor.verify.VerifyTruffleProcessor; |
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
|
28 import java.util.Locale; |
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
|
29 import java.util.ServiceLoader; |
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
|
30 import javax.annotation.processing.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
|
31 import javax.tools.Diagnostic; |
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
|
32 import javax.tools.JavaFileObject; |
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
|
33 import static org.junit.Assert.*; |
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
|
34 import org.junit.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
|
35 |
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 * 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
|
38 */ |
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 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
|
40 // |
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 // 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
|
42 // |
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 |
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
|
44 @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
|
45 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
|
46 // @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
|
47 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
|
48 "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
|
49 "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
|
50 " @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
|
51 " 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
|
52 " 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
|
53 " };\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 // @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
|
56 |
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 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 } |
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 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
|
67 } |
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 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
|
69 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
|
70 } |
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 @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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 } |
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 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
|
86 } |
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
|
87 |
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 // 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
|
90 // |
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 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
|
93 @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
|
94 |
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 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
|
96 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
|
97 } |
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 } |
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
|
99 } |