jdk

Форк
0
/
T6716452.java 
112 строк · 4.1 Кб
1
/*
2
 * Copyright (c) 2008, 2015, 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 6716452
26
 * @summary need a method to get an index of an attribute
27
 * @enablePreview
28
 */
29

30
import java.io.*;
31
import java.nio.file.Files;
32

33
import java.lang.classfile.*;
34
import java.lang.classfile.attribute.*;
35

36
public class T6716452 {
37
    public static void main(String[] args) throws Exception {
38
        new T6716452().run();
39
    }
40

41
    public void run() throws Exception {
42
        File javaFile = writeTestFile();
43
        File classFile = compileTestFile(javaFile);
44
        ClassModel cm = ClassFile.of().parse(classFile.toPath());
45
        for (MethodModel mm: cm.methods()) {
46
            test(mm);
47
        }
48

49
        if (errors > 0)
50
            throw new Exception(errors + " errors found");
51
    }
52

53
    void test(MethodModel mm) {
54
        test(mm, Attributes.code(), CodeAttribute.class);
55
        test(mm, Attributes.exceptions(), ExceptionsAttribute.class);
56
    }
57

58
    // test the result of MethodModel.findAttribute, MethodModel.attributes().indexOf() according to expectations
59
    // encoded in the method's name
60
    <T extends Attribute<T>> void test(MethodModel mm, AttributeMapper<T> attr, Class<?> c) {
61
        Attribute<T> attr_instance = mm.findAttribute(attr).orElse(null);
62
        int index = mm.attributes().indexOf(attr_instance);
63
        String mm_name = mm.methodName().stringValue();
64
        System.err.println("Method " + mm_name + " name:" + attr.name() + " index:" + index + " class: " + c);
65
        boolean expect = (mm_name.equals("<init>") && attr.name().equals("Code"))
66
                || (mm_name.contains(attr.name()));
67
        boolean found = (index != -1);
68
        if (expect) {
69
            if (found) {
70
                if (!c.isAssignableFrom(mm.attributes().get(index).getClass())) {
71
                    error(mm + ": unexpected attribute found,"
72
                            + " expected " + c.getName()
73
                            + " found " + mm.attributes().get(index).attributeName());
74
                }
75
            } else {
76
                error(mm + ": expected attribute " + attr.name() + " not found");
77
            }
78
        } else {
79
            if (found) {
80
                error(mm + ": unexpected attribute " + attr.name());
81
            }
82
        }
83
    }
84

85
    File writeTestFile() throws IOException {
86
        File f = new File("Test.java");
87
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
88
        out.println("abstract class Test { ");
89
        out.println("  abstract void m();");
90
        out.println("  void m_Code() { }");
91
        out.println("  abstract void m_Exceptions() throws Exception;");
92
        out.println("  void m_Code_Exceptions() throws Exception { }");
93
        out.println("}");
94
        out.close();
95
        return f;
96
    }
97

98
    File compileTestFile(File f) {
99
        int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });
100
        if (rc != 0)
101
            throw new Error("compilation failed. rc=" + rc);
102
        String path = f.getPath();
103
        return new File(path.substring(0, path.length() - 5) + ".class");
104
    }
105

106
    void error(String msg) {
107
        System.err.println("error: " + msg);
108
        errors++;
109
    }
110

111
    int errors;
112
}
113

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

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

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

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