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