Path: blob/master/test/langtools/tools/javap/T6716452.java
41144 views
/*1* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test 671645225* @summary need a method to get an index of an attribute26* @modules jdk.jdeps/com.sun.tools.classfile27*/2829import java.io.*;30import com.sun.tools.classfile.*;3132public class T6716452 {33public static void main(String[] args) throws Exception {34new T6716452().run();35}3637public void run() throws Exception {38File javaFile = writeTestFile();39File classFile = compileTestFile(javaFile);4041ClassFile cf = ClassFile.read(classFile);42for (Method m: cf.methods) {43test(cf, m);44}4546if (errors > 0)47throw new Exception(errors + " errors found");48}4950void test(ClassFile cf, Method m) {51test(cf, m, Attribute.Code, Code_attribute.class);52test(cf, m, Attribute.Exceptions, Exceptions_attribute.class);53}5455// test the result of Attributes.getIndex according to expectations56// encoded in the method's name57void test(ClassFile cf, Method m, String name, Class<?> c) {58int index = m.attributes.getIndex(cf.constant_pool, name);59try {60String m_name = m.getName(cf.constant_pool);61System.err.println("Method " + m_name + " name:" + name + " index:" + index + " class: " + c);62boolean expect = (m_name.equals("<init>") && name.equals("Code"))63|| (m_name.indexOf(name) != -1);64boolean found = (index != -1);65if (expect) {66if (found) {67Attribute attr = m.attributes.get(index);68if (!c.isAssignableFrom(attr.getClass())) {69error(m + ": unexpected attribute found,"70+ " expected " + c.getName()71+ " found " + attr.getClass().getName());72}73} else {74error(m + ": expected attribute " + name + " not found");75}76} else {77if (found) {78error(m + ": unexpected attribute " + name);79}80}81} catch (ConstantPoolException e) {82error(m + ": " + e);83}84}8586File writeTestFile() throws IOException {87File f = new File("Test.java");88PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));89out.println("abstract class Test { ");90out.println(" abstract void m();");91out.println(" void m_Code() { }");92out.println(" abstract void m_Exceptions() throws Exception;");93out.println(" void m_Code_Exceptions() throws Exception { }");94out.println("}");95out.close();96return f;97}9899File compileTestFile(File f) {100int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });101if (rc != 0)102throw new Error("compilation failed. rc=" + rc);103String path = f.getPath();104return new File(path.substring(0, path.length() - 5) + ".class");105}106107void error(String msg) {108System.err.println("error: " + msg);109errors++;110}111112int errors;113}114115116