Exemplo n.º 1
0
 /** {@inheritDoc} */
 public String name(int value) {
   return (String) mapValueToName.get(value);
 }
Exemplo n.º 2
0
  @Test
  public void testCallableProcToIfaceMatchIsNotOrderSensitive() throws Exception {
    final Ruby runtime = Ruby.newInstance();

    final Method list1 = java.io.File.class.getMethod("listFiles", java.io.FileFilter.class);
    final Method list2 = java.io.File.class.getMethod("listFiles", java.io.FilenameFilter.class);

    IntHashMap cache;
    JavaMethod[] methods;
    Binding binding = new Binding(new Frame(), null, new BacktraceElement());
    JavaMethod result;
    IRubyObject[] args;

    // arity 1 :

    BlockBody body1 =
        new NullBlockBody() {
          // @Override public Arity arity() { return Arity.ONE_ARGUMENT; }
          @Override
          public Signature getSignature() {
            return Signature.ONE_ARGUMENT;
          }
        };
    RubyProc dummyProc = RubyProc.newProc(runtime, new Block(body1, binding), Block.Type.PROC);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list1), new JavaMethod(runtime, list2)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list1), result);

    cache = IntHashMap.nullMap();
    args = new IRubyObject[] {dummyProc};
    result = CallableSelector.matchingCallableArityN(runtime, cache, methods, args);
    assertEquals(new JavaMethod(runtime, list1), result);

    cache = IntHashMap.nullMap();
    methods =
        new JavaMethod[] { // "reverse" method order
          new JavaMethod(runtime, list2), new JavaMethod(runtime, list1)
        };
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list1), result);

    cache = IntHashMap.nullMap();
    args = new IRubyObject[] {dummyProc};
    result = CallableSelector.matchingCallableArityN(runtime, cache, methods, args);
    assertEquals(new JavaMethod(runtime, list1), result);

    // arity 2 :

    BlockBody body2 =
        new NullBlockBody() {
          // @Override public Arity arity() { return Arity.TWO_ARGUMENTS; }
          @Override
          public Signature getSignature() {
            return Signature.TWO_ARGUMENTS;
          }
        };
    dummyProc = RubyProc.newProc(runtime, new Block(body2, binding), Block.Type.PROC);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list1), new JavaMethod(runtime, list2)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list2), result);

    cache = IntHashMap.nullMap();
    args = new IRubyObject[] {dummyProc};
    result = CallableSelector.matchingCallableArityN(runtime, cache, methods, args);
    assertEquals(new JavaMethod(runtime, list2), result);

    cache = IntHashMap.nullMap();
    methods =
        new JavaMethod[] { // "reverse" method order
          new JavaMethod(runtime, list2), new JavaMethod(runtime, list1)
        };
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list2), result);

    cache = IntHashMap.nullMap();
    args = new IRubyObject[] {dummyProc};
    result = CallableSelector.matchingCallableArityN(runtime, cache, methods, args);
    assertEquals(new JavaMethod(runtime, list2), result);

    // arity -1 :

    BlockBody body_1 = new NullBlockBody() { // arity -1
          // @Override public Arity arity() { return Arity.OPTIONAL; }
          @Override
          public Signature getSignature() {
            return Signature.OPTIONAL;
          }
        };
    dummyProc = RubyProc.newProc(runtime, new Block(body_1, binding), Block.Type.PROC);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list1), new JavaMethod(runtime, list2)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list1), result);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list2), new JavaMethod(runtime, list1)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list1), result);

    // arity -3 :

    BlockBody body_3 = new NullBlockBody() { // arity -3
          // @Override public Arity arity() { return Arity.TWO_REQUIRED; }
          @Override
          public Signature getSignature() {
            return Signature.TWO_REQUIRED;
          }
        };
    dummyProc = RubyProc.newProc(runtime, new Block(body_3, binding), Block.Type.PROC);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list1), new JavaMethod(runtime, list2)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list2), result);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list2), new JavaMethod(runtime, list1)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list2), result);

    // arity -2 :

    BlockBody body_2 = new NullBlockBody() { // arity -2 (arg1, *rest) should prefer (single)
          // @Override public Arity arity() { return Arity.ONE_REQUIRED; }
          @Override
          public Signature getSignature() {
            return Signature.ONE_REQUIRED;
          }
        };
    dummyProc = RubyProc.newProc(runtime, new Block(body_2, binding), Block.Type.PROC);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list1), new JavaMethod(runtime, list2)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list1), result);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list2), new JavaMethod(runtime, list1)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list1), result);
  }
Exemplo n.º 3
0
 /** {@inheritDoc} */
 public void add(String name, int value) {
   mapNameToValue.put(name, new Integer(value));
   mapValueToName.put(value, name);
 }