001    /*
002    @license.text@
003     */
004    package biz.hammurapi.eval;
005    
006    import java.lang.reflect.InvocationTargetException;
007    import java.lang.reflect.Method;
008    
009    
010    public class MethodEntry {
011        Object object;
012        Method method;
013        String name;
014        
015        public MethodEntry(Object object, Method method) {
016            super();
017            this.object = object;
018            this.method = method;
019            this.name = method.getName();
020        }
021        
022        public MethodEntry(Object object, Method method, String name) {
023            super();
024            this.object = object;
025            this.method = method;
026            this.name = name;
027        }
028        
029        Object invoke(Object[] args) throws EvaluationException {
030            try {
031                return method.invoke(object, args);
032            } catch (IllegalAccessException e) {
033                throw new EvaluationException(e);
034            } catch (InvocationTargetException e) {
035                throw new EvaluationException(e);
036            }
037        }
038        
039        /**
040         * @param otherEntry
041         * @return 0 - neither is more specific, 1 - this one is more specific
042         * -1 - the other one is more specific.
043         */
044        int isMoreSpecific(MethodEntry otherEntry) {
045            Class[] otherTypes = otherEntry.method.getParameterTypes();
046            Class[] types = method.getParameterTypes();
047            if (otherTypes.length!=types.length) {
048                return 0;
049            }
050                        
051            for (int i=0; i<types.length; i++) {
052                if (types[i].equals(otherTypes[i])) {
053                    continue;
054                } else if (types[i].isAssignableFrom(otherTypes[i])) {
055                    return -1;
056                } else if (otherTypes[i].isAssignableFrom(types[i])) {
057                    return 1;
058                }
059            }
060            
061            return 0;
062        }
063    }