Main.java revision 7baeded82e40352ebfb43b716e0b1dd752e72e7e
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;
18
19import com.android.dx.Version;
20
21import junit.textui.TestRunner;
22
23/**
24 * Main class for dx. It recognizes enough options to be able to dispatch
25 * to the right "actual" main.
26 */
27public class Main {
28    private static String USAGE_MESSAGE =
29        "usage:\n" +
30        "  dx --dex [--debug] [--verbose] [--positions=<style>] " +
31        "[--no-locals]\n" +
32        "  [--no-optimize] [--statistics] [--[no-]optimize-list=<file>] " +
33        "[--no-strict]\n" +
34        "  [--keep-classes] [--output=<file>] [--dump-to=<file>] " +
35        "[--dump-width=<n>]\n" +
36        "  [--dump-method=<name>[*]] [--verbose-dump] [--no-files] " +
37        "[--core-library]\n" +
38        "  [--num-threads=<n>] [--target-api=<n>]\n" +
39        "  [<file>.class | <file>.{zip,jar,apk} | <directory>] ...\n" +
40        "    Convert a set of classfiles into a dex file, optionally " +
41        "embedded in a\n" +
42        "    jar/zip. Output name must end with one of: .dex .jar " +
43        ".zip .apk. Positions\n" +
44        "    options: none, important, lines.\n" +
45        "  dx --annotool --annotation=<class> [--element=<element types>]\n" +
46        "  [--print=<print types>]\n" +
47        "  dx --dump [--debug] [--strict] [--bytes] [--optimize]\n" +
48        "  [--basic-blocks | --rop-blocks | --ssa-blocks | --dot] " +
49        "[--ssa-step=<step>]\n" +
50        "  [--width=<n>] [<file>.class | <file>.txt] ...\n" +
51        "    Dump classfiles, or transformations thereof, in a " +
52        "human-oriented format.\n" +
53        "  dx --junit [-wait] <TestClass>\n" +
54        "    Run the indicated unit test.\n" +
55        "  dx --find-usages <file.dex> <declaring type> <member>\n" +
56        "    Find references and declarations to a field or method.\n" +
57        "    declaring type: a class name in internal form, like " +
58        "Ljava/lang/Object;\n" +
59        "    member: a field or method name, like hashCode\n" +
60        "  dx -J<option> ... <arguments, in one of the above " +
61        "forms>\n" +
62        "    Pass VM-specific options to the virtual machine that " +
63        "runs dx.\n" +
64        "  dx --version\n" +
65        "    Print the version of this tool (" + Version.VERSION +
66        ").\n" +
67        "  dx --help\n" +
68        "    Print this message.";
69
70    /**
71     * This class is uninstantiable.
72     */
73    private Main() {
74        // This space intentionally left blank.
75    }
76
77    /**
78     * Run!
79     */
80    public static void main(String[] args) {
81        boolean gotCmd = false;
82        boolean showUsage = false;
83
84        try {
85            for (int i = 0; i < args.length; i++) {
86                String arg = args[i];
87                if (arg.equals("--") || !arg.startsWith("--")) {
88                    gotCmd = false;
89                    showUsage = true;
90                    break;
91                }
92
93                gotCmd = true;
94                if (arg.equals("--dex")) {
95                    com.android.dx.command.dexer.Main.main(without(args, i));
96                    break;
97                } else if (arg.equals("--dump")) {
98                    com.android.dx.command.dump.Main.main(without(args, i));
99                    break;
100                } else if (arg.equals("--annotool")) {
101                    com.android.dx.command.annotool.Main.main(
102                            without(args, i));
103                    break;
104                } else if (arg.equals("--junit")) {
105                    TestRunner.main(without(args, i));
106                    break;
107                } else if (arg.equals("--find-usages")) {
108                    com.android.dx.command.findusages.Main.main(without(args, i));
109                    break;
110                } else if (arg.equals("--version")) {
111                    version();
112                    break;
113                } else if (arg.equals("--help")) {
114                    showUsage = true;
115                    break;
116                } else {
117                    gotCmd = false;
118                }
119            }
120        } catch (UsageException ex) {
121            showUsage = true;
122        } catch (RuntimeException ex) {
123            System.err.println("\nUNEXPECTED TOP-LEVEL EXCEPTION:");
124            ex.printStackTrace();
125            System.exit(2);
126        } catch (Throwable ex) {
127            System.err.println("\nUNEXPECTED TOP-LEVEL ERROR:");
128            ex.printStackTrace();
129            if ((ex instanceof NoClassDefFoundError)
130                    || (ex instanceof NoSuchMethodError)) {
131                System.err.println(
132                        "Note: You may be using an incompatible " +
133                        "virtual machine or class library.\n" +
134                        "(This program is known to be incompatible " +
135                        "with recent releases of GCJ.)");
136            }
137            System.exit(3);
138        }
139
140        if (!gotCmd) {
141            System.err.println("error: no command specified");
142            showUsage = true;
143        }
144
145        if (showUsage) {
146            usage();
147            System.exit(1);
148        }
149    }
150
151    /**
152     * Prints the version message.
153     */
154    private static void version() {
155        System.err.println("dx version " + Version.VERSION);
156        System.exit(0);
157    }
158
159    /**
160     * Prints the usage message.
161     */
162    private static void usage() {
163        System.err.println(USAGE_MESSAGE);
164    }
165
166    /**
167     * Returns a copy of the given args array, but without the indicated
168     * element.
169     *
170     * @param orig {@code non-null;} original array
171     * @param n which element to omit
172     * @return {@code non-null;} new array
173     */
174    private static String[] without(String[] orig, int n) {
175        int len = orig.length - 1;
176        String[] newa = new String[len];
177        System.arraycopy(orig, 0, newa, 0, n);
178        System.arraycopy(orig, n + 1, newa, n, len - n);
179        return newa;
180    }
181}
182