File: MethodCollection.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 (143 lines) | stat: -rw-r--r-- 4,259 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
/* 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: MethodCollection.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 java.util.ArrayList;
import java.util.List;

import com.vladium.jcd.lib.UDataOutputStream;
import com.vladium.util.IntVector;

// ----------------------------------------------------------------------------
/**
 * @author (C) 2001, Vlad Roubtsov
 */
final class MethodCollection implements IMethodCollection
{
    // public: ................................................................

    // ACCESSORS:
        
    public Method_info get (final int offset)
    {
        return (Method_info) m_methods.get (offset);
    }
    
    public int [] get (final ClassDef cls, final String name)
    {
        if (cls == null) throw new IllegalArgumentException  ("null input: cls");
        
        // TODO: hash impl [not possible without having access to the parent ClassDef]
        
        final int count = m_methods.size (); // use size() if class becomes non-final
        final IntVector result = new IntVector (count);

        for (int m = 0; m < count; ++ m)
        {
            final Method_info method = (Method_info) m_methods.get (m);
            
            if (method.getName (cls).equals (name))  
                result.add (m);
        }
        
        return result.values (); // IntVector optimizes for the empty case 
    }
    
    public int size ()
    {
        return m_methods.size ();
    }
    
    // Cloneable:
    
    /**
     * Performs a deep copy.
     */
    public Object clone ()
    {
        try
        {
            final MethodCollection _clone = (MethodCollection) super.clone ();
            
            // deep clone:
            final int methods_count = m_methods.size (); // use size() if class becomes non-final
            _clone.m_methods = new ArrayList (methods_count);
            for (int m = 0; m < methods_count; ++ m)
            {
                _clone.m_methods.add (((Method_info) m_methods.get (m)).clone ());
            }
            
            return _clone;
        }
        catch (CloneNotSupportedException e)
        {
            throw new InternalError (e.toString ());
        }        
    }
    
    // IClassFormatOutput:
    
    public void writeInClassFormat (final UDataOutputStream out) throws IOException
    {
        final int methods_count = m_methods.size (); // use size() if class becomes non-final
        out.writeU2 (methods_count);
        
        for (int i = 0; i < methods_count; i++)
        {
            get (i).writeInClassFormat (out);
        }
    }
    
    // Visitor:
    
    public void accept (final IClassDefVisitor visitor, final Object ctx)
    {
        visitor.visit (this, ctx);
    }
    

    // MUTATORS:

    public int add (final Method_info method)
    {
        final int newoffset = m_methods.size ();  // use size() if class becomes non-final
        m_methods.add (method);
        
        return newoffset;
    }
    
    public Method_info set (final int offset, final Method_info method)
    {
        return (Method_info) m_methods.set (offset, method);
    }
    
    public Method_info remove (final int offset)
    {
        return (Method_info) m_methods.remove (offset);
    }
    
    // protected: .............................................................

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


    MethodCollection (final int capacity)
    {
        m_methods = capacity < 0 ? new ArrayList () : new ArrayList (capacity);
    }

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

    
    private List/* Method_info */ m_methods; // method collection

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