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.commons.compress.harmony.unpack200.bytecode;
018
019import java.io.DataOutputStream;
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023
024/**
025 * AnnotationDefault class file attribute
026 */
027public class AnnotationDefaultAttribute extends AnnotationsAttribute {
028
029    private static CPUTF8 attributeName;
030
031    public static void setAttributeName(final CPUTF8 cpUTF8Value) {
032        attributeName = cpUTF8Value;
033    }
034
035    private final ElementValue elementValue;
036
037    /**
038     * Constructs a new instance.
039     *
040     * @param elementValue element value to track.
041     */
042    public AnnotationDefaultAttribute(final ElementValue elementValue) {
043        super(attributeName);
044        this.elementValue = elementValue;
045    }
046
047    @Override
048    public boolean equals(final Object obj) {
049        return this == obj;
050    }
051
052    @Override
053    protected int getLength() {
054        return elementValue.getLength();
055    }
056
057    @Override
058    protected ClassFileEntry[] getNestedClassFileEntries() {
059        final List<Object> nested = new ArrayList<>();
060        nested.add(attributeName);
061        nested.addAll(elementValue.getClassFileEntries());
062        final ClassFileEntry[] nestedEntries = new ClassFileEntry[nested.size()];
063        for (int i = 0; i < nestedEntries.length; i++) {
064            nestedEntries[i] = (ClassFileEntry) nested.get(i);
065        }
066        return nestedEntries;
067    }
068
069    @Override
070    protected void resolve(final ClassConstantPool pool) {
071        super.resolve(pool);
072        elementValue.resolve(pool);
073    }
074
075    @Override
076    public String toString() {
077        return "AnnotationDefault: " + elementValue;
078    }
079
080    @Override
081    protected void writeBody(final DataOutputStream dos) throws IOException {
082        elementValue.writeBody(dos);
083    }
084
085}