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.generic;
020
021import java.io.DataOutputStream;
022import java.io.IOException;
023
024import org.apache.bcel.classfile.AnnotationElementValue;
025import org.apache.bcel.classfile.ElementValue;
026
027/**
028 * Generates annotation element values.
029 *
030 * @since 6.0
031 */
032public class AnnotationElementValueGen extends ElementValueGen {
033    // For annotation element values, this is the annotation
034    private final AnnotationEntryGen a;
035
036    /**
037     * Constructs an AnnotationElementValueGen.
038     *
039     * @param value the annotation element value.
040     * @param cpool the constant pool generator.
041     * @param copyPoolEntries whether to copy pool entries.
042     */
043    public AnnotationElementValueGen(final AnnotationElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
044        super(ANNOTATION, cpool);
045        a = new AnnotationEntryGen(value.getAnnotationEntry(), cpool, copyPoolEntries);
046    }
047
048    /**
049     * Constructs an AnnotationElementValueGen.
050     *
051     * @param a the annotation entry generator.
052     * @param cpool the constant pool generator.
053     */
054    public AnnotationElementValueGen(final AnnotationEntryGen a, final ConstantPoolGen cpool) {
055        super(ANNOTATION, cpool);
056        this.a = a;
057    }
058
059    /**
060     * Constructs an AnnotationElementValueGen.
061     *
062     * @param type the type.
063     * @param annotation the annotation.
064     * @param cpool the constant pool generator.
065     */
066    public AnnotationElementValueGen(final int type, final AnnotationEntryGen annotation, final ConstantPoolGen cpool) {
067        super(type, cpool);
068        if (type != ANNOTATION) {
069            throw new IllegalArgumentException("Only element values of type annotation can be built with this ctor - type specified: " + type);
070        }
071        this.a = annotation;
072    }
073
074    @Override
075    public void dump(final DataOutputStream dos) throws IOException {
076        dos.writeByte(super.getElementValueType()); // u1 type of value (ANNOTATION == '@')
077        a.dump(dos);
078    }
079
080    /**
081     * Gets the annotation.
082     *
083     * @return the annotation.
084     */
085    public AnnotationEntryGen getAnnotation() {
086        return a;
087    }
088
089    /**
090     * Returns an immutable variant of this AnnotationElementValueGen.
091     *
092     * @return an immutable variant of this AnnotationElementValueGen.
093     */
094    @Override
095    public ElementValue getElementValue() {
096        return new AnnotationElementValue(super.getElementValueType(), a.getAnnotation(), getConstantPool().getConstantPool());
097    }
098
099    @Override
100    public String stringifyValue() {
101        throw new UnsupportedOperationException("Not implemented yet");
102    }
103}