| 9 | import java.lang.reflect.*; |
| 10 | |
| 11 | public class DynaFactory { |
| 12 | private Map<String, Constructor> constructors = |
| 13 | new HashMap<>(); |
| 14 | private String packageName; |
| 15 | public DynaFactory(String packageName) { |
| 16 | this.packageName = packageName; |
| 17 | } |
| 18 | @SuppressWarnings("unchecked") |
| 19 | public |
| 20 | <T extends Trash> T create(TrashInfo info) { |
| 21 | try { |
| 22 | String typename = |
| 23 | "patterns." + packageName + "." + info.type; |
| 24 | return (T)constructors.computeIfAbsent( |
| 25 | typename, this::findConstructor |
| 26 | ).newInstance(info.data); |
| 27 | } catch(Exception e) { |
| 28 | throw new RuntimeException( |
| 29 | "Cannot create() Trash: " + info, e); |
| 30 | } |
| 31 | } |
| 32 | private |
| 33 | Constructor findConstructor(String typename) { |
| 34 | try { |
| 35 | System.out.println("Loading " + typename); |
| 36 | return Class.forName(typename) |
| 37 | .getConstructor(double.class); |
| 38 | } catch(Exception e) { |
| 39 | throw new RuntimeException( |
| 40 | "Trash(double) Constructor Not Found: " + |
| 41 | typename, e); |
| 42 | } |
| 43 | } |
| 44 | } |
nothing calls this directly
no outgoing calls
no test coverage detected