1 /* |
1 /* |
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 /* |
24 /* |
25 * @test |
25 * @test |
26 * @bug 8005046 |
26 * @bug 8005046 8011052 |
27 * @summary Test basic properties of javax.lang.element.Element |
27 * @summary Test basic properties of javax.lang.element.Element |
28 * @author Joseph D. Darcy |
28 * @author Joseph D. Darcy |
29 * @library /tools/javac/lib |
29 * @library /tools/javac/lib |
30 * @build JavacTestingAbstractProcessor TestExecutableElement |
30 * @build JavacTestingAbstractProcessor TestExecutableElement |
31 * @compile -processor TestExecutableElement -proc:only TestExecutableElement.java |
31 * @compile -processor TestExecutableElement -proc:only TestExecutableElement.java |
33 |
33 |
34 import java.lang.annotation.*; |
34 import java.lang.annotation.*; |
35 import java.util.Formatter; |
35 import java.util.Formatter; |
36 import java.util.Set; |
36 import java.util.Set; |
37 import java.util.Objects; |
37 import java.util.Objects; |
|
38 import java.util.regex.*; |
38 import javax.annotation.processing.*; |
39 import javax.annotation.processing.*; |
39 import javax.lang.model.SourceVersion; |
40 import javax.lang.model.SourceVersion; |
40 import static javax.lang.model.SourceVersion.*; |
41 import static javax.lang.model.SourceVersion.*; |
41 import javax.lang.model.element.*; |
42 import javax.lang.model.element.*; |
42 import javax.lang.model.util.*; |
43 import javax.lang.model.util.*; |
77 expectedIsDefault.value() : |
78 expectedIsDefault.value() : |
78 false; |
79 false; |
79 |
80 |
80 boolean methodIsDefault = method.isDefault(); |
81 boolean methodIsDefault = method.isDefault(); |
81 |
82 |
|
83 if (expectedDefault) { |
|
84 if (!method.getModifiers().contains(Modifier.DEFAULT)) { |
|
85 messager.printMessage(ERROR, |
|
86 "Modifier \"default\" not present as expected.", |
|
87 method); |
|
88 } |
|
89 |
|
90 // Check printing output |
|
91 java.io.Writer stringWriter = new java.io.StringWriter(); |
|
92 eltUtils.printElements(stringWriter, method); |
|
93 Pattern p = Pattern.compile(expectedIsDefault.expectedTextRegex(), Pattern.DOTALL); |
|
94 |
|
95 if (! p.matcher(stringWriter.toString()).matches()) { |
|
96 messager.printMessage(ERROR, |
|
97 new Formatter().format("Unexpected printing ouptput:%n\tgot %s,%n\texpected pattern %s.", |
|
98 stringWriter.toString(), |
|
99 expectedIsDefault.expectedTextRegex()).toString(), |
|
100 method); |
|
101 } |
|
102 |
|
103 System.out.println("\t" + stringWriter.toString()); |
|
104 |
|
105 } else { |
|
106 if (method.getModifiers().contains(Modifier.DEFAULT)) { |
|
107 messager.printMessage(ERROR, |
|
108 "Modifier \"default\" present when not expected.", |
|
109 method); |
|
110 } |
|
111 } |
|
112 |
82 if (methodIsDefault != expectedDefault) { |
113 if (methodIsDefault != expectedDefault) { |
83 messager.printMessage(ERROR, |
114 messager.printMessage(ERROR, |
84 new Formatter().format("Unexpected Executable.isDefault result: got %s, expected %s", |
115 new Formatter().format("Unexpected Executable.isDefault result: got ``%s'', expected ``%s''.", |
85 expectedDefault, |
116 expectedDefault, |
86 methodIsDefault).toString(), |
117 methodIsDefault).toString(), |
87 method); |
118 method); |
88 return 1; |
119 return 1; |
89 } |
120 } |
96 */ |
127 */ |
97 @Retention(RetentionPolicy.RUNTIME) |
128 @Retention(RetentionPolicy.RUNTIME) |
98 @Target(ElementType.METHOD) |
129 @Target(ElementType.METHOD) |
99 @interface IsDefault { |
130 @interface IsDefault { |
100 boolean value(); |
131 boolean value(); |
|
132 String expectedTextRegex() default ""; |
101 } |
133 } |
102 |
134 |
103 /** |
135 /** |
104 * Test interface to provide a default method. |
136 * Test interface to provide a default method. |
105 */ |
137 */ |
106 interface ProviderOfDefault { |
138 interface ProviderOfDefault { |
107 @IsDefault(false) |
139 @IsDefault(false) |
108 boolean process(Set<? extends TypeElement> annotations, |
140 boolean process(Set<? extends TypeElement> annotations, |
109 RoundEnvironment roundEnv); |
141 RoundEnvironment roundEnv); |
110 |
142 |
111 @IsDefault(true) |
143 @IsDefault(value=true, expectedTextRegex="\\s*@IsDefault\\(.*\\)\\s*default strictfp void quux\\(\\);\\s*$") |
112 default void quux() {}; |
144 default strictfp void quux() {}; |
113 } |
145 } |