This method checks whether a given class is a linked node or not. @param clazz the class you want to check @param doublyLinked whether or not the list can be doubly linked
(Class<?> clazz, boolean doublyLinked)
| 17 | * @param doublyLinked whether or not the list <em>can</em> be doubly linked |
| 18 | */ |
| 19 | public static boolean isNode(Class<?> clazz, boolean doublyLinked) { |
| 20 | // Get fields |
| 21 | SortedSet<String> fields = Stream |
| 22 | .of(clazz.getDeclaredFields()) |
| 23 | .filter(f -> !f.isSynthetic()) |
| 24 | .map(Field::getName) |
| 25 | .collect(Collectors.toCollection(TreeSet::new)); |
| 26 | |
| 27 | boolean hasData = false; |
| 28 | int nodeFields = 0; |
| 29 | |
| 30 | // Check fields |
| 31 | for (String field : fields) { |
| 32 | Field f = null; |
| 33 | try { |
| 34 | f = clazz.getDeclaredField(field); |
| 35 | f.setAccessible(true); |
| 36 | } catch (NoSuchFieldException ex) { |
| 37 | ex.printStackTrace(); |
| 38 | fail(); |
| 39 | } |
| 40 | |
| 41 | if (f.getType().equals(clazz)) { |
| 42 | // Linked to another node |
| 43 | nodeFields++; |
| 44 | if (nodeFields == 2 && !doublyLinked) { |
| 45 | // Returns false if the list is doubly linked |
| 46 | return false; |
| 47 | } else if (nodeFields == 3) { |
| 48 | // Don't allow triply linked and up |
| 49 | return false; |
| 50 | } |
| 51 | } else if (f.getType().equals(Object.class)) { |
| 52 | // Has a generic type to store data |
| 53 | if (hasData) { |
| 54 | // Checks for multiple data fields |
| 55 | return false; |
| 56 | } |
| 57 | hasData = true; |
| 58 | } else { |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Get constructors |
| 64 | Constructor<?>[] constructors = clazz.getDeclaredConstructors(); |
| 65 | |
| 66 | // Checks arguments to the constructors |
| 67 | for (Constructor<?> c : constructors) { |
| 68 | boolean hasGenericArgument = false; |
| 69 | int nodeArguments = 0; |
| 70 | Class<?>[] paramTypes = c.getParameterTypes(); |
| 71 | for (int i = 0; i < paramTypes.length; i++) { |
| 72 | if (i == 0 && !Modifier.isStatic(clazz.getModifiers())) { |
| 73 | continue; |
| 74 | } |
| 75 | Class<?> type = paramTypes[i]; |
| 76 | if (type.equals(Object.class)) { |
no test coverage detected