Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javap/AnnotationWriter.java
38899 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.javap;2627import com.sun.tools.classfile.Annotation;28import com.sun.tools.classfile.TypeAnnotation;29import com.sun.tools.classfile.Annotation.Annotation_element_value;30import com.sun.tools.classfile.Annotation.Array_element_value;31import com.sun.tools.classfile.Annotation.Class_element_value;32import com.sun.tools.classfile.Annotation.Enum_element_value;33import com.sun.tools.classfile.Annotation.Primitive_element_value;34import com.sun.tools.classfile.ConstantPool;35import com.sun.tools.classfile.ConstantPoolException;36import com.sun.tools.classfile.Descriptor;37import com.sun.tools.classfile.Descriptor.InvalidDescriptor;3839/**40* A writer for writing annotations as text.41*42* <p><b>This is NOT part of any supported API.43* If you write code that depends on this, you do so at your own risk.44* This code and its internal interfaces are subject to change or45* deletion without notice.</b>46*/47public class AnnotationWriter extends BasicWriter {48static AnnotationWriter instance(Context context) {49AnnotationWriter instance = context.get(AnnotationWriter.class);50if (instance == null)51instance = new AnnotationWriter(context);52return instance;53}5455protected AnnotationWriter(Context context) {56super(context);57classWriter = ClassWriter.instance(context);58constantWriter = ConstantWriter.instance(context);59}6061public void write(Annotation annot) {62write(annot, false);63}6465public void write(Annotation annot, boolean resolveIndices) {66writeDescriptor(annot.type_index, resolveIndices);67boolean showParens = annot.num_element_value_pairs > 0 || !resolveIndices;68if (showParens)69print("(");70for (int i = 0; i < annot.num_element_value_pairs; i++) {71if (i > 0)72print(",");73write(annot.element_value_pairs[i], resolveIndices);74}75if (showParens)76print(")");77}7879public void write(TypeAnnotation annot) {80write(annot, true, false);81}8283public void write(TypeAnnotation annot, boolean showOffsets, boolean resolveIndices) {84write(annot.annotation, resolveIndices);85print(": ");86write(annot.position, showOffsets);87}8889public void write(TypeAnnotation.Position pos, boolean showOffsets) {90print(pos.type);9192switch (pos.type) {93// instanceof94case INSTANCEOF:95// new expression96case NEW:97// constructor/method reference receiver98case CONSTRUCTOR_REFERENCE:99case METHOD_REFERENCE:100if (showOffsets) {101print(", offset=");102print(pos.offset);103}104break;105// local variable106case LOCAL_VARIABLE:107// resource variable108case RESOURCE_VARIABLE:109if (pos.lvarOffset == null) {110print(", lvarOffset is Null!");111break;112}113print(", {");114for (int i = 0; i < pos.lvarOffset.length; ++i) {115if (i != 0) print("; ");116if (showOffsets) {117print("start_pc=");118print(pos.lvarOffset[i]);119}120print(", length=");121print(pos.lvarLength[i]);122print(", index=");123print(pos.lvarIndex[i]);124}125print("}");126break;127// exception parameter128case EXCEPTION_PARAMETER:129print(", exception_index=");130print(pos.exception_index);131break;132// method receiver133case METHOD_RECEIVER:134// Do nothing135break;136// type parameter137case CLASS_TYPE_PARAMETER:138case METHOD_TYPE_PARAMETER:139print(", param_index=");140print(pos.parameter_index);141break;142// type parameter bound143case CLASS_TYPE_PARAMETER_BOUND:144case METHOD_TYPE_PARAMETER_BOUND:145print(", param_index=");146print(pos.parameter_index);147print(", bound_index=");148print(pos.bound_index);149break;150// class extends or implements clause151case CLASS_EXTENDS:152print(", type_index=");153print(pos.type_index);154break;155// throws156case THROWS:157print(", type_index=");158print(pos.type_index);159break;160// method parameter161case METHOD_FORMAL_PARAMETER:162print(", param_index=");163print(pos.parameter_index);164break;165// type cast166case CAST:167// method/constructor/reference type argument168case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:169case METHOD_INVOCATION_TYPE_ARGUMENT:170case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT:171case METHOD_REFERENCE_TYPE_ARGUMENT:172if (showOffsets) {173print(", offset=");174print(pos.offset);175}176print(", type_index=");177print(pos.type_index);178break;179// We don't need to worry about these180case METHOD_RETURN:181case FIELD:182break;183case UNKNOWN:184throw new AssertionError("AnnotationWriter: UNKNOWN target type should never occur!");185default:186throw new AssertionError("AnnotationWriter: Unknown target type for position: " + pos);187}188189// Append location data for generics/arrays.190if (!pos.location.isEmpty()) {191print(", location=");192print(pos.location);193}194}195196public void write(Annotation.element_value_pair pair) {197write(pair, false);198}199200public void write(Annotation.element_value_pair pair, boolean resolveIndices) {201writeIndex(pair.element_name_index, resolveIndices);202print("=");203write(pair.value, resolveIndices);204}205206public void write(Annotation.element_value value) {207write(value, false);208}209210public void write(Annotation.element_value value, boolean resolveIndices) {211ev_writer.write(value, resolveIndices);212}213214private void writeDescriptor(int index, boolean resolveIndices) {215if (resolveIndices) {216try {217ConstantPool constant_pool = classWriter.getClassFile().constant_pool;218Descriptor d = new Descriptor(index);219print(d.getFieldType(constant_pool));220return;221} catch (ConstantPoolException ignore) {222} catch (InvalidDescriptor ignore) {223}224}225226print("#" + index);227}228229private void writeIndex(int index, boolean resolveIndices) {230if (resolveIndices) {231print(constantWriter.stringValue(index));232} else233print("#" + index);234}235236element_value_Writer ev_writer = new element_value_Writer();237238class element_value_Writer implements Annotation.element_value.Visitor<Void,Boolean> {239public void write(Annotation.element_value value, boolean resolveIndices) {240value.accept(this, resolveIndices);241}242243public Void visitPrimitive(Primitive_element_value ev, Boolean resolveIndices) {244if (resolveIndices)245writeIndex(ev.const_value_index, resolveIndices);246else247print(((char) ev.tag) + "#" + ev.const_value_index);248return null;249}250251public Void visitEnum(Enum_element_value ev, Boolean resolveIndices) {252if (resolveIndices) {253writeIndex(ev.type_name_index, resolveIndices);254print(".");255writeIndex(ev.const_name_index, resolveIndices);256} else257print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index);258return null;259}260261public Void visitClass(Class_element_value ev, Boolean resolveIndices) {262if (resolveIndices) {263writeIndex(ev.class_info_index, resolveIndices);264print(".class");265} else266print(((char) ev.tag) + "#" + ev.class_info_index);267return null;268}269270public Void visitAnnotation(Annotation_element_value ev, Boolean resolveIndices) {271print((char) ev.tag);272AnnotationWriter.this.write(ev.annotation_value, resolveIndices);273return null;274}275276public Void visitArray(Array_element_value ev, Boolean resolveIndices) {277print("[");278for (int i = 0; i < ev.num_values; i++) {279if (i > 0)280print(",");281write(ev.values[i], resolveIndices);282}283print("]");284return null;285}286287}288289private ClassWriter classWriter;290private ConstantWriter constantWriter;291}292293294