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.commons.compress.harmony.unpack200.bytecode;
020
021import java.io.DataOutputStream;
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025
026/**
027 * Annotations class file attribute, either a RuntimeVisibleAnnotations attribute or a RuntimeInvisibleAnnotations attribute.
028 */
029public class RuntimeVisibleorInvisibleAnnotationsAttribute extends AnnotationsAttribute {
030
031    private final Annotation[] annotations;
032
033    /**
034     * Constructs a new instance for an attribute name.
035     *
036     * @param name an attribute name.
037     * @param annotations Annotations.
038     */
039    public RuntimeVisibleorInvisibleAnnotationsAttribute(final CPUTF8 name, final Annotation[] annotations) {
040        super(name);
041        this.annotations = annotations;
042    }
043
044    @Override
045    protected int getLength() {
046        int length = 2;
047        for (final Annotation annotation : annotations) {
048            length += annotation.getLength();
049        }
050        return length;
051    }
052
053    @Override
054    protected ClassFileEntry[] getNestedClassFileEntries() {
055        final List<Object> nested = new ArrayList<>();
056        nested.add(attributeName);
057        for (final Annotation annotation : annotations) {
058            nested.addAll(annotation.getClassFileEntries());
059        }
060        return nested.toArray(NONE);
061    }
062
063    @Override
064    protected void resolve(final ClassConstantPool pool) {
065        super.resolve(pool);
066        for (final Annotation annotation : annotations) {
067            annotation.resolve(pool);
068        }
069    }
070
071    @Override
072    public String toString() {
073        return attributeName.underlyingString() + ": " + annotations.length + " annotations";
074    }
075
076    @Override
077    protected void writeBody(final DataOutputStream dos) throws IOException {
078        final int size = dos.size();
079        dos.writeShort(annotations.length);
080        for (final Annotation annotation : annotations) {
081            annotation.writeBody(dos);
082        }
083        if (dos.size() - size != getLength()) {
084            throw new Error();
085        }
086    }
087}