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