You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2021/03/03 18:09:10 UTC
[maven-surefire] branch master updated: refactoring of returned
values of methods in ReflectionUtils.java
This is an automated email from the ASF dual-hosted git repository.
tibordigana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git
The following commit(s) were added to refs/heads/master by this push:
new 8c18e5e refactoring of returned values of methods in ReflectionUtils.java
8c18e5e is described below
commit 8c18e5e24e42a5f438b7ee70bbd52d8bed02d922
Author: tibordigana <ti...@gmail.com>
AuthorDate: Wed Mar 3 19:06:48 2021 +0100
refactoring of returned values of methods in ReflectionUtils.java
---
.../plugin/surefire/AbstractSurefireMojo.java | 2 +-
.../maven/surefire/api/util/ReflectionUtils.java | 40 +++++++++++++---------
.../apache/maven/surefire/booter/ForkedBooter.java | 2 +-
.../maven/surefire/booter/ProviderFactory.java | 7 ++--
.../maven/surefire/booter/SurefireReflector.java | 8 ++---
.../apache/maven/surefire/booter/SystemUtils.java | 5 +--
.../surefire/common/junit3/JUnit3Reflector.java | 14 ++++----
.../surefire/common/junit4/JUnit4Reflector.java | 6 ++--
8 files changed, 45 insertions(+), 39 deletions(-)
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
index 7ae8486..0db4e6b 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
@@ -970,7 +970,7 @@ public abstract class AbstractSurefireMojo
if ( getToolchainsMethod != null )
{
//noinspection unchecked
- List<Toolchain> tcs = (List<Toolchain>) invokeMethodWithArray( toolchainManager,
+ List<Toolchain> tcs = invokeMethodWithArray( toolchainManager,
getToolchainsMethod, session, "jdk", toolchainArgs );
if ( tcs.isEmpty() )
{
diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/ReflectionUtils.java b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/ReflectionUtils.java
index de47bb4..48b4032 100644
--- a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/ReflectionUtils.java
+++ b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/ReflectionUtils.java
@@ -65,12 +65,12 @@ public final class ReflectionUtils
}
}
- public static Object invokeGetter( Object instance, String methodName )
+ public static <T> T invokeGetter( Object instance, String methodName )
{
return invokeGetter( instance.getClass(), instance, methodName );
}
- public static Object invokeGetter( Class<?> instanceType, Object instance, String methodName )
+ public static <T> T invokeGetter( Class<?> instanceType, Object instance, String methodName )
{
Method method = getMethod( instanceType, methodName );
return invokeMethodWithArray( instance, method );
@@ -88,11 +88,12 @@ public final class ReflectionUtils
}
}
- public static Object newInstance( Constructor<?> constructor, Object... params )
+ public static <T> T newInstance( Constructor<?> constructor, Object... params )
{
try
{
- return constructor.newInstance( params );
+ //noinspection unchecked
+ return (T) constructor.newInstance( params );
}
catch ( ReflectiveOperationException e )
{
@@ -113,14 +114,15 @@ public final class ReflectionUtils
}
}
- public static Object instantiateOneArg( ClassLoader classLoader, String className, Class<?> param1Class,
+ public static <T> T instantiateOneArg( ClassLoader classLoader, String className, Class<?> param1Class,
Object param1 )
{
try
{
Class<?> aClass = loadClass( classLoader, className );
Constructor<?> constructor = getConstructor( aClass, param1Class );
- return constructor.newInstance( param1 );
+ //noinspection unchecked
+ return (T) constructor.newInstance( param1 );
}
catch ( InvocationTargetException e )
{
@@ -138,16 +140,17 @@ public final class ReflectionUtils
invokeSetter( o, setter, value );
}
- public static Object invokeSetter( Object target, Method method, Object value )
+ public static <T> T invokeSetter( Object target, Method method, Object value )
{
return invokeMethodWithArray( target, method, value );
}
- public static Object invokeMethodWithArray( Object target, Method method, Object... args )
+ public static <T> T invokeMethodWithArray( Object target, Method method, Object... args )
{
try
{
- return method.invoke( target, args );
+ //noinspection unchecked
+ return (T) method.invoke( target, args );
}
catch ( IllegalAccessException e )
{
@@ -159,12 +162,13 @@ public final class ReflectionUtils
}
}
- public static Object invokeMethodWithArray2( Object target, Method method, Object... args )
+ public static <T> T invokeMethodWithArray2( Object target, Method method, Object... args )
throws InvocationTargetException
{
try
{
- return method.invoke( target, args );
+ //noinspection unchecked
+ return (T) method.invoke( target, args );
}
catch ( IllegalAccessException e )
{
@@ -172,7 +176,7 @@ public final class ReflectionUtils
}
}
- public static Object instantiateObject( String className, Class<?>[] types, Object[] params, ClassLoader cl )
+ public static <T> T instantiateObject( String className, Class<?>[] types, Object[] params, ClassLoader cl )
{
Class<?> clazz = loadClass( cl, className );
final Constructor<?> constructor = getConstructor( clazz, types );
@@ -222,8 +226,8 @@ public final class ReflectionUtils
* @throws SurefireReflectionException if the method could not be called or threw an exception.
* It has original cause Exception.
*/
- public static Object invokeStaticMethod( Class<?> clazz, String methodName,
- Class<?>[] parameterTypes, Object[] parameters )
+ public static <T> T invokeStaticMethod( Class<?> clazz, String methodName,
+ Class<?>[] parameterTypes, Object[] parameters )
{
if ( parameterTypes.length != parameters.length )
{
@@ -242,7 +246,7 @@ public final class ReflectionUtils
* @return successfully returned value from the last method call; {@code fallback} otherwise
* @throws IllegalArgumentException if {@code classes} and {@code noArgMethodNames} have different array length
*/
- public static Object invokeMethodChain( Class<?>[] classesChain, String[] noArgMethodNames, Object fallback )
+ public static <T> T invokeMethodChain( Class<?>[] classesChain, String[] noArgMethodNames, Object fallback )
{
if ( classesChain.length != noArgMethodNames.length )
{
@@ -264,11 +268,13 @@ public final class ReflectionUtils
obj = invokeMethodWithArray( obj, method );
}
}
- return obj;
+ //noinspection unchecked
+ return (T) obj;
}
catch ( RuntimeException e )
{
- return fallback;
+ //noinspection unchecked
+ return (T) fallback;
}
}
}
diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
index f00cb6a..d170237 100644
--- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
+++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
@@ -496,7 +496,7 @@ public final class ForkedBooter
bpf.setSkipAfterFailureCount( providerConfiguration.getSkipAfterFailureCount() );
bpf.setSystemExitTimeout( providerConfiguration.getSystemExitTimeout() );
String providerClass = startupConfiguration.getActualClassName();
- return (SurefireProvider) instantiateOneArg( classLoader, providerClass, ProviderParameters.class, bpf );
+ return instantiateOneArg( classLoader, providerClass, ProviderParameters.class, bpf );
}
/**
diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ProviderFactory.java b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ProviderFactory.java
index 6003419..86b430e 100644
--- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ProviderFactory.java
+++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ProviderFactory.java
@@ -51,9 +51,9 @@ public class ProviderFactory
private final Object reporterManagerFactory;
- private static final Class[] INVOKE_PARAMETERS = { Object.class };
+ private static final Class<?>[] INVOKE_PARAMETERS = { Object.class };
- private static final Class[] INVOKE_EMPTY_PARAMETER_TYPES = { };
+ private static final Class<?>[] INVOKE_EMPTY_PARAMETER_TYPES = { };
private static final Object[] INVOKE_EMPTY_PARAMETERS = { };
@@ -136,13 +136,12 @@ public class ProviderFactory
}
@Override
- @SuppressWarnings( "unchecked" )
public Iterable<Class<?>> getSuites()
{
ClassLoader current = swapClassLoader( testsClassLoader );
try
{
- return (Iterable<Class<?>>) invokeGetter( providerInOtherClassLoader, "getSuites" );
+ return invokeGetter( providerInOtherClassLoader, "getSuites" );
}
finally
{
diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireReflector.java b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireReflector.java
index b10b322..d4f459a 100644
--- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireReflector.java
+++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireReflector.java
@@ -114,10 +114,10 @@ public final class SurefireReflector
{
return result;
}
- int getCompletedCount1 = (Integer) invokeGetter( result, "getCompletedCount" );
- int getErrors = (Integer) invokeGetter( result, "getErrors" );
- int getSkipped = (Integer) invokeGetter( result, "getSkipped" );
- int getFailures = (Integer) invokeGetter( result, "getFailures" );
+ int getCompletedCount1 = invokeGetter( result, "getCompletedCount" );
+ int getErrors = invokeGetter( result, "getErrors" );
+ int getSkipped = invokeGetter( result, "getSkipped" );
+ int getFailures = invokeGetter( result, "getFailures" );
return new RunResult( getCompletedCount1, getErrors, getFailures, getSkipped );
}
diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemUtils.java b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemUtils.java
index 40ba232..ab7538a 100644
--- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemUtils.java
+++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemUtils.java
@@ -36,6 +36,7 @@ import java.util.StringTokenizer;
import static java.lang.Character.isDigit;
import static java.lang.Thread.currentThread;
import static java.util.Objects.requireNonNull;
+import static org.apache.maven.surefire.api.util.ReflectionUtils.invokeMethodWithArray;
import static org.apache.maven.surefire.shared.lang3.StringUtils.isNumeric;
import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_FREE_BSD;
import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_LINUX;
@@ -379,7 +380,7 @@ public final class SystemUtils
Class<?> processHandle = tryLoadClass( classLoader, "java.lang.ProcessHandle" );
Class<?>[] classesChain = { processHandle, processHandle };
String[] methodChain = { "current", "pid" };
- return (Long) invokeMethodChain( classesChain, methodChain, null );
+ return invokeMethodChain( classesChain, methodChain, null );
}
static ClassLoader reflectClassLoader( Class<?> target, String getterMethodName )
@@ -387,7 +388,7 @@ public final class SystemUtils
try
{
Method getter = ReflectionUtils.getMethod( target, getterMethodName );
- return (ClassLoader) ReflectionUtils.invokeMethodWithArray( null, getter );
+ return invokeMethodWithArray( null, getter );
}
catch ( RuntimeException e )
{
diff --git a/surefire-providers/common-junit3/src/main/java/org/apache/maven/surefire/common/junit3/JUnit3Reflector.java b/surefire-providers/common-junit3/src/main/java/org/apache/maven/surefire/common/junit3/JUnit3Reflector.java
index ec55459..9a7b032 100644
--- a/surefire-providers/common-junit3/src/main/java/org/apache/maven/surefire/common/junit3/JUnit3Reflector.java
+++ b/surefire-providers/common-junit3/src/main/java/org/apache/maven/surefire/common/junit3/JUnit3Reflector.java
@@ -45,11 +45,11 @@ public final class JUnit3Reflector
private static final String TEST_SUITE = "junit.framework.TestSuite";
- private static final Class[] EMPTY_CLASS_ARRAY = { };
+ private static final Class<?>[] EMPTY_CLASS_ARRAY = { };
private static final Object[] EMPTY_OBJECT_ARRAY = { };
- private final Class[] interfacesImplementedByDynamicProxy;
+ private final Class<?>[] interfacesImplementedByDynamicProxy;
private final Class<?> testResultClass;
@@ -61,7 +61,7 @@ public final class JUnit3Reflector
private final Class<?> testCase;
- private final Constructor testsSuiteConstructor;
+ private final Constructor<?> testsSuiteConstructor;
public JUnit3Reflector( ClassLoader testClassLoader )
{
@@ -118,7 +118,7 @@ public final class JUnit3Reflector
}
- public Object constructTestObject( Class testClass )
+ public Object constructTestObject( Class<?> testClass )
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException
{
Object testObject = createInstanceFromSuiteMethod( testClass );
@@ -130,7 +130,7 @@ public final class JUnit3Reflector
if ( testObject == null )
{
- Constructor testConstructor = getTestConstructor( testClass );
+ Constructor<?> testConstructor = getTestConstructor( testClass );
if ( testConstructor.getParameterTypes().length == 0 )
{
@@ -164,7 +164,7 @@ public final class JUnit3Reflector
return testObject;
}
- private static Constructor getTestConstructor( Class<?> testClass )
+ private static Constructor<?> getTestConstructor( Class<?> testClass )
throws NoSuchMethodException
{
try
@@ -177,7 +177,7 @@ public final class JUnit3Reflector
}
}
- public Class[] getInterfacesImplementedByDynamicProxy()
+ public Class<?>[] getInterfacesImplementedByDynamicProxy()
{
return interfacesImplementedByDynamicProxy;
}
diff --git a/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4Reflector.java b/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4Reflector.java
index 0d59b83..1274603 100644
--- a/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4Reflector.java
+++ b/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4Reflector.java
@@ -35,11 +35,11 @@ import static org.apache.maven.surefire.api.util.ReflectionUtils.tryGetMethod;
*/
public final class JUnit4Reflector
{
- private static final Class[] PARAMS = { Class.class };
+ private static final Class<?>[] PARAMS = { Class.class };
private static final Object[] IGNORE_PARAMS = { Ignore.class };
- private static final Class[] PARAMS_WITH_ANNOTATIONS = { String.class, Annotation[].class };
+ private static final Class<?>[] PARAMS_WITH_ANNOTATIONS = { String.class, Annotation[].class };
private JUnit4Reflector()
{
@@ -68,7 +68,7 @@ public final class JUnit4Reflector
{
Method method = getMethod( Description.class, "createSuiteDescription", PARAMS_WITH_ANNOTATIONS );
// may throw exception probably with broken JUnit 4.x
- return (Description) invokeMethodWithArray( null, method, description, new Annotation[0] );
+ return invokeMethodWithArray( null, method, description, new Annotation[0] );
}
}