public class Compat
extends java.lang.Object
Vona's java compatibility utilities.
Copyright (C) 2005, Marsette A. Vona, III
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Modifier and Type | Field and Description |
---|---|
private static boolean |
checked15
Memo for
is15() . |
private static boolean |
checked16
Memo for
is16() . |
private static boolean |
checkedLinux
Memo for
isLinux() . |
private static boolean |
checkedOSX
Memo for
isOSX() . |
private static boolean |
checkedWindows
Memo for
isWindows() . |
static java.lang.ClassLoader |
classLoader
The class loader to use, if null then
Class.forName() . |
private static java.lang.String |
cvsid
CVS id.
|
private static boolean |
osLinux
Memo for
isLinux() . |
private static boolean |
osOSX
Memo for
isOSX() . |
private static boolean |
osWindows
Memo for
isWindows() . |
private static java.io.File |
tempDir
Memo for
getTempDir() . |
private static boolean |
version15
Memo for
is15() . |
private static boolean |
version16
Memo for
is16() . |
Constructor and Description |
---|
Compat() |
Modifier and Type | Method and Description |
---|---|
static java.lang.Object |
construct(java.lang.String className)
Covers
construct(String, Object[]) , calls the no-arg
constructor. |
static java.lang.Object |
construct(java.lang.String className,
java.lang.Object[] arg)
Covers
construct(String, Object[], String[]) , infers argument
types. |
static java.lang.Object |
construct(java.lang.String className,
java.lang.Object[] arg,
java.lang.String[] argTypeName)
Attempt to make an instance of a class that may or may not exist and
which may or may not have a constructor matching the specified
arguments.
|
static java.lang.Object |
constructSafely(java.lang.String className)
Covers
construct(String) , swallows exceptions, returns null on
failure. |
static java.lang.Object |
constructSafely(java.lang.String className,
java.lang.Object[] arg)
Covers
construct(String, Object[]) , swallows exceptions,
returns null on failure. |
static java.lang.Object |
constructSafely(java.lang.String className,
java.lang.Object[] arg,
java.lang.String[] argTypeName)
Covers
construct(String, Object[], String[]) , swallows
exceptions, returns null on failure. |
static void |
dumpPlatformProperties()
dumpPlatformProperties(PrintStream) to
System.out . |
static void |
dumpPlatformProperties(java.io.PrintStream out)
Dump the platform identifying system properties.
|
static java.lang.String |
getPropertySafely(java.lang.String key)
Get a system property, handling any runtime exception.
|
static java.lang.String |
getSimpleName(java.lang.Class clazz)
Get the simple name of a class.
|
static java.io.File |
getTempDir()
Get the temp dir on this platform.
|
static java.lang.Object |
invoke(java.lang.Object obj,
java.lang.String methodName)
Covers
invoke(Object, String, Object[]) , uses no
arguments. |
static java.lang.Object |
invoke(java.lang.Object obj,
java.lang.String methodName,
java.lang.Object[] arg)
Covers
invoke(Object, String, Object[], String[]) , infers
argument types. |
static java.lang.Object |
invoke(java.lang.Object obj,
java.lang.String methodName,
java.lang.Object[] arg,
java.lang.String[] argTypeName)
Covers
invoke(String, Object, String, Object[], String[]) ,
infers class name from obj . |
protected static java.lang.Object |
invoke(java.lang.String className,
java.lang.Object obj,
java.lang.String methodName,
java.lang.Object[] arg,
java.lang.String[] argTypeName)
Attempt to call a static or instance method that may or may not
exist.
|
static java.lang.Object |
invoke(java.lang.String className,
java.lang.String methodName)
Covers
invoke(String, String, Object[]) , uses no
arguments. |
static java.lang.Object |
invoke(java.lang.String className,
java.lang.String methodName,
java.lang.Object[] arg)
Covers
invoke(String, String, Object[], String[]) , infers
argument types. |
static java.lang.Object |
invoke(java.lang.String className,
java.lang.String methodName,
java.lang.Object[] arg,
java.lang.String[] argTypeName)
Covers
invoke(String, Object, String, Object[], String[]) ,
uses null obj , thus calling a static method. |
static java.lang.Object |
invokeSafely(java.lang.Object obj,
java.lang.String methodName)
Covers
invoke(Object, String) , swallows exceptions, returns
null on failure. |
static java.lang.Object |
invokeSafely(java.lang.Object obj,
java.lang.String methodName,
java.lang.Object[] arg)
Covers
invoke(Object, String, Object[]) , swallows exceptions,
returns null on failure. |
static java.lang.Object |
invokeSafely(java.lang.Object obj,
java.lang.String methodName,
java.lang.Object[] arg,
java.lang.String[] argTypeName)
Covers
invoke(Object, String, Object[], String[]) , swallows
exceptions, returns null on failure. |
static java.lang.Object |
invokeSafely(java.lang.String className,
java.lang.String methodName)
Covers
invoke(String, String) swallows exceptions, returns
null on failure. |
static java.lang.Object |
invokeSafely(java.lang.String className,
java.lang.String methodName,
java.lang.Object[] arg)
Covers
invoke(String, String, Object[]) swallows exceptions,
returns null on failure. |
static java.lang.Object |
invokeSafely(java.lang.String className,
java.lang.String methodName,
java.lang.Object[] arg,
java.lang.String[] argTypeName)
Covers
invoke(String, String, Object[], String[]) swallows
exceptions, returns null on failure. |
static boolean |
is15()
Check if we're running with a 1.5 JRE.
|
static boolean |
is16()
Check if we're running with a 1.6 JRE.
|
static boolean |
isLinux()
Check if we're running on Linux.
|
static boolean |
isOSX()
Check if we're running on OS X.
|
static boolean |
isWindows()
Check if we're running on Windows.
|
static void |
main(java.lang.String[] arg)
Test driver dumps dbg to System.out.
|
protected static java.lang.Class[] |
makeArgType(java.lang.Object[] arg,
java.lang.String[] argTypeName)
Collect argument types.
|
static void |
setLocationByPlatform(java.awt.Window window,
boolean value)
Request a change to the location-by-platform status of a frame.
|
private static final java.lang.String cvsid
CVS id.
private static boolean checkedLinux
Memo for isLinux()
.
private static boolean osLinux
Memo for isLinux()
.
private static boolean checkedWindows
Memo for isWindows()
.
private static boolean osWindows
Memo for isWindows()
.
private static boolean checkedOSX
Memo for isOSX()
.
private static boolean osOSX
Memo for isOSX()
.
private static boolean checked15
Memo for is15()
.
private static boolean version15
Memo for is15()
.
private static boolean checked16
Memo for is16()
.
private static boolean version16
Memo for is16()
.
private static java.io.File tempDir
Memo for getTempDir()
.
public static java.lang.ClassLoader classLoader
The class loader to use, if null then Class.forName()
.
public static java.lang.Object construct(java.lang.String className) throws java.lang.reflect.InvocationTargetException
Covers construct(String, Object[])
, calls the no-arg
constructor.
java.lang.reflect.InvocationTargetException
public static java.lang.Object constructSafely(java.lang.String className)
Covers construct(String)
, swallows exceptions, returns null on
failure.
public static java.lang.Object construct(java.lang.String className, java.lang.Object[] arg) throws java.lang.reflect.InvocationTargetException
Covers construct(String, Object[], String[])
, infers argument
types.
java.lang.reflect.InvocationTargetException
public static java.lang.Object constructSafely(java.lang.String className, java.lang.Object[] arg)
Covers construct(String, Object[])
, swallows exceptions,
returns null on failure.
public static java.lang.Object construct(java.lang.String className, java.lang.Object[] arg, java.lang.String[] argTypeName) throws java.lang.reflect.InvocationTargetException
Attempt to make an instance of a class that may or may not exist and which may or may not have a constructor matching the specified arguments.
className
- the full name (i.e. with package) of the class to
instantiate, not null. Resolved to a class via
Class.forName()
, unless classLoader
is non-null.arg
- see makeArgType(java.lang.Object[], java.lang.String[])
argTypeName
- see makeArgType(java.lang.Object[], java.lang.String[])
java.lang.reflect.InvocationTargetException
- wrapping another exception if the
class was not found or was inaccessible, did not have a matching
constructor, an argument type was not found, or the constructor threw an
exceptionpublic static java.lang.Object constructSafely(java.lang.String className, java.lang.Object[] arg, java.lang.String[] argTypeName)
Covers construct(String, Object[], String[])
, swallows
exceptions, returns null on failure.
protected static java.lang.Class[] makeArgType(java.lang.Object[] arg, java.lang.String[] argTypeName) throws java.lang.reflect.InvocationTargetException
Collect argument types.
arg
- the list of arguments, if null a zero-length argument list is
implied. If non-null, all elements must be non-null.argTypeName
- the full names of the types of the arguments, to be
resolved by Class.forName()
, unless classLoader
is
non-null. If argTypeName
is null the argument types will be
inferred from the types of the elements of arg
, but in that
case all the latter must be non-null (arg
itself may still be
null, indicating no arguments)java.lang.reflect.InvocationTargetException
- wrapping a ClassNotFoundException if
any of the argument classes could not be foundpublic static java.lang.Object invoke(java.lang.Object obj, java.lang.String methodName) throws java.lang.reflect.InvocationTargetException
Covers invoke(Object, String, Object[])
, uses no
arguments.
java.lang.reflect.InvocationTargetException
public static java.lang.Object invokeSafely(java.lang.Object obj, java.lang.String methodName)
Covers invoke(Object, String)
, swallows exceptions, returns
null on failure.
public static java.lang.Object invoke(java.lang.Object obj, java.lang.String methodName, java.lang.Object[] arg) throws java.lang.reflect.InvocationTargetException
Covers invoke(Object, String, Object[], String[])
, infers
argument types.
java.lang.reflect.InvocationTargetException
public static java.lang.Object invokeSafely(java.lang.Object obj, java.lang.String methodName, java.lang.Object[] arg)
Covers invoke(Object, String, Object[])
, swallows exceptions,
returns null on failure.
public static java.lang.Object invoke(java.lang.Object obj, java.lang.String methodName, java.lang.Object[] arg, java.lang.String[] argTypeName) throws java.lang.reflect.InvocationTargetException
Covers invoke(String, Object, String, Object[], String[])
,
infers class name from obj
.
java.lang.reflect.InvocationTargetException
public static java.lang.Object invokeSafely(java.lang.Object obj, java.lang.String methodName, java.lang.Object[] arg, java.lang.String[] argTypeName)
Covers invoke(Object, String, Object[], String[])
, swallows
exceptions, returns null on failure.
protected static java.lang.Object invoke(java.lang.String className, java.lang.Object obj, java.lang.String methodName, java.lang.Object[] arg, java.lang.String[] argTypeName) throws java.lang.reflect.InvocationTargetException
Attempt to call a static or instance method that may or may not exist.
className
- the full name (i.e. with package) of the class on which
to call the method. If null then inferred from obj
, which
must not be null in that case. Resolved to a class via
Class.forName()
, unless classLoader
is non-null.obj
- the object against which to call an instance method. If null
then a static method is called, and in that case className
must be non-null. If both className
and obj
are
specified they must be consistent.methodName
- the name of the method to call, not nullarg
- see makeArgType(java.lang.Object[], java.lang.String[])
argTypeName
- see makeArgType(java.lang.Object[], java.lang.String[])
java.lang.reflect.InvocationTargetException
- wrapping another exception if the
class was not found or was inaccessible, did not have a matching method,
an argument type was not found, or the method threw an exceptionpublic static java.lang.Object invoke(java.lang.String className, java.lang.String methodName) throws java.lang.reflect.InvocationTargetException
Covers invoke(String, String, Object[])
, uses no
arguments.
java.lang.reflect.InvocationTargetException
public static java.lang.Object invokeSafely(java.lang.String className, java.lang.String methodName)
Covers invoke(String, String)
swallows exceptions, returns
null on failure.
public static java.lang.Object invoke(java.lang.String className, java.lang.String methodName, java.lang.Object[] arg) throws java.lang.reflect.InvocationTargetException
Covers invoke(String, String, Object[], String[])
, infers
argument types.
java.lang.reflect.InvocationTargetException
public static java.lang.Object invokeSafely(java.lang.String className, java.lang.String methodName, java.lang.Object[] arg)
Covers invoke(String, String, Object[])
swallows exceptions,
returns null on failure.
public static java.lang.Object invoke(java.lang.String className, java.lang.String methodName, java.lang.Object[] arg, java.lang.String[] argTypeName) throws java.lang.reflect.InvocationTargetException
Covers invoke(String, Object, String, Object[], String[])
,
uses null obj
, thus calling a static method.
java.lang.reflect.InvocationTargetException
public static java.lang.Object invokeSafely(java.lang.String className, java.lang.String methodName, java.lang.Object[] arg, java.lang.String[] argTypeName)
Covers invoke(String, String, Object[], String[])
swallows
exceptions, returns null on failure.
public static boolean isLinux()
Check if we're running on Linux.
Memoized.
public static boolean isWindows()
Check if we're running on Windows.
Memoized.
public static boolean isOSX()
Check if we're running on OS X.
Memoized.
public static boolean is15()
Check if we're running with a 1.5 JRE.
Memoized.
public static boolean is16()
Check if we're running with a 1.6 JRE.
Memoized.
public static java.lang.String getSimpleName(java.lang.Class clazz)
Get the simple name of a class.
This is a new API in 1.5.
The simple name is the class name without any package.
clazz
- the Classclazz
public static void setLocationByPlatform(java.awt.Window window, boolean value)
Request a change to the location-by-platform status of a frame.
This is a new API in 1.5.
If the runtime JRE is not 1.5 then this call will have no effect.
window
- the window for which to change the location-by-platform
statusvalue
- the new statuspublic static void dumpPlatformProperties()
dumpPlatformProperties(PrintStream)
to
System.out
.
public static void dumpPlatformProperties(java.io.PrintStream out)
Dump the platform identifying system properties.
out
- the dump destinationpublic static java.lang.String getPropertySafely(java.lang.String key)
Get a system property, handling any runtime exception.
key
- the property keypublic static java.io.File getTempDir()
Get the temp dir on this platform.
Attempts to return the persistent temp dir (i.e. /var/tmp) when possible.
Attempts to verify that the returned directory exists and is writeable; however if the runtime security manager doesn't allow this to be determined it makes a best guess.
public static void main(java.lang.String[] arg)
Test driver dumps dbg to System.out.