Attempts to force initialization of an instance of the library interface by loading a public static field of the requisite type. Returns whether an instance variable was instantiated. Expects that lock on libraries is already held
(Class<?> cls)
| 749 | * Expects that lock on libraries is already held |
| 750 | */ |
| 751 | private static void loadLibraryInstance(Class<?> cls) { |
| 752 | if (cls != null && !libraries.containsKey(cls)) { |
| 753 | try { |
| 754 | Field[] fields = cls.getFields(); |
| 755 | for (int i=0;i < fields.length;i++) { |
| 756 | Field field = fields[i]; |
| 757 | if (field.getType() == cls |
| 758 | && Modifier.isStatic(field.getModifiers())) { |
| 759 | // Ensure the field gets initialized by reading it |
| 760 | field.setAccessible(true); // interface might be private |
| 761 | libraries.put(cls, new WeakReference<>(field.get(null))); |
| 762 | break; |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | catch (Exception e) { |
| 767 | throw new IllegalArgumentException("Could not access instance of " |
| 768 | + cls + " (" + e + ")"); |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * Find the library interface corresponding to the given class. Checks |
no test coverage detected