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