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