jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/AnnotationEntry.java
author joehw
Sun, 13 Aug 2017 21:10:40 -0700
changeset 46174 5611d2529b49
permissions -rw-r--r--
8163121: BCEL: update to the latest 6.0 release Reviewed-by: dfuchs, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
     1
/*
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
     2
 * reserved comment block
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
     4
 */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
     5
/*
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
     8
 * this work for additional information regarding copyright ownership.
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    11
 * the License.  You may obtain a copy of the License at
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    12
 *
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    14
 *
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    18
 * See the License for the specific language governing permissions and
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    19
 * limitations under the License.
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    20
 */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    21
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    22
package com.sun.org.apache.bcel.internal.classfile;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    23
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    24
import java.io.DataInput;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    25
import java.io.DataOutputStream;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    26
import java.io.IOException;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    27
import java.util.ArrayList;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    28
import java.util.Collections;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    29
import java.util.List;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    30
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    31
import com.sun.org.apache.bcel.internal.Const;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    32
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    33
/**
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    34
 * represents one annotation in the annotation table
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    35
 *
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    36
 * @version $Id: AnnotationEntry
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    37
 * @since 6.0
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    38
 */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    39
public class AnnotationEntry implements Node {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    40
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    41
    private final int type_index;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    42
    private final ConstantPool constant_pool;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    43
    private final boolean isRuntimeVisible;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    44
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    45
    private List<ElementValuePair> element_value_pairs;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    46
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    47
    /*
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    48
     * Factory method to create an AnnotionEntry from a DataInput
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    49
     *
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    50
     * @param input
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    51
     * @param constant_pool
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    52
     * @param isRuntimeVisible
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    53
     * @return the entry
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    54
     * @throws IOException
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    55
     */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    56
    public static AnnotationEntry read(final DataInput input, final ConstantPool constant_pool, final boolean isRuntimeVisible) throws IOException {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    57
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    58
        final AnnotationEntry annotationEntry = new AnnotationEntry(input.readUnsignedShort(), constant_pool, isRuntimeVisible);
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    59
        final int num_element_value_pairs = input.readUnsignedShort();
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    60
        annotationEntry.element_value_pairs = new ArrayList<>();
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    61
        for (int i = 0; i < num_element_value_pairs; i++) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    62
            annotationEntry.element_value_pairs.add(
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    63
                    new ElementValuePair(input.readUnsignedShort(), ElementValue.readElementValue(input, constant_pool),
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    64
                    constant_pool));
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    65
        }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    66
        return annotationEntry;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    67
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    68
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    69
    public AnnotationEntry(final int type_index, final ConstantPool constant_pool, final boolean isRuntimeVisible) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    70
        this.type_index = type_index;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    71
        this.constant_pool = constant_pool;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    72
        this.isRuntimeVisible = isRuntimeVisible;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    73
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    74
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    75
    public int getTypeIndex() {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    76
        return type_index;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    77
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    78
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    79
    public ConstantPool getConstantPool() {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    80
        return constant_pool;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    81
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    82
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    83
    public boolean isRuntimeVisible() {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    84
        return isRuntimeVisible;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    85
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    86
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    87
    /**
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    88
     * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class.
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    89
     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    90
     *
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    91
     * @param v Visitor object
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    92
     */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    93
    @Override
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    94
    public void accept(final Visitor v) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    95
        v.visitAnnotationEntry(this);
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    96
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    97
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    98
    /**
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
    99
     * @return the annotation type name
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   100
     */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   101
    public String getAnnotationType() {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   102
        final ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(type_index, Const.CONSTANT_Utf8);
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   103
        return c.getBytes();
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   104
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   105
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   106
    /**
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   107
     * @return the annotation type index
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   108
     */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   109
    public int getAnnotationTypeIndex() {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   110
        return type_index;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   111
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   112
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   113
    /**
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   114
     * @return the number of element value pairs in this annotation entry
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   115
     */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   116
    public final int getNumElementValuePairs() {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   117
        return element_value_pairs.size();
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   118
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   119
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   120
    /**
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   121
     * @return the element value pairs in this annotation entry
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   122
     */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   123
    public ElementValuePair[] getElementValuePairs() {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   124
        // TODO return List
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   125
        return element_value_pairs.toArray(new ElementValuePair[element_value_pairs.size()]);
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   126
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   127
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   128
    public void dump(final DataOutputStream dos) throws IOException {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   129
        dos.writeShort(type_index); // u2 index of type name in cpool
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   130
        dos.writeShort(element_value_pairs.size()); // u2 element_value pair
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   131
        // count
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   132
        for (final ElementValuePair envp : element_value_pairs) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   133
            envp.dump(dos);
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   134
        }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   135
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   136
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   137
    public void addElementNameValuePair(final ElementValuePair elementNameValuePair) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   138
        element_value_pairs.add(elementNameValuePair);
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   139
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   140
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   141
    public String toShortString() {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   142
        final StringBuilder result = new StringBuilder();
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   143
        result.append("@");
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   144
        result.append(getAnnotationType());
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   145
        final ElementValuePair[] evPairs = getElementValuePairs();
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   146
        if (evPairs.length > 0) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   147
            result.append("(");
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   148
            for (final ElementValuePair element : evPairs) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   149
                result.append(element.toShortString());
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   150
            }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   151
            result.append(")");
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   152
        }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   153
        return result.toString();
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   154
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   155
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   156
    @Override
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   157
    public String toString() {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   158
        return toShortString();
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   159
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   160
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   161
    public static AnnotationEntry[] createAnnotationEntries(final Attribute[] attrs) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   162
        // Find attributes that contain annotation data
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   163
        final List<AnnotationEntry> accumulatedAnnotations = new ArrayList<>(attrs.length);
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   164
        for (final Attribute attribute : attrs) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   165
            if (attribute instanceof Annotations) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   166
                final Annotations runtimeAnnotations = (Annotations) attribute;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   167
                Collections.addAll(accumulatedAnnotations, runtimeAnnotations.getAnnotationEntries());
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   168
            }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   169
        }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   170
        return accumulatedAnnotations.toArray(new AnnotationEntry[accumulatedAnnotations.size()]);
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   171
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents:
diff changeset
   172
}