EnclosingMethodAttribute.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. /**
  23.  * Enclosing method class file attribute.
  24.  */
  25. public class EnclosingMethodAttribute extends Attribute {

  26.     private static CPUTF8 attributeName;

  27.     public static void setAttributeName(final CPUTF8 cpUTF8Value) {
  28.         attributeName = cpUTF8Value;
  29.     }

  30.     private int classIndex;
  31.     private int methodIndex;
  32.     private final CPClass cpClass;

  33.     private final CPNameAndType method;

  34.     /**
  35.      * Constructs a new instance.
  36.      *
  37.      * @param cpClass a constant pool class.
  38.      * @param method a constant pool name and type.
  39.      */
  40.     public EnclosingMethodAttribute(final CPClass cpClass, final CPNameAndType method) {
  41.         super(attributeName);
  42.         this.cpClass = cpClass;
  43.         this.method = method;
  44.     }

  45.     /*
  46.      * (non-Javadoc)
  47.      *
  48.      * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#getLength()
  49.      */
  50.     @Override
  51.     protected int getLength() {
  52.         return 4;
  53.     }

  54.     @Override
  55.     protected ClassFileEntry[] getNestedClassFileEntries() {
  56.         if (method != null) {
  57.             return new ClassFileEntry[] { attributeName, cpClass, method };
  58.         }
  59.         return new ClassFileEntry[] { attributeName, cpClass };
  60.     }

  61.     @Override
  62.     protected void resolve(final ClassConstantPool pool) {
  63.         super.resolve(pool);
  64.         cpClass.resolve(pool);
  65.         classIndex = pool.indexOf(cpClass);
  66.         if (method != null) {
  67.             method.resolve(pool);
  68.             methodIndex = pool.indexOf(method);
  69.         } else {
  70.             methodIndex = 0;
  71.         }
  72.     }

  73.     /*
  74.      * (non-Javadoc)
  75.      *
  76.      * @see org.apache.commons.compress.harmony.unpack200.bytecode.ClassFileEntry#toString()
  77.      */
  78.     @Override
  79.     public String toString() {
  80.         return "EnclosingMethod";
  81.     }

  82.     /*
  83.      * (non-Javadoc)
  84.      *
  85.      * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#writeBody(java.io.DataOutputStream)
  86.      */
  87.     @Override
  88.     protected void writeBody(final DataOutputStream dos) throws IOException {
  89.         dos.writeShort(classIndex);
  90.         dos.writeShort(methodIndex);
  91.     }

  92. }