View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.harmony.unpack200.bytecode;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /**
27   * Parameter annotations class file attribute, either a RuntimeVisibleParameterAnnotations attribute or a RuntimeInvisibleParameterAnnotations attribute.
28   */
29  public class RuntimeVisibleorInvisibleParameterAnnotationsAttribute extends AnnotationsAttribute {
30  
31      /**
32       * ParameterAnnotation represents the annotations on a single parameter.
33       */
34      public static class ParameterAnnotation {
35  
36          private final Annotation[] annotations;
37  
38          /**
39           * Constructs a new instance.
40           *
41           * @param annotations Annotation.
42           */
43          public ParameterAnnotation(final Annotation[] annotations) {
44              this.annotations = annotations;
45          }
46  
47          /**
48           * Gets all annotation class file entries.
49           *
50           * @return all annotation class file entries.
51           */
52          public List<Object> getClassFileEntries() {
53              final List<Object> nested = new ArrayList<>();
54              for (final Annotation annotation : annotations) {
55                  nested.addAll(annotation.getClassFileEntries());
56              }
57              return nested;
58          }
59  
60          /**
61           * Gets the cumulative length of all annotations.
62           *
63           * @return the cumulative length of all annotations.
64           */
65          public int getLength() {
66              int length = 2;
67              for (final Annotation annotation : annotations) {
68                  length += annotation.getLength();
69              }
70              return length;
71          }
72  
73          /**
74           * Resolves all annotations in this instance against the given pool.
75           *
76           * @param pool A class constant pool.
77           */
78          public void resolve(final ClassConstantPool pool) {
79              for (final Annotation annotation : annotations) {
80                  annotation.resolve(pool);
81              }
82          }
83  
84          /**
85           * Writes this body to the given output stream.
86           *
87           * @param dos the output stream.
88           * @throws IOException if an I/O error occurs.
89           */
90          public void writeBody(final DataOutputStream dos) throws IOException {
91              dos.writeShort(annotations.length);
92              for (final Annotation annotation : annotations) {
93                  annotation.writeBody(dos);
94              }
95          }
96  
97      }
98  
99      private final ParameterAnnotation[] parameterAnnotations;
100 
101     /**
102      * Constructs a new instance for an attribute name.
103      *
104      * @param name an attribute name.
105      * @param parameterAnnotations Annotations.
106      */
107     public RuntimeVisibleorInvisibleParameterAnnotationsAttribute(final CPUTF8 name, final ParameterAnnotation[] parameterAnnotations) {
108         super(name);
109         this.parameterAnnotations = parameterAnnotations;
110     }
111 
112     @Override
113     protected int getLength() {
114         int length = 1;
115         for (final ParameterAnnotation parameterAnnotation : parameterAnnotations) {
116             length += parameterAnnotation.getLength();
117         }
118         return length;
119     }
120 
121     @Override
122     protected ClassFileEntry[] getNestedClassFileEntries() {
123         final List<Object> nested = new ArrayList<>();
124         nested.add(attributeName);
125         for (final ParameterAnnotation parameterAnnotation : parameterAnnotations) {
126             nested.addAll(parameterAnnotation.getClassFileEntries());
127         }
128         return nested.toArray(NONE);
129     }
130 
131     @Override
132     protected void resolve(final ClassConstantPool pool) {
133         super.resolve(pool);
134         for (final ParameterAnnotation parameterAnnotation : parameterAnnotations) {
135             parameterAnnotation.resolve(pool);
136         }
137     }
138 
139     @Override
140     public String toString() {
141         return attributeName.underlyingString() + ": " + parameterAnnotations.length + " parameter annotations";
142     }
143 
144     @Override
145     protected void writeBody(final DataOutputStream dos) throws IOException {
146         dos.writeByte(parameterAnnotations.length);
147         for (final ParameterAnnotation parameterAnnotation : parameterAnnotations) {
148             parameterAnnotation.writeBody(dos);
149         }
150     }
151 
152 }