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.
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 need a method to get an index of an attribute
31
import java.nio.file.Files;
33
import java.lang.classfile.*;
34
import java.lang.classfile.attribute.*;
36
public class T6716452 {
37
public static void main(String[] args) throws Exception {
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()) {
50
throw new Exception(errors + " errors found");
53
void test(MethodModel mm) {
54
test(mm, Attributes.code(), CodeAttribute.class);
55
test(mm, Attributes.exceptions(), ExceptionsAttribute.class);
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);
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());
76
error(mm + ": expected attribute " + attr.name() + " not found");
80
error(mm + ": unexpected attribute " + attr.name());
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 { }");
98
File compileTestFile(File f) {
99
int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });
101
throw new Error("compilation failed. rc=" + rc);
102
String path = f.getPath();
103
return new File(path.substring(0, path.length() - 5) + ".class");
106
void error(String msg) {
107
System.err.println("error: " + msg);