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