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