/** * An empty array or null is mapped to "[]" (empty array). * * @param array e.g. an int array {1, 2, 3} * @return e.g. "[ 1, 2, 3 ]" */ public static String toString(final Object[] array) { return toString("[ ", " ]", array); }
/** * Gets the message. * * @return the message * @see java.lang.Throwable#getMessage() */ @Override public String getMessage() { return Converter.toString(this.errors.keySet()) + " failed"; }
/** * @see patterntesting.concurrent.DeadLockListener#deadLockDetected(java.lang.Thread[]) */ @Override public void deadLockDetected(Thread[] threads) { if (log.isInfoEnabled()) { log.info("deadlock in " + threads.length + " threads detected: " + Converter.toString(threads)); } }
/** * To short string. * * @param obj the obj * * @return the string */ public static String toShortString(final Object obj) { if (obj == null) { return ""; } try { Class<?> clazz = (Class<?>) obj; return clazz.getSimpleName(); } catch (ClassCastException cce) { return toString(obj); } }
/** * Converts an object into its string representation. * A null object is mapped to "" (empty string). * * @param obj the obj * * @return "" if the given object is null */ public static String toString(final Object obj) { try { if (obj instanceof Object[]) { return toString((Object[]) obj); } return obj.toString(); } catch (RuntimeException e) { return ""; } }
/** * An empty array or null is mapped to "" (empty string). * * @param array e.g. an int array {1, 2, 3} * * @return e.g. "1, 2, 3" */ public static String toString(final Object[] array) { try { StringBuffer sbuf = new StringBuffer(toString(array[0])); for (int i = 1; i < array.length; i++) { sbuf.append(", "); sbuf.append(toString(array[i])); } return sbuf.toString(); } catch (RuntimeException e) { return ""; } }
private void logToMemory(final String joinPoint, final Object returnValue) { List<Object> loggedReturnValues = new ArrayList<>(); if (loggedJoinpoints.containsKey(joinPoint)) { loggedReturnValues = loggedJoinpoints.get(joinPoint); } loggedReturnValues.add(returnValue); loggedJoinpoints.put(joinPoint, loggedReturnValues); if (LOG.isTraceEnabled()) { LOG.trace("logged (" + loggedReturnValues.size() + "): {} = {}", joinPoint, Converter.toString(returnValue)); } }
private static String toAbbreviatedString(final Object obj) { String s = toString(obj); if (s.startsWith(obj.getClass().getName())) { int lengthPackagename = obj.getClass().getPackage().getName().length(); return s.substring(lengthPackagename + 1); } return StringUtils.abbreviate(s, 24); }
/** * Maps an array to its string representation. * * @param prefix the prefix, e.g. "[ " * @param postfix the postfix, e.g. " ]" * @param array e.g. an int array {1, 2, 3} * @return e.g. "[ 1, 2, 3 ]" */ public static String toString(String prefix, String postfix, Object[] array) { if (ArrayUtils.isEmpty(array)) { return prefix.trim() + postfix.trim(); } StringBuilder sbuf = new StringBuilder(prefix); sbuf.append(toString(array[0])); for (int i = 1; i < array.length; i++) { sbuf.append(", "); sbuf.append(toString(array[i])); } sbuf.append(postfix); return sbuf.toString(); }
/** * Execute batch. * * @return the int[] * @throws SQLException * the sQL exception * @see java.sql.Statement#executeBatch() */ @Override public final int[] executeBatch() throws SQLException { LogWatch watch = new LogWatch(); int[] ret = statement.executeBatch(); if (LOG.isDebugEnabled()) { LOG.debug("Batch execution returns with {} after {}.", Converter.toString(ret), watch); } return ret; }
private static void logMissingElement(Object element, Collection<?> collection) { if (LOG.isTraceEnabled()) { LOG.trace("Element \"{}\" is missing in {}.", Converter.toLongString(element), Converter.toLongString(collection)); } else if (LOG.isDebugEnabled()) { LOG.debug("Element \"{}\" is missing in {}.", Converter.toString(element), Converter.toString(collection)); } }
private static Method findMethod(final Class<?> mbeanInterface, final String name, final String... paramTypes) { try { final ClassLoader loader = mbeanInterface.getClassLoader(); final Class<?>[] paramClasses = new Class<?>[paramTypes.length]; for (int i = 0; i < paramTypes.length; i++) { paramClasses[i] = classForName(paramTypes[i], loader); } return mbeanInterface.getMethod(name, paramClasses); } catch (ClassNotFoundException ex) { LOG.error("Cannot find all parameter classes " + Converter.toString(paramTypes) + ":", ex); } catch (NoSuchMethodException ex) { LOG.error("Cannot find method '" + name + "' for " + mbeanInterface + ":", ex); } LOG.info("Will return 'null' as fallback for {} and method '{}'.", mbeanInterface, name); return null; }
private static void logMissingElement(Object element, final Collection<?> collection, final String msg) { if (LOG.isTraceEnabled()) { LOG.trace("{}: [{}] is not in {}.", msg, Converter.toLongString(element), Converter.toLongString(collection)); } else if (LOG.isDebugEnabled()) { LOG.debug("{}: [{}] is not in {}.", msg, Converter.toString(element), Converter.toString(collection)); } }
private static void logDifferentElements(int i, Object elem1, Object elem2) { if (LOG.isTraceEnabled()) { LOG.trace("{}. element differs ({} != {}).", i + 1, Converter.toLongString(elem1), Converter.toLongString(elem2)); } else if (LOG.isDebugEnabled()) { LOG.debug("{}. element differs ({} != {}).", i + 1, Converter.toString(elem1), Converter.toString(elem2)); } }
/** * Normally you'll find the local Maven repository at ~/.m2/repository. But * on cloudbees it is the local .repository directory. So we try to find out * here which one we should use. * * @return the root dir of the local Maven repository * @throws IOException * Signals that an I/O exception has occurred. */ public static File getLocalMavenRepositoryDir() throws IOException { File[] repoDirs = { getLocalRepository(), new File(".repository").getAbsoluteFile(), new File("../.repository").getAbsoluteFile() }; for (int i = 0; i < repoDirs.length; i++) { if (repoDirs[i].exists() && repoDirs[i].isDirectory()) { return repoDirs[i]; } } throw new IOException("local maven repository not found in " + Converter.toString(repoDirs)); }
private static void assertEqualSizedMaps(final Map<?, ?> m2, Map<Object, Object> m1) throws AssertionError { for (Entry<Object, Object> entry : m1.entrySet()) { Object value2 = m2.get(entry.getKey()); if (entry.getValue() == null) { if (value2 != null) { LOG.debug("Key \"{}\" has different values: null <--> {}.", entry.getKey(), Converter.toString(value2)); throw new AssertionError("key \"" + entry.getKey() + "\" has different values: null <--> " + Converter.toShortString(value2)); } } else if (value2 == null) { LOG.debug("{{}}\" is missing in {}.", entry, Converter.toString(m2)); throw new AssertionError("{" + entry + "} is missing in " + Converter.toShortString(m2)); } else if (!(value2.equals(entry.getValue()))) { LOG.debug("Key \"{}\" has different values: {} <--> {}.", entry.getKey(), Converter.toString(entry.getValue()), Converter.toString(value2)); throw new AssertionError("key \"" + entry.getKey() + "\" has different values: " + Converter.toShortString(entry.getValue()) + " <--> " + Converter.toShortString(value2)); } } }
/** * If you want to print each element of an array into a single line you can * use this method here. * * @param array * the array * @return the string * @since 1.4.2 */ public static String toLongString(final Object[] array) { if (array instanceof StackTraceElement[]) { return toLongString((StackTraceElement[]) array); } StringBuilder buf = new StringBuilder(); for (int i = 0; i < array.length; i++) { buf.append('['); buf.append(i); buf.append("]\t"); buf.append(toString(array[i])); buf.append('\n'); } return buf.toString(); }
/** * Gets the caller stacktrace. To find the real caller we ignore the first 3 * elements from the stacktrace because this is e.g. the method * {@link Thread#getStackTrace()} which is not relevant here. * * @param ignoredClasses * the ignored classes * @return the caller stacktrace */ public static StackTraceElement[] getCallerStacktrace(final Class<?>... ignoredClasses) { StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); for (int i = 3; i < stacktrace.length; i++) { String classname = stacktrace[i].getClassName(); if (!(classname.startsWith("com.sun.proxy.") || matches(classname, ignoredClasses))) { StackTraceElement[] stacktraceCaller = new StackTraceElement[stacktrace.length - i]; System.arraycopy(stacktrace, i, stacktraceCaller, 0, stacktrace.length - i); return stacktraceCaller; } } throw new IllegalStateException("no caller found for " + Converter.toString(ignoredClasses)); }
private static void assertEquals(final byte[] a1, final byte[] a2, final int i) { if (a1[i] != a2[i]) { if (LOG.isTraceEnabled()) { LOG.trace("{}. element differs ({} != {}).", i + 1, Converter.toLongString(a1[i]), Converter.toLongString(a2[i])); } else if (LOG.isDebugEnabled()) { LOG.debug("{}. element differs ({} != {}).", i + 1, Converter.toString(a1[i]), Converter.toString(a2[i])); } throw new AssertionError((i + 1) + ". element differs (" + Converter.toShortString(a1[i]) + " != " + Converter.toShortString(a2[i]) + ")"); } }
/** * Converts an object into a long representation as the normal toString * method. E.g. maps are splitted into several lines. * * @param obj * the obj * @return the string * @since 1.4 */ public static String toLongString(final Object obj) { if (obj instanceof Map<?, ?>) { return toLongString((Map<?, ?>) obj); } else if (obj instanceof Object[]) { return toLongString((Object[]) obj); } return toString(obj); }