ClassDumper.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dx.command.dump;
18
19import com.android.dx.cf.direct.DirectClassFile;
20import com.android.dx.cf.direct.StdAttributeFactory;
21import com.android.dx.util.ByteArray;
22
23import java.io.PrintStream;
24
25/**
26 * Utility to dump the contents of class files in a human-friendly form.
27 */
28public final class ClassDumper
29        extends BaseDumper {
30    /**
31     * Dumps the given array, interpreting it as a class file.
32     *
33     * @param bytes non-null; bytes of the (alleged) class file
34     * @param out non-null; where to dump to
35     * passed in as <= 0
36     * @param filePath the file path for the class, excluding any base
37     * directory specification
38     * @param args bag of commandline arguments
39     */
40    public static void dump(byte[] bytes, PrintStream out,
41                            String filePath, Args args) {
42        ClassDumper cd =
43            new ClassDumper(bytes, out, filePath, args);
44        cd.dump();
45    }
46
47    /**
48     * Constructs an instance. This class is not publicly instantiable.
49     * Use {@link #dump}.
50     */
51    private ClassDumper(byte[] bytes, PrintStream out,
52                        String filePath, Args args) {
53        super(bytes, out, filePath, args);
54    }
55
56    /**
57     * Does the dumping.
58     */
59    public void dump() {
60        byte[] bytes = getBytes();
61        ByteArray ba = new ByteArray(bytes);
62        DirectClassFile cf =
63            new DirectClassFile(ba, getFilePath(), getStrictParse());
64
65        cf.setAttributeFactory(StdAttributeFactory.THE_ONE);
66        cf.setObserver(this);
67        cf.getMagic(); // Force parsing to happen.
68
69        int at = getAt();
70        if (at != bytes.length) {
71            parsed(ba, at, bytes.length - at, "<extra data at end of file>");
72        }
73    }
74}
75