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     * Constructs an AnnotationDefault attribute.
050     *
051     * @param nameIndex Index pointing to the name <em>Code</em>.
052     * @param length Content length in bytes.
053     * @param defaultValue the annotation's default value.
054     * @param constantPool Array of constants.
055     */
056    public AnnotationDefault(final int nameIndex, final int length, final ElementValue defaultValue, final ConstantPool constantPool) {
057        super(Const.ATTR_ANNOTATION_DEFAULT, nameIndex, length, constantPool);
058        this.defaultValue = defaultValue;
059    }
060
061    /**
062     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
063     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
064     *
065     * @param v Visitor object.
066     */
067    @Override
068    public void accept(final Visitor v) {
069        v.visitAnnotationDefault(this);
070    }
071
072    @Override
073    public Attribute copy(final ConstantPool constantPool) {
074        return (Attribute) clone();
075    }
076
077    @Override
078    public final void dump(final DataOutputStream dos) throws IOException {
079        super.dump(dos);
080        defaultValue.dump(dos);
081    }
082
083    /**
084     * Gets the default value.
085     *
086     * @return the default value.
087     */
088    public final ElementValue getDefaultValue() {
089        return defaultValue;
090    }
091
092    /**
093     * Sets the default value of this methodinfo's annotation.
094     *
095     * @param defaultValue the default value of this methodinfo's annotation.
096     */
097    public final void setDefaultValue(final ElementValue defaultValue) {
098        this.defaultValue = defaultValue;
099    }
100}