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 * @since 6.0
029 */
030public class AnnotationElementValueGen extends ElementValueGen {
031    // For annotation element values, this is the annotation
032    private final AnnotationEntryGen a;
033
034    public AnnotationElementValueGen(final AnnotationElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
035        super(ANNOTATION, cpool);
036        a = new AnnotationEntryGen(value.getAnnotationEntry(), cpool, copyPoolEntries);
037    }
038
039    public AnnotationElementValueGen(final AnnotationEntryGen a, final ConstantPoolGen cpool) {
040        super(ANNOTATION, cpool);
041        this.a = a;
042    }
043
044    public AnnotationElementValueGen(final int type, final AnnotationEntryGen annotation, final ConstantPoolGen cpool) {
045        super(type, cpool);
046        if (type != ANNOTATION) {
047            throw new IllegalArgumentException("Only element values of type annotation can be built with this ctor - type specified: " + type);
048        }
049        this.a = annotation;
050    }
051
052    @Override
053    public void dump(final DataOutputStream dos) throws IOException {
054        dos.writeByte(super.getElementValueType()); // u1 type of value (ANNOTATION == '@')
055        a.dump(dos);
056    }
057
058    public AnnotationEntryGen getAnnotation() {
059        return a;
060    }
061
062    /**
063     * Return immutable variant of this AnnotationElementValueGen
064     */
065    @Override
066    public ElementValue getElementValue() {
067        return new AnnotationElementValue(super.getElementValueType(), a.getAnnotation(), getConstantPool().getConstantPool());
068    }
069
070    @Override
071    public String stringifyValue() {
072        throw new UnsupportedOperationException("Not implemented yet");
073    }
074}