fan-1.0 /src/sys/java/fan/sys/Method.java

Language Java Lines 635
MD5 Hash 02961f0bc5d9b445eba8fe0471cd5617 Estimated Cost $12,419 (why?)
Repository https://bitbucket.org/bedlaczech/fan-1.0 View Raw File View Project SPDX
  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
//
// Copyright (c) 2006, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
//   4 Jan 06  Brian Frank  Creation
//   9 Jul 07  Brian Frank  Split into Func
//
package fan.sys;

import java.lang.reflect.*;
import fanx.fcode.*;

/**
 * Method is an invocable operation on a Type.
 */
public class Method
  extends Slot
{

//////////////////////////////////////////////////////////////////////////
// Java Constructor
//////////////////////////////////////////////////////////////////////////

  /**
   * Constructor used by Type.reflect.
   */
  public Method(Type parent, String name, int flags, Facets facets, int lineNum, Type returns, Type inheritedReturns, List params)
  {
    this(parent, name, flags, facets, lineNum, returns, inheritedReturns, params, null);
  }

  /**
   * Constructor used by GenericType and we are given the generic
   * method that is being parameterized.
   */
  public Method(Type parent, String name, int flags, Facets facets, int lineNum, Type returns, Type inheritedReturns, List params, Method generic)
  {
    super(parent, name, flags, facets, lineNum);

    this.params = params;
    this.inheritedReturns = inheritedReturns;
    this.mask = (generic != null) ? 0 : toMask(parent, returns, params);
    this.generic = generic;
    this.func = new MethodFunc(returns);
  }

  /**
   * Compute if the method signature contains generic parameter types.
   */
  private static int toMask(Type parent, Type returns, List params)
  {
    // we only use generics in Sys
    if (parent.pod() != Sys.sysPod) return 0;

    int p = returns.isGenericParameter() ? 1 : 0;
    for (int i=0; i<params.sz(); ++i)
      p |= ((Param)params.get(i)).type.isGenericParameter() ? 1 : 0;

    int mask = 0;
    if (p != 0) mask |= GENERIC;
    return mask;
  }

//////////////////////////////////////////////////////////////////////////
// Methods
//////////////////////////////////////////////////////////////////////////

  public Type typeof() { return Sys.MethodType; }

  public Type returns() { return func.returns(); }

  public Type inheritedReturns() { return inheritedReturns; }

  public List params() { return params.ro(); }

  public Func func() { return func; }

  public String signature()
  {
    StringBuilder s = new StringBuilder();
    s.append(returns()).append(' ').append(name).append('(');
    for (int i=0; i<params.sz(); ++i)
    {
      if (i > 0) s.append(", ");
      Param p = (Param)params.get(i);
      s.append(p.type).append(' ').append(p.name);
    }
    s.append(')');
    return s.toString();
  }

  public Object trap(String name, List args)
  {
    // private undocumented access
    if (name.equals("inheritedReturnType"))
      return inheritedReturns;
    else
      return super.trap(name, args);
  }

//////////////////////////////////////////////////////////////////////////
// Generics
//////////////////////////////////////////////////////////////////////////

  /**
   * Return if this method contains generic parameters in it's signature.
   */
  public boolean isGenericMethod()
  {
    return (mask & GENERIC) != 0;
  }

  /**
   * Return if this method is the parameterization of a generic method,
   * with all the generic parameters filled in with real types.
   */
  public boolean isGenericInstance()
  {
    return generic != null;
  }

  /**
   * If isGenericInstance is true, then return the generic method which
   * this method instantiates.  The generic method may be used to access
   * the actual signatures used in the Java code (via getRawType).  If
   * this method is not a generic instance, return null.
   */
  public Method getGenericMethod()
  {
    return generic;
  }

//////////////////////////////////////////////////////////////////////////
// Call Conveniences
//////////////////////////////////////////////////////////////////////////

  public final Object callList(List args) { return func.callList(args); }
  public final Object callOn(Object target, List args) { return func.callOn(target, args); }
  public final Object call() { return func.call(); }
  public final Object call(Object a) { return func.call(a); }
  public final Object call(Object a, Object b) { return func.call(a,b); }
  public final Object call(Object a, Object b, Object c) { return func.call(a,b,c); }
  public final Object call(Object a, Object b, Object c, Object d) { return func.call(a,b,c,d); }
  public final Object call(Object a, Object b, Object c, Object d, Object e) { return func.call(a,b,c,d,e); }
  public final Object call(Object a, Object b, Object c, Object d, Object e, Object f) { return func.call(a,b,c,d,e,f); }
  public final Object call(Object a, Object b, Object c, Object d, Object e, Object f, Object g) { return func.call(a,b,c,d,e,f,g); }
  public final Object call(Object a, Object b, Object c, Object d, Object e, Object f, Object g, Object h) { return func.call(a,b,c,d,e,f,g,h); }

//////////////////////////////////////////////////////////////////////////
// MethodFunc
//////////////////////////////////////////////////////////////////////////

  class MethodFunc extends Func
  {
    MethodFunc(Type returns) { this.returns = returns; }

    public Type returns() { return returns; }
    private final Type returns;

    public long arity() { return params().size(); }

    public List params()
    {
      // lazy load functions param
      if (fparams == null)
      {
        List mparams = Method.this.params;
        List fparams = mparams;
        if ((flags & (FConst.Static|FConst.Ctor)) == 0)
        {
          Object[] temp = new Object[mparams.sz()+1];
          temp[0] = new Param("this", parent, 0);
          mparams.copyInto(temp, 1, mparams.sz());
          fparams = new List(Sys.ParamType, temp);
        }
        this.fparams = fparams.ro();
      }
      return fparams;
    }
    private List fparams;

    public Method method() { return Method.this; }

    public boolean isImmutable() { return true; }

    public Object callList(List args)
    {
      int argsSize = args == null ? 0 : args.sz();

      boolean isStatic = isStatic();
      int p = checkArgs(argsSize, isStatic, false);
      Object[] a = new Object[p];

      if (isStatic)
      {
        if (args != null && a.length > 0) args.toArray(a, 0, a.length);
        return invoke(null, a);
      }
      else
      {
        Object i = args.get(0);
        if (a.length > 0) args.toArray(a, 1, a.length);
        return invoke(i, a);
      }
    }

    public Object callOn(Object target, List args)
    {
      int argsSize = args == null ? 0 : args.sz();
      boolean javaStatic = isStatic();
      boolean fanStatic = ((flags & (FConst.Static|FConst.Ctor)) != 0);

      if (javaStatic && !fanStatic)
      {
        // if Java static doesn't match Fantom static, then this is
        // a FanXXX method which we need to call as Java static
        int p = checkArgs(argsSize, false, true);
        Object[] a = new Object[p+1];
        a[0] = target;
        if (args != null && a.length > 0) args.copyInto(a, 1, a.length-1);
        return invoke(null, a);
      }
      else
      {
        // we don't include target as part of arguments
        int p = checkArgs(argsSize, javaStatic, true);
        Object[] a = new Object[p];
        if (args != null && a.length > 0) args.toArray(a, 0, a.length);
        return invoke(target, a);
      }
    }

    public Object call()
    {
      boolean isStatic = isStatic();
      checkArgs(0, isStatic, false);
      return invoke(null, noArgs);
    }

    public Object call(Object a)
    {
      boolean isStatic = isStatic();
      int p = checkArgs(1, isStatic, false);
      Object[] args = new Object[p];
      if (isStatic)
      {
        switch (p)
        {
          case 1: args[0] = a;
        }
        return invoke(null, args);
      }
      else
      {
        return invoke(a, args);
      }
    }

    public Object call(Object a, Object b)
    {
      boolean isStatic = isStatic();
      int p = checkArgs(2, isStatic, false);
      Object[] args = new Object[p];
      if (isStatic)
      {
        switch (p)
        {
          case 2: args[1] = b;
          case 1: args[0] = a;
        }
        return invoke(null, args);
      }
      else
      {
        switch (p)
        {
          case 1: args[0] = b;
        }
        return invoke(a, args);
      }
    }

    public Object call(Object a, Object b, Object c)
    {
      boolean isStatic = isStatic();
      int p = checkArgs(3, isStatic, false);
      Object[] args = new Object[p];
      if (isStatic)
      {
        switch (p)
        {
          case 3: args[2] = c;
          case 2: args[1] = b;
          case 1: args[0] = a;
        }
        return invoke(null, args);
      }
      else
      {
        switch (p)
        {
          case 2: args[1] = c;
          case 1: args[0] = b;
        }
        return invoke(a, args);
      }
    }

    public Object call(Object a, Object b, Object c, Object d)
    {
      boolean isStatic = isStatic();
      int p = checkArgs(4, isStatic, false);
      Object[] args = new Object[p];
      if (isStatic)
      {
        switch (p)
        {
          case 4: args[3] = d;
          case 3: args[2] = c;
          case 2: args[1] = b;
          case 1: args[0] = a;
        }
        return invoke(null, args);
      }
      else
      {
        switch (p)
        {
          case 3: args[2] = d;
          case 2: args[1] = c;
          case 1: args[0] = b;
        }
        return invoke(a, args);
      }
    }

    public Object call(Object a, Object b, Object c, Object d, Object e)
    {
      boolean isStatic = isStatic();
      int p = checkArgs(5, isStatic, false);
      Object[] args = new Object[p];
      if (isStatic)
      {
        switch (p)
        {
          case 5: args[4] = e;
          case 4: args[3] = d;
          case 3: args[2] = c;
          case 2: args[1] = b;
          case 1: args[0] = a;
        }
        return invoke(null, args);
      }
      else
      {
        switch (p)
        {
          case 4: args[3] = e;
          case 3: args[2] = d;
          case 2: args[1] = c;
          case 1: args[0] = b;
        }
        return invoke(a, args);
      }
    }

    public Object call(Object a, Object b, Object c, Object d, Object e, Object f)
    {
      boolean isStatic = isStatic();
      int p = checkArgs(6, isStatic, false);
      Object[] args = new Object[p];
      if (isStatic)
      {
        switch (p)
        {
          case 6: args[5] = f;
          case 5: args[4] = e;
          case 4: args[3] = d;
          case 3: args[2] = c;
          case 2: args[1] = b;
          case 1: args[0] = a;
        }
        return invoke(null, args);
      }
      else
      {
        switch (p)
        {
          case 5: args[4] = f;
          case 4: args[3] = e;
          case 3: args[2] = d;
          case 2: args[1] = c;
          case 1: args[0] = b;
        }
        return invoke(a, args);
      }
    }

    public Object call(Object a, Object b, Object c, Object d, Object e, Object f, Object g)
    {
      boolean isStatic = isStatic();
      int p = checkArgs(7, isStatic, false);
      Object[] args = new Object[p];
      if (isStatic)
      {
        switch (p)
        {
          case 7: args[6] = g;
          case 6: args[5] = f;
          case 5: args[4] = e;
          case 4: args[3] = d;
          case 3: args[2] = c;
          case 2: args[1] = b;
          case 1: args[0] = a;
        }
        return invoke(null, args);
      }
      else
      {
        switch (p)
        {
          case 6: args[5] = g;
          case 5: args[4] = f;
          case 4: args[3] = e;
          case 3: args[2] = d;
          case 2: args[1] = c;
          case 1: args[0] = b;
        }
        return invoke(a, args);
      }
    }

    public Object call(Object a, Object b, Object c, Object d, Object e, Object f, Object g, Object h)
    {
      boolean isStatic = isStatic();
      int p = checkArgs(8, isStatic, false);
      Object[] args = new Object[p];
      if (isStatic)
      {
        switch (p)
        {
          case 8: args[7] = h;
          case 7: args[6] = g;
          case 6: args[5] = f;
          case 5: args[4] = e;
          case 4: args[3] = d;
          case 3: args[2] = c;
          case 2: args[1] = b;
          case 1: args[0] = a;
        }
        return invoke(null, args);
      }
      else
      {
        switch (p)
        {
          case 7: args[6] = h;
          case 6: args[5] = g;
          case 5: args[4] = f;
          case 4: args[3] = e;
          case 3: args[2] = d;
          case 2: args[1] = c;
          case 1: args[0] = b;
        }
        return invoke(a, args);
      }
    }

    private boolean isStatic()
    {
      try
      {
        // ensure parent has finished emitting so that reflect is populated
        parent.finish();

        // return if Java method(s) is static
        return Modifier.isStatic(reflect[0].getModifiers());
      }
      catch (Exception e)
      {
        throw Err.make("Method not mapped to java.lang.reflect correctly " + qname());
      }
    }

    private int checkArgs(int args, boolean isStatic, boolean isCallOn)
    {
      // don't check args for JavaTypes - we route these calls
      // to the JavaType to deal with method overloading
      // NOTE: we figure out how to package the arguments into a target
      // and argument array based on whether the first method overload
      // is static or not; this means that if a method is overloaded
      // with both an instance and static version that reflection may
      // not correctly work when using the callX methods
      if (parent.isJava()) return isStatic || isCallOn ? args : args - 1;

      // compuate min/max parameters - reflect contains all the method versions
      // with full params at index zero, and full defaults at reflect.length-1
      int max = this.params().sz();
      if (!isStatic) max--;
      int min = max-reflect.length+1;

      // do checking
      if (isStatic || isCallOn)
      {
        if (args < min) throw ArgErr.make("Too few arguments: " + args + " < " + min+".."+max);
      }
      else
      {
        if (args < min+1) throw ArgErr.make("Too few arguments: " + args + " < instance+" + min+".."+max);
        args--;
      }

      // return size of arguments to pass to java method
      return args <= max ? args : max;
    }
  }

//////////////////////////////////////////////////////////////////////////
// Reflection
//////////////////////////////////////////////////////////////////////////

  public int minParams()
  {
    if (minParams < 0)
    {
      int min = 0;
      for (; min<params.sz(); ++min)
        if (((Param)params.get(min)).hasDefault()) break;
      minParams = min;
    }
    return minParams;
  }

  private boolean isInstance() { return (flags & (FConst.Static|FConst.Ctor)) == 0; }

  public Object invoke(Object instance, Object[] args)
  {
    if (reflect == null) parent.finish();

    java.lang.reflect.Method jm = null;
    try
    {
      // if parent is FFI Java class, then route to JavaType for handling
      if (parent.isJava()) return JavaType.invoke(this, instance, args);

      // zero index is full signature up to using max defaults
      int index = params.sz()-args.length;
      if (parent.javaRepr() && isInstance()) index++;
      if (index < 0) index = 0;

      // route to Java reflection
      jm = reflect[index];
      if (jm == null)
      {
        fixReflect();
        jm = reflect[index];
      }
      return jm.invoke(instance, args);
    }
    catch (IllegalArgumentException e)
    {
      throw ArgErr.make(e);
    }
    catch (InvocationTargetException e)
    {
      if (e.getCause() instanceof Err)
        throw (Err)e.getCause();
      else
        throw Err.make(e.getCause());
    }
    catch (Exception e)
    {

      if (instance == null && jm != null && !java.lang.reflect.Modifier.isStatic(jm.getModifiers()))
        throw Err.make("Cannot call method '" + this + "' with null instance");

      if (reflect == null)
        throw Err.make("Method not mapped to java.lang.reflect correctly " + qname());

      /*
      System.out.println("ERROR:      " + signature());
      System.out.println("  instance: " + instance);
      System.out.println("  args:     " + (args == null ? "null" : ""+args.length));
      for (int i=0; args != null && i<args.length; ++i)
        System.out.println("    args[" + i + "] = " + args[i]);
      for (int i=0; i<reflect.length; ++i)
        System.out.println("    reflect[" + i + "] = " + reflect[i]);
      e.printStackTrace();
      */

      throw Err.make("Cannot call '" + this + "': " + e);
    }
  }

  private void fixReflect()
  {
    // this code is used to fix up our reflect table which maps
    // parameter arity to java.lang.reflect.Methods; in sys code we
    // don't necessarily override every version of a method with default
    // parameters in subclasses; so if a reflection table is incomplete
    // then we fill in missing entries from the base type's method
    try
    {
      parent.base().finish();
      Method inherit = parent.base().method(name);
      for (int i=0; i<reflect.length; ++i)
      {
        if (reflect[i] == null)
           reflect[i] = inherit.reflect[i];
      }
    }
    catch (Exception e)
    {
      System.out.println("ERROR Method.fixReflect " + qname);
      e.printStackTrace();
    }
  }

//////////////////////////////////////////////////////////////////////////
// Fields
//////////////////////////////////////////////////////////////////////////

  static final int GENERIC = 0x01;  // is this a generic method
  static final Object[] noArgs = new Object[0];

  Func func;
  List params;             // might be different from func.params is instance method
  Type inheritedReturns;   // for covariance
  int mask;
  Method generic;
  java.lang.reflect.Method[] reflect;
  private int minParams = -1;

}
Back to Top