|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.ow2.bonita.util.Misc
public abstract class Misc
Nested Class Summary | |
---|---|
static class |
Misc.NullCheckResult
Represents null value returned by findNull(Object...) . |
Field Summary | |
---|---|
static long |
DAY
Number of milleseconds in a given day |
static java.lang.String |
LINE_SEPARATOR
The line separator as defined by the property line.separator . |
static java.util.Random |
RANDOM
A RANDOM instance. |
static java.io.File |
TMP_DIR
The file that represents the temporary directory. |
Method Summary | ||
---|---|---|
static void |
badStateIfEquals(java.lang.Object a,
java.lang.Object b,
java.lang.String msg)
This method throw an IllegalStateException if the given parameters are equals (using Object.equals(Object) |
|
static void |
badStateIfFalse(boolean valueToCheck,
java.lang.String msg)
This method throw an IllegalStateException if the given parameter is false |
|
static void |
badStateIfNotNull(java.lang.Object valueToCheck,
java.lang.String msg)
This method throw an IllegalStateException if the given parameter is not null |
|
static void |
badStateIfNull(java.lang.Object valueToCheck,
java.lang.String msg)
This method throw an IllegalStateException if the given parameter is null |
|
static void |
badStateIfTrue(boolean valueToCheck,
java.lang.String msg)
This method throw an IllegalStateException if the given parameter is true |
|
static void |
checkArgsNotNull(int offset,
java.lang.Object... params)
Check that the given parameters are not null. |
|
static void |
checkArgsNotNull(java.lang.Object... params)
Check that the given parameters are not null. |
|
static
|
checkReallySerializable(T object)
Check that the given object is actually serializable. |
|
static java.lang.Exception |
close(java.io.Closeable closeable)
Equivalent to #reflectClose(closeable) |
|
static java.lang.Exception |
close(java.beans.XMLDecoder decoder)
Equivalent to {@link #reflectClose(decoder) |
|
static java.lang.Exception |
close(java.beans.XMLEncoder encoder)
Equivalent to {@link #reflectClose(encoder) |
|
static java.lang.String |
componentsToString(java.lang.Object[] args,
boolean deepToString)
Smart toString() implementation of arrays. |
|
static java.io.File |
createTempFile(java.lang.String prefix,
java.lang.String suffix,
java.io.File directory)
Perform java.io.File.createTempFile with retries when it fail (limit of 10 retries) (Use to by-pass bug #6325169 on SUN JDK 1.5 on windows) Sames parameter as java.io.File.createTempFile method |
|
static java.lang.String |
deepToString(java.lang.Object o)
|
|
static boolean |
deleteDir(java.io.File dir)
Delete a given directory and all its contents (whether they are file or directory) WARNING: this method unconditionally modifies the underlying filesystem by erasing files and directories. |
|
static java.io.Serializable |
deserialize(byte[] buf)
|
|
static void |
dynamicLog(int offset,
java.util.logging.Level level,
java.lang.String msg)
Log a message to the logger of the caller at the given offset in the stack trace. |
|
static java.util.Set<java.lang.Class<?>> |
findAllInterfaces(java.lang.Class<?> type)
Return the list of Class objects representing all interfaces a
given class implements. |
|
static java.util.Set<java.lang.Class<?>> |
findAllSuperTypes(java.lang.Class<?> type)
Return the list of Class objects representing all super type a
given class implements. |
|
static java.util.Set<java.lang.Class<?>> |
findAllTypes(java.lang.Class<?> type)
Return the list of Class objects representing every types a
given class implements. |
|
static java.lang.Class<?>[] |
findConstructorClassArgs(java.lang.Class<?>[] subClasses,
java.lang.Class<?> classToTest)
Return the Class[] array representing the types a constructor
take as parameters. |
|
static java.lang.Class<?>[] |
findMethodClassArgs(java.lang.Class<?>[] subClasses,
java.lang.Class<?> classToTest,
java.lang.String methodName)
Return the Class[] array representing the types a given method
take as parameters. |
|
static Misc.NullCheckResult |
findNull(java.lang.Object... params)
Find null parameters in the given list. |
|
static java.lang.String |
formatDelay(double delay)
Equivalent to formatDelay(long) . |
|
static java.lang.String |
formatDelay(long delay)
Format a delay. |
|
static byte[] |
generateJar(java.lang.Class<?>... classes)
|
|
static byte[] |
generateJar(java.util.Map<java.lang.String,byte[]> resources)
|
|
static byte[] |
getAllContentFrom(java.io.File file)
Equivalent to getAllContentFrom(new
FileInputStream(file)) ; |
|
static byte[] |
getAllContentFrom(org.xml.sax.InputSource source)
Equivalent to getAllContentFrom(source.getByteStream(source)) ; |
|
static byte[] |
getAllContentFrom(java.io.InputStream in)
Return the whole underlying stream content into a single String. |
|
static byte[] |
getAllContentFrom(java.net.URL url)
Return the whole underlying stream content into a single String. |
|
static java.lang.StackTraceElement |
getCaller(int offset)
Return the StackTraceElement at the given offset from this method invocation. |
|
static
|
getChainOf(java.util.List<T> elements)
Return a proxy that forward void-method invocations to each object specified in the list elements . |
|
static java.lang.String |
getCurrentThreadStackTrace()
|
|
static java.lang.String |
getGenericFullName(java.lang.Class<?> clazz)
Return the generic name of a given class. |
|
static long |
getHumanReadableId()
Generates a human readable id as a long. |
|
static java.lang.String |
getHumanReadableId(java.lang.String prefix)
Generates a human readable id prefixed by the given String. |
|
static
|
getLoggerProxyFor(T target,
java.util.logging.Logger logger)
Return a proxy that log method invocations through the provided logger. |
|
static
|
getMBeanProxy(java.lang.Class<T> mbeanInterface,
java.lang.String jmxServiceUrl,
java.lang.String jmxObjectName)
Return a proxy implementing all the interfaces specified that forward method invocations to the specified MBean. |
|
static int |
getPermissionsSize(java.security.Permissions permissions)
|
|
static java.lang.String |
getRandomString(int size)
Generate a RANDOM String of the given size. |
|
static java.util.Map<java.lang.String,byte[]> |
getResources(java.lang.Class<?>... classes)
|
|
static java.util.Map<java.lang.String,byte[]> |
getResourcesFromZip(byte[] barContent)
|
|
static java.lang.String |
getStackTraceFrom(java.lang.Throwable t)
|
|
static java.util.List<java.lang.String> |
getStringFrom(Misc.NullCheckResult nullCheckResult,
java.lang.String... names)
Return strings mapped to null values in a given @{link Misc.NullCheckResult . |
|
static java.lang.String |
getUniqueId(java.lang.String prefix)
Generate a unique identifier prefixed by the given String. |
|
static java.lang.String |
identityToString(java.lang.Object o)
Return an "identity string" for a given object. |
|
static void |
log(java.util.logging.Level level,
java.lang.String msg)
Log the given message at the given level using caller's Logger. |
|
static
|
lookup(java.lang.String name,
java.util.Hashtable<java.lang.String,java.lang.String> environment)
|
|
static java.lang.String |
prefixAllLines(java.lang.String message,
java.lang.String prefix)
Return a new string based on the given message string where all lines are prefixed by the given prefix. |
|
static java.lang.String |
primitiveComponentsToString(java.lang.Object array)
Smart toString() implementation of an array of primitive types. |
|
static int |
random(int min,
int max)
Returns a random number between min and max value |
|
static java.lang.Exception |
reflectClose(java.lang.Object o)
Invoke the close() method on the given object. |
|
static byte[] |
serialize(java.io.Serializable object)
|
|
static
|
stringToEnum(java.lang.Class<E> c,
java.lang.String s)
|
|
static void |
unreachableStatement()
|
|
static void |
unreachableStatement(java.lang.String reason)
|
|
static void |
warnIfEquals(java.util.logging.Level level,
java.lang.Object a,
java.lang.Object b)
This method logs at the given level a "warning" if the given parameter are equals |
|
static void |
warnIfFalse(java.util.logging.Level level,
boolean valueToCheck,
java.lang.String variableName)
This method logs at the given level a "warning" if the given parameter is false |
|
static void |
warnIfNotEquals(java.util.logging.Level level,
java.lang.Object a,
java.lang.Object b)
This method logs at the given level a "warning" if the given parameter are not equals |
|
static void |
warnIfNotNull(java.util.logging.Level level,
java.lang.Object valueToCheck,
java.lang.String variableName)
This method logs at the given level a "warning" if the given parameter is not null |
|
static void |
warnIfNull(java.util.logging.Level level,
java.lang.Object valueToCheck,
java.lang.String variableName)
This method logs at the given level a "warning message" if the given parameter is null |
|
static void |
warnIfTrue(java.util.logging.Level level,
boolean valueToCheck,
java.lang.String variableName)
This method logs at the given level a "warning" if the given parameter is true |
|
static void |
write(java.io.File file,
byte[] fileContent)
|
|
static void |
write(java.lang.String s,
java.io.File f)
Write the given String to the given file using the default encoding. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.util.Random RANDOM
public static final java.lang.String LINE_SEPARATOR
line.separator
.
public static final java.io.File TMP_DIR
public static final long DAY
Method Detail |
---|
public static java.lang.String getUniqueId(java.lang.String prefix)
prefix
- the prefix String
prefix
UUID
public static java.lang.String getHumanReadableId(java.lang.String prefix)
prefix
- the string prefiy
prefix
.public static long getHumanReadableId()
public static int random(int min, int max)
min
- a positive integermax
- a positive integer
java.lang.IllegalArgumentException
- if min >= max
public static java.lang.String getRandomString(int size)
size
- the size of the generated string
public static final java.lang.String formatDelay(long delay)
Format a delay.
This method returns a human readable string for delay such as the one used in benchmarks. This method is thread safe.
delay
- a long
value
String
valuepublic static final java.lang.String formatDelay(double delay)
Equivalent to formatDelay(long)
.
delay
- a double
value
String
valuepublic static java.util.Set<java.lang.Class<?>> findAllTypes(java.lang.Class<?> type)
Return the list of Class
objects representing every types a
given class implements.
type
- a Class
value
Set
valuepublic static java.lang.String getGenericFullName(java.lang.Class<?> clazz)
clazz
- the class
public static java.util.Set<java.lang.Class<?>> findAllSuperTypes(java.lang.Class<?> type)
Return the list of Class
objects representing all super type a
given class implements.
type
- a Class
value
Set
valuepublic static java.util.Set<java.lang.Class<?>> findAllInterfaces(java.lang.Class<?> type)
Return the list of Class
objects representing all interfaces a
given class implements.
type
- a Class
value
List
valuepublic static java.lang.Class<?>[] findMethodClassArgs(java.lang.Class<?>[] subClasses, java.lang.Class<?> classToTest, java.lang.String methodName) throws java.lang.NoSuchMethodException
Return the Class[]
array representing the types a given method
take as parameters.
subClasses
- the classes which are to be subclasses of parametersclassToTest
- the class which declares the given methodmethodName
- the method name
java.lang.NoSuchMethodException
- if a method cannot be found.public static java.lang.Class<?>[] findConstructorClassArgs(java.lang.Class<?>[] subClasses, java.lang.Class<?> classToTest) throws java.lang.NoSuchMethodException
Return the Class[]
array representing the types a constructor
take as parameters.
subClasses
- the classes which are to be subclasses of parametersclassToTest
- the class which declares the constructor
java.lang.NoSuchMethodException
- if a constructor cannot be foundpublic static java.lang.String identityToString(java.lang.Object o)
Return an "identity string" for a given object.
The string returned is:
"null" if o == null
- Otherwize,
o.getClass().getName() + "#" + System.identityHashCode(o)
o
- the object to return the identity string of
public static java.lang.String componentsToString(java.lang.Object[] args, boolean deepToString)
Smart toString() implementation of arrays.
args
- the array to return a smart string of.
public static java.lang.String primitiveComponentsToString(java.lang.Object array)
Smart toString() implementation of an array of primitive types.
array
- the array to return a smart string of.
public static byte[] getAllContentFrom(java.io.File file) throws java.io.IOException
getAllContentFrom(new
FileInputStream(file))
;
file
- the file to read
java.io.IOException
- If an I/O exception occuredpublic static byte[] getAllContentFrom(org.xml.sax.InputSource source) throws java.io.IOException
getAllContentFrom(source.getByteStream(source))
;
file
- the file to read
java.io.IOException
- If an I/O exception occuredpublic static byte[] getAllContentFrom(java.net.URL url) throws java.io.IOException
in
- the stream to read
java.io.IOException
- if an I/O exception occuredpublic static byte[] getAllContentFrom(java.io.InputStream in) throws java.io.IOException
in
- the stream to read
java.io.IOException
- if an I/O exception occuredpublic static java.lang.Exception close(java.io.Closeable closeable)
#reflectClose(closeable)
public static java.lang.Exception close(java.beans.XMLEncoder encoder)
public static java.lang.Exception close(java.beans.XMLDecoder decoder)
public static java.lang.Exception reflectClose(java.lang.Object o)
LOG
and returned. If the parameter is null,
nothing is done and null is returned.
o
- the object to call the close() method on.
public static int getPermissionsSize(java.security.Permissions permissions)
permissions
-
public static <T> T getMBeanProxy(java.lang.Class<T> mbeanInterface, java.lang.String jmxServiceUrl, java.lang.String jmxObjectName) throws java.io.IOException, javax.management.MalformedObjectNameException, javax.management.InstanceNotFoundException, javax.management.MBeanException, javax.management.ReflectionException
T
- mbeanInterface
- the interface the proxy should implement (the MBean
should obviously also implement that interface).jmxServiceUrl
- the JMX service URLjmxObjectName
- the name the MBean has been registered to
java.io.IOException
- for any IO problem
javax.management.MalformedObjectNameException
- for any JMX Naming problem
javax.management.MBeanException
- for any MBean problem
javax.management.ReflectionException
- for any problem related to reflection
javax.management.InstanceNotFoundException
public static <T> T getChainOf(java.util.List<T> elements)
elements
.
The invocation order follows the given list order.
T
- the interface type of the returned proxy and of each elements.elements
- the elements to forward method invocations to
Chainer
,
InvocationHandler
,
Proxy
public static <T> T getLoggerProxyFor(T target, java.util.logging.Logger logger)
T
- the target object typetarget
- the target object method invocations should be forwarded tologger
- the logger to use for logging
LoggingInvocationHandler
,
InvocationHandler
,
Proxy
public static Misc.NullCheckResult findNull(java.lang.Object... params)
Misc.NullCheckResult
.
params
- the parameters to check
Misc.NullCheckResult
representing null parameters.Misc.NullCheckResult
public static void checkArgsNotNull(java.lang.Object... params)
params
- the parameters to check
an
- IllegalArgumentException if at least one of the parameters is
nullpublic static void checkArgsNotNull(int offset, java.lang.Object... params)
offset
- the offset to use in the stack trace to produce error messageparams
- the parameters to check
an
- IllegalArgumentException if at least one of the parameters is
nullpublic static java.lang.StackTraceElement getCaller(int offset)
offset
-
public static java.util.List<java.lang.String> getStringFrom(Misc.NullCheckResult nullCheckResult, java.lang.String... names)
Misc.NullCheckResult
. .
If the returned @{link List} of String is called l
then, it
verifies: l.contains(names[i])
if and only if
nullCheckResult.isNull(i)
returns true
.
Note that the number of String names given should be of the same size that
the one used to get the given Misc.NullCheckResult
using
findNull(Object...)
. An IllegalArgumentException
is thrown
otherwise.
nullCheckResult
- the result as returned by
findNull(Object...)
names
- the strings that should be mapped to null values
Misc.NullCheckResult
.
java.lang.IllegalArgumentException
- if the number of given names is different
that NullCheckResult#size()
findNull(Object...)
,
Misc.NullCheckResult
public static void badStateIfNull(java.lang.Object valueToCheck, java.lang.String msg)
valueToCheck
- the value to checkmsg
- the message for the thrown exceptionIllegalStateException
public static void badStateIfNotNull(java.lang.Object valueToCheck, java.lang.String msg)
valueToCheck
- the value to checkmsg
- the message for the thrown exceptionIllegalStateException
public static void badStateIfTrue(boolean valueToCheck, java.lang.String msg)
valueToCheck
- the value to checkmsg
- the message for the thrown exceptionIllegalStateException
public static void badStateIfFalse(boolean valueToCheck, java.lang.String msg)
valueToCheck
- the value to checkmsg
- the message for the thrown exceptionIllegalStateException
public static void badStateIfEquals(java.lang.Object a, java.lang.Object b, java.lang.String msg)
Object.equals(Object)
a
- the first objectb
- the second objectmsg
- the message for the thrown exceptionIllegalStateException
public static void dynamicLog(int offset, java.util.logging.Level level, java.lang.String msg)
A.f()
calls B.g()
that finally calls
dynamicLog(1, msg)
then, the msg will be logged with a code
similar to: getLogger(B.getClass().getName()).log(level, msg);
dynamicLog(2, msg)
then, the code would be:getLogger(A.getClass().getName()).log(level, msg);
offset
- the offset in the stack tracelevel
- the level to log the message tomsg
- the message to logpublic static void log(java.util.logging.Level level, java.lang.String msg)
level
- a levelmsg
- the messagedynamicLog(int, Level, String)
public static void warnIfNull(java.util.logging.Level level, java.lang.Object valueToCheck, java.lang.String variableName)
level
- a log levelvalueToCheck
- the value to checkvariableName
- the variable name holding valueToCheck. Can be null.public static void warnIfNotNull(java.util.logging.Level level, java.lang.Object valueToCheck, java.lang.String variableName)
level
- a log levelvalueToCheck
- the value to checkvariableName
- the variable name holding valueToCheck. Can be null.public static void warnIfTrue(java.util.logging.Level level, boolean valueToCheck, java.lang.String variableName)
level
- a log levelvalueToCheck
- the value to checkvariableName
- the variable name holding valueToCheck. Can be null.public static void warnIfFalse(java.util.logging.Level level, boolean valueToCheck, java.lang.String variableName)
level
- a log levelvalueToCheck
- the value to checkvariableName
- the variable name holding valueToCheck. Can be null.public static void warnIfEquals(java.util.logging.Level level, java.lang.Object a, java.lang.Object b)
level
- a log levela
- an objectb
- another objectpublic static void warnIfNotEquals(java.util.logging.Level level, java.lang.Object a, java.lang.Object b)
level
- a log levela
- an objectb
- another objectpublic static java.lang.String getCurrentThreadStackTrace()
public static java.lang.String getStackTraceFrom(java.lang.Throwable t)
public static java.lang.String deepToString(java.lang.Object o)
public static boolean deleteDir(java.io.File dir)
dir
- the directory to deletepublic static void unreachableStatement()
public static void unreachableStatement(java.lang.String reason)
public static <E extends java.lang.Enum<E>> E stringToEnum(java.lang.Class<E> c, java.lang.String s)
public static byte[] serialize(java.io.Serializable object) throws java.io.IOException, java.lang.ClassNotFoundException
java.io.IOException
java.lang.ClassNotFoundException
public static java.io.Serializable deserialize(byte[] buf) throws java.io.IOException, java.lang.ClassNotFoundException
java.io.IOException
java.lang.ClassNotFoundException
public static <T extends java.io.Serializable> java.lang.Throwable checkReallySerializable(T object)
final Throwable error = checkReallySerializable(myObject);
if (error == null) {
// success
} else {
// failure, do something with error...
error.printStackTrace();
}
T
- A type (usually extending Serializable
object
- the object to check
public static void write(java.lang.String s, java.io.File f) throws java.io.IOException
s
- the string to be writtenf
- the file to write the given string to
java.io.IOException
- if an IO error is encoutered (file not found, read-only
file, and so on).public static void write(java.io.File file, byte[] fileContent) throws java.io.IOException
java.io.IOException
public static java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File directory) throws java.io.IOException
java.io.File.createTempFile
method
prefix
- Prefix of the filesuffix
- Suffix of the filedirectory
- Target directory
java.io.IOException
- If a file could not be createdpublic static byte[] generateJar(java.lang.Class<?>... classes) throws java.io.IOException
java.io.IOException
public static java.util.Map<java.lang.String,byte[]> getResources(java.lang.Class<?>... classes) throws java.io.IOException
java.io.IOException
public static byte[] generateJar(java.util.Map<java.lang.String,byte[]> resources) throws java.io.IOException
java.io.IOException
public static java.util.Map<java.lang.String,byte[]> getResourcesFromZip(byte[] barContent) throws java.io.IOException
java.io.IOException
public static <T> T lookup(java.lang.String name, java.util.Hashtable<java.lang.String,java.lang.String> environment) throws javax.naming.NamingException
javax.naming.NamingException
public static java.lang.String prefixAllLines(java.lang.String message, java.lang.String prefix)
message
- the message to transformprefix
- the prefix to use
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |