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