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   * Annotations class file attribute, either a RuntimeVisibleAnnotations attribute or a RuntimeInvisibleAnnotations attribute.
28   */
29  public class RuntimeVisibleorInvisibleAnnotationsAttribute extends AnnotationsAttribute {
30  
31      private final Annotation[] annotations;
32  
33      /**
34       * Constructs a new instance for an attribute name.
35       *
36       * @param name an attribute name.
37       * @param annotations Annotations.
38       */
39      public RuntimeVisibleorInvisibleAnnotationsAttribute(final CPUTF8 name, final Annotation[] annotations) {
40          super(name);
41          this.annotations = annotations;
42      }
43  
44      @Override
45      protected int getLength() {
46          int length = 2;
47          for (final Annotation annotation : annotations) {
48              length += annotation.getLength();
49          }
50          return length;
51      }
52  
53      @Override
54      protected ClassFileEntry[] getNestedClassFileEntries() {
55          final List<Object> nested = new ArrayList<>();
56          nested.add(attributeName);
57          for (final Annotation annotation : annotations) {
58              nested.addAll(annotation.getClassFileEntries());
59          }
60          return nested.toArray(NONE);
61      }
62  
63      @Override
64      protected void resolve(final ClassConstantPool pool) {
65          super.resolve(pool);
66          for (final Annotation annotation : annotations) {
67              annotation.resolve(pool);
68          }
69      }
70  
71      @Override
72      public String toString() {
73          return attributeName.underlyingString() + ": " + annotations.length + " annotations";
74      }
75  
76      @Override
77      protected void writeBody(final DataOutputStream dos) throws IOException {
78          final int size = dos.size();
79          dos.writeShort(annotations.length);
80          for (final Annotation annotation : annotations) {
81              annotation.writeBody(dos);
82          }
83          if (dos.size() - size != getLength()) {
84              throw new Error();
85          }
86      }
87  }