RuntimeVisibleorInvisibleAnnotationsAttribute.java

  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. import java.io.DataOutputStream;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. /**
  25.  * Annotations class file attribute, either a RuntimeVisibleAnnotations attribute or a RuntimeInvisibleAnnotations attribute.
  26.  */
  27. public class RuntimeVisibleorInvisibleAnnotationsAttribute extends AnnotationsAttribute {

  28.     private final Annotation[] annotations;

  29.     /**
  30.      * Constructs a new instance for an attribute name.
  31.      *
  32.      * @param name an attribute name.
  33.      * @param annotations Annotations.
  34.      */
  35.     public RuntimeVisibleorInvisibleAnnotationsAttribute(final CPUTF8 name, final Annotation[] annotations) {
  36.         super(name);
  37.         this.annotations = annotations;
  38.     }

  39.     @Override
  40.     protected int getLength() {
  41.         int length = 2;
  42.         for (final Annotation annotation : annotations) {
  43.             length += annotation.getLength();
  44.         }
  45.         return length;
  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.     @Override
  57.     protected void resolve(final ClassConstantPool pool) {
  58.         super.resolve(pool);
  59.         for (final Annotation annotation : annotations) {
  60.             annotation.resolve(pool);
  61.         }
  62.     }

  63.     @Override
  64.     public String toString() {
  65.         return attributeName.underlyingString() + ": " + annotations.length + " annotations";
  66.     }

  67.     @Override
  68.     protected void writeBody(final DataOutputStream dos) throws IOException {
  69.         final int size = dos.size();
  70.         dos.writeShort(annotations.length);
  71.         for (final Annotation annotation : annotations) {
  72.             annotation.writeBody(dos);
  73.         }
  74.         if (dos.size() - size != getLength()) {
  75.             throw new Error();
  76.         }
  77.     }
  78. }