View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.compress.harmony.unpack200.bytecode;
18  
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  /**
25   * Parameter annotations class file attribute, either a RuntimeVisibleParameterAnnotations attribute or a RuntimeInvisibleParameterAnnotations attribute.
26   */
27  public class RuntimeVisibleorInvisibleParameterAnnotationsAttribute extends AnnotationsAttribute {
28  
29      /**
30       * ParameterAnnotation represents the annotations on a single parameter.
31       */
32      public static class ParameterAnnotation {
33  
34          private final Annotation[] annotations;
35          private final int numAnnotations;
36  
37          public ParameterAnnotation(final Annotation[] annotations) {
38              this.numAnnotations = annotations.length;
39              this.annotations = annotations;
40          }
41  
42          public List<Object> getClassFileEntries() {
43              final List<Object> nested = new ArrayList<>();
44              for (final Annotation annotation : annotations) {
45                  nested.addAll(annotation.getClassFileEntries());
46              }
47              return nested;
48          }
49  
50          public int getLength() {
51              int length = 2;
52              for (final Annotation annotation : annotations) {
53                  length += annotation.getLength();
54              }
55              return length;
56          }
57  
58          public void resolve(final ClassConstantPool pool) {
59              for (final Annotation annotation : annotations) {
60                  annotation.resolve(pool);
61              }
62          }
63  
64          public void writeBody(final DataOutputStream dos) throws IOException {
65              dos.writeShort(numAnnotations);
66              for (final Annotation annotation : annotations) {
67                  annotation.writeBody(dos);
68              }
69          }
70  
71      }
72  
73      private final int numParameters;
74  
75      private final ParameterAnnotation[] parameterAnnotations;
76  
77      public RuntimeVisibleorInvisibleParameterAnnotationsAttribute(final CPUTF8 name, final ParameterAnnotation[] parameterAnnotations) {
78          super(name);
79          this.numParameters = parameterAnnotations.length;
80          this.parameterAnnotations = parameterAnnotations;
81      }
82  
83      @Override
84      protected int getLength() {
85          int length = 1;
86          for (int i = 0; i < numParameters; i++) {
87              length += parameterAnnotations[i].getLength();
88          }
89          return length;
90      }
91  
92      @Override
93      protected ClassFileEntry[] getNestedClassFileEntries() {
94          final List<Object> nested = new ArrayList<>();
95          nested.add(attributeName);
96          for (final ParameterAnnotation parameterAnnotation : parameterAnnotations) {
97              nested.addAll(parameterAnnotation.getClassFileEntries());
98          }
99          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 }