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 name_index    Index pointing to the name <em>Code</em>
037     * @param length        Content length in bytes
038     * @param input         Input stream
039     * @param constant_pool Array of constants
040     */
041    AnnotationDefault(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException {
042        this(name_index, length, (ElementValue) null, constant_pool);
043        defaultValue = ElementValue.readElementValue(input, constant_pool);
044    }
045
046    /**
047     * @param name_index    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 constant_pool Array of constants
051     */
052    public AnnotationDefault(final int name_index, final int length, final ElementValue defaultValue, final ConstantPool constant_pool) {
053        super(Const.ATTR_ANNOTATION_DEFAULT, name_index, length, constant_pool);
054        this.defaultValue = defaultValue;
055    }
056
057    /**
058     * Called by objects that are traversing the nodes of the tree implicitely
059     * defined by the contents of a Java class. I.e., the hierarchy of methods,
060     * fields, attributes, etc. spawns a tree of objects.
061     *
062     * @param v Visitor object
063     */
064    @Override
065    public void accept(final Visitor v) {
066        v.visitAnnotationDefault(this);
067    }
068
069    /**
070     * @param defaultValue the default value of this methodinfo's annotation
071     */
072    public final void setDefaultValue(final ElementValue defaultValue) {
073        this.defaultValue = defaultValue;
074    }
075
076    /**
077     * @return the default value
078     */
079    public final ElementValue getDefaultValue() {
080        return defaultValue;
081    }
082
083    @Override
084    public Attribute copy(final ConstantPool _constant_pool) {
085        return (Attribute) clone();
086    }
087
088    @Override
089    public final void dump(final DataOutputStream dos) throws IOException {
090        super.dump(dos);
091        defaultValue.dump(dos);
092    }
093}