protected String getClassType(Method method, List<Class<?>> alsoRead) { Field field = getFieldForMethod(method); if (field == null) { return "n/a"; } if (List.class.isAssignableFrom(field.getType())) { Type generic = field.getGenericType(); if (listWithSingleGenericType(generic)) { Type genericType = ((ParameterizedType) generic).getActualTypeArguments()[0]; // if the generic type is not specified, we'll just return "list of T" if (!(genericType instanceof Class<?>)) { return "List of T"; } else { Class genericClass = (Class) genericType; if (!skipForInspection(genericClass)) { alsoRead.add(genericClass); } return "List of ".concat(genericClass.getSimpleName()); } } else { return "List"; } } else { if (!skipForInspection(method.getReturnType())) { alsoRead.add(method.getReturnType()); } return method.getReturnType().getSimpleName(); } }
/** * Is this a simple type? * * @param type * @return */ protected boolean skipForInspection(Class<?> type) { return type.isEnum() || type.isArray() || type.isPrimitive() || type == Integer.class || type == Double.class || type == Byte.class || type == Boolean.class || (String.class.isAssignableFrom(type)) || type == Long.class || type == Map.class || type == Object.class; }
/** * Extract enumeration constraints * * @param method is the method to have a look at * @return a constraints string */ protected String getConstraints(Method method) { Class<?> returnType = method.getReturnType(); if (returnType.isEnum()) { return getEnumerationConstants(returnType); } Field field = this.getFieldForMethod(method); if (field != null && field.getDeclaredAnnotations().length > 0) { return getFieldAnnotationConstraints(field); } return null; }
/** * Extract the enumeration constants * * @param enumeration is the enumeration * @return is the string with constants */ protected String getEnumerationConstants(Class<?> enumeration) { Object[] constants = enumeration.getEnumConstants(); int idx = 0; StringBuffer strBuff = new StringBuffer(); for (Object konst : constants) { strBuff.append(konst.toString()); if (idx < constants.length - 1) { strBuff.append(", "); } ++idx; } return strBuff.toString(); }