test/tools/javap/T6716452.java

Thu, 04 Apr 2013 19:05:42 -0700

author
katleman
date
Thu, 04 Apr 2013 19:05:42 -0700
changeset 1662
4a48f3173534
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8-b84 for changeset cfb65ca92082

jjg@67 1 /*
ohair@554 2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
jjg@67 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@67 4 *
jjg@67 5 * This code is free software; you can redistribute it and/or modify it
jjg@67 6 * under the terms of the GNU General Public License version 2 only, as
jjg@67 7 * published by the Free Software Foundation.
jjg@67 8 *
jjg@67 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@67 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@67 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@67 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@67 13 * accompanied this code).
jjg@67 14 *
jjg@67 15 * You should have received a copy of the GNU General Public License version
jjg@67 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@67 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@67 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
jjg@67 22 */
jjg@67 23
jjg@67 24 /*
jjg@67 25 * @test 6716452
jjg@67 26 * @summary need a method to get an index of an attribute
jjg@67 27 */
jjg@67 28
jjg@67 29 import java.io.*;
jjg@67 30 import com.sun.tools.classfile.*;
jjg@67 31
jjg@67 32 public class T6716452 {
jjg@67 33 public static void main(String[] args) throws Exception {
jjg@67 34 new T6716452().run();
jjg@67 35 }
jjg@67 36
jjg@67 37 public void run() throws Exception {
jjg@67 38 File javaFile = writeTestFile();
jjg@67 39 File classFile = compileTestFile(javaFile);
jjg@67 40
jjg@67 41 ClassFile cf = ClassFile.read(classFile);
jjg@67 42 for (Method m: cf.methods) {
jjg@67 43 test(cf, m);
jjg@67 44 }
jjg@67 45
jjg@67 46 if (errors > 0)
jjg@67 47 throw new Exception(errors + " errors found");
jjg@67 48 }
jjg@67 49
jjg@67 50 void test(ClassFile cf, Method m) {
jjg@67 51 test(cf, m, Attribute.Code, Code_attribute.class);
jjg@67 52 test(cf, m, Attribute.Exceptions, Exceptions_attribute.class);
jjg@67 53 }
jjg@67 54
jjg@67 55 // test the result of Attributes.getIndex according to expectations
jjg@67 56 // encoded in the method's name
jjg@67 57 void test(ClassFile cf, Method m, String name, Class<?> c) {
jjg@67 58 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@67 59 try {
jjg@67 60 String m_name = m.getName(cf.constant_pool);
jjg@67 61 System.err.println("Method " + m_name + " name:" + name + " index:" + index + " class: " + c);
jjg@67 62 boolean expect = (m_name.equals("<init>") && name.equals("Code"))
jjg@67 63 || (m_name.indexOf(name) != -1);
jjg@67 64 boolean found = (index != -1);
jjg@67 65 if (expect) {
jjg@67 66 if (found) {
jjg@67 67 Attribute attr = m.attributes.get(index);
jjg@67 68 if (!c.isAssignableFrom(attr.getClass())) {
jjg@67 69 error(m + ": unexpected attribute found,"
jjg@67 70 + " expected " + c.getName()
jjg@67 71 + " found " + attr.getClass().getName());
jjg@67 72 }
jjg@67 73 } else {
jjg@67 74 error(m + ": expected attribute " + name + " not found");
jjg@67 75 }
jjg@67 76 } else {
jjg@67 77 if (found) {
jjg@67 78 error(m + ": unexpected attribute " + name);
jjg@67 79 }
jjg@67 80 }
jjg@67 81 } catch (ConstantPoolException e) {
jjg@67 82 error(m + ": " + e);
jjg@67 83 }
jjg@67 84 }
jjg@67 85
jjg@67 86 File writeTestFile() throws IOException {
jjg@67 87 File f = new File("Test.java");
jjg@67 88 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
jjg@67 89 out.println("abstract class Test { ");
jjg@67 90 out.println(" abstract void m();");
jjg@67 91 out.println(" void m_Code() { }");
jjg@67 92 out.println(" abstract void m_Exceptions() throws Exception;");
jjg@67 93 out.println(" void m_Code_Exceptions() throws Exception { }");
jjg@67 94 out.println("}");
jjg@67 95 out.close();
jjg@67 96 return f;
jjg@67 97 }
jjg@67 98
jjg@67 99 File compileTestFile(File f) {
jjg@67 100 int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });
jjg@67 101 if (rc != 0)
jjg@67 102 throw new Error("compilation failed. rc=" + rc);
jjg@67 103 String path = f.getPath();
jjg@67 104 return new File(path.substring(0, path.length() - 5) + ".class");
jjg@67 105 }
jjg@67 106
jjg@67 107 void error(String msg) {
jjg@67 108 System.err.println("error: " + msg);
jjg@67 109 errors++;
jjg@67 110 }
jjg@67 111
jjg@67 112 int errors;
jjg@67 113 }

mercurial