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 */
019package org.apache.bcel.classfile;
020
021import java.io.DataOutputStream;
022import java.io.IOException;
023
024/**
025 * Represents an annotation element value.
026 *
027 * @since 6.0
028 */
029public class AnnotationElementValue extends ElementValue {
030    // For annotation element values, this is the annotation
031    private final AnnotationEntry annotationEntry;
032
033    /**
034     * Constructs an AnnotationElementValue.
035     *
036     * @param type the type.
037     * @param annotationEntry the annotation entry.
038     * @param cpool the constant pool.
039     */
040    public AnnotationElementValue(final int type, final AnnotationEntry annotationEntry, final ConstantPool cpool) {
041        super(type, cpool);
042        if (type != ANNOTATION) {
043            throw new ClassFormatException("Only element values of type annotation can be built with this ctor - type specified: " + type);
044        }
045        this.annotationEntry = annotationEntry;
046    }
047
048    @Override
049    public void dump(final DataOutputStream dos) throws IOException {
050        dos.writeByte(super.getType()); // u1 type of value (ANNOTATION == '@')
051        annotationEntry.dump(dos);
052    }
053
054    /**
055     * Gets the annotation entry.
056     *
057     * @return the annotation entry.
058     */
059    public AnnotationEntry getAnnotationEntry() {
060        return annotationEntry;
061    }
062
063    @Override
064    public String stringifyValue() {
065        return annotationEntry.toString();
066    }
067
068    @Override
069    public String toString() {
070        return stringifyValue();
071    }
072}