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.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.apache.bcel.Const;
28  
29  /**
30   * represents one annotation in the annotation table
31   *
32   * @version $Id: AnnotationEntry
33   * @since 6.0
34   */
35  public class AnnotationEntry implements Node {
36  
37      private final int type_index;
38      private final ConstantPool constant_pool;
39      private final boolean isRuntimeVisible;
40  
41      private List<ElementValuePair> element_value_pairs;
42  
43      /*
44       * Factory method to create an AnnotionEntry from a DataInput
45       *
46       * @param input
47       * @param constant_pool
48       * @param isRuntimeVisible
49       * @return the entry
50       * @throws IOException
51       */
52      public static AnnotationEntry read(final DataInput input, final ConstantPool constant_pool, final boolean isRuntimeVisible) throws IOException {
53  
54          final AnnotationEntry annotationEntry = new AnnotationEntry(input.readUnsignedShort(), constant_pool, isRuntimeVisible);
55          final int num_element_value_pairs = input.readUnsignedShort();
56          annotationEntry.element_value_pairs = new ArrayList<>();
57          for (int i = 0; i < num_element_value_pairs; i++) {
58              annotationEntry.element_value_pairs.add(
59                      new ElementValuePair(input.readUnsignedShort(), ElementValue.readElementValue(input, constant_pool),
60                      constant_pool));
61          }
62          return annotationEntry;
63      }
64  
65      public AnnotationEntry(final int type_index, final ConstantPool constant_pool, final boolean isRuntimeVisible) {
66          this.type_index = type_index;
67          this.constant_pool = constant_pool;
68          this.isRuntimeVisible = isRuntimeVisible;
69      }
70  
71      public int getTypeIndex() {
72          return type_index;
73      }
74  
75      public ConstantPool getConstantPool() {
76          return constant_pool;
77      }
78  
79      public boolean isRuntimeVisible() {
80          return isRuntimeVisible;
81      }
82  
83      /**
84       * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class.
85       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
86       *
87       * @param v Visitor object
88       */
89      @Override
90      public void accept(final Visitor v) {
91          v.visitAnnotationEntry(this);
92      }
93  
94      /**
95       * @return the annotation type name
96       */
97      public String getAnnotationType() {
98          final ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(type_index, Const.CONSTANT_Utf8);
99          return c.getBytes();
100     }
101 
102     /**
103      * @return the annotation type index
104      */
105     public int getAnnotationTypeIndex() {
106         return type_index;
107     }
108 
109     /**
110      * @return the number of element value pairs in this annotation entry
111      */
112     public final int getNumElementValuePairs() {
113         return element_value_pairs.size();
114     }
115 
116     /**
117      * @return the element value pairs in this annotation entry
118      */
119     public ElementValuePair[] getElementValuePairs() {
120         // TODO return List
121         return element_value_pairs.toArray(new ElementValuePair[element_value_pairs.size()]);
122     }
123 
124     public void dump(final DataOutputStream dos) throws IOException {
125         dos.writeShort(type_index); // u2 index of type name in cpool
126         dos.writeShort(element_value_pairs.size()); // u2 element_value pair
127         // count
128         for (final ElementValuePair envp : element_value_pairs) {
129             envp.dump(dos);
130         }
131     }
132 
133     public void addElementNameValuePair(final ElementValuePair elementNameValuePair) {
134         element_value_pairs.add(elementNameValuePair);
135     }
136 
137     public String toShortString() {
138         final StringBuilder result = new StringBuilder();
139         result.append("@");
140         result.append(getAnnotationType());
141         final ElementValuePair[] evPairs = getElementValuePairs();
142         if (evPairs.length > 0) {
143             result.append("(");
144             for (final ElementValuePair element : evPairs) {
145                 result.append(element.toShortString());
146             }
147             result.append(")");
148         }
149         return result.toString();
150     }
151 
152     @Override
153     public String toString() {
154         return toShortString();
155     }
156 
157     public static AnnotationEntry[] createAnnotationEntries(final Attribute[] attrs) {
158         // Find attributes that contain annotation data
159         final List<AnnotationEntry> accumulatedAnnotations = new ArrayList<>(attrs.length);
160         for (final Attribute attribute : attrs) {
161             if (attribute instanceof Annotations) {
162                 final Annotations runtimeAnnotations = (Annotations) attribute;
163                 Collections.addAll(accumulatedAnnotations, runtimeAnnotations.getAnnotationEntries());
164             }
165         }
166         return accumulatedAnnotations.toArray(new AnnotationEntry[accumulatedAnnotations.size()]);
167     }
168 }