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