View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.bcel.classfile;
21  
22  import java.io.DataInput;
23  import java.io.DataOutputStream;
24  import java.io.IOException;
25  
26  import org.apache.bcel.Const;
27  
28  /**
29   * Represents the default value of a annotation for a method info.
30   *
31   * @since 6.0
32   */
33  public class AnnotationDefault extends Attribute {
34  
35      private ElementValue defaultValue;
36  
37      /**
38       * @param nameIndex Index pointing to the name <em>Code</em>
39       * @param length Content length in bytes
40       * @param input Input stream
41       * @param constantPool Array of constants
42       */
43      AnnotationDefault(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
44          this(nameIndex, length, (ElementValue) null, constantPool);
45          defaultValue = ElementValue.readElementValue(input, constantPool);
46      }
47  
48      /**
49       * @param nameIndex Index pointing to the name <em>Code</em>
50       * @param length Content length in bytes
51       * @param defaultValue the annotation's default value
52       * @param constantPool Array of constants
53       */
54      public AnnotationDefault(final int nameIndex, final int length, final ElementValue defaultValue, final ConstantPool constantPool) {
55          super(Const.ATTR_ANNOTATION_DEFAULT, nameIndex, length, constantPool);
56          this.defaultValue = defaultValue;
57      }
58  
59      /**
60       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
61       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
62       *
63       * @param v Visitor object
64       */
65      @Override
66      public void accept(final Visitor v) {
67          v.visitAnnotationDefault(this);
68      }
69  
70      @Override
71      public Attribute copy(final ConstantPool constantPool) {
72          return (Attribute) clone();
73      }
74  
75      @Override
76      public final void dump(final DataOutputStream dos) throws IOException {
77          super.dump(dos);
78          defaultValue.dump(dos);
79      }
80  
81      /**
82       * @return the default value
83       */
84      public final ElementValue getDefaultValue() {
85          return defaultValue;
86      }
87  
88      /**
89       * @param defaultValue the default value of this methodinfo's annotation
90       */
91      public final void setDefaultValue(final ElementValue defaultValue) {
92          this.defaultValue = defaultValue;
93      }
94  }