public final class OptionalMethods extends Object
Example:
The JRE class "java.lang.Character
" has a method "boolean isAlphabetic(int)
", but only since Java
7, so you cannot invoke this method from your code if you compile it with JDK 6 (so that it is still runnable
in a JRE 6). To make the method available for your code, proceed as follows:
class MyClass { // ... private static final MethodWrapper1CHARACTER__IS_ALPHABETIC = OptionalMethods.get1( Character.class, // declaringClass "isAlphabetic", // methodName int.class, // parameterType RuntimeException.class // checkedException ); // ... public void m(int cp) { // Will throw an UnsupportedOperationException if this code runs in a JRE 6. if (CHARACTER__IS_ALPHABETIC.invoke(null, cp)) { // ... } // Alternatively, it is possible to check whether the wrapped method is available: if (CHARACTER__IS_ALPHABETIC.isAvailable() && CHARACTER__IS_ALPHABETIC.invoke(null, cp)) { // ... } } }
Modifier and Type | Class and Description |
---|---|
static interface |
OptionalMethods.MethodWrapper0<DC,R,EX extends Throwable>
Wrapper for a method with zero parameters.
|
static interface |
OptionalMethods.MethodWrapper1<DC,R,P,EX extends Throwable>
Wrapper for a method with one parameter.
|
static interface |
OptionalMethods.MethodWrapper2<DC,R,P1,P2,EX extends Throwable>
Wrapper for a method with two parameters.
|
Modifier and Type | Method and Description |
---|---|
static <DC,R> OptionalMethods.MethodWrapper0<DC,R,RuntimeException> |
get0(Class<DC> declaringClass,
String methodName)
Returns a wrapper for a zero-parameter method of the declaringClass, based on the
methodName.
|
static <DC,R,EX extends Throwable> |
get0(Class<DC> declaringClass,
String methodName,
Class<EX> checkedException)
Returns a wrapper for a zero-parameter method of the declaringClass, based on the
methodName.
|
static <R> OptionalMethods.MethodWrapper0<Object,R,RuntimeException> |
get0(ClassLoader declaringClassLoader,
String declaringClassName,
String methodName)
Returns a wrapper for a zero-parameter method of the declaringClass, based on the
methodName.
|
static <R,EX extends Throwable> |
get0(ClassLoader declaringClassLoader,
String declaringClassName,
String methodName,
Class<EX> checkedException)
Returns a wrapper for a zero-parameter method of the declaringClass, based on the
methodName.
|
static <DC,R,P> OptionalMethods.MethodWrapper1<DC,R,P,RuntimeException> |
get1(Class<DC> declaringClass,
String methodName,
Class<P> parameterType)
Returns a wrapper for a single-parameter method of the declaringClass, based on
methodName and parameterType.
|
static <DC,R,P,EX extends Throwable> |
get1(Class<DC> declaringClass,
String methodName,
Class<P> parameterType,
Class<EX> checkedException)
Returns a wrapper for a single-parameter method of the declaringClass, based on
methodName and parameterType.
|
static <R,P> OptionalMethods.MethodWrapper1<?,R,P,RuntimeException> |
get1(ClassLoader declaringClassLoader,
String declaringClassName,
String methodName,
Class<P> parameterType)
Returns a wrapper for a single-parameter method of the declaringClass, based on
methodName and parameterType.
|
static <R,P,EX extends Throwable> |
get1(ClassLoader declaringClassLoader,
String declaringClassName,
String methodName,
Class<P> parameterType,
Class<EX> checkedException)
Returns a wrapper for a single-parameter method of the declaringClass, based on
methodName and parameterType.
|
static <DC,R,P1,P2> |
get2(Class<DC> declaringClass,
String methodName,
Class<P1> parameterType1,
Class<P2> parameterType2)
Returns a wrapper for a two-parameter method of the declaringClass, based on
methodName, parameterType1 and parameterType2.
|
static <DC,R,P1,P2,EX extends Throwable> |
get2(Class<DC> declaringClass,
String methodName,
Class<P1> parameterType1,
Class<P2> parameterType2,
Class<EX> checkedException)
Returns a wrapper for a two-parameter method of the declaringClass, based on
methodName, parameterType1 and parameterType2.
|
static <R,P1,P2> OptionalMethods.MethodWrapper2<?,R,P1,P2,RuntimeException> |
get2(ClassLoader declaringClassLoader,
String declaringClassName,
String methodName,
Class<P1> parameterType1,
Class<P2> parameterType2)
Returns a wrapper for a two-parameter method of the declaringClass, based on
methodName, parameterType1 and parameterType2.
|
static <R,P1,P2,EX extends Throwable> |
get2(ClassLoader declaringClassLoader,
String declaringClassName,
String methodName,
Class<P1> parameterType1,
Class<P2> parameterType2,
Class<EX> checkedException)
Returns a wrapper for a two-parameter method of the declaringClass, based on
methodName, parameterType1 and parameterType2.
|
public static <R> OptionalMethods.MethodWrapper0<Object,R,RuntimeException> get0(@Nullable ClassLoader declaringClassLoader, String declaringClassName, String methodName)
UnsupportedOperationException
when invoked.R
- The return type of the methodpublic static <R,EX extends Throwable> OptionalMethods.MethodWrapper0<Object,R,EX> get0(@Nullable ClassLoader declaringClassLoader, String declaringClassName, String methodName, @Nullable Class<EX> checkedException)
UnsupportedOperationException
when invoked.R
- The return type of the methodEX
- The (single) checked exception declared for the methodpublic static <DC,R> OptionalMethods.MethodWrapper0<DC,R,RuntimeException> get0(Class<DC> declaringClass, String methodName)
UnsupportedOperationException
when invoked.DC
- The class that declares the methodR
- The return type of the methodpublic static <DC,R,EX extends Throwable> OptionalMethods.MethodWrapper0<DC,R,EX> get0(Class<DC> declaringClass, String methodName, @Nullable Class<EX> checkedException)
UnsupportedOperationException
when invoked.DC
- The class that declares the methodR
- The return type of the methodEX
- The (single) checked exception declared for the methodpublic static <R,P> OptionalMethods.MethodWrapper1<?,R,P,RuntimeException> get1(@Nullable ClassLoader declaringClassLoader, String declaringClassName, String methodName, Class<P> parameterType)
UnsupportedOperationException
when invoked.R
- The return type of the wrapped methodP
- The type of the single parameter of the wrapped methodpublic static <R,P,EX extends Throwable> OptionalMethods.MethodWrapper1<?,R,P,EX> get1(@Nullable ClassLoader declaringClassLoader, String declaringClassName, String methodName, Class<P> parameterType, @Nullable Class<EX> checkedException)
UnsupportedOperationException
when invoked.R
- The return type of the wrapped methodP
- The type of the single parameter of the wrapped methodEX
- The (single) checked exception declared for the methodpublic static <DC,R,P> OptionalMethods.MethodWrapper1<DC,R,P,RuntimeException> get1(Class<DC> declaringClass, String methodName, Class<P> parameterType)
UnsupportedOperationException
when invoked.DC
- The class that declares the wrapped methodR
- The return type of the wrapped methodP
- The type of the single parameter of the wrapped methodpublic static <DC,R,P,EX extends Throwable> OptionalMethods.MethodWrapper1<DC,R,P,EX> get1(Class<DC> declaringClass, String methodName, Class<P> parameterType, @Nullable Class<EX> checkedException)
UnsupportedOperationException
when invoked.DC
- The class that declares the wrapped methodR
- The return type of the wrapped methodP
- The type of the single parameter of the wrapped methodEX
- The (single) checked exception declared for the methodpublic static <R,P1,P2> OptionalMethods.MethodWrapper2<?,R,P1,P2,RuntimeException> get2(@Nullable ClassLoader declaringClassLoader, String declaringClassName, String methodName, Class<P1> parameterType1, Class<P2> parameterType2)
UnsupportedOperationException
when invoked.R
- The return type of the wrapped methodP1
- The type of the first parameter of the wrapped methodP2
- The type of the second parameter of the wrapped methodpublic static <R,P1,P2,EX extends Throwable> OptionalMethods.MethodWrapper2<?,R,P1,P2,EX> get2(@Nullable ClassLoader declaringClassLoader, String declaringClassName, String methodName, Class<P1> parameterType1, Class<P2> parameterType2, @Nullable Class<EX> checkedException)
UnsupportedOperationException
when invoked.R
- The return type of the wrapped methodP1
- The type of the first parameter of the wrapped methodP2
- The type of the second parameter of the wrapped methodEX
- The (single) checked exception declared for the methodpublic static <DC,R,P1,P2> OptionalMethods.MethodWrapper2<DC,R,P1,P2,RuntimeException> get2(Class<DC> declaringClass, String methodName, Class<P1> parameterType1, Class<P2> parameterType2)
UnsupportedOperationException
when invoked.DC
- The class that declares the wrapped methodR
- The return type of the wrapped methodP1
- The type of the first parameter of the wrapped methodP2
- The type of the second parameter of the wrapped methodpublic static <DC,R,P1,P2,EX extends Throwable> OptionalMethods.MethodWrapper2<DC,R,P1,P2,EX> get2(Class<DC> declaringClass, String methodName, Class<P1> parameterType1, Class<P2> parameterType2, @Nullable Class<EX> checkedException)
UnsupportedOperationException
when invoked.DC
- The class that declares the wrapped methodR
- The return type of the wrapped methodP1
- The type of the first parameter of the wrapped methodP2
- The type of the second parameter of the wrapped methodEX
- The (single) checked exception declared for the methodCopyright © 2018 Arno Unkrig. All rights reserved.