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