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  
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import java.util.Arrays;
24  
25  import org.apache.bcel.Const;
26  import org.apache.commons.lang3.ArrayUtils;
27  
28  /**
29   * This class represents a bootstrap method attribute, i.e., the bootstrap method ref, the number of bootstrap arguments
30   * and an array of the bootstrap arguments.
31   *
32   * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.23"> The class File Format :
33   *      The BootstrapMethods Attribute</a>
34   * @since 6.0
35   */
36  public class BootstrapMethod implements Cloneable {
37  
38      static final BootstrapMethod[] EMPTY_ARRAY = {};
39  
40      /** Index of the CONSTANT_MethodHandle_info structure in the constant_pool table */
41      private int bootstrapMethodRef;
42  
43      /** Array of references to the constant_pool table */
44      private int[] bootstrapArguments;
45  
46      /**
47       * Initialize from another object.
48       *
49       * @param c Source to copy.
50       */
51      public BootstrapMethod(final BootstrapMethod c) {
52          this(c.getBootstrapMethodRef(), c.getBootstrapArguments());
53      }
54  
55      /**
56       * Constructs object from input stream.
57       *
58       * @param input Input stream
59       * @throws IOException if an I/O error occurs.
60       */
61      BootstrapMethod(final DataInput input) throws IOException {
62          this(input.readUnsignedShort(), input.readUnsignedShort());
63  
64          for (int i = 0; i < bootstrapArguments.length; i++) {
65              bootstrapArguments[i] = input.readUnsignedShort();
66          }
67      }
68  
69      // helper method
70      private BootstrapMethod(final int bootstrapMethodRef, final int numBootstrapArguments) {
71          this(bootstrapMethodRef, new int[numBootstrapArguments]);
72      }
73  
74      /**
75       * @param bootstrapMethodRef int index into constant_pool of CONSTANT_MethodHandle
76       * @param bootstrapArguments int[] indices into constant_pool of CONSTANT_[type]_info
77       */
78      public BootstrapMethod(final int bootstrapMethodRef, final int[] bootstrapArguments) {
79          this.bootstrapMethodRef = bootstrapMethodRef;
80          setBootstrapArguments(bootstrapArguments);
81      }
82  
83      /**
84       * @return deep copy of this object
85       */
86      public BootstrapMethod copy() {
87          try {
88              return (BootstrapMethod) clone();
89          } catch (final CloneNotSupportedException ignore) {
90              // TODO should this throw?
91          }
92          return null;
93      }
94  
95      /**
96       * Dump object to file stream in binary format.
97       *
98       * @param file Output file stream
99       * @throws IOException if an I/O error occurs.
100      */
101     public final void dump(final DataOutputStream file) throws IOException {
102         file.writeShort(bootstrapMethodRef);
103         file.writeShort(bootstrapArguments.length);
104         for (final int bootstrapArgument : bootstrapArguments) {
105             file.writeShort(bootstrapArgument);
106         }
107     }
108 
109     /**
110      * @return int[] of bootstrap_method indices into constant_pool of CONSTANT_[type]_info
111      */
112     public int[] getBootstrapArguments() {
113         return bootstrapArguments;
114     }
115 
116     /**
117      * @return index into constant_pool of bootstrap_method
118      */
119     public int getBootstrapMethodRef() {
120         return bootstrapMethodRef;
121     }
122 
123     /**
124      * @return count of number of boostrap arguments
125      */
126     public int getNumBootstrapArguments() {
127         return bootstrapArguments.length;
128     }
129 
130     /**
131      * @param bootstrapArguments int[] indices into constant_pool of CONSTANT_[type]_info
132      */
133     public void setBootstrapArguments(final int[] bootstrapArguments) {
134         this.bootstrapArguments = ArrayUtils.nullToEmpty(bootstrapArguments);
135     }
136 
137     /**
138      * @param bootstrapMethodRef int index into constant_pool of CONSTANT_MethodHandle
139      */
140     public void setBootstrapMethodRef(final int bootstrapMethodRef) {
141         this.bootstrapMethodRef = bootstrapMethodRef;
142     }
143 
144     /**
145      * @return String representation.
146      */
147     @Override
148     public final String toString() {
149         return "BootstrapMethod(" + bootstrapMethodRef + ", " + bootstrapArguments.length + ", " + Arrays.toString(bootstrapArguments) + ")";
150     }
151 
152     /**
153      * @return Resolved string representation
154      */
155     public final String toString(final ConstantPool constantPool) {
156         final StringBuilder buf = new StringBuilder();
157         final String bootstrapMethodName = constantPool.constantToString(bootstrapMethodRef, Const.CONSTANT_MethodHandle);
158         buf.append(Utility.compactClassName(bootstrapMethodName, false));
159         final int bootstrapArgumentsLen = bootstrapArguments.length;
160         if (bootstrapArgumentsLen > 0) {
161             buf.append("\nMethod Arguments:");
162             for (int i = 0; i < bootstrapArgumentsLen; i++) {
163                 buf.append("\n  ").append(i).append(": ");
164                 buf.append(constantPool.constantToString(constantPool.getConstant(bootstrapArguments[i])));
165             }
166         }
167         return buf.toString();
168     }
169 }