Skip to content

Commit

Permalink
Add since tag to hasMethod(Class, Method)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Jan 8, 2020
1 parent 8e5cad2 commit c39ed52
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions spring-core/src/main/java/org/springframework/util/ClassUtils.java
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -533,7 +533,7 @@ public static Class<?> resolvePrimitiveIfNecessary(Class<?> clazz) {
* @param lhsType the target type
* @param rhsType the value type that should be assigned to the target type
* @return if the target type is assignable from the value type
* @see TypeUtils#isAssignable
* @see TypeUtils#isAssignable(java.lang.reflect.Type, java.lang.reflect.Type)
*/
public static boolean isAssignable(Class<?> lhsType, Class<?> rhsType) {
Assert.notNull(lhsType, "Left-hand side type must not be null");
Expand Down Expand Up @@ -1088,25 +1088,12 @@ public static <T> Constructor<T> getConstructorIfAvailable(Class<T> clazz, Class
}
}

/**
* Determine whether the given class has a public method with the given signature.
* <p>Essentially translates {@code NoSuchMethodException} to "false".
* @param clazz the clazz to analyze
* @param methodName the name of the method
* @param paramTypes the parameter types of the method
* @return whether the class has a corresponding method
* @see Class#getMethod
*/
public static boolean hasMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
return (getMethodIfAvailable(clazz, methodName, paramTypes) != null);
}

/**
* Determine whether the given class has a public method with the given signature.
* @param clazz the clazz to analyze
* @param method checked method
* @param method the method to look for
* @return whether the class has a corresponding method
* @see Method#getDeclaringClass
* @since 5.2.3
*/
public static boolean hasMethod(Class<?> clazz, Method method) {
Assert.notNull(clazz, "Class must not be null");
Expand All @@ -1119,6 +1106,19 @@ public static boolean hasMethod(Class<?> clazz, Method method) {
return getMethodOrNull(clazz, methodName, paramTypes) != null;
}

/**
* Determine whether the given class has a public method with the given signature.
* <p>Essentially translates {@code NoSuchMethodException} to "false".
* @param clazz the clazz to analyze
* @param methodName the name of the method
* @param paramTypes the parameter types of the method
* @return whether the class has a corresponding method
* @see Class#getMethod
*/
public static boolean hasMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
return (getMethodIfAvailable(clazz, methodName, paramTypes) != null);
}

/**
* Determine whether the given class has a public method with the given signature,
* and return it if available (else throws an {@code IllegalStateException}).
Expand Down Expand Up @@ -1373,6 +1373,17 @@ public static Method getStaticMethod(Class<?> clazz, String methodName, Class<?>
}
}


@Nullable
private static Method getMethodOrNull(Class<?> clazz, String methodName, Class<?>[] paramTypes) {
try {
return clazz.getMethod(methodName, paramTypes);
}
catch (NoSuchMethodException ex) {
return null;
}
}

private static Set<Method> findMethodCandidatesByName(Class<?> clazz, String methodName) {
Set<Method> candidates = new HashSet<>(1);
Method[] methods = clazz.getMethods();
Expand All @@ -1384,12 +1395,4 @@ private static Set<Method> findMethodCandidatesByName(Class<?> clazz, String met
return candidates;
}

@Nullable
private static Method getMethodOrNull(Class<?> clazz, String methodName, Class<?>[] paramTypes) {
try {
return clazz.getMethod(methodName, paramTypes);
} catch (NoSuchMethodException ex) {
return null;
}
}
}

0 comments on commit c39ed52

Please sign in to comment.