OverloadedMethod implements MethodAccess { @Override public Object invokeDynamic(Object object, Object... args) { final int length = args.length; final List<MethodAccess> methodAccesses = this.methodAccessListByArgNumber.get(length); int maxScore = Integer.MIN_VALUE; MethodAccess methodAccess = null; for (MethodAccess m : methodAccesses) { int score = 1; final List<TypeType> paramTypeEnumList = m.paramTypeEnumList(); if (object == null && !m.isStatic()) { continue; } loop: for (int argIndex=0; argIndex < args.length; argIndex++) { TypeType type =paramTypeEnumList.get(argIndex); Object arg = args[argIndex]; final TypeType instanceType = TypeType.getInstanceType(arg); if (instanceType == type) { score += 2_000; continue; } switch (type){ case BYTE_WRAPPER: case BYTE: score = handleByteArg(score, arg, instanceType); break; case SHORT_WRAPPER: case SHORT: score = handleShortArg(score, arg, instanceType); break; case INTEGER_WRAPPER: case INT: score = handleIntArg(score, arg, instanceType); break; case NULL: score--; break; case LONG_WRAPPER: case LONG: score = handleLongArg(score, arg, instanceType); break; case FLOAT_WRAPPER: case FLOAT: score = handleFloatArg(score, arg, instanceType); break; case DOUBLE_WRAPPER: case DOUBLE: score = handleDoubleArg(score, arg, instanceType); break; case CHAR_WRAPPER: case CHAR: if (instanceType == TypeType.CHAR || instanceType == TypeType.CHAR_WRAPPER) { score+=1000; } break; case STRING: if (instanceType == TypeType.STRING) { score +=1_000; } else if (instanceType == TypeType.CHAR_SEQUENCE || arg instanceof CharSequence) { score +=500; } break; case INSTANCE: if (instanceType == TypeType.INSTANCE) { if (m.parameterTypes()[argIndex].isInstance(arg)){ score+=1000; } } else if (instanceType == TypeType.MAP) { score +=1_000; } else if (instanceType == TypeType.LIST) { score +=500; } break; default: if (instanceType == type) { score+=1000; } else { if (m.parameterTypes()[argIndex].isInstance(arg)){ score+=1000; } } } } if (score>maxScore) { maxScore = score; methodAccess = m; } } if (methodAccess!=null) { return methodAccess.invokeDynamic(object, args); } else { List<MethodAccess> varargMethods = this.methodAccessListByArgNumberWithVarArg.get(0); if (varargMethods!=null) { varargMethods.get(0).invokeDynamic(args); } } return null; }  OverloadedMethod add(MethodAccess methodAccess); OverloadedMethod init(); void lock(); @Override Object invokeDynamic(Object object, Object... args); @Override Object invoke(Object object, Object... args); @Override boolean isStatic(); @Override boolean isPublic(); @Override boolean isPrivate(); @Override MethodReturnAccess returnAccess(); @Override String name(); @Override Class<?> declaringType(); @Override Class<?> returnType(); @Override boolean respondsTo(Class<?>... types); @Override boolean respondsTo(Object... args); @Override Object invokeStatic(Object... args); @Override MethodAccess bind(Object instance); @Override MethodHandle methodHandle(); @Override MethodAccess methodAccess(); @Override Object bound(); @Override ConstantCallSite invokeReducerLongIntReturnLongMethodHandle(T object); @Override Method method(); @Override int score(); @Override List<TypeType> paramTypeEnumList(); Object invokeDynamicObject(final Object object, final Object args); @Override List<List<AnnotationData>> annotationDataForParams(); @Override List<MethodParamAccess> parameters(); Object invokeDynamicList(final Object object, List<?> args); @Override Class<?>[] parameterTypes(); @Override Type[] getGenericParameterTypes(); @Override Iterable<AnnotationData> annotationData(); @Override boolean hasAnnotation(String annotationName); @Override AnnotationData annotation(String annotationName); @Override int compareTo(MethodAccess o);  }
@Test public void addStringAndChar() { String str = (String) method.invokeDynamic(new SomeClass(), "a", 'b'); puts(str); assertEquals("addStringChar_a_b", str); }
@Test public void testTwoInts() { String str = (String) method.invokeDynamic(new SomeClass(), 1, 2); assertEquals("addTwoInts_1_2", str); puts(str); }
@Test public void testTwoChars() { String str = (String) method.invokeDynamic(new SomeClass(), 'a', 'b'); assertEquals("addTwoChars_a_b", str); puts(str); }
@Test public void testTwoStrings() { String str = (String) method.invokeDynamic(new SomeClass(), "a", "b"); assertEquals("addTwoStrings_a_b", str); puts(str); }
@Test public void testTwoLongs() { String str = (String) method.invokeDynamic(new SomeClass(), 1L, 1L); assertEquals("addTwoLongs1_1", str); puts(str); }
@Test public void testTwoEmployee() { String str = (String) method.invokeDynamic(new SomeClass(), new Employee(), new Employee()); puts(str); assertEquals("addTwoEmployeesEmployee{name='employee'}_Employee{name='employee'}", str); }
@Test public void testTwoEmployeeWithMap() { String str = (String) method.invokeDynamic(new SomeClass(), Maps.map("name", "emp1"), Maps.map("name", "emp2")); puts(str); assertEquals("addTwoEmployeesEmployee{name='emp1'}_Employee{name='emp2'}", str); }
@Test public void testTwoEmployeeWithList() { String str = (String) method.invokeDynamic(new SomeClass(), Lists.list("emp1"), Lists.list("emp2")); puts(str); assertEquals("addTwoEmployeesEmployee{name='emp1'}_Employee{name='emp2'}", str); }
@Test public void addCharAndString() { String str = (String) method.invokeDynamic(new SomeClass(), 'a', "b"); puts(str); assertEquals("addTwoStrings_a_b", str); }
@Test public void addIntLong() { String str = (String) method.invokeDynamic(new SomeClass(), 1, 1L); puts(str); assertEquals("addIntLong1_1", str); }
@Test public void addLongInt() { String str = (String) method.invokeDynamic(new SomeClass(), 1L, 1); puts(str); assertEquals("addTwoLongs1_1", str); }
@Test public void addLongInt2() { String str = (String) method.invokeDynamic(new SomeClass(), Long.MAX_VALUE, 1); puts(str); assertEquals("addTwoLongs9223372036854775807_1", str); }