Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javap/T6716452.java
32285 views
/*1* Copyright (c) 2008, 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*/2728import java.io.*;29import com.sun.tools.classfile.*;3031public class T6716452 {32public static void main(String[] args) throws Exception {33new T6716452().run();34}3536public void run() throws Exception {37File javaFile = writeTestFile();38File classFile = compileTestFile(javaFile);3940ClassFile cf = ClassFile.read(classFile);41for (Method m: cf.methods) {42test(cf, m);43}4445if (errors > 0)46throw new Exception(errors + " errors found");47}4849void test(ClassFile cf, Method m) {50test(cf, m, Attribute.Code, Code_attribute.class);51test(cf, m, Attribute.Exceptions, Exceptions_attribute.class);52}5354// test the result of Attributes.getIndex according to expectations55// encoded in the method's name56void test(ClassFile cf, Method m, String name, Class<?> c) {57int index = m.attributes.getIndex(cf.constant_pool, name);58try {59String m_name = m.getName(cf.constant_pool);60System.err.println("Method " + m_name + " name:" + name + " index:" + index + " class: " + c);61boolean expect = (m_name.equals("<init>") && name.equals("Code"))62|| (m_name.indexOf(name) != -1);63boolean found = (index != -1);64if (expect) {65if (found) {66Attribute attr = m.attributes.get(index);67if (!c.isAssignableFrom(attr.getClass())) {68error(m + ": unexpected attribute found,"69+ " expected " + c.getName()70+ " found " + attr.getClass().getName());71}72} else {73error(m + ": expected attribute " + name + " not found");74}75} else {76if (found) {77error(m + ": unexpected attribute " + name);78}79}80} catch (ConstantPoolException e) {81error(m + ": " + e);82}83}8485File writeTestFile() throws IOException {86File f = new File("Test.java");87PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));88out.println("abstract class Test { ");89out.println(" abstract void m();");90out.println(" void m_Code() { }");91out.println(" abstract void m_Exceptions() throws Exception;");92out.println(" void m_Code_Exceptions() throws Exception { }");93out.println("}");94out.close();95return f;96}9798File compileTestFile(File f) {99int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });100if (rc != 0)101throw new Error("compilation failed. rc=" + rc);102String path = f.getPath();103return new File(path.substring(0, path.length() - 5) + ".class");104}105106void error(String msg) {107System.err.println("error: " + msg);108errors++;109}110111int errors;112}113114115