jdk

Форк
0
/
ApplicableAnnotationsOnRecords.java 
86 строк · 3.9 Кб
1
/*
2
 * Copyright (c) 2020, 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 test for com.sun.tools.javac.comp.Check::validateAnnotation, com.sun.tools.javac.code.SymbolMetadata::removeDeclarationMetadata and ::removeFromCompoundList
27
 * @bug 8241312 8246774
28
 * @library /tools/lib
29
 * @enablePreview
30
 * @modules jdk.compiler/com.sun.tools.javac.util
31
 *          java.base/jdk.internal.classfile.impl
32
 * @run main ApplicableAnnotationsOnRecords
33
 */
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;
39

40
@Retention(RetentionPolicy.RUNTIME)
41
@Target({ElementType.FIELD})
42
@interface FieldAnnotation {
43
}
44

45
@Retention(RetentionPolicy.RUNTIME)
46
@Target({ElementType.METHOD})
47
@interface MethodAnnotation {
48
}
49

50
@Retention(RetentionPolicy.RUNTIME)
51
@Target({ElementType.PARAMETER})
52
@interface ParameterAnnotation {
53
}
54

55
public record ApplicableAnnotationsOnRecords(@FieldAnnotation @MethodAnnotation @ParameterAnnotation String s, @FieldAnnotation @MethodAnnotation @ParameterAnnotation int i) {
56

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")) {
64
                    // ignore
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;"));
71
                    }
72
                } else {
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;"));
76
                }
77
            }
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;"));
83
            }
84
        }
85
    }
86
}
87

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.