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