2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.dx.command.annotool;
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;
31 import java.lang.annotation.ElementType;
32 import java.util.EnumSet;
33 import java.util.Arrays;
38 private static class InvalidArgumentException extends Exception {
39 InvalidArgumentException() {
43 InvalidArgumentException(String s) {
56 static class Arguments {
58 * from --annotation, dot-seperated classname
59 * of annotation to look for
64 EnumSet<ElementType> eTypes = EnumSet.noneOf(ElementType.class);
67 EnumSet<PrintType> printTypes = EnumSet.noneOf(PrintType.class);
69 /** remaining positional arguments */
75 void parse (String[] argArray) throws InvalidArgumentException {
76 for (int i = 0; i < argArray.length; i++) {
77 String arg = argArray[i];
79 if (arg.startsWith("--annotation=")) {
80 String argParam = arg.substring(arg.indexOf('=') + 1);
82 throw new InvalidArgumentException(
83 "--annotation can only be specified once.");
85 aclass = argParam.replace('.','/');
86 } else if (arg.startsWith("--element=")) {
87 String argParam = arg.substring(arg.indexOf('=') + 1);
90 for (String p : argParam.split(",")) {
91 eTypes.add(ElementType.valueOf(p.toUpperCase()));
93 } catch (IllegalArgumentException ex) {
94 throw new InvalidArgumentException(
97 } else if (arg.startsWith("--print=")) {
98 String argParam = arg.substring(arg.indexOf('=') + 1);
101 for (String p : argParam.split(",")) {
102 printTypes.add(PrintType.valueOf(p.toUpperCase()));
104 } catch (IllegalArgumentException ex) {
105 throw new InvalidArgumentException("invalid --print");
108 files = new String[argArray.length - i];
109 System.arraycopy(argArray, i, files, 0, files.length);
114 if (aclass == null) {
115 throw new InvalidArgumentException(
116 "--annotation must be specified");
119 if (printTypes.isEmpty()) {
120 printTypes.add(PrintType.CLASS);
123 if (eTypes.isEmpty()) {
124 eTypes.add(ElementType.TYPE);
127 EnumSet<ElementType> set = eTypes.clone();
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' "
140 * This class is uninstantiable.
143 // This space intentionally left blank.
146 public static void main(String[] argArray) {
148 final Arguments args = new Arguments();
151 args.parse(argArray);
152 } catch (InvalidArgumentException ex) {
153 System.err.println(ex.getMessage());
155 throw new RuntimeException("usage");
158 new AnnotationLister(args).process();