2
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
26
* @summary test for com.sun.tools.javac.comp.Check::validateAnnotation, com.sun.tools.javac.code.SymbolMetadata::removeDeclarationMetadata and ::removeFromCompoundList
27
* @bug 8241312 8246774
30
* @modules jdk.compiler/com.sun.tools.javac.util
31
* java.base/jdk.internal.classfile.impl
32
* @run main ApplicableAnnotationsOnRecords
34
import java.lang.classfile.*;
35
import com.sun.tools.javac.util.Assert;
36
import java.lang.annotation.*;
37
import java.io.InputStream;
38
import java.util.Objects;
40
@Retention(RetentionPolicy.RUNTIME)
41
@Target({ElementType.FIELD})
42
@interface FieldAnnotation {
45
@Retention(RetentionPolicy.RUNTIME)
46
@Target({ElementType.METHOD})
47
@interface MethodAnnotation {
50
@Retention(RetentionPolicy.RUNTIME)
51
@Target({ElementType.PARAMETER})
52
@interface ParameterAnnotation {
55
public record ApplicableAnnotationsOnRecords(@FieldAnnotation @MethodAnnotation @ParameterAnnotation String s, @FieldAnnotation @MethodAnnotation @ParameterAnnotation int i) {
57
public static void main(String... args) throws Exception {
58
try ( InputStream in = ApplicableAnnotationsOnRecords.class.getResourceAsStream("ApplicableAnnotationsOnRecords.class")) {
59
ClassModel cm = ClassFile.of().parse(Objects.requireNonNull(in).readAllBytes());
60
Assert.check(cm.methods().size() > 5);
61
for (MethodModel mm : cm.methods()) {
62
String methodName = mm.methodName().stringValue();
63
if (methodName.equals("toString") || methodName.equals("hashCode") || methodName.equals("equals") || methodName.equals("main")) {
65
} else if (methodName.equals("<init>")) {
66
var paAnnos = mm.findAttribute(Attributes.runtimeVisibleParameterAnnotations()).orElseThrow().parameterAnnotations();
67
Assert.check(paAnnos.size() > 0);
68
for (var pa : paAnnos) {
69
Assert.check(pa.size() == 1);
70
Assert.check(Objects.equals(pa.get(0).classSymbol().descriptorString(), "LParameterAnnotation;"));
73
var annos = mm.findAttribute(Attributes.runtimeVisibleAnnotations()).orElseThrow().annotations();
74
Assert.check(annos.size() == 1);
75
Assert.check(Objects.equals(annos.get(0).classSymbol().descriptorString(), "LMethodAnnotation;"));
78
Assert.check(cm.fields().size() > 0);
79
for (FieldModel fm : cm.fields()) {
80
var annos = fm.findAttribute(Attributes.runtimeVisibleAnnotations()).orElseThrow().annotations();
81
Assert.check(annos.size() == 1);
82
Assert.check(Objects.equals(annos.getFirst().classSymbol().descriptorString(), "LFieldAnnotation;"));