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