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