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; 22import java.io.PrintStream; 23 24/** 25 * Utility to dump the contents of class files in a human-friendly form. 26 */ 27public final class ClassDumper 28 extends BaseDumper { 29 /** 30 * Dumps the given array, interpreting it as a class file. 31 * 32 * @param bytes {@code non-null;} bytes of the (alleged) class file 33 * @param out {@code non-null;} where to dump to 34 * passed in as <= 0 35 * @param filePath the file path for the class, excluding any base 36 * directory specification 37 * @param args bag of commandline arguments 38 */ 39 public static void dump(byte[] bytes, PrintStream out, 40 String filePath, Args args) { 41 ClassDumper cd = 42 new ClassDumper(bytes, out, filePath, args); 43 cd.dump(); 44 } 45 46 /** 47 * Constructs an instance. This class is not publicly instantiable. 48 * Use {@link #dump}. 49 */ 50 private ClassDumper(byte[] bytes, PrintStream out, 51 String filePath, Args args) { 52 super(bytes, out, filePath, args); 53 } 54 55 /** 56 * Does the dumping. 57 */ 58 public void dump() { 59 byte[] bytes = getBytes(); 60 ByteArray ba = new ByteArray(bytes); 61 DirectClassFile cf = 62 new DirectClassFile(ba, getFilePath(), getStrictParse()); 63 64 cf.setAttributeFactory(StdAttributeFactory.THE_ONE); 65 cf.setObserver(this); 66 cf.getMagic(); // Force parsing to happen. 67 68 int readBytes = getReadBytes(); 69 if (readBytes != bytes.length) { 70 parsed(ba, readBytes, bytes.length - readBytes, "<extra data at end of file>"); 71 } 72 } 73} 74