View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInputStream;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import java.io.Serializable;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.bcel.Constants;
28  
29  /**
30   * represents one annotation in the annotation table
31   * 
32   * @version $Id: AnnotationEntry
33   * @author <A HREF="mailto:dbrosius@mebigfatguy.com">D. Brosius</A>
34   * @since 5.3
35   */
36  public class AnnotationEntry implements Node, Constants, Serializable {
37  
38      private static final long serialVersionUID = 1L;
39  
40      private final int type_index;
41      private final ConstantPool constant_pool;
42      private final boolean isRuntimeVisible;
43  
44      private List<ElementValuePair> element_value_pairs;
45  
46      /**
47       * Factory method to create an AnnotionEntry from a DataInputStream
48       * 
49       * @param file
50       * @param constant_pool
51       * @param isRuntimeVisible
52       * @return the entry
53       * @throws IOException
54       */
55      public static AnnotationEntry read(DataInputStream file, ConstantPool constant_pool, boolean isRuntimeVisible) throws IOException {
56  
57          final AnnotationEntry annotationEntry = new AnnotationEntry(file.readUnsignedShort(), constant_pool, isRuntimeVisible);
58          final int num_element_value_pairs = (file.readUnsignedShort());
59          annotationEntry.element_value_pairs = new ArrayList<ElementValuePair>();
60          for (int i = 0; i < num_element_value_pairs; i++) {
61              annotationEntry.element_value_pairs.add(new ElementValuePair(file.readUnsignedShort(), ElementValue.readElementValue(file, constant_pool),
62                      constant_pool));
63          }
64          return annotationEntry;
65      }
66  
67      public AnnotationEntry(int type_index, ConstantPool constant_pool, boolean isRuntimeVisible) {
68          this.type_index = type_index;
69          this.constant_pool = constant_pool;
70          this.isRuntimeVisible = isRuntimeVisible;
71      }
72  
73      public int getTypeIndex() {
74          return type_index;
75      }
76  
77      public ConstantPool getConstantPool() {
78          return constant_pool;
79      }
80  
81      public boolean isRuntimeVisible() {
82          return isRuntimeVisible;
83      }
84  
85      /**
86       * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class.
87       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
88       * 
89       * @param v Visitor object
90       */
91      public void accept(Visitor v) {
92          // v.visitAnnotationEntry(this);
93      }
94  
95      /**
96       * @return the annotation type name
97       */
98      public String getAnnotationType() {
99          final ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(type_index, CONSTANT_Utf8);
100         return c.getBytes();
101     }
102 
103     /**
104      * @return the annotation type index
105      */
106     public int getAnnotationTypeIndex() {
107         return type_index;
108     }
109 
110     /**
111      * @return the number of element value pairs in this annotation entry
112      */
113     public final int getNumElementValuePairs() {
114         return element_value_pairs.size();
115     }
116 
117     /**
118      * @return the element value pairs in this annotation entry
119      */
120     public ElementValuePair[] getElementValuePairs() {
121         // TODO return List
122         return element_value_pairs.toArray(new ElementValuePair[element_value_pairs.size()]);
123     }
124 
125     public void dump(DataOutputStream dos) throws IOException {
126         dos.writeShort(type_index); // u2 index of type name in cpool
127         dos.writeShort(element_value_pairs.size()); // u2 element_value pair
128         // count
129         for (int i = 0; i < element_value_pairs.size(); i++) {
130             final ElementValuePair envp = element_value_pairs.get(i);
131             envp.dump(dos);
132         }
133     }
134 
135     public void addElementNameValuePair(ElementValuePair elementNameValuePair) {
136         element_value_pairs.add(elementNameValuePair);
137     }
138 
139     public String toShortString() {
140         final StringBuilder result = new StringBuilder();
141         result.append("@");
142         result.append(getAnnotationType());
143         if (getElementValuePairs().length > 0) {
144             result.append("(");
145             for (int i = 0; i < getElementValuePairs().length; i++) {
146                 final ElementValuePair element = getElementValuePairs()[i];
147                 result.append(element.toShortString());
148             }
149             result.append(")");
150         }
151         return result.toString();
152     }
153 }