| 18 | //import jdk.nashorn.internal.runtime.ScriptFunction; |
| 19 | //import jdk.nashorn.internal.runtime.ScriptObject; |
| 20 | //import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory; |
| 21 | |
| 22 | public class Conversions { |
| 23 | |
| 24 | static SetMultimap<List<Class>, Conversion> inputs = MultimapBuilder.linkedHashKeys() |
| 25 | .linkedHashSetValues() |
| 26 | .build(); |
| 27 | static SetMultimap<List<Class>, Conversion> outputs = MultimapBuilder.linkedHashKeys() |
| 28 | .linkedHashSetValues() |
| 29 | .build(); |
| 30 | |
| 31 | /** |
| 32 | * note: this doesn't do interfaces right now |
| 33 | * <p> |
| 34 | * take a type and expand it out recursively to a flat list of parameterized types. There's no ambiguity here --- type in Java take a fixed number of parameters |
| 35 | */ |
| 36 | static public List<Class> linearize(Type c) { |
| 37 | List<Class> r = new ArrayList<>(); |
| 38 | if (c instanceof ParameterizedType) { |
| 39 | r.add((Class) ((ParameterizedType) c).getRawType()); |
| 40 | Type[] args = ((ParameterizedType) c).getActualTypeArguments(); |
| 41 | for (Type t : args) { |
| 42 | r.addAll(linearize(t)); |
| 43 | } |
| 44 | } else if (c instanceof Class) |
| 45 | r.add((Class) c); |
| 46 | else if (c instanceof WildcardType) |
| 47 | r.addAll(linearize(((WildcardType) c).getUpperBounds()[0])); |
| 48 | return r; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * the opposite of linearize. Take a flat list of parameterized types and build up a human-readable String representation of them. Filters the names of classes through "cleaner" |
| 53 | */ |
| 54 | static public String fold(List<Class> typeInformation, Function<String, String> cleaner) { |
| 55 | return typeInformation == null ? "" : _fold(new ArrayList<>(typeInformation), cleaner); |
| 56 | } |
| 57 | |
| 58 | static protected String _fold(List<Class> typeInformation, Function<String, String> cleaner) { |
| 59 | return _fold(typeInformation, cleaner, true); |
| 60 | } |
| 61 | |
| 62 | static protected String _fold(List<Class> typeInformation, Function<String, String> cleaner, boolean append) { |
| 63 | if (typeInformation == null) return ""; |
| 64 | if (typeInformation.size() == 0) return ""; |
| 65 | Class c = typeInformation.remove(0); |
| 66 | TypeVariable[] tp = c.getTypeParameters(); |
| 67 | if (tp == null || tp.length == 0) { |
| 68 | return cleaner.apply(c.getName()) + (append ? (typeInformation.size() == 0 ? "" : (", " + _fold( |
| 69 | typeInformation, cleaner))) : ""); |
| 70 | } |
| 71 | |
| 72 | if (cleaner.apply(c.getName()) |
| 73 | .equals("FunctionOfBox")) { |
| 74 | String inside = ""; |
| 75 | for (int i = 0; i < tp.length; i++) { |
| 76 | if (typeInformation.size() == 0) break; |
| 77 | inside += _fold(typeInformation, cleaner); |