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