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 * Parameter annotations class file attribute, either a RuntimeVisibleParameterAnnotations attribute or a
026 * RuntimeInvisibleParameterAnnotations attribute.
027 */
028public class RuntimeVisibleorInvisibleParameterAnnotationsAttribute extends AnnotationsAttribute {
029
030    private final int num_parameters;
031    private final ParameterAnnotation[] parameter_annotations;
032
033    public RuntimeVisibleorInvisibleParameterAnnotationsAttribute(final CPUTF8 name,
034        final ParameterAnnotation[] parameter_annotations) {
035        super(name);
036        this.num_parameters = parameter_annotations.length;
037        this.parameter_annotations = parameter_annotations;
038    }
039
040    @Override
041    protected int getLength() {
042        int length = 1;
043        for (int i = 0; i < num_parameters; i++) {
044            length += parameter_annotations[i].getLength();
045        }
046        return length;
047    }
048
049    @Override
050    protected void resolve(final ClassConstantPool pool) {
051        super.resolve(pool);
052        for (int i = 0; i < parameter_annotations.length; i++) {
053            parameter_annotations[i].resolve(pool);
054        }
055    }
056
057    @Override
058    protected void writeBody(final DataOutputStream dos) throws IOException {
059        dos.writeByte(num_parameters);
060        for (int i = 0; i < num_parameters; i++) {
061            parameter_annotations[i].writeBody(dos);
062        }
063    }
064
065    @Override
066    public String toString() {
067        return attributeName.underlyingString() + ": " + num_parameters + " parameter annotations";
068    }
069
070    /**
071     * ParameterAnnotation represents the annotations on a single parameter.
072     */
073    public static class ParameterAnnotation {
074
075        private final Annotation[] annotations;
076        private final int num_annotations;
077
078        public ParameterAnnotation(final Annotation[] annotations) {
079            this.num_annotations = annotations.length;
080            this.annotations = annotations;
081        }
082
083        public void writeBody(final DataOutputStream dos) throws IOException {
084            dos.writeShort(num_annotations);
085            for (int i = 0; i < annotations.length; i++) {
086                annotations[i].writeBody(dos);
087            }
088        }
089
090        public void resolve(final ClassConstantPool pool) {
091            for (int i = 0; i < annotations.length; i++) {
092                annotations[i].resolve(pool);
093            }
094        }
095
096        public int getLength() {
097            int length = 2;
098            for (int i = 0; i < annotations.length; i++) {
099                length += annotations[i].getLength();
100            }
101            return length;
102        }
103
104        public List getClassFileEntries() {
105            final List nested = new ArrayList();
106            for (int i = 0; i < annotations.length; i++) {
107                nested.addAll(annotations[i].getClassFileEntries());
108            }
109            return nested;
110        }
111
112    }
113
114    @Override
115    protected ClassFileEntry[] getNestedClassFileEntries() {
116        final List nested = new ArrayList();
117        nested.add(attributeName);
118        for (int i = 0; i < parameter_annotations.length; i++) {
119            nested.addAll(parameter_annotations[i].getClassFileEntries());
120        }
121        final ClassFileEntry[] nestedEntries = new ClassFileEntry[nested.size()];
122        for (int i = 0; i < nestedEntries.length; i++) {
123            nestedEntries[i] = (ClassFileEntry) nested.get(i);
124        }
125        return nestedEntries;
126    }
127
128}