jjg@46: /* jjg@815: * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. jjg@46: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@46: * jjg@46: * This code is free software; you can redistribute it and/or modify it jjg@46: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this jjg@46: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. jjg@46: * jjg@46: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@46: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@46: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@46: * version 2 for more details (a copy is included in the LICENSE file that jjg@46: * accompanied this code). jjg@46: * jjg@46: * You should have received a copy of the GNU General Public License version jjg@46: * 2 along with this work; if not, write to the Free Software Foundation, jjg@46: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@46: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@46: */ jjg@46: jjg@46: package com.sun.tools.javap; jjg@46: jjg@46: import com.sun.tools.classfile.Annotation; jjg@46: import com.sun.tools.classfile.Annotation.Annotation_element_value; jjg@46: import com.sun.tools.classfile.Annotation.Array_element_value; jjg@46: import com.sun.tools.classfile.Annotation.Class_element_value; jjg@46: import com.sun.tools.classfile.Annotation.Enum_element_value; jjg@46: import com.sun.tools.classfile.Annotation.Primitive_element_value; jjg@338: import com.sun.tools.classfile.ConstantPool; jjg@338: import com.sun.tools.classfile.ConstantPoolException; jjg@338: import com.sun.tools.classfile.Descriptor; jjg@338: import com.sun.tools.classfile.Descriptor.InvalidDescriptor; jjg@46: jjg@46: /** jjg@46: * A writer for writing annotations as text. jjg@46: * jjg@581: *
This is NOT part of any supported API.
jjg@581: * If you write code that depends on this, you do so at your own risk.
jjg@46: * This code and its internal interfaces are subject to change or
jjg@46: * deletion without notice.
jjg@46: */
jjg@46: public class AnnotationWriter extends BasicWriter {
jjg@46: static AnnotationWriter instance(Context context) {
jjg@46: AnnotationWriter instance = context.get(AnnotationWriter.class);
jjg@46: if (instance == null)
jjg@46: instance = new AnnotationWriter(context);
jjg@46: return instance;
jjg@46: }
jjg@46:
jjg@46: protected AnnotationWriter(Context context) {
jjg@46: super(context);
jjg@338: classWriter = ClassWriter.instance(context);
jjg@338: constantWriter = ConstantWriter.instance(context);
jjg@46: }
jjg@46:
jjg@46: public void write(Annotation annot) {
jjg@338: write(annot, false);
jjg@338: }
jjg@338:
jjg@338: public void write(Annotation annot, boolean resolveIndices) {
jjg@338: writeDescriptor(annot.type_index, resolveIndices);
jjg@338: boolean showParens = annot.num_element_value_pairs > 0 || !resolveIndices;
jjg@338: if (showParens)
jjg@338: print("(");
jjg@46: for (int i = 0; i < annot.num_element_value_pairs; i++) {
jjg@46: if (i > 0)
jjg@46: print(",");
jjg@338: write(annot.element_value_pairs[i], resolveIndices);
jjg@46: }
jjg@338: if (showParens)
jjg@338: print(")");
jjg@46: }
jjg@46:
jjg@46: public void write(Annotation.element_value_pair pair) {
jjg@338: write(pair, false);
jjg@338: }
jjg@338:
jjg@338: public void write(Annotation.element_value_pair pair, boolean resolveIndices) {
jjg@338: writeIndex(pair.element_name_index, resolveIndices);
jjg@338: print("=");
jjg@338: write(pair.value, resolveIndices);
jjg@46: }
jjg@46:
jjg@46: public void write(Annotation.element_value value) {
jjg@338: write(value, false);
jjg@338: }
jjg@338:
jjg@338: public void write(Annotation.element_value value, boolean resolveIndices) {
jjg@338: ev_writer.write(value, resolveIndices);
jjg@338: }
jjg@338:
jjg@338: private void writeDescriptor(int index, boolean resolveIndices) {
jjg@338: if (resolveIndices) {
jjg@338: try {
jjg@338: ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
jjg@338: Descriptor d = new Descriptor(index);
jjg@338: print(d.getFieldType(constant_pool));
jjg@338: return;
jjg@338: } catch (ConstantPoolException ignore) {
jjg@338: } catch (InvalidDescriptor ignore) {
jjg@338: }
jjg@338: }
jjg@338:
jjg@338: print("#" + index);
jjg@338: }
jjg@338:
jjg@338: private void writeIndex(int index, boolean resolveIndices) {
jjg@338: if (resolveIndices) {
jjg@338: print(constantWriter.stringValue(index));
jjg@338: } else
jjg@338: print("#" + index);
jjg@46: }
jjg@46:
jjg@46: element_value_Writer ev_writer = new element_value_Writer();
jjg@46:
jjg@338: class element_value_Writer implements Annotation.element_value.Visitor