|
1 /* |
|
2 * Copyright 2016 Google Inc. 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. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 import static java.util.stream.Collectors.toCollection; |
|
25 |
|
26 import java.lang.annotation.ElementType; |
|
27 import java.lang.annotation.Retention; |
|
28 import java.lang.annotation.RetentionPolicy; |
|
29 import java.lang.annotation.Target; |
|
30 import java.util.ArrayList; |
|
31 import java.util.Arrays; |
|
32 import java.util.List; |
|
33 import java.util.Set; |
|
34 import javax.annotation.processing.RoundEnvironment; |
|
35 import javax.annotation.processing.SupportedAnnotationTypes; |
|
36 import javax.lang.model.element.Element; |
|
37 import javax.lang.model.element.ExecutableElement; |
|
38 import javax.lang.model.element.TypeElement; |
|
39 import javax.tools.Diagnostic.Kind; |
|
40 |
|
41 /* |
|
42 * @test |
|
43 * @bug 8170667 |
|
44 * @summary ClassReader assigns method parameters from MethodParameters incorrectly when long/double |
|
45 * parameters are present |
|
46 * @library /tools/javac/lib |
|
47 * @modules java.compiler |
|
48 * jdk.compiler |
|
49 * @compile -parameters ParameterProcessor.java |
|
50 * @compile/process -proc:only -processor ParameterProcessor ParameterProcessor |
|
51 */ |
|
52 @SupportedAnnotationTypes("ParameterProcessor.ParameterNames") |
|
53 public class ParameterProcessor extends JavacTestingAbstractProcessor { |
|
54 |
|
55 @Retention(RetentionPolicy.RUNTIME) |
|
56 @Target(ElementType.METHOD) |
|
57 @interface ParameterNames { |
|
58 String[] value() default {}; |
|
59 } |
|
60 |
|
61 @ParameterProcessor.ParameterNames({"a", "b", "c"}) |
|
62 void f(int a, int b, int c) {} |
|
63 |
|
64 @ParameterProcessor.ParameterNames({"d", "e", "f"}) |
|
65 void g(int d, long e, int f) {} |
|
66 |
|
67 @ParameterProcessor.ParameterNames({"g", "h", "i", "j"}) |
|
68 void h(int g, double h, int i, int j) {} |
|
69 |
|
70 @Override |
|
71 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
|
72 for (Element element : roundEnv.getElementsAnnotatedWith(ParameterNames.class)) { |
|
73 ParameterNames names = element.getAnnotation(ParameterNames.class); |
|
74 if (names == null) { |
|
75 continue; |
|
76 } |
|
77 List<String> expected = Arrays.asList(names.value()); |
|
78 List<String> actual = |
|
79 ((ExecutableElement) element) |
|
80 .getParameters() |
|
81 .stream() |
|
82 .map(p -> p.getSimpleName().toString()) |
|
83 .collect(toCollection(ArrayList::new)); |
|
84 if (!expected.equals(actual)) { |
|
85 String message = |
|
86 String.format( |
|
87 "bad parameter names for %s#%s; expected: %s, was: %s", |
|
88 element, element, expected, actual); |
|
89 messager.printMessage(Kind.ERROR, message); |
|
90 } |
|
91 } |
|
92 return false; |
|
93 } |
|
94 } |