T6866657.java revision 2942:08092deced3f
1196155Ssam/*
2196155Ssam * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
3196155Ssam * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4196155Ssam *
5196155Ssam * This code is free software; you can redistribute it and/or modify it
6196155Ssam * under the terms of the GNU General Public License version 2 only, as
7196155Ssam * published by the Free Software Foundation.
8196155Ssam *
9196155Ssam * This code is distributed in the hope that it will be useful, but WITHOUT
10196155Ssam * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11196155Ssam * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12196155Ssam * version 2 for more details (a copy is included in the LICENSE file that
13196155Ssam * accompanied this code).
14196155Ssam *
15196155Ssam * You should have received a copy of the GNU General Public License version
16196155Ssam * 2 along with this work; if not, write to the Free Software Foundation,
17196155Ssam * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18196155Ssam *
19196155Ssam * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20196155Ssam * or visit www.oracle.com if you need additional information or have any
21196155Ssam * questions.
22196155Ssam */
23196155Ssam
24196155Ssam
25196155Ssam/*
26196155Ssam * @test
27196155Ssam * @bug 6866657
28196155Ssam * @summary add byteLength() method to primary classfile types
29196155Ssam * @modules jdk.jdeps/com.sun.tools.classfile
30196155Ssam */
31196155Ssam
32196155Ssamimport java.io.*;
33196155Ssamimport java.util.*;
34196155Ssamimport javax.tools.*;
35196155Ssamimport com.sun.tools.javap.*;
36196155Ssam
37196155Ssampublic class T6866657
38196155Ssam{
39196155Ssam    public static void main(String... args) {
40196155Ssam        new T6866657().run();
41196155Ssam    }
42196155Ssam
43196155Ssam    void run() {
44196155Ssam        verify("java.lang.Object");
45196155Ssam        verify("java.lang.String");
46196155Ssam        verify("java.util.List");
47196155Ssam        verify("java.util.ArrayList");
48196155Ssam        if (errors > 0)
49196155Ssam            throw new Error(errors + " found.");
50196155Ssam    }
51196155Ssam
52196155Ssam    void verify(String className) {
53196155Ssam        try {
54196155Ssam            PrintWriter log = new PrintWriter(System.out);
55196155Ssam            JavaFileManager fileManager = JavapFileManager.create(null, log);
56197300Sbrueffer            JavaFileObject fo = fileManager.getJavaFileForInput(StandardLocation.PLATFORM_CLASS_PATH, className, JavaFileObject.Kind.CLASS);
57196155Ssam            if (fo == null) {
58196155Ssam                error("Can't find " + className);
59196155Ssam            } else {
60196155Ssam                JavapTask t = new JavapTask(log, fileManager, null);
61196155Ssam                t.handleOptions(new String[] { "-sysinfo", className });
62196155Ssam                JavapTask.ClassFileInfo cfInfo = t.read(fo);
63196155Ssam                expectEqual(cfInfo.cf.byteLength(), cfInfo.size);
64196155Ssam            }
65196155Ssam        } catch (Exception e) {
66196155Ssam            e.printStackTrace();
67196155Ssam            error("Exception: " + e);
68196155Ssam        }
69196155Ssam    }
70196155Ssam
71197300Sbrueffer    void expectEqual(int found, int expected) {
72        if (found != expected)
73            error("bad value found: " + found + " expected: " + expected);
74    }
75
76    void error(String msg) {
77        System.err.println(msg);
78        errors++;
79    }
80
81    int errors;
82}
83
84