OSDN Git Service

7661c3d458383a0202287ec3b1172f999bc8ff01
[android-x86/dalvik.git] / dx / src / com / android / dx / command / annotool / Main.java
1 /*
2  * Copyright (C) 2008 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.annotool;
18
19 import com.android.dx.cf.direct.ClassPathOpener;
20 import com.android.dx.cf.direct.DirectClassFile;
21 import com.android.dx.cf.direct.StdAttributeFactory;
22 import com.android.dx.cf.iface.AttributeList;
23 import com.android.dx.cf.iface.Attribute;
24 import com.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations;
25 import com.android.dx.cf.attrib.BaseAnnotations;
26 import com.android.dx.cf.attrib.AttRuntimeVisibleAnnotations;
27 import com.android.dx.util.ByteArray;
28 import com.android.dx.rop.annotation.Annotation;
29
30 import java.io.File;
31 import java.lang.annotation.ElementType;
32 import java.util.EnumSet;
33 import java.util.Arrays;
34
35
36 public class Main {
37
38     private static class InvalidArgumentException extends Exception {
39         InvalidArgumentException() {
40             super();
41         }
42
43         InvalidArgumentException(String s) {
44             super(s);
45         }
46     }
47
48     enum PrintType {
49         CLASS,
50         INNERCLASS,
51         METHOD,
52         PACKAGE
53     }
54
55
56     static class Arguments {
57         /**
58          * from --annotation, dot-seperated classname
59          * of annotation to look for
60          */
61         String aclass;
62
63         /** from --eTypes */
64         EnumSet<ElementType> eTypes = EnumSet.noneOf(ElementType.class);
65
66         /** from --print */
67         EnumSet<PrintType> printTypes = EnumSet.noneOf(PrintType.class);
68
69         /** remaining positional arguments */
70         String[] files;
71
72         Arguments() {
73         }
74
75         void parse (String[] argArray) throws InvalidArgumentException {
76             for (int i = 0; i < argArray.length; i++) {
77                 String arg = argArray[i];
78
79                 if (arg.startsWith("--annotation=")) {
80                     String argParam = arg.substring(arg.indexOf('=') + 1);
81                     if (aclass != null) {
82                         throw new InvalidArgumentException(
83                                 "--annotation can only be specified once.");
84                     }
85                     aclass = argParam.replace('.','/');
86                 } else if (arg.startsWith("--element=")) {
87                     String argParam = arg.substring(arg.indexOf('=') + 1);
88
89                     try {
90                         for (String p : argParam.split(",")) {
91                             eTypes.add(ElementType.valueOf(p.toUpperCase()));
92                         }
93                     } catch (IllegalArgumentException ex) {
94                         throw new InvalidArgumentException(
95                                 "invalid --element");
96                     }
97                 } else if (arg.startsWith("--print=")) {
98                     String argParam = arg.substring(arg.indexOf('=') + 1);
99
100                     try {
101                         for (String p : argParam.split(",")) {
102                             printTypes.add(PrintType.valueOf(p.toUpperCase()));
103                         }
104                     } catch (IllegalArgumentException ex) {
105                         throw new InvalidArgumentException("invalid --print");
106                     }
107                 } else {
108                     files = new String[argArray.length - i];
109                     System.arraycopy(argArray, i, files, 0, files.length);
110                     break;
111                 }
112             }
113
114             if (aclass == null) {
115                 throw new InvalidArgumentException(
116                         "--annotation must be specified");
117             }
118
119             if (printTypes.isEmpty()) {
120                 printTypes.add(PrintType.CLASS);
121             }
122
123             if (eTypes.isEmpty()) {
124                 eTypes.add(ElementType.TYPE);
125             }
126
127             EnumSet<ElementType> set = eTypes.clone();
128
129             set.remove(ElementType.TYPE);
130             set.remove(ElementType.PACKAGE);
131             if (!set.isEmpty()) {
132                 throw new InvalidArgumentException(
133                         "only --element parameters 'type' and 'package' "
134                                 + "supported");
135             }
136         }
137     }
138
139     /**
140      * This class is uninstantiable.
141      */
142     private Main() {
143         // This space intentionally left blank.
144     }
145
146     public static void main(String[] argArray) {
147
148         final Arguments args = new Arguments();
149
150         try {
151             args.parse(argArray);
152         } catch (InvalidArgumentException ex) {
153             System.err.println(ex.getMessage());
154
155             throw new RuntimeException("usage");
156         }
157
158         new AnnotationLister(args).process();
159     }
160 }