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