RuntimeVisibleorInvisibleAnnotationsAttribute.java

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

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

  26.     private final int numAnnotations;
  27.     private final Annotation[] annotations;

  28.     public RuntimeVisibleorInvisibleAnnotationsAttribute(final CPUTF8 name, final Annotation[] annotations) {
  29.         super(name);
  30.         this.numAnnotations = annotations.length;
  31.         this.annotations = annotations;
  32.     }

  33.     @Override
  34.     protected int getLength() {
  35.         int length = 2;
  36.         for (int i = 0; i < numAnnotations; i++) {
  37.             length += annotations[i].getLength();
  38.         }
  39.         return length;
  40.     }

  41.     @Override
  42.     protected ClassFileEntry[] getNestedClassFileEntries() {
  43.         final List<Object> nested = new ArrayList<>();
  44.         nested.add(attributeName);
  45.         for (final Annotation annotation : annotations) {
  46.             nested.addAll(annotation.getClassFileEntries());
  47.         }
  48.         return nested.toArray(NONE);
  49.     }

  50.     @Override
  51.     protected void resolve(final ClassConstantPool pool) {
  52.         super.resolve(pool);
  53.         for (final Annotation annotation : annotations) {
  54.             annotation.resolve(pool);
  55.         }
  56.     }

  57.     @Override
  58.     public String toString() {
  59.         return attributeName.underlyingString() + ": " + numAnnotations + " annotations";
  60.     }

  61.     @Override
  62.     protected void writeBody(final DataOutputStream dos) throws IOException {
  63.         final int size = dos.size();
  64.         dos.writeShort(numAnnotations);
  65.         for (int i = 0; i < numAnnotations; i++) {
  66.             annotations[i].writeBody(dos);
  67.         }
  68.         if (dos.size() - size != getLength()) {
  69.             throw new Error();
  70.         }
  71.     }
  72. }