Source code

    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 * @since 6.0
    033 */
    034public class AnnotationEntry implements Node {
    035
    036    private final int type_index;
    037    private final ConstantPool constant_pool;
    038    private final boolean isRuntimeVisible;
    039
    040    private List<ElementValuePair> element_value_pairs;
    041
    042    /*
    043     * Factory method to create an AnnotionEntry from a DataInput
    044     *
    045     * @param input
    046     * @param constant_pool
    047     * @param isRuntimeVisible
    048     * @return the entry
    049     * @throws IOException
    050     */
    051    public static AnnotationEntry read(final DataInput input, final ConstantPool constant_pool, final boolean isRuntimeVisible) throws IOException {
    052
    053        final AnnotationEntry annotationEntry = new AnnotationEntry(input.readUnsignedShort(), constant_pool, isRuntimeVisible);
    054        final int num_element_value_pairs = input.readUnsignedShort();
    055        annotationEntry.element_value_pairs = new ArrayList<>();
    056        for (int i = 0; i < num_element_value_pairs; i++) {
    057            annotationEntry.element_value_pairs.add(
    058                    new ElementValuePair(input.readUnsignedShort(), ElementValue.readElementValue(input, constant_pool),
    059                    constant_pool));
    060        }
    061        return annotationEntry;
    062    }
    063
    064    public AnnotationEntry(final int type_index, final ConstantPool constant_pool, final boolean isRuntimeVisible) {
    065        this.type_index = type_index;
    066        this.constant_pool = constant_pool;
    067        this.isRuntimeVisible = isRuntimeVisible;
    068    }
    069
    070    public int getTypeIndex() {
    071        return type_index;
    072    }
    073
    074    public ConstantPool getConstantPool() {
    075        return constant_pool;
    076    }
    077
    078    public boolean isRuntimeVisible() {
    079        return isRuntimeVisible;
    080    }
    081
    082    /**
    083     * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class.
    084     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    085     *
    086     * @param v Visitor object
    087     */
    088    @Override
    089    public void accept(final Visitor v) {
    090        v.visitAnnotationEntry(this);
    091    }
    092
    093    /**
    094     * @return the annotation type name
    095     */
    096    public String getAnnotationType() {
    097        final ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(type_index, Const.CONSTANT_Utf8);
    098        return c.getBytes();
    099    }
    100
    101    /**
    102     * @return the annotation type index
    103     */
    104    public int getAnnotationTypeIndex() {
    105        return type_index;
    106    }
    107
    108    /**
    109     * @return the number of element value pairs in this annotation entry
    110     */
    111    public final int getNumElementValuePairs() {
    112        return element_value_pairs.size();
    113    }
    114
    115    /**
    116     * @return the element value pairs in this annotation entry
    117     */
    118    public ElementValuePair[] getElementValuePairs() {
    119        // TODO return List
    120        return element_value_pairs.toArray(new ElementValuePair[element_value_pairs.size()]);
    121    }
    122
    123    public void dump(final DataOutputStream dos) throws IOException {
    124        dos.writeShort(type_index); // u2 index of type name in cpool
    125        dos.writeShort(element_value_pairs.size()); // u2 element_value pair
    126        // count
    127        for (final ElementValuePair envp : element_value_pairs) {
    128            envp.dump(dos);
    129        }
    130    }
    131
    132    public void addElementNameValuePair(final ElementValuePair elementNameValuePair) {
    133        element_value_pairs.add(elementNameValuePair);
    134    }
    135
    136    public String toShortString() {
    137        final StringBuilder result = new StringBuilder();
    138        result.append("@");
    139        result.append(getAnnotationType());
    140        final ElementValuePair[] evPairs = getElementValuePairs();
    141        if (evPairs.length > 0) {
    142            result.append("(");
    143            for (final ElementValuePair element : evPairs) {
    144                result.append(element.toShortString());
    145            }
    146            result.append(")");
    147        }
    148        return result.toString();
    149    }
    150
    151    @Override
    152    public String toString() {
    153        return toShortString();
    154    }
    155
    156    public static AnnotationEntry[] createAnnotationEntries(final Attribute[] attrs) {
    157        // Find attributes that contain annotation data
    158        final List<AnnotationEntry> accumulatedAnnotations = new ArrayList<>(attrs.length);
    159        for (final Attribute attribute : attrs) {
    160            if (attribute instanceof Annotations) {
    161                final Annotations runtimeAnnotations = (Annotations) attribute;
    162                Collections.addAll(accumulatedAnnotations, runtimeAnnotations.getAnnotationEntries());
    163            }
    164        }
    165        return accumulatedAnnotations.toArray(new AnnotationEntry[accumulatedAnnotations.size()]);
    166    }
    167}