|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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. |
| 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 | +/* |
| 25 | + * @test |
| 26 | + * @summary Check if error is thrown if annotation array exceeds limit |
| 27 | + * @library /tools/lib |
| 28 | + * @run main ParameterArrayLimit |
| 29 | + */ |
| 30 | + |
| 31 | +import java.io.BufferedWriter; |
| 32 | +import java.io.IOException; |
| 33 | +import java.net.URI; |
| 34 | +import java.nio.file.Files; |
| 35 | +import java.nio.file.Path; |
| 36 | +import java.nio.file.Paths; |
| 37 | +import java.text.MessageFormat; |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.List; |
| 40 | +import javax.tools.*; |
| 41 | + |
| 42 | +import com.sun.source.util.JavacTask; |
| 43 | + |
| 44 | +public class ParameterArrayLimit { |
| 45 | + |
| 46 | + public static void main(String[] args) throws IOException { |
| 47 | + |
| 48 | + int[] values = new int[]{65536, 65537, 512000}; |
| 49 | + String[] retPolicies = {"RUNTIME", "CLASS"}; |
| 50 | + |
| 51 | + for (var value : values) { |
| 52 | + Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")); |
| 53 | + |
| 54 | + for (String retPolicy : retPolicies) { |
| 55 | + String className = MessageFormat.format("ClassAnnotationWithLength_{0,number,#}_{1}.java", |
| 56 | + value, |
| 57 | + retPolicy); |
| 58 | + Path out = tmpDir.resolve(className); |
| 59 | + createAnnotationFile(out, value, retPolicy, false); |
| 60 | + checkParamArrayWarning(className, out); |
| 61 | + } |
| 62 | + |
| 63 | + for (String retPolicy : retPolicies) { |
| 64 | + String className = MessageFormat.format("TypeAnnotationWithLength_{0,number,#}_{1}.java", |
| 65 | + value, |
| 66 | + retPolicy); |
| 67 | + Path out = tmpDir.resolve(className); |
| 68 | + createAnnotationFile(out, value, retPolicy, true); |
| 69 | + checkParamArrayWarning(className, out); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static void checkParamArrayWarning(String className, Path out) throws IOException { |
| 75 | + JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); |
| 76 | + DiagnosticCollector<JavaFileObject> d = new DiagnosticCollector<>(); |
| 77 | + JavacTask task = (JavacTask) javaCompiler.getTask( |
| 78 | + null, |
| 79 | + null, |
| 80 | + d, |
| 81 | + null, |
| 82 | + null, |
| 83 | + Collections.singletonList( |
| 84 | + SimpleJavaFileObject.forSource( |
| 85 | + URI.create("myfo:/" + className), |
| 86 | + Files.readString(out) |
| 87 | + ))); |
| 88 | + task.call(); |
| 89 | + |
| 90 | + List<Diagnostic<? extends JavaFileObject>> diagnosticList = d.getDiagnostics(); |
| 91 | + if (diagnosticList.isEmpty()) { |
| 92 | + throw new RuntimeException("No diagnostic found"); |
| 93 | + } |
| 94 | + |
| 95 | + for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticList) { |
| 96 | + if (!(diagnostic.getKind() == Diagnostic.Kind.ERROR |
| 97 | + && diagnostic.getCode() |
| 98 | + .equals("compiler.err.annotation.array.too.large"))) { |
| 99 | + throw new RuntimeException("Unexpected diagnostic: " + diagnostic.getMessage(null)); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private static void createAnnotationFile(Path out, int value, String retPolicy, boolean isTypeAnnotation) throws IOException { |
| 105 | + StringBuilder sb = new StringBuilder(); |
| 106 | + |
| 107 | + if (isTypeAnnotation) { |
| 108 | + sb.append(MessageFormat.format(""" |
| 109 | + import java.lang.annotation.*; |
| 110 | + @Retention(RetentionPolicy.{0}) |
| 111 | + @Target(ElementType.TYPE_USE) |
| 112 | + @interface TypeAnno '{' |
| 113 | + long[] arr(); |
| 114 | + '}' |
| 115 | + """, retPolicy)); |
| 116 | + sb.append(MessageFormat.format(""" |
| 117 | + public class TypeAnnotationWithLength_{0,number,#}_{1}'{' |
| 118 | + @TypeAnno(arr = '{' |
| 119 | + """, value, retPolicy)); |
| 120 | + } else { |
| 121 | + sb.append(MessageFormat.format(""" |
| 122 | + import java.lang.annotation.*; |
| 123 | + @Retention(RetentionPolicy.{0}) |
| 124 | + @interface MyCustomAnno '{' |
| 125 | + String value() default "default value"; |
| 126 | + long[] arr(); |
| 127 | + int count() default 0; |
| 128 | + '}' |
| 129 | + """, retPolicy)); |
| 130 | + sb.append(MessageFormat.format(""" |
| 131 | + public class ClassAnnotationWithLength_{0,number,#}_{1}'{' |
| 132 | + @MyCustomAnno(value = "custom", count = 42, arr = '{' |
| 133 | + """, value, retPolicy)); |
| 134 | + } |
| 135 | + |
| 136 | + sb.append("-1,".repeat(Math.max(0, value - 1))); |
| 137 | + sb.append("-1})"); |
| 138 | + |
| 139 | + sb.append(""" |
| 140 | + static int x = 3; |
| 141 | +
|
| 142 | + public void myAnnotatedMethod() { } |
| 143 | + } |
| 144 | + """); |
| 145 | + |
| 146 | + try (BufferedWriter bufferedWriter = Files.newBufferedWriter(out)) { |
| 147 | + bufferedWriter.write(sb.toString()); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments