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