1 /* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved. 2 * 3 * This program and the accompanying materials are made available under 4 * the terms of the Common Public License v1.0 which accompanies this distribution, 5 * and is available at http://www.eclipse.org/legal/cpl-v10.html 6 * 7 * $Id: MethodCollection.java,v 1.1.1.1 2004/05/09 16:57:47 vlad_r Exp $ 8 */ 9 package com.vladium.jcd.cls; 10 11 import java.io.IOException; 12 import java.util.ArrayList; 13 import java.util.List; 14 15 import com.vladium.jcd.lib.UDataOutputStream; 16 import com.vladium.util.IntVector; 17 18 // ---------------------------------------------------------------------------- 19 /** 20 * @author (C) 2001, Vlad Roubtsov 21 */ 22 final class MethodCollection implements IMethodCollection 23 { 24 // public: ................................................................ 25 26 // ACCESSORS: 27 28 public Method_info get (final int offset) 29 { 30 return (Method_info) m_methods.get (offset); 31 } 32 33 public int [] get (final ClassDef cls, final String name) 34 { 35 if (cls == null) throw new IllegalArgumentException ("null input: cls"); 36 37 // TODO: hash impl [not possible without having access to the parent ClassDef] 38 39 final int count = m_methods.size (); // use size() if class becomes non-final 40 final IntVector result = new IntVector (count); 41 42 for (int m = 0; m < count; ++ m) 43 { 44 final Method_info method = (Method_info) m_methods.get (m); 45 46 if (method.getName (cls).equals (name)) 47 result.add (m); 48 } 49 50 return result.values (); // IntVector optimizes for the empty case 51 } 52 53 public int size () 54 { 55 return m_methods.size (); 56 } 57 58 // Cloneable: 59 60 /** 61 * Performs a deep copy. 62 */ 63 public Object clone () 64 { 65 try 66 { 67 final MethodCollection _clone = (MethodCollection) super.clone (); 68 69 // deep clone: 70 final int methods_count = m_methods.size (); // use size() if class becomes non-final 71 _clone.m_methods = new ArrayList (methods_count); 72 for (int m = 0; m < methods_count; ++ m) 73 { 74 _clone.m_methods.add (((Method_info) m_methods.get (m)).clone ()); 75 } 76 77 return _clone; 78 } 79 catch (CloneNotSupportedException e) 80 { 81 throw new InternalError (e.toString ()); 82 } 83 } 84 85 // IClassFormatOutput: 86 87 public void writeInClassFormat (final UDataOutputStream out) throws IOException 88 { 89 final int methods_count = m_methods.size (); // use size() if class becomes non-final 90 out.writeU2 (methods_count); 91 92 for (int i = 0; i < methods_count; i++) 93 { 94 get (i).writeInClassFormat (out); 95 } 96 } 97 98 // Visitor: 99 100 public void accept (final IClassDefVisitor visitor, final Object ctx) 101 { 102 visitor.visit (this, ctx); 103 } 104 105 106 // MUTATORS: 107 108 public int add (final Method_info method) 109 { 110 final int newoffset = m_methods.size (); // use size() if class becomes non-final 111 m_methods.add (method); 112 113 return newoffset; 114 } 115 116 public Method_info set (final int offset, final Method_info method) 117 { 118 return (Method_info) m_methods.set (offset, method); 119 } 120 121 public Method_info remove (final int offset) 122 { 123 return (Method_info) m_methods.remove (offset); 124 } 125 126 // protected: ............................................................. 127 128 // package: ............................................................... 129 130 131 MethodCollection (final int capacity) 132 { 133 m_methods = capacity < 0 ? new ArrayList () : new ArrayList (capacity); 134 } 135 136 // private: ............................................................... 137 138 139 private List/* Method_info */ m_methods; // method collection 140 141 } // end of class 142 // ---------------------------------------------------------------------------- 143 144