File: Method_info.java

package info (click to toggle)
emma-coverage 2.0.5312%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 2,000 kB
  • ctags: 3,667
  • sloc: java: 23,109; xml: 414; makefile: 22
file content (220 lines) | stat: -rw-r--r-- 7,041 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
 * 
 * This program and the accompanying materials are made available under
 * the terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * $Id: Method_info.java,v 1.1.1.1 2004/05/09 16:57:47 vlad_r Exp $
 */
package com.vladium.jcd.cls;

import java.io.IOException;

import com.vladium.jcd.cls.attribute.*;
import com.vladium.jcd.cls.constant.CONSTANT_Utf8_info;
import com.vladium.jcd.lib.UDataInputStream;
import com.vladium.jcd.lib.UDataOutputStream;

// ----------------------------------------------------------------------------
/**
 * Each class method, and each instance initialization method <init>, is described
 * by a variable-length method_info structure. The structure has the following
 * format:
 * <PRE>
 *  method_info {
 *          u2 access_flags;
 *          u2 name_index;
 *          u2 descriptor_index;
 *          u2 attributes_count;
 *          attribute_info attributes[attributes_count];
 *  }
 * </PRE>
 * 
 * The value of the access_flags item is a mask of modifiers used to describe
 * access permission to and properties of a method or instance initialization method.<P>
 * 
 * The value of the name_index item must be a valid index into the constant pool
 * table. The constant pool entry at that index must be a {@link CONSTANT_Utf8_info}
 * structure representing either one of the special internal method names, either
 * &lt;init&gt; or &lt;clinit&gt;, or a valid Java method name, stored as a simple
 * (not fully qualified) name.<P>
 * 
 * The value of the descriptor_index item must be a valid index into the constant pool
 * table. The constant pool entry at that index must be a {@link CONSTANT_Utf8_info}
 * structure representing a valid Java method descriptor.<P>
 * 
 * Each value of the attributes table must be a variable-length attribute structure.
 * A method can have any number of optional attributes associated with it. The only
 * attributes defined by this specification for the attributes table of a method_info
 * structure are the Code and Exceptions attributes. See {@link CodeAttribute_info}
 * and {@link ExceptionsAttribute_info}.
 *  
 * @author (C) 2001, Vlad Roubtsov
 */
public
final class Method_info implements Cloneable, IAccessFlags
{
    // public: ................................................................

    
    public int m_name_index;
    public int m_descriptor_index;
    
    
    public Method_info (int access_flags, int name_index, int descriptor_index, IAttributeCollection attributes)
    {
        m_access_flags = access_flags;
        
        m_name_index = name_index;
        m_descriptor_index = descriptor_index;
        
        m_attributes = attributes;
    }


    public Method_info (final IConstantCollection constants,
                        final UDataInputStream bytes)
        throws IOException
    {
        m_access_flags = bytes.readU2 ();
        
        m_name_index = bytes.readU2 ();
        m_descriptor_index = bytes.readU2 ();
        
        // TODO: put this logic into AttributeCollection
        
        final int attributes_count = bytes.readU2 ();        
        m_attributes = ElementFactory.newAttributeCollection (attributes_count);
        
        for (int i = 0; i < attributes_count; ++ i)
        {
            final Attribute_info attribute_info = Attribute_info.new_Attribute_info (constants, bytes);
            
            m_attributes.add (attribute_info);
        }
    }
   
    /**
     * Returns the method name within the context of 'cls' class definition.
     * 
     * @param cls class that contains this method
     * @return method name
     */
    public String getName (final ClassDef cls)
    {
        return ((CONSTANT_Utf8_info) cls.getConstants ().get (m_name_index)).m_value;
    }
    
    /**
     * Returns the descriptor string for this method within the context of 'cls'
     * class definition.
     * 
     * @param cls class that contains this method
     * @return field typename descriptor
     */
    public String getDescriptor (final ClassDef cls)
    {
        return ((CONSTANT_Utf8_info) cls.getConstants ().get (m_descriptor_index)).m_value;
    }
    
    public boolean isNative ()
    {
        return (m_access_flags & ACC_NATIVE) != 0;
    }
    
    public boolean isAbstract ()
    {
        return (m_access_flags & ACC_ABSTRACT) != 0;
    }
    
    public boolean isSynthetic ()
    {
        return m_attributes.hasSynthetic ();
    }
    
    public boolean isBridge ()
    {
        return ((m_access_flags & ACC_BRIDGE) != 0) || m_attributes.hasBridge ();
    }
    
    // IAccessFlags:
        
    public final void setAccessFlags (final int flags)
    {
        m_access_flags = flags;
    }

    public final int getAccessFlags ()
    {
        return m_access_flags;
    }
    
    
    public IAttributeCollection getAttributes ()
    {
        return m_attributes;
    }
    
    
    public String toString ()
    {
        StringBuffer s = new StringBuffer ();
        
        s.append ("method_info: [modifiers: 0x" + Integer.toHexString(m_access_flags) + ", name_index = " + m_name_index + ", descriptor_index = " + m_descriptor_index + "]\n");
        for (int i = 0; i < m_attributes.size (); i++)
        {
            Attribute_info attribute_info = m_attributes.get (i);
            
            s.append ("\t[" + i + "] attribute: " + attribute_info + "\n");
        }

        return s.toString ();
    }
    
    
    // Cloneable:
    
    /**
     * Performs a deep copy.
     */
    public Object clone ()
    {
        try
        {
            final Method_info _clone = (Method_info) super.clone ();
            
            // do deep copy:
            _clone.m_attributes = (IAttributeCollection) m_attributes.clone ();
            
            return _clone;
        }
        catch (CloneNotSupportedException e)
        {
            throw new InternalError (e.toString ());
        }
    }
    
    // IClassFormatOutput:
    
    public void writeInClassFormat (final UDataOutputStream out) throws IOException
    {
        out.writeU2 (m_access_flags);
        
        out.writeU2 (m_name_index);
        out.writeU2 (m_descriptor_index);
        
        m_attributes.writeInClassFormat (out);
    }
    
    // protected: .............................................................

    // package: ...............................................................

    // private: ...............................................................

    
    private int m_access_flags;
    private IAttributeCollection m_attributes;

} // end of class
// ----------------------------------------------------------------------------