001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.bcel.classfile;
019
020import java.io.DataInput;
021import java.io.DataOutputStream;
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.List;
026
027import org.apache.bcel.Const;
028
029/**
030 * represents one annotation in the annotation table
031 *
032 * @version $Id: AnnotationEntry
033 * @since 6.0
034 */
035public class AnnotationEntry implements Node {
036
037    private final int type_index;
038    private final ConstantPool constant_pool;
039    private final boolean isRuntimeVisible;
040
041    private List<ElementValuePair> element_value_pairs;
042
043    /*
044     * Factory method to create an AnnotionEntry from a DataInput
045     *
046     * @param input
047     * @param constant_pool
048     * @param isRuntimeVisible
049     * @return the entry
050     * @throws IOException
051     */
052    public static AnnotationEntry read(final DataInput input, final ConstantPool constant_pool, final boolean isRuntimeVisible) throws IOException {
053
054        final AnnotationEntry annotationEntry = new AnnotationEntry(input.readUnsignedShort(), constant_pool, isRuntimeVisible);
055        final int num_element_value_pairs = input.readUnsignedShort();
056        annotationEntry.element_value_pairs = new ArrayList<>();
057        for (int i = 0; i < num_element_value_pairs; i++) {
058            annotationEntry.element_value_pairs.add(
059                    new ElementValuePair(input.readUnsignedShort(), ElementValue.readElementValue(input, constant_pool),
060                    constant_pool));
061        }
062        return annotationEntry;
063    }
064
065    public AnnotationEntry(final int type_index, final ConstantPool constant_pool, final boolean isRuntimeVisible) {
066        this.type_index = type_index;
067        this.constant_pool = constant_pool;
068        this.isRuntimeVisible = isRuntimeVisible;
069    }
070
071    public int getTypeIndex() {
072        return type_index;
073    }
074
075    public ConstantPool getConstantPool() {
076        return constant_pool;
077    }
078
079    public boolean isRuntimeVisible() {
080        return isRuntimeVisible;
081    }
082
083    /**
084     * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class.
085     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
086     *
087     * @param v Visitor object
088     */
089    @Override
090    public void accept(final Visitor v) {
091        v.visitAnnotationEntry(this);
092    }
093
094    /**
095     * @return the annotation type name
096     */
097    public String getAnnotationType() {
098        final ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(type_index, Const.CONSTANT_Utf8);
099        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}