• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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.mkstubs.sourcer;
18 
19 import org.objectweb.asm.AnnotationVisitor;
20 
21 /**
22  * An annotation visitor that generates Java source for an annotation.
23  */
24 class AnnotationSourcer implements AnnotationVisitor {
25 
26     private final String mOpenChar;
27     private final String mCloseChar;
28     private final Output mOutput;
29     private boolean mNeedClose;
30 
AnnotationSourcer(Output output)31     public AnnotationSourcer(Output output) {
32         this(output, false /*isArray*/);
33     }
34 
AnnotationSourcer(Output output, boolean isArray)35     public AnnotationSourcer(Output output, boolean isArray) {
36         mOutput = output;
37         mOpenChar = isArray ? "[" : "(";
38         mCloseChar = isArray ? "]" : ")";
39     }
40 
visit(String name, Object value)41     public void visit(String name, Object value) {
42         startOpen();
43 
44         if (name != null) {
45             mOutput.write("%s=", name);
46         }
47         if (value != null) {
48             mOutput.write(name.toString());
49         }
50     }
51 
startOpen()52     private void startOpen() {
53         if (!mNeedClose) {
54             mNeedClose = true;
55             mOutput.write(mOpenChar);
56         }
57     }
58 
visitEnd()59     public void visitEnd() {
60         if (mNeedClose) {
61             mOutput.write(mCloseChar);
62         }
63         mOutput.write("\n");
64     }
65 
visitAnnotation(String name, String desc)66     public AnnotationVisitor visitAnnotation(String name, String desc) {
67         startOpen();
68 
69         mOutput.write("@%s", name);
70         return this;
71     }
72 
visitArray(String name)73     public AnnotationVisitor visitArray(String name) {
74         startOpen();
75         return new AnnotationSourcer(mOutput, true /*isArray*/);
76     }
77 
visitEnum(String name, String desc, String value)78     public void visitEnum(String name, String desc, String value) {
79         mOutput.write("/* annotation enum not supported: %s */\n", name);
80     }
81 
82 }
83