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