View Javadoc
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.bcel.classfile;
20  
21  import java.io.DataInput;
22  import java.io.DataOutputStream;
23  import java.io.IOException;
24  
25  import org.apache.bcel.Const;
26  
27  /**
28   * This class is derived from the abstract {@link Constant} and represents a reference to a method type.
29   *
30   * @see Constant
31   * @since 6.0
32   */
33  public final class ConstantMethodType extends Constant {
34  
35      private int descriptorIndex;
36  
37      /**
38       * Initialize from another object.
39       *
40       * @param c Source to copy.
41       */
42      public ConstantMethodType(final ConstantMethodType c) {
43          this(c.getDescriptorIndex());
44      }
45  
46      /**
47       * Initialize instance from file data.
48       *
49       * @param file Input stream.
50       * @throws IOException if an I/O error occurs.
51       */
52      ConstantMethodType(final DataInput file) throws IOException {
53          this(file.readUnsignedShort());
54      }
55  
56      /**
57       * Constructs a ConstantMethodType.
58       *
59       * @param descriptorIndex Index to the method descriptor.
60       */
61      public ConstantMethodType(final int descriptorIndex) {
62          super(Const.CONSTANT_MethodType);
63          this.descriptorIndex = descriptorIndex;
64      }
65  
66      /**
67       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
68       * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
69       *
70       * @param v Visitor object.
71       */
72      @Override
73      public void accept(final Visitor v) {
74          v.visitConstantMethodType(this);
75      }
76  
77      /**
78       * Dumps name and signature index to file stream in binary format.
79       *
80       * @param file Output file stream.
81       * @throws IOException if an I/O error occurs.
82       */
83      @Override
84      public void dump(final DataOutputStream file) throws IOException {
85          file.writeByte(super.getTag());
86          file.writeShort(descriptorIndex);
87      }
88  
89      /**
90       * Gets the descriptor index.
91       *
92       * @return the descriptor index.
93       */
94      public int getDescriptorIndex() {
95          return descriptorIndex;
96      }
97  
98      /**
99       * Sets the descriptor index.
100      *
101      * @param descriptorIndex the descriptor index.
102      */
103     public void setDescriptorIndex(final int descriptorIndex) {
104         this.descriptorIndex = descriptorIndex;
105     }
106 
107     /**
108      * @return String representation.
109      */
110     @Override
111     public String toString() {
112         return super.toString() + "(descriptorIndex = " + descriptorIndex + ")";
113     }
114 }