/** * Gets the name of all fields (public, private, protected, default) of the given instance or class. This includes as well all * fields (public, private, protected, default) of all its super classes. * * @param instanceOrClass the instance or class to get the fields of * @return the collection of field names of the given instance or class */ public static Collection<String> getFieldNames(final Object instanceOrClass) { if (instanceOrClass == null) return Collections.EMPTY_LIST; Class<?> clazz = getClass(instanceOrClass); Field[] fields = clazz.getDeclaredFields(); Collection<String> fieldNames = new ArrayList<String>(fields.length); for (Field field : fields) { fieldNames.add(field.getName()); } fieldNames.addAll(getFieldNames(clazz.getSuperclass())); return fieldNames; }
/** * Gets the method with the given name and parameters from the given instance or class. If instanceOrClass is a class, then we get a * static method. * * @param instanceOrClass the instance or class to get the method of * @param methodName the name of the method * @param parameterTypes the parameter-types of the method to get * @return the method * @throws NoSuchMethodException if the method could not be found */ private static Method getMethod(final Object instanceOrClass, final String methodName, final Class<?>[] parameterTypes) throws NoSuchMethodException { Class<?> type; type = getClass(instanceOrClass); Method accessMethod = getMethod(type, methodName, parameterTypes); accessMethod.setAccessible(true); return accessMethod; }
/** * Gets the signatures of all methods (public, private, protected, default) of the given instance or class. This includes as well * all methods (public, private, protected, default) of all its super classes. This does not include constructors. * * @param instanceOrClass the instance or class to get the method signatures of * @return the collection of method signatures of the given instance or class */ public static Collection<String> getMethodSignatures(final Object instanceOrClass) { if (instanceOrClass == null) return Collections.EMPTY_LIST; Class<?> clazz = getClass(instanceOrClass); Method[] methods = clazz.getDeclaredMethods(); Collection<String> methodSignatures = new ArrayList<String>(methods.length + Object.class.getDeclaredMethods().length); for (Method method : methods) { methodSignatures.add(method.getName() + "(" + getParameterTypesAsString(method.getParameterTypes()) + ")"); } methodSignatures.addAll(getMethodSignatures(clazz.getSuperclass())); return methodSignatures; }
/** * Returns a string representation of the given object. The string has the following format: "<classname> {<attributes and values>}" * whereas <attributes and values> is a comma separated list with <attributeName>=<attributeValue> <atributes and values> includes * all attributes of the objects class followed by the attributes of its superclass (if any) and so on. * * @param instanceOrClass the object or class to get a string representation of * @return a string representation of the given object */ public static String toString(final Object instanceOrClass) { Collection<String> fields = getFieldNames(instanceOrClass); if (fields.isEmpty()) return getClass(instanceOrClass).getName(); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(getClass(instanceOrClass).getName() + " {"); for (String fieldName : fields) { try { stringBuffer.append(fieldName + "=" + getValue(instanceOrClass, fieldName) + ", "); } catch (NoSuchFieldException e) { assert false : "It should always be possible to get a field that was just here"; } } stringBuffer.replace(stringBuffer.lastIndexOf(", "), stringBuffer.length(), "}"); return stringBuffer.toString(); }
/** * Return the named field from the given instance or class. Returns a static field if instanceOrClass is a class. * * @param instanceOrClass the instance or class to get the field from * @param fieldName the name of the field to get * @return the field * @throws NoSuchFieldException if no such field can be found * @throws InvalidParameterException if instanceOrClass was null */ private static Field getField(final Object instanceOrClass, final String fieldName) throws NoSuchFieldException, InvalidParameterException { if (instanceOrClass == null) throw new InvalidParameterException("Can't get field on null object/class"); Class<?> type = getClass(instanceOrClass); try { Field field = type.getDeclaredField(fieldName); field.setAccessible(true); return field; } catch (NoSuchFieldException e) { if (type.getSuperclass() == null) throw e; return getField(type.getSuperclass(), fieldName); } }