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