summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/mop/Toolbox.java
blob: c2782967765b4eabb835bcf9857923400b5e6821 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/*
 * Toolbox.java
 *
 * comments here.
 *
 * @author   Michiaki Tatsubori
 * @version  %VERSION% %DATE%
 * @see      java.lang.Object
 *
 * COPYRIGHT 1999 by Michiaki Tatsubori, ALL RIGHTS RESERVED.
 */
package io.devnulllabs.openjava.mop;

import java.util.Enumeration;
import java.util.Hashtable;

import io.devnulllabs.openjava.ptree.Parameter;
import io.devnulllabs.openjava.ptree.ParameterList;
import io.devnulllabs.openjava.ptree.TypeName;

/**
 * The class <code>Toolbox</code> is an utility class.
 * <p>
 * <pre>
 * </pre>
 * <p>
 *
 * @author   Michiaki Tatsubori
 * @version  $Id: Toolbox.java,v 1.2 2003/02/19 02:55:01 tatsubori Exp $
 */
public abstract class Toolbox {
    /**
     * Generates an array of classes containing the declared classes and
     * the based classes except the declared one.
     *
     * @param  declareds  declared classes to override
     * @param  bases  based classes.
     * @return  classes which contains the declared classes and the based
     *         classes except the declared one.
     * @see io.devnulllabs.openjava.mop.OJClass
     */
    public static final OJClass[] overridesOn(
        OJClass[] declareds,
        OJClass[] bases) {
        Hashtable table = new Hashtable();
        for (int i = 0; i < bases.length; ++i) {
            table.put(bases[i].signature(), bases[i]);
        }
        for (int i = 0; i < declareds.length; ++i) {
            table.put(declareds[i].signature(), declareds[i]);
        }

        OJClass[] result = new OJClass[table.size()];
        Enumeration it = table.elements();
        for (int i = 0; it.hasMoreElements(); ++i) {
            result[i] = (OJClass) it.nextElement();
        }

        return result;
    }

    /**
     * Generates an array of fields containing the declared fields and
     * the based fields except the declared one.
     *
     * @param  declareds  declared fields to override
     * @param  bases  based fields.
     * @return  fields which contains the declared fields and the based
     *         fields except the declared one.
     * @see io.devnulllabs.openjava.mop.OJField
     */
    public static final OJField[] overridesOn(
        OJField[] declareds,
        OJField[] bases) {
        Hashtable table = new Hashtable();
        for (int i = 0; i < bases.length; ++i) {
            table.put(bases[i].signature(), bases[i]);
        }
        for (int i = 0; i < declareds.length; ++i) {
            table.put(declareds[i].signature(), declareds[i]);
        }

        OJField[] result = new OJField[table.size()];
        Enumeration it = table.elements();
        for (int i = 0; it.hasMoreElements(); ++i) {
            result[i] = (OJField) it.nextElement();
        }

        return result;
    }

    /**
     * Generates an array of methods containing the declared methods and
     * the based methods except the declared one.
     *
     * @param  declareds  declared methods to override
     * @param  bases  based methods.
     * @return  methods which contains the declared methods and the based
     *         methods except the declared one.
     * @see io.devnulllabs.openjava.mop.OJMethod
     */
    public static final OJMethod[] overridesOn(
        OJMethod[] declareds,
        OJMethod[] bases) {
        Hashtable table = new Hashtable();
        for (int i = 0; i < bases.length; ++i) {
            table.put(bases[i].signature(), bases[i]);
        }
        for (int i = 0; i < declareds.length; ++i) {
            table.put(declareds[i].signature(), declareds[i]);
        }

        OJMethod[] result = new OJMethod[table.size()];
        Enumeration it = table.elements();
        for (int i = 0; it.hasMoreElements(); ++i) {
            result[i] = (OJMethod) it.nextElement();
        }

        return result;
    }

    /**
     * Generates an array of classes containing the source classes
     * except ones with private access modifier.
     *
     * @param  src_classes  source classes.
     * @return  classes except ones with private access modifier.
     * @see io.devnulllabs.openjava.mop.OJModifier
     */
    public static final OJClass[] removeThePrivates(OJClass[] src_classes) {
        int dest_length = 0;
        for (int i = 0; i < src_classes.length; ++i) {
            OJModifier modif = src_classes[i].getModifiers();
            if (!modif.isPrivate())
                dest_length++;
        }

        OJClass[] result = new OJClass[dest_length];
        for (int i = 0, count = 0; i < src_classes.length; ++i) {
            OJModifier modif = src_classes[i].getModifiers();
            if (!modif.isPrivate())
                result[count++] = src_classes[i];
        }

        return result;
    }

    /**
     * Generates an array of fields containing the source fields
     * except ones with private access modifier.
     *
     * @param  src_fields  source fields.
     * @return  fields except ones with private access modifier.
     * @see io.devnulllabs.openjava.mop.OJModifier
     */
    public static final OJField[] removeThePrivates(OJField[] src_fields) {
        int dest_length = 0;
        for (int i = 0; i < src_fields.length; ++i) {
            OJModifier modif = src_fields[i].getModifiers();
            if (!modif.isPrivate())
                dest_length++;
        }

        OJField[] result = new OJField[dest_length];
        for (int i = 0, count = 0; i < src_fields.length; ++i) {
            OJModifier modif = src_fields[i].getModifiers();
            if (!modif.isPrivate())
                result[count++] = src_fields[i];
        }

        return result;
    }

    /**
     * Generates an array of methods containing the source methods
     * except ones with private access modifier.
     *
     * @param  src_methods  source methods.
     * @return  methods except ones with private access modifier.
     * @see io.devnulllabs.openjava.mop.OJModifier
     */
    public static final OJMethod[] removeThePrivates(OJMethod[] src_methods) {
        int dest_length = 0;
        for (int i = 0; i < src_methods.length; ++i) {
            OJModifier modif = src_methods[i].getModifiers();
            if (!modif.isPrivate())
                dest_length++;
        }

        OJMethod[] result = new OJMethod[dest_length];
        for (int i = 0, count = 0; i < src_methods.length; ++i) {
            OJModifier modif = src_methods[i].getModifiers();
            if (!modif.isPrivate())
                result[count++] = src_methods[i];
        }

        return result;
    }

    /**
     * Generates an array of constructors containing the source
     * constructors except ones with private access modifier.
     *
     * @param  src_constrs  source constructors.
     * @return  constructors except ones with private access modifier.
     * @see io.devnulllabs.openjava.mop.OJModifier
     */
    public static final OJConstructor[] removeThePrivates(OJConstructor[] src_constrs) {
        int dest_length = 0;
        for (int i = 0; i < src_constrs.length; ++i) {
            OJModifier modif = src_constrs[i].getModifiers();
            if (!modif.isPrivate())
                dest_length++;
        }

        OJConstructor[] result = new OJConstructor[dest_length];
        for (int i = 0, count = 0; i < src_constrs.length; ++i) {
            OJModifier modif = src_constrs[i].getModifiers();
            if (!modif.isPrivate())
                result[count++] = src_constrs[i];
        }

        return result;
    }

    /**
     * Generates an array of classes containing the source classes
     * except ones with private access modifier.
     *
     * @param  src_classes  source classes.
     * @return  classes except ones with private access modifier.
     * @see io.devnulllabs.openjava.mop.OJModifier
     */
    public static final OJClass[] removeTheDefaults(OJClass[] src_classes) {
        int dest_length = 0;
        for (int i = 0; i < src_classes.length; ++i) {
            OJModifier modif = src_classes[i].getModifiers();
            if (modif.isPrivate() || modif.isProtected() || modif.isPublic()) {
                dest_length++;
            }
        }

        OJClass[] result = new OJClass[dest_length];
        for (int i = 0, count = 0; i < src_classes.length; ++i) {
            OJModifier modif = src_classes[i].getModifiers();
            if (modif.isPrivate() || modif.isProtected() || modif.isPublic()) {
                result[count++] = src_classes[i];
            }
        }

        return result;
    }

    /**
     * Generates an array of fields containing the source fields
     * except ones with private access modifier.
     *
     * @param  src_fields  source fields.
     * @return  fields except ones with private access modifier.
     * @see io.devnulllabs.openjava.mop.OJModifier
     */
    public static final OJField[] removeTheDefaults(OJField[] src_fields) {
        int dest_length = 0;
        for (int i = 0; i < src_fields.length; ++i) {
            OJModifier modif = src_fields[i].getModifiers();
            if (modif.isPrivate() || modif.isProtected() || modif.isPublic()) {
                dest_length++;
            }
        }

        OJField[] result = new OJField[dest_length];
        for (int i = 0, count = 0; i < src_fields.length; ++i) {
            OJModifier modif = src_fields[i].getModifiers();
            if (modif.isPrivate() || modif.isProtected() || modif.isPublic()) {
                result[count++] = src_fields[i];
            }
        }

        return result;
    }

    /**
     * Generates an array of methods containing the source methods
     * except ones with private access modifier.
     *
     * @param  src_methods  source methods.
     * @return  methods except ones with private access modifier.
     * @see io.devnulllabs.openjava.mop.OJModifier
     */
    public static final OJMethod[] removeTheDefaults(OJMethod[] src_methods) {
        int dest_length = 0;
        for (int i = 0; i < src_methods.length; ++i) {
            OJModifier modif = src_methods[i].getModifiers();
            if (modif.isPrivate() || modif.isProtected() || modif.isPublic()) {
                dest_length++;
            }
        }

        OJMethod[] result = new OJMethod[dest_length];
        for (int i = 0, count = 0; i < src_methods.length; ++i) {
            OJModifier modif = src_methods[i].getModifiers();
            if (modif.isPrivate() || modif.isProtected() || modif.isPublic()) {
                result[count++] = src_methods[i];
            }
        }

        return result;
    }

    /**
     * Generates an array of constructors containing the source
     * constructors except ones with private access modifier.
     *
     * @param  src_constrs  source constructors.
     * @return  constructors except ones with private access modifier.
     * @see io.devnulllabs.openjava.mop.OJModifier
     */
    public static final OJConstructor[] removeTheDefaults(OJConstructor[] src_constrs) {
        int dest_length = 0;
        for (int i = 0; i < src_constrs.length; ++i) {
            OJModifier modif = src_constrs[i].getModifiers();
            if (modif.isPrivate() || modif.isProtected() || modif.isPublic()) {
                dest_length++;
            }
        }

        OJConstructor[] result = new OJConstructor[dest_length];
        for (int i = 0, count = 0; i < src_constrs.length; ++i) {
            OJModifier modif = src_constrs[i].getModifiers();
            if (modif.isPrivate() || modif.isProtected() || modif.isPublic()) {
                result[count++] = src_constrs[i];
            }
        }

        return result;
    }

    /**
     * Generates an array of classes containing the source classes
     * except ones with non-public access modifier;
     * one of private, protected or package level access modifiers.
     *
     * @param  src_classes  source classes.
     * @return  classes except ones with non-public access modifier.
     * @see io.devnulllabs.openjava.mop.OJModifier
     */
    public static final OJClass[] removeTheNonPublics(OJClass[] src_classes) {
        int dest_length = 0;
        for (int i = 0; i < src_classes.length; ++i) {
            OJModifier modif = src_classes[i].getModifiers();
            if (modif.isPublic())
                dest_length++;
        }

        OJClass[] result = new OJClass[dest_length];
        for (int i = 0, count = 0; i < src_classes.length; ++i) {
            OJModifier modif = src_classes[i].getModifiers();
            if (modif.isPublic())
                result[count++] = src_classes[i];
        }

        return result;
    }

    /**
     * Generates an array of fields containing the source fields
     * except ones with non-public access modifier;
     * one of private, protected or package level access modifiers.
     *
     * @param  src_fields  source fields.
     * @return  fields except ones with non-public access modifier.
     * @see io.devnulllabs.openjava.mop.OJModifier
     */
    public static final OJField[] removeTheNonPublics(OJField[] src_fields) {
        int dest_length = 0;
        for (int i = 0; i < src_fields.length; ++i) {
            OJModifier modif = src_fields[i].getModifiers();
            if (modif.isPublic())
                dest_length++;
        }

        OJField[] result = new OJField[dest_length];
        for (int i = 0, count = 0; i < src_fields.length; ++i) {
            OJModifier modif = src_fields[i].getModifiers();
            if (modif.isPublic())
                result[count++] = src_fields[i];
        }

        return result;
    }

    /**
     * Generates an array of methods containing the source methods
     * except ones with non-public access modifier;
     * one of private, protected or package level access modifiers.
     *
     * @param  src_methods  source methods.
     * @return  methods except ones with non-public access modifier.
     * @see io.devnulllabs.openjava.mop.OJModifier
     */
    public static final OJMethod[] removeTheNonPublics(OJMethod[] src_methods) {
        int dest_length = 0;
        for (int i = 0; i < src_methods.length; ++i) {
            OJModifier modif = src_methods[i].getModifiers();
            if (modif.isPublic())
                dest_length++;
        }

        OJMethod[] result = new OJMethod[dest_length];
        for (int i = 0, count = 0; i < src_methods.length; ++i) {
            OJModifier modif = src_methods[i].getModifiers();
            if (modif.isPublic())
                result[count++] = src_methods[i];
        }

        return result;
    }

    /**
     * Generates an array of constructors containing the source constructors
     * except ones with non-public access modifier;
     * one of private, protected or package level access modifiers.
     *
     * @param  src_constrs  source constructors.
     * @return  constructors except ones with non-public access modifier.
     * @see io.devnulllabs.openjava.mop.OJModifier
     */
    public static final OJConstructor[] removeTheNonPublics(OJConstructor[] src_constrs) {
        int dest_length = 0;
        for (int i = 0; i < src_constrs.length; ++i) {
            OJModifier modif = src_constrs[i].getModifiers();
            if (modif.isPublic())
                dest_length++;
        }

        OJConstructor[] result = new OJConstructor[dest_length];
        for (int i = 0, count = 0; i < src_constrs.length; ++i) {
            OJModifier modif = src_constrs[i].getModifiers();
            if (modif.isPublic())
                result[count++] = src_constrs[i];
        }

        return result;
    }

    /**
     * Pick up a field with the specified name in the source array
     * of fields.
     *
     * @param  src_constrs  source fields.
     * @param  name  a name to specify.
     * @return  a field with the specified name.
     * @see io.devnulllabs.openjava.mop.OJClass
     */
    public static final OJField pickupField(
        OJField[] src_fields,
        String name) {
        for (int i = 0; i < src_fields.length; ++i) {
            if (name.equals(src_fields[i].getName())) {
                return src_fields[i];
            }
        }
        return null;
    }

    /**
     * Pick up a method with the specified signature in the source
     * array of methods.
     *
     * @param  src_methods  source methods.
     * @param  name  a name to specify.
     * @param  param_types  parameter types to specify.
     * @return  a method with the specified signature.
     *        This returns null if it doesn't exist.
     * @see io.devnulllabs.openjava.mop.OJClass
     */
    public static final OJMethod pickupMethod(
        OJMethod[] src_methods,
        String name,
        OJClass[] param_types) {
        src_methods = pickupMethodsByName(src_methods, name);
        return pickupMethodByParameterTypes(src_methods, param_types);
    }

    /**
     * Pick up a method with the signature acceptable the specified
     * signature in the source array of methods.
     *
     * @param  src_constrs  source methods.
     * @param  name  a name to specify.
     * @param  param_types  parameter types to specify.
     * @return  a method with the specified signature.
     *        This returns null if it doesn't exist.
     * @see io.devnulllabs.openjava.mop.OJClass
     */
    public static final OJMethod pickupAcceptableMethod(
        OJMethod[] src_methods,
        String name,
        OJClass[] param_types) {
        src_methods = pickupAcceptableMethods(src_methods, name, param_types);
        return pickupMostSpecified(src_methods);
    }

    /**
     * Generates an array of methods containing the methods with the
     * signature acceptable the specified signature in the source
     * array of methods.
     *
     * @param  src_methods  source methods.
     * @param  name  a name to specify.
     * @param  param_types  parameter types to specify.
     * @return  methods with the specified signature.
     * @see io.devnulllabs.openjava.mop.OJClass
     */
    public static final OJMethod[] pickupAcceptableMethods(
        OJMethod[] src_methods,
        String name,
        OJClass[] param_types) {
        src_methods = pickupMethodsByName(src_methods, name);
        return pickupAcceptableMethodsByParameterTypes(
            src_methods,
            param_types);
    }

    /**
     * Pick up a constructor with the specified signature in the source
     * array of constructors.
     *
     * @param  src_constrs  source constructors.
     * @param  name  a name to specify.
     * @param  param_types  parameter types to specify.
     * @return  a Constructor with the specified signature.
     *        This returns null if it doesn't exist.
     * @see io.devnulllabs.openjava.mop.OJClass
     */
    public static final OJConstructor pickupConstructor(
        OJConstructor[] src_constrs,
        OJClass[] param_types) {
        if (param_types == null)
            param_types = new OJClass[0];
        for (int i = 0; i < src_constrs.length; ++i) {
            OJClass[] accepter = src_constrs[i].getParameterTypes();
            if (isSame(accepter, param_types))
                return src_constrs[i];
        }
        return null;
    }

    /**
     * Pick up a constructor with the signature acceptable the specified
     * signature in the source array of constructors.
     *
     * @param  src_constrs  source constructors.
     * @param  name  a name to specify.
     * @param  param_types  parameter types to specify.
     * @return  a constructor with the specified signature.
     *        This returns null if it doesn't exist.
     * @see io.devnulllabs.openjava.mop.OJClass
     */
    public static final OJConstructor pickupAcceptableConstructor(
        OJConstructor[] src_constrs,
        OJClass[] param_types) {
        src_constrs = pickupAcceptableConstructors(src_constrs, param_types);
        return pickupMostSpecified(src_constrs);
    }

    /**
     * Generates an array of constructors containing the constructors
     * with the specified parameter types in the source array of
     * constructors.
     *
     * @param  src_constrs  source constructors.
     * @param  param_types  parameter types to specify.
     * @return  constructors acceptable the specified parameter types.
     * @see io.devnulllabs.openjava.mop.OJClass
     */
    public static final OJConstructor[] pickupAcceptableConstructors(
        OJConstructor[] src_constrs,
        OJClass[] param_types) {
        param_types = (param_types == null) ? new OJClass[0] : param_types;
        int dest_length = 0;
        for (int i = 0; i < src_constrs.length; ++i) {
            OJClass[] accepter = src_constrs[i].getParameterTypes();
            if (isAcceptable(accepter, param_types))
                dest_length++;
        }

        OJConstructor[] result = new OJConstructor[dest_length];
        for (int i = 0, count = 0; i < src_constrs.length; ++i) {
            OJClass[] accepter = src_constrs[i].getParameterTypes();
            if (isAcceptable(accepter, param_types)) {
                result[count++] = src_constrs[i];
            }
        }

        return result;
    }

    /**
     * Generates an array of methods containing the methods with the
     * specified name in the source array of methods.
     *
     * @param  src_methods  source methods.
     * @param  name  a name to specify.
     * @return  methods with the specified name.
     * @see io.devnulllabs.openjava.mop.OJClass
     */
    public static final OJMethod[] pickupMethodsByName(
        OJMethod[] src_methods,
        String name) {
        int dest_length = 0;
        for (int i = 0; i < src_methods.length; ++i) {
            if (name.equals(src_methods[i].getName()))
                dest_length++;
        }

        OJMethod[] result = new OJMethod[dest_length];
        for (int i = 0, count = 0; i < src_methods.length; ++i) {
            if (name.equals(src_methods[i].getName())) {
                result[count++] = src_methods[i];
            }
        }

        return result;
    }

    /**
     * Picks up a method with the specified parameter types in the source
     * array of methods.
     *
     * @param  src_methods  source methods.
     * @param  param_types  parameter types to specify.
     * @return  a method with the specified parameter types.
     * @see io.devnulllabs.openjava.mop.OJClass
     */
    public static final OJMethod pickupMethodByParameterTypes(
        OJMethod[] src_methods,
        OJClass[] param_types) {
        if (param_types == null)
            param_types = new OJClass[0];
        for (int i = 0; i < src_methods.length; ++i) {
            OJClass[] accepter = src_methods[i].getParameterTypes();
            if (isSame(accepter, param_types))
                return src_methods[i];
        }
        return null;
    }

    /**
     * Generates an array of methods containing the methods with the
     * parameter types acceptable specified parameter types in the source
     * array of methods.
     *
     * @param  src_methods  source methods.
     * @param  param_types  parameter types to specify.
     * @return  methods acceptable the specified parameter types.
     * @see io.devnulllabs.openjava.mop.OJClass
     */
    public static final OJMethod[] pickupAcceptableMethodsByParameterTypes(
        OJMethod[] src_methods,
        OJClass[] param_types) {
        if (param_types == null)
            param_types = new OJClass[0];
        int dest_length = 0;
        for (int i = 0; i < src_methods.length; ++i) {
            OJClass[] accepter = src_methods[i].getParameterTypes();
            if (isAcceptable(accepter, param_types))
                dest_length++;
        }

        OJMethod[] result = new OJMethod[dest_length];
        for (int i = 0, count = 0; i < src_methods.length; ++i) {
            OJClass[] accepter = src_methods[i].getParameterTypes();
            if (isAcceptable(accepter, param_types)) {
                result[count++] = src_methods[i];
            }
        }

        return result;
    }

    public static final boolean isSame(
        OJClass[] accepter,
        OJClass[] acceptee) {
        if (accepter.length != acceptee.length)
            return false;
        for (int i = 0; i < acceptee.length; ++i) {
            if (accepter[i] != acceptee[i])
                return false;
        }
        return true;
    }

    public static final boolean isAcceptable(
        OJClass[] accepter,
        OJClass[] acceptee) {
        if (accepter.length != acceptee.length)
            return false;
        for (int i = 0; i < acceptee.length; ++i) {
            if (!accepter[i].isAssignableFrom(acceptee[i]))
                return false;
        }
        return true;
    }

    public static final boolean isAdaptableTo(
        OJClass[] adapter,
        OJClass[] adaptee) {
        if (adapter.length != adaptee.length)
            return false;
        for (int i = 0; i < adaptee.length; ++i) {
            if (!adaptee[i].isAssignableFrom(adapter[i]))
                return false;
        }
        return true;
    }

    public static final OJConstructor pickupMostSpecified(OJConstructor[] constrs) {
        if (constrs.length == 0)
            return null;
        OJConstructor most = constrs[0];
        for (int i = 0; i < constrs.length; ++i) {
            OJClass[] adapter = most.getParameterTypes();
            OJClass[] adaptee = constrs[i].getParameterTypes();
            if (!isAdaptableTo(adapter, adaptee))
                most = constrs[i];
        }
        return most;
    }

    public static final OJMethod pickupMostSpecified(OJMethod[] methods) {
        if (methods.length == 0)
            return null;
        OJMethod most = methods[0];
        for (int i = 0; i < methods.length; ++i) {
            OJClass[] adapter = most.getParameterTypes();
            OJClass[] adaptee = methods[i].getParameterTypes();
            if (!isAdaptableTo(adapter, adaptee))
                most = methods[i];
        }
        return most;
    }

    public static final OJClass[] append(OJClass[] a, OJClass[] b) {
        OJClass[] result = new OJClass[a.length + b.length];
        System.arraycopy(a, 0, result, 0, a.length);
        System.arraycopy(b, 0, result, a.length, b.length);
        return result;
    }

    public static final OJField[] append(OJField[] a, OJField[] b) {
        OJField[] result = new OJField[a.length + b.length];
        System.arraycopy(a, 0, result, 0, a.length);
        System.arraycopy(b, 0, result, a.length, b.length);
        return result;
    }

    public static final OJMethod[] append(OJMethod[] a, OJMethod[] b) {
        OJMethod[] result = new OJMethod[a.length + b.length];
        System.arraycopy(a, 0, result, 0, a.length);
        System.arraycopy(b, 0, result, a.length, b.length);
        return result;
    }

    public static final OJConstructor[] append(
        OJConstructor[] a,
        OJConstructor[] b) {
        OJConstructor[] result = new OJConstructor[a.length + b.length];
        System.arraycopy(a, 0, result, 0, a.length);
        System.arraycopy(b, 0, result, a.length, b.length);
        return result;
    }

    public static final String nameForJavaClassName(String jcname) {
        if (!jcname.startsWith("["))
            return jcname;

        String stripped = stripHeadBracket(jcname);
        String result;

        if (stripped.startsWith("[")) {
            /* array of array like "[[Ljava.lang.Object;" or "[[I" */
            result = (nameForJavaClassName(stripped) + "[]");
        } else if (stripped.endsWith(";")) {
            /* array of class type like "[Ljava.lang.Object;" */
            result = (stripped.substring(1, stripped.length() - 1) + "[]");
        } else {
            /* array of primitive type like "[I" */
            switch (stripped.charAt(stripped.length() - 1)) {
                case 'Z' :
                    return "boolean[]";
                case 'B' :
                    return "byte[]";
                case 'C' :
                    return "char[]";
                case 'D' :
                    return "double[]";
                case 'F' :
                    return "float[]";
                case 'I' :
                    return "int[]";
                case 'J' :
                    return "long[]";
                case 'S' :
                    return "short[]";
                default :
                    return "<unknown primitive type>";
            }
        }
        return result.replace('$', '.');
    }

    public static final String nameToJavaClassName(String ojcname) {
        if (!ojcname.endsWith("[]"))
            return ojcname;

        String stripped = stripBrackets(ojcname);

        if (stripped.endsWith("[]")) {
            /* array of array like "java.lang.Object[][]" or "int[][]" */
            return ("[" + nameToJavaClassName(stripped));
        } else if (stripped.equals("boolean")) {
            return "[Z";
        } else if (stripped.equals("byte")) {
            return "[B";
        } else if (stripped.equals("char")) {
            return "[C";
        } else if (stripped.equals("double")) {
            return "[D";
        } else if (stripped.equals("float")) {
            return "[F";
        } else if (stripped.equals("int")) {
            return "[I";
        } else if (stripped.equals("long")) {
            return "[J";
        } else if (stripped.equals("short")) {
            return "[S";
        } else {
            return ("[L" + stripBrackets(ojcname) + ";");
        }
    }

    private static final String stripHeadBracket(String jcname) {
        return jcname.substring(1);
    }

    private static final String stripBrackets(String ojcname) {
        return ojcname.substring(0, ojcname.length() - 2);
    }

    public static final OJClass forNameAnyway(Environment env, String name) {
        name = env.toQualifiedName(name);
        OJClass result = env.lookupClass(name);
        if (result != null)
            return result;
        try {
            return OJClass.forName(name);
        } catch (OJClassNotFoundException e) {
            System.err.println(
                "OJClass.forNameAnyway() failed for : " + name + " " + e);
            System.err.print(env);
            return OJClass.forClass(Object.class);
        }
    }

    public static final OJClass[] arrayForNames(
        Environment env,
        String[] names) {
        OJClass[] result = new OJClass[names.length];
        for (int i = 0; i < result.length; ++i) {
            result[i] = forNameAnyway(env, names[i]);
        }
        return result;
    }

    public static final TypeName[] TNsForOJClasses(OJClass[] classes) {
        TypeName[] result =
            new TypeName[(classes == null) ? 0 : classes.length];
        for (int i = 0; i < result.length; ++i) {
            result[i] = TypeName.forOJClass(classes[i]);
        }
        return result;
    }

    private static final String PARAMETER_NAME = "oj_param";
    public static final ParameterList generateParameters(OJClass[] parameterTypes) {
        ParameterList result = new ParameterList();
        if (parameterTypes == null)
            return result;
        for (int i = 0; i < parameterTypes.length; ++i) {
            TypeName type = TypeName.forOJClass(parameterTypes[i]);
            Parameter param = new Parameter(type, PARAMETER_NAME + i);
            result.add(param);
        }
        return result;
    }

    public static final ParameterList generateParameters(
        OJClass[] parameterTypes,
        String[] parameterNames) {
        ParameterList result = new ParameterList();
        if (parameterTypes == null)
            return result;
        for (int i = 0; i < parameterTypes.length; ++i) {
            TypeName type = TypeName.forOJClass(parameterTypes[i]);
            Parameter param = new Parameter(type, parameterNames[i]);
            result.add(param);
        }
        return result;
    }

}