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