001/* Copyright (C) 2013 TU Dortmund
002 * This file is part of LearnLib, http://www.learnlib.de/.
003 *
004 * LearnLib is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License version 3.0 as published by the Free Software Foundation.
007 *
008 * LearnLib is distributed in the hope that it will be useful,
009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
011 * Lesser General Public License for more details.
012 *
013 * You should have received a copy of the GNU Lesser General Public
014 * License along with LearnLib; if not, see
015 * <http://www.gnu.de/documents/lgpl.en.html>.
016 */
017package de.learnlib.drivers.reflect;
018
019import java.lang.reflect.Method;
020import java.util.Arrays;
021import java.util.Collection;
022import java.util.HashMap;
023import java.util.Map;
024import java.util.Map.Entry;
025
026/**
027 * abstract method input, may have abstract parameters.
028 * 
029 * @author falkhowar
030 */
031public final class AbstractMethodInput {
032    
033    private final String name;
034    
035    private final Method method;
036        
037    private final Map<String, Integer> parameters;
038
039    private final Object[] values;
040
041    public AbstractMethodInput(String name, Method method, Map<String, Integer> parameters, Object[] values) {
042        this.name = name;
043        this.method = method;
044        this.parameters = parameters;
045        this.values = values;        
046    }
047        
048    public String name() {
049        return this.name;
050    }
051    
052    @Override
053    public String toString() {
054        return this.name() + Arrays.toString(this.parameters.keySet().toArray());
055    }
056    
057    public String getCall() {
058        Map<String, Object> names = new HashMap<>();
059        for (String p : getParameterNames()) {
060            names.put(p, p);
061        }
062        return this.method.getName() + Arrays.toString(getParameters(names));
063    }
064    
065    public Collection<String> getParameterNames() {
066        return this.parameters.keySet();
067    }
068    
069    public Class<?> getParameterType(String name) {
070        int id = parameters.get(name);
071        return this.method.getParameterTypes()[id];
072    }
073    
074    public Object[] getParameters(Map<String, Object> fill) {
075        Object[] ret = new Object[this.values.length];
076        System.arraycopy(this.values, 0, ret, 0, this.values.length);
077        for (Entry<String, Object> e : fill.entrySet()) {
078            Integer idx = this.parameters.get(e.getKey());
079            ret[idx] = e.getValue();
080        }        
081        return ret;
082    }
083
084    public Method getMethod() {
085        return this.method;
086    }
087    
088}