    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 * @version $Id: AnnotationDefault 1 2005-02-13 03:15:08Z dbrosius $
    030 * @since 6.0
    031 */
    032public class AnnotationDefault extends Attribute {
    033
    034    private ElementValue default_value;
    035
    036    /**
    037     * @param name_index    Index pointing to the name <em>Code</em>
    038     * @param length        Content length in bytes
    039     * @param input         Input stream
    040     * @param constant_pool Array of constants
    041     */
    042    AnnotationDefault(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException {
    043        this(name_index, length, (ElementValue) null, constant_pool);
    044        default_value = ElementValue.readElementValue(input, constant_pool);
    045    }
    046
    047    /**
    048     * @param name_index    Index pointing to the name <em>Code</em>
    049     * @param length        Content length in bytes
    050     * @param defaultValue  the annotation's default value
    051     * @param constant_pool Array of constants
    052     */
    053    public AnnotationDefault(final int name_index, final int length, final ElementValue defaultValue, final ConstantPool constant_pool) {
    054        super(Const.ATTR_ANNOTATION_DEFAULT, name_index, length, constant_pool);
    055        this.default_value = defaultValue;
    056    }
    057
    058    /**
    059     * Called by objects that are traversing the nodes of the tree implicitely
    060     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    061     * 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    /**
    071     * @param defaultValue the default value of this methodinfo's annotation
    072     */
    073    public final void setDefaultValue(final ElementValue defaultValue) {
    074        default_value = defaultValue;
    075    }
    076
    077    /**
    078     * @return the default value
    079     */
    080    public final ElementValue getDefaultValue() {
    081        return default_value;
    082    }
    083
    084    @Override
    085    public Attribute copy(final ConstantPool _constant_pool) {
    086        return (Attribute) clone();
    087    }
    088
    089    @Override
    090    public final void dump(final DataOutputStream dos) throws IOException {
    091        super.dump(dos);
    092        default_value.dump(dos);
    093    }
    094}