RuntimeVisibleorInvisibleParameterAnnotationsAttribute.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.  * Parameter annotations class file attribute, either a RuntimeVisibleParameterAnnotations attribute or a RuntimeInvisibleParameterAnnotations attribute.
  24.  */
  25. public class RuntimeVisibleorInvisibleParameterAnnotationsAttribute extends AnnotationsAttribute {

  26.     /**
  27.      * ParameterAnnotation represents the annotations on a single parameter.
  28.      */
  29.     public static class ParameterAnnotation {

  30.         private final Annotation[] annotations;
  31.         private final int numAnnotations;

  32.         public ParameterAnnotation(final Annotation[] annotations) {
  33.             this.numAnnotations = annotations.length;
  34.             this.annotations = annotations;
  35.         }

  36.         public List<Object> getClassFileEntries() {
  37.             final List<Object> nested = new ArrayList<>();
  38.             for (final Annotation annotation : annotations) {
  39.                 nested.addAll(annotation.getClassFileEntries());
  40.             }
  41.             return nested;
  42.         }

  43.         public int getLength() {
  44.             int length = 2;
  45.             for (final Annotation annotation : annotations) {
  46.                 length += annotation.getLength();
  47.             }
  48.             return length;
  49.         }

  50.         public void resolve(final ClassConstantPool pool) {
  51.             for (final Annotation annotation : annotations) {
  52.                 annotation.resolve(pool);
  53.             }
  54.         }

  55.         public void writeBody(final DataOutputStream dos) throws IOException {
  56.             dos.writeShort(numAnnotations);
  57.             for (final Annotation annotation : annotations) {
  58.                 annotation.writeBody(dos);
  59.             }
  60.         }

  61.     }

  62.     private final int numParameters;

  63.     private final ParameterAnnotation[] parameterAnnotations;

  64.     public RuntimeVisibleorInvisibleParameterAnnotationsAttribute(final CPUTF8 name, final ParameterAnnotation[] parameterAnnotations) {
  65.         super(name);
  66.         this.numParameters = parameterAnnotations.length;
  67.         this.parameterAnnotations = parameterAnnotations;
  68.     }

  69.     @Override
  70.     protected int getLength() {
  71.         int length = 1;
  72.         for (int i = 0; i < numParameters; i++) {
  73.             length += parameterAnnotations[i].getLength();
  74.         }
  75.         return length;
  76.     }

  77.     @Override
  78.     protected ClassFileEntry[] getNestedClassFileEntries() {
  79.         final List<Object> nested = new ArrayList<>();
  80.         nested.add(attributeName);
  81.         for (final ParameterAnnotation parameterAnnotation : parameterAnnotations) {
  82.             nested.addAll(parameterAnnotation.getClassFileEntries());
  83.         }
  84.         return nested.toArray(NONE);
  85.     }

  86.     @Override
  87.     protected void resolve(final ClassConstantPool pool) {
  88.         super.resolve(pool);
  89.         for (final ParameterAnnotation parameterAnnotation : parameterAnnotations) {
  90.             parameterAnnotation.resolve(pool);
  91.         }
  92.     }

  93.     @Override
  94.     public String toString() {
  95.         return attributeName.underlyingString() + ": " + numParameters + " parameter annotations";
  96.     }

  97.     @Override
  98.     protected void writeBody(final DataOutputStream dos) throws IOException {
  99.         dos.writeByte(numParameters);
  100.         for (int i = 0; i < numParameters; i++) {
  101.             parameterAnnotations[i].writeBody(dos);
  102.         }
  103.     }

  104. }