📄 util.java
字号:
package utils;
import java.util.Properties;
import java.lang.reflect.Field;
/**
* @author rross
*/
public class Util
{
/**
* Adds a slash '/' to the end of a string if none exists
*
* NOTE: This will trim trailing and leading space as well.
*
* @param path
* @return The same string with a slash '/' appended if there was none as the last character. Can return
* null if null was passed in.
*/
public static String forceSlash(String path)
{
if (path != null && !path.trim().endsWith("/"))
return path.trim() + "/";
else
return path;
}
/**
* Updates a Properties object by prepending path to the value in the given key and putting the
* new value back in under the key.
*
* @param props
* @param key
* @param path
*/
public static void prependPath(Properties props, String key, String path)
{
props.setProperty(key, forceSlash(path) + props.getProperty(key));
}
/**
* Examines an object and creates a string representation of it's fields
* @param ob Any object
* @return a String reasonably formatted
*/
public static String toString(Object ob)
{
if (ob == null)
{
return "Object is null";
}
StringBuffer sb = new StringBuffer(ob.getClass().getName() + "\n");
try
{
Field[] fa = ob.getClass().getDeclaredFields();
for (int i = 0; i < fa.length; i++)
{
Field f = fa[i];
// Here's the majick. Override the default accessibility
// of the object
// This could be blocked by classes that are paranoid
// so don't panic if the toString fails to get values
// for every object.
f.setAccessible(true);
Object fob = fa[i].get(ob);
sb.append(" " + f.getName() + " : " + fob + "\n");
if (fob instanceof Object[])
{
Object[] oba = (Object[]) fob;
for (int j = 0; j < oba.length; j++)
{
sb.append(" " + oba[j] + "\n");
}
}
}
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
return sb.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -