001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.bcel.classfile;
021
022import java.io.DataInput;
023import java.io.DataOutputStream;
024import java.io.IOException;
025
026import org.apache.bcel.Const;
027
028/**
029 * Represents the default value of a annotation for a method info.
030 *
031 * @since 6.0
032 */
033public class AnnotationDefault extends Attribute {
034
035    private ElementValue defaultValue;
036
037    /**
038     * @param nameIndex Index pointing to the name <em>Code</em>
039     * @param length Content length in bytes
040     * @param input Input stream
041     * @param constantPool Array of constants
042     */
043    AnnotationDefault(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
044        this(nameIndex, length, (ElementValue) null, constantPool);
045        defaultValue = ElementValue.readElementValue(input, constantPool);
046    }
047
048    /**
049     * @param nameIndex Index pointing to the name <em>Code</em>
050     * @param length Content length in bytes
051     * @param defaultValue the annotation's default value
052     * @param constantPool Array of constants
053     */
054    public AnnotationDefault(final int nameIndex, final int length, final ElementValue defaultValue, final ConstantPool constantPool) {
055        super(Const.ATTR_ANNOTATION_DEFAULT, nameIndex, length, constantPool);
056        this.defaultValue = defaultValue;
057    }
058
059    /**
060     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
061     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
062     *
063     * @param v Visitor object
064     */
065    @Override
066    public void accept(final Visitor v) {
067        v.visitAnnotationDefault(this);
068    }
069
070    @Override
071    public Attribute copy(final ConstantPool constantPool) {
072        return (Attribute) clone();
073    }
074
075    @Override
076    public final void dump(final DataOutputStream dos) throws IOException {
077        super.dump(dos);
078        defaultValue.dump(dos);
079    }
080
081    /**
082     * @return the default value
083     */
084    public final ElementValue getDefaultValue() {
085        return defaultValue;
086    }
087
088    /**
089     * @param defaultValue the default value of this methodinfo's annotation
090     */
091    public final void setDefaultValue(final ElementValue defaultValue) {
092        this.defaultValue = defaultValue;
093    }
094}