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 nameIndex Index pointing to the name <em>Code</em>
37       * @param length Content length in bytes
38       * @param input Input stream
39       * @param constantPool Array of constants
40       */
41      AnnotationDefault(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
42          this(nameIndex, length, (ElementValue) null, constantPool);
43          defaultValue = ElementValue.readElementValue(input, constantPool);
44      }
45  
46      /**
47       * @param nameIndex 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 constantPool Array of constants
51       */
52      public AnnotationDefault(final int nameIndex, final int length, final ElementValue defaultValue, final ConstantPool constantPool) {
53          super(Const.ATTR_ANNOTATION_DEFAULT, nameIndex, length, constantPool);
54          this.defaultValue = defaultValue;
55      }
56  
57      /**
58       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
59       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
60       *
61       * @param v Visitor object
62       */
63      @Override
64      public void accept(final Visitor v) {
65          v.visitAnnotationDefault(this);
66      }
67  
68      @Override
69      public Attribute copy(final ConstantPool constantPool) {
70          return (Attribute) clone();
71      }
72  
73      @Override
74      public final void dump(final DataOutputStream dos) throws IOException {
75          super.dump(dos);
76          defaultValue.dump(dos);
77      }
78  
79      /**
80       * @return the default value
81       */
82      public final ElementValue getDefaultValue() {
83          return defaultValue;
84      }
85  
86      /**
87       * @param defaultValue the default value of this methodinfo's annotation
88       */
89      public final void setDefaultValue(final ElementValue defaultValue) {
90          this.defaultValue = defaultValue;
91      }
92  }