| 1 | package com.hammurapi.extract; |
| 2 | |
| 3 | import java.lang.reflect.InvocationTargetException; |
| 4 | import java.lang.reflect.Method; |
| 5 | import java.lang.reflect.Modifier; |
| 6 | import java.util.Collections; |
| 7 | import java.util.Map; |
| 8 | import java.util.Set; |
| 9 | import java.util.TreeSet; |
| 10 | import java.util.concurrent.TimeUnit; |
| 11 | |
| 12 | public class MethodExtractor<T, V, C> extends ExtractorBase<T, V, C> implements Extractor<T, V, C> { |
| 13 | |
| 14 | private Method method; |
| 15 | private Set<Integer> parameterIndices; |
| 16 | |
| 17 | public MethodExtractor(double initialCost, TimeUnit costUnit, Method method) { |
| 18 | super(initialCost, costUnit); |
| 19 | this.method = method; |
| 20 | Set<Integer> pi = new TreeSet<Integer>(); |
| 21 | for (int i=0, l=method.getParameterTypes().length; i<l; ++i) { |
| 22 | pi.add(i); |
| 23 | } |
| 24 | parameterIndices = Collections.unmodifiableSet(pi); |
| 25 | } |
| 26 | |
| 27 | @Override |
| 28 | public Set<Integer> parameterIndices() { |
| 29 | return parameterIndices; |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public boolean isContextDependent() { |
| 34 | return Modifier.isStatic(method.getModifiers()); |
| 35 | } |
| 36 | |
| 37 | @SuppressWarnings("unchecked") |
| 38 | @Override |
| 39 | protected V extractInternal( |
| 40 | C context, |
| 41 | Map<C, Map<Extractor<T, ? super V, C>, ? super V>> cache, |
| 42 | T... obj) { |
| 43 | try { |
| 44 | return (V) method.invoke(context, obj); |
| 45 | } catch (IllegalAccessException e) { |
| 46 | throw new ExtractorException(e); |
| 47 | } catch (InvocationTargetException e) { |
| 48 | throw new ExtractorException(e); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | } |