You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/12/09 13:03:44 UTC
(commons-lang) branch master updated (daa63aa59 -> 01b2da35a)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
from daa63aa59 Normalize spelling
new b62fc004d Add some tests that use reflection
new 01b2da35a Javadoc
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../lang3/builder/ReflectionDiffBuilder.java | 28 ++++++-
.../org/apache/commons/lang3/event/EventUtils.java | 2 +
.../commons/lang3/reflect/ConstructorUtils.java | 6 ++
.../apache/commons/lang3/reflect/FieldUtils.java | 91 ++++++++++++++++++----
.../apache/commons/lang3/reflect/MemberUtils.java | 2 +
.../apache/commons/lang3/reflect/MethodUtils.java | 45 +++++++----
.../org/apache/commons/lang3/AbstractLangTest.java | 1 +
.../EqualsBuilderReflectJreImplementationTest.java | 22 +++++-
.../HashCodeBuilderAndEqualsBuilderTest.java | 10 +++
.../lang3/builder/ReflectionDiffBuilderTest.java | 18 +++++
.../commons/lang3/builder/TestClassBuilder.java | 60 ++++++++++++++
11 files changed, 252 insertions(+), 33 deletions(-)
create mode 100644 src/test/java/org/apache/commons/lang3/builder/TestClassBuilder.java
(commons-lang) 02/02: Javadoc
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit 01b2da35a55c8e527a3f166266a463352dc16ebb
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Dec 9 08:03:38 2024 -0500
Javadoc
Add @throws and @see tags for SecurityException and SecurityManager
respectively
---
.../lang3/builder/ReflectionDiffBuilder.java | 27 +++++++
.../org/apache/commons/lang3/event/EventUtils.java | 2 +
.../commons/lang3/reflect/ConstructorUtils.java | 6 ++
.../apache/commons/lang3/reflect/FieldUtils.java | 91 ++++++++++++++++++----
.../apache/commons/lang3/reflect/MemberUtils.java | 2 +
.../apache/commons/lang3/reflect/MethodUtils.java | 45 +++++++----
6 files changed, 144 insertions(+), 29 deletions(-)
diff --git a/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java b/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java
index 8e4d0970f..df23bede6 100644
--- a/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java
+++ b/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java
@@ -193,6 +193,12 @@ public class ReflectionDiffBuilder<T> implements Builder<DiffResult<T>> {
return !field.isAnnotationPresent(DiffExclude.class);
}
+ /**
+ * Appends fields using reflection.
+ *
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
+ */
private void appendFields(final Class<?> clazz) {
for (final Field field : FieldUtils.getAllFields(clazz)) {
if (accept(field)) {
@@ -207,6 +213,12 @@ public class ReflectionDiffBuilder<T> implements Builder<DiffResult<T>> {
}
}
+ /**
+ * {@inheritDoc}
+ *
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
+ */
@Override
public DiffResult<T> build() {
if (getLeft().equals(getRight())) {
@@ -234,6 +246,21 @@ public class ReflectionDiffBuilder<T> implements Builder<DiffResult<T>> {
return diffBuilder.getRight();
}
+ /**
+ * Reads a {@link Field}, forcing access if needed.
+ *
+ * @param field
+ * the field to use
+ * @param target
+ * the object to call on, may be {@code null} for {@code static} fields
+ * @return the field value
+ * @throws NullPointerException
+ * if the field is {@code null}
+ * @throws IllegalAccessException
+ * if the field is not made accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
+ */
private Object readField(final Field field, final Object target) throws IllegalAccessException {
return FieldUtils.readField(field, target, true);
}
diff --git a/src/main/java/org/apache/commons/lang3/event/EventUtils.java b/src/main/java/org/apache/commons/lang3/event/EventUtils.java
index c5a21415d..a1f14fb4d 100644
--- a/src/main/java/org/apache/commons/lang3/event/EventUtils.java
+++ b/src/main/java/org/apache/commons/lang3/event/EventUtils.java
@@ -68,6 +68,8 @@ public class EventUtils {
* @param method the method to be invoked
* @param parameters the parameters for the method invocation
* @return the result of the method call
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
* @throws Throwable if an error occurs
*/
@Override
diff --git a/src/main/java/org/apache/commons/lang3/reflect/ConstructorUtils.java b/src/main/java/org/apache/commons/lang3/reflect/ConstructorUtils.java
index e9972f730..a3ea63a1f 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/ConstructorUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/ConstructorUtils.java
@@ -103,6 +103,8 @@ public class ConstructorUtils {
* @param parameterTypes find method with compatible parameters
* @return the constructor, null if no matching accessible constructor found
* @throws NullPointerException if {@code cls} is {@code null}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static <T> Constructor<T> getMatchingAccessibleConstructor(final Class<T> cls,
final Class<?>... parameterTypes) {
@@ -157,6 +159,8 @@ public class ConstructorUtils {
* @throws IllegalAccessException if invocation is not permitted by security
* @throws InvocationTargetException if an error occurs on invocation
* @throws InstantiationException if an error occurs on instantiation
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
* @see #invokeConstructor(Class, Object[], Class[])
*/
public static <T> T invokeConstructor(final Class<T> cls, Object... args)
@@ -183,6 +187,8 @@ public class ConstructorUtils {
* @throws IllegalAccessException if invocation is not permitted by security
* @throws InvocationTargetException if an error occurs on invocation
* @throws InstantiationException if an error occurs on instantiation
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
* @see Constructor#newInstance
*/
public static <T> T invokeConstructor(final Class<T> cls, Object[] args, Class<?>[] parameterTypes)
diff --git a/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java b/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java
index 8810abefe..1d931f269 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java
@@ -17,6 +17,7 @@
package org.apache.commons.lang3.reflect;
import java.lang.annotation.Annotation;
+import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
@@ -91,6 +92,8 @@ public class FieldUtils {
* if the class is {@code null}
* @throws IllegalArgumentException
* if the field name is {@code null}, blank, or empty
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Field getDeclaredField(final Class<?> cls, final String fieldName) {
return getDeclaredField(cls, fieldName, false);
@@ -106,13 +109,15 @@ public class FieldUtils {
* the field name to obtain
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only
+ * {@link AccessibleObject#setAccessible(boolean)} method. {@code false} will only
* match {@code public} fields.
* @return the Field object
* @throws NullPointerException
* if the class is {@code null}
* @throws IllegalArgumentException
* if the field name is {@code null}, blank, or empty
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Field getDeclaredField(final Class<?> cls, final String fieldName, final boolean forceAccess) {
Objects.requireNonNull(cls, "cls");
@@ -145,6 +150,8 @@ public class FieldUtils {
* if the class is {@code null}
* @throws IllegalArgumentException
* if the field name is {@code null}, blank, or empty
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Field getField(final Class<?> cls, final String fieldName) {
return MemberUtils.setAccessibleWorkaround(getField(cls, fieldName, false));
@@ -160,12 +167,14 @@ public class FieldUtils {
* the field name to obtain
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only
+ * {@link AccessibleObject#setAccessible(boolean)} method. {@code false} will only
* match {@code public} fields.
* @return the Field object
* @throws NullPointerException if the class is {@code null}
* @throws IllegalArgumentException if the field name is blank or empty or is matched at multiple places
* in the inheritance hierarchy
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Field getField(final Class<?> cls, final String fieldName, final boolean forceAccess) {
Objects.requireNonNull(cls, "cls");
@@ -263,6 +272,8 @@ public class FieldUtils {
* if {@code fieldName} is {@code null}, blank or empty, or could not be found
* @throws IllegalAccessException
* if the named field is not {@code public}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object readDeclaredField(final Object target, final String fieldName) throws IllegalAccessException {
return readDeclaredField(target, fieldName, false);
@@ -277,7 +288,7 @@ public class FieldUtils {
* the field name to obtain
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only
+ * {@link AccessibleObject#setAccessible(boolean)} method. {@code false} will only
* match public fields.
* @return the Field object
* @throws NullPointerException
@@ -286,6 +297,8 @@ public class FieldUtils {
* if {@code fieldName} is {@code null}, blank or empty, or could not be found
* @throws IllegalAccessException
* if the field is not made accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object readDeclaredField(final Object target, final String fieldName, final boolean forceAccess) throws IllegalAccessException {
Objects.requireNonNull(target, "target");
@@ -311,6 +324,8 @@ public class FieldUtils {
* if the field name is {@code null}, blank, empty, or is not {@code static}
* @throws IllegalAccessException
* if the field is not accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName) throws IllegalAccessException {
return readDeclaredStaticField(cls, fieldName, false);
@@ -325,7 +340,7 @@ public class FieldUtils {
* the field name to obtain
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only
+ * {@link AccessibleObject#setAccessible(boolean)} method. {@code false} will only
* match {@code public} fields.
* @return the Field object
* @throws NullPointerException
@@ -334,6 +349,8 @@ public class FieldUtils {
* if the field name is blank or empty, is not {@code static}
* @throws IllegalAccessException
* if the field is not made accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName, final boolean forceAccess) throws IllegalAccessException {
final Field field = getDeclaredField(cls, fieldName, forceAccess);
@@ -354,6 +371,8 @@ public class FieldUtils {
* if the field is {@code null}
* @throws IllegalAccessException
* if the field is not accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object readField(final Field field, final Object target) throws IllegalAccessException {
return readField(field, target, false);
@@ -368,12 +387,16 @@ public class FieldUtils {
* the object to call on, may be {@code null} for {@code static} fields
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method.
+ * {@link AccessibleObject#setAccessible(boolean)} method.
* @return the field value
* @throws NullPointerException
* if the field is {@code null}
* @throws IllegalAccessException
* if the field is not made accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object readField(final Field field, final Object target, final boolean forceAccess) throws IllegalAccessException {
Objects.requireNonNull(field, "field");
@@ -399,6 +422,8 @@ public class FieldUtils {
* if the field name is {@code null}, blank, empty, or could not be found
* @throws IllegalAccessException
* if the named field is not {@code public}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object readField(final Object target, final String fieldName) throws IllegalAccessException {
return readField(target, fieldName, false);
@@ -413,7 +438,7 @@ public class FieldUtils {
* the field name to obtain
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only
+ * {@link AccessibleObject#setAccessible(boolean)} method. {@code false} will only
* match {@code public} fields.
* @return the field value
* @throws NullPointerException
@@ -422,6 +447,8 @@ public class FieldUtils {
* if the field name is {@code null}, blank, empty, or could not be found
* @throws IllegalAccessException
* if the named field is not made accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object readField(final Object target, final String fieldName, final boolean forceAccess) throws IllegalAccessException {
Objects.requireNonNull(target, "target");
@@ -446,6 +473,8 @@ public class FieldUtils {
* if the field name is {@code null}, blank or empty, or is not {@code static}
* @throws IllegalAccessException
* if the field is not accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object readStaticField(final Class<?> cls, final String fieldName) throws IllegalAccessException {
return readStaticField(cls, fieldName, false);
@@ -460,7 +489,7 @@ public class FieldUtils {
* the field name to obtain
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only
+ * {@link AccessibleObject#setAccessible(boolean)} method. {@code false} will only
* match {@code public} fields.
* @return the Field object
* @throws NullPointerException
@@ -469,6 +498,8 @@ public class FieldUtils {
* if the field name is {@code null}, blank or empty, or is not {@code static}
* @throws IllegalAccessException
* if the field is not made accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object readStaticField(final Class<?> cls, final String fieldName, final boolean forceAccess) throws IllegalAccessException {
final Field field = getField(cls, fieldName, forceAccess);
@@ -489,6 +520,8 @@ public class FieldUtils {
* if the field is not {@code static}
* @throws IllegalAccessException
* if the field is not accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object readStaticField(final Field field) throws IllegalAccessException {
return readStaticField(field, false);
@@ -501,7 +534,7 @@ public class FieldUtils {
* to read
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method.
+ * {@link AccessibleObject#setAccessible(boolean)} method.
* @return the field value
* @throws NullPointerException
* if the field is {@code null}
@@ -509,6 +542,8 @@ public class FieldUtils {
* if the field is not {@code static}
* @throws IllegalAccessException
* if the field is not made accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object readStaticField(final Field field, final boolean forceAccess) throws IllegalAccessException {
Objects.requireNonNull(field, "field");
@@ -523,6 +558,8 @@ public class FieldUtils {
* to remove the final modifier
* @throws NullPointerException
* if the field is {@code null}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
* @since 3.2
*/
public static void removeFinalModifier(final Field field) {
@@ -536,10 +573,12 @@ public class FieldUtils {
* to remove the final modifier
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only
+ * {@link AccessibleObject#setAccessible(boolean)} method. {@code false} will only
* match {@code public} fields.
* @throws NullPointerException
* if the field is {@code null}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
* @deprecated As of Java 12, we can no longer drop the {@code final} modifier, thus
* rendering this method obsolete. The JDK discussion about this change can be found
* here: https://mail.openjdk.java.net/pipermail/core-libs-dev/2018-November/056486.html
@@ -592,6 +631,8 @@ public class FieldUtils {
* or {@code value} is not assignable
* @throws IllegalAccessException
* if the field is not made accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static void writeDeclaredField(final Object target, final String fieldName, final Object value) throws IllegalAccessException {
writeDeclaredField(target, fieldName, value, false);
@@ -608,13 +649,15 @@ public class FieldUtils {
* to set
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only
+ * {@link AccessibleObject#setAccessible(boolean)} method. {@code false} will only
* match {@code public} fields.
* @throws IllegalArgumentException
* if {@code fieldName} is {@code null}, blank or empty, or could not be found,
* or {@code value} is not assignable
* @throws IllegalAccessException
* if the field is not made accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static void writeDeclaredField(final Object target, final String fieldName, final Object value, final boolean forceAccess)
throws IllegalAccessException {
@@ -641,6 +684,8 @@ public class FieldUtils {
* if the field name is {@code null}, blank, empty, not {@code static}, or {@code value} is not assignable
* @throws IllegalAccessException
* if the field is not {@code public} or is {@code final}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static void writeDeclaredStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException {
writeDeclaredStaticField(cls, fieldName, value, false);
@@ -664,6 +709,8 @@ public class FieldUtils {
* if the field name is {@code null}, blank, empty, not {@code static}, or {@code value} is not assignable
* @throws IllegalAccessException
* if the field is not made accessible or is {@code final}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static void writeDeclaredStaticField(final Class<?> cls, final String fieldName, final Object value, final boolean forceAccess)
throws IllegalAccessException {
@@ -688,6 +735,8 @@ public class FieldUtils {
* if {@code value} is not assignable
* @throws IllegalAccessException
* if the field is not accessible or is {@code final}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static void writeField(final Field field, final Object target, final Object value) throws IllegalAccessException {
writeField(field, target, value, false);
@@ -704,7 +753,7 @@ public class FieldUtils {
* to set
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only
+ * {@link AccessibleObject#setAccessible(boolean)} method. {@code false} will only
* match {@code public} fields.
* @throws NullPointerException
* if the field is {@code null}
@@ -712,6 +761,8 @@ public class FieldUtils {
* if {@code value} is not assignable
* @throws IllegalAccessException
* if the field is not made accessible or is {@code final}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static void writeField(final Field field, final Object target, final Object value, final boolean forceAccess)
throws IllegalAccessException {
@@ -740,6 +791,8 @@ public class FieldUtils {
* or {@code value} is not assignable
* @throws IllegalAccessException
* if the field is not accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static void writeField(final Object target, final String fieldName, final Object value) throws IllegalAccessException {
writeField(target, fieldName, value, false);
@@ -756,7 +809,7 @@ public class FieldUtils {
* to set
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only
+ * {@link AccessibleObject#setAccessible(boolean)} method. {@code false} will only
* match {@code public} fields.
* @throws NullPointerException
* if {@code target} is {@code null}
@@ -765,6 +818,8 @@ public class FieldUtils {
* or {@code value} is not assignable
* @throws IllegalAccessException
* if the field is not made accessible
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static void writeField(final Object target, final String fieldName, final Object value, final boolean forceAccess)
throws IllegalAccessException {
@@ -792,6 +847,8 @@ public class FieldUtils {
* not {@code static}, or {@code value} is not assignable
* @throws IllegalAccessException
* if the field is not {@code public} or is {@code final}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static void writeStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException {
writeStaticField(cls, fieldName, value, false);
@@ -808,7 +865,7 @@ public class FieldUtils {
* to set
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only
+ * {@link AccessibleObject#setAccessible(boolean)} method. {@code false} will only
* match {@code public} fields.
* @throws NullPointerException
* if {@code cls} is {@code null} or the field cannot be located
@@ -816,6 +873,8 @@ public class FieldUtils {
* if {@code fieldName} is {@code null}, blank or empty, the field not {@code static}, or {@code value} is not assignable
* @throws IllegalAccessException
* if the field is not made accessible or is {@code final}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static void writeStaticField(final Class<?> cls, final String fieldName, final Object value, final boolean forceAccess)
throws IllegalAccessException {
@@ -838,6 +897,8 @@ public class FieldUtils {
* if the field is not {@code static}, or {@code value} is not assignable
* @throws IllegalAccessException
* if the field is not {@code public} or is {@code final}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static void writeStaticField(final Field field, final Object value) throws IllegalAccessException {
writeStaticField(field, value, false);
@@ -852,7 +913,7 @@ public class FieldUtils {
* to set
* @param forceAccess
* whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only
+ * {@link AccessibleObject#setAccessible(boolean)} method. {@code false} will only
* match {@code public} fields.
* @throws NullPointerException
* if the field is {@code null}
@@ -860,6 +921,8 @@ public class FieldUtils {
* if the field is not {@code static}, or {@code value} is not assignable
* @throws IllegalAccessException
* if the field is not made accessible or is {@code final}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static void writeStaticField(final Field field, final Object value, final boolean forceAccess) throws IllegalAccessException {
Objects.requireNonNull(field, "field");
diff --git a/src/main/java/org/apache/commons/lang3/reflect/MemberUtils.java b/src/main/java/org/apache/commons/lang3/reflect/MemberUtils.java
index 1612e6034..c6ba44d68 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/MemberUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/MemberUtils.java
@@ -322,6 +322,8 @@ final class MemberUtils {
*
* @param obj the AccessibleObject to set as accessible, may be null.
* @return a boolean indicating whether the accessibility of the object was set to true.
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
static <T extends AccessibleObject> T setAccessibleWorkaround(final T obj) {
if (obj == null || obj.isAccessible()) {
diff --git a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
index f1b6e04c6..3685af926 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
@@ -263,19 +263,18 @@ public class MethodUtils {
* determines if underlying method has to be accessible
* @return the first matching annotation, or {@code null} if not found
* @throws NullPointerException if either the method or annotation class is {@code null}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
* @since 3.6
*/
public static <A extends Annotation> A getAnnotation(final Method method, final Class<A> annotationCls,
final boolean searchSupers, final boolean ignoreAccess) {
-
Objects.requireNonNull(method, "method");
Objects.requireNonNull(annotationCls, "annotationCls");
if (!ignoreAccess && !MemberUtils.isAccessible(method)) {
return null;
}
-
A annotation = method.getAnnotation(annotationCls);
-
if (annotation == null && searchSupers) {
final Class<?> mcls = method.getDeclaringClass();
final List<Class<?>> classes = getAllSuperclassesAndInterfaces(mcls);
@@ -290,7 +289,6 @@ public class MethodUtils {
}
}
}
-
return annotation;
}
@@ -315,6 +313,8 @@ public class MethodUtils {
* @param methodName find method with this name
* @param parameterTypes find method with most compatible parameters
* @return The accessible method
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Method getMatchingAccessibleMethod(final Class<?> cls,
final String methodName, final Class<?>... parameterTypes) {
@@ -429,8 +429,10 @@ public class MethodUtils {
* @param name the name of the method
* @param parameterTypes the list of parameters
* @return a Method or null.
- * @since 3.15.0
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
* @see Class#getMethod(String, Class...)
+ * @since 3.15.0
*/
public static Method getMethodObject(final Class<?> cls, final String name, final Class<?>... parameterTypes) {
try {
@@ -528,6 +530,8 @@ public class MethodUtils {
* @param interfacesBehavior whether to search interfaces, {@code null} {@code implies} false
* @return a {@code Set<Method>} in ascending order from sub- to superclass
* @throws NullPointerException if the specified method is {@code null}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
* @since 3.2
*/
public static Set<Method> getOverrideHierarchy(final Method method, final Interfaces interfacesBehavior) {
@@ -756,6 +760,8 @@ public class MethodUtils {
* @throws NoSuchMethodException if there is no such accessible method
* @throws InvocationTargetException wraps an exception thrown by the method invoked
* @throws IllegalAccessException if the requested method is not accessible via reflection
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
* @since 3.5
*/
public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName)
@@ -783,6 +789,8 @@ public class MethodUtils {
* @throws InvocationTargetException wraps an exception thrown by the method invoked
* @throws IllegalAccessException if the requested method is not accessible via reflection
* @throws NullPointerException if the object or method name are {@code null}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
* @since 3.5
*/
public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName,
@@ -809,6 +817,8 @@ public class MethodUtils {
* @throws InvocationTargetException wraps an exception thrown by the method invoked
* @throws IllegalAccessException if the requested method is not accessible via reflection
* @throws NullPointerException if the object or method name are {@code null}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
* @since 3.5
*/
public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName, Object[] args, Class<?>[] parameterTypes)
@@ -855,8 +865,9 @@ public class MethodUtils {
* @throws NoSuchMethodException if there is no such accessible method
* @throws InvocationTargetException wraps an exception thrown by the method invoked
* @throws IllegalAccessException if the requested method is not accessible via reflection
- *
- * @since 3.4
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
+ * @since 3.4
*/
public static Object invokeMethod(final Object object, final String methodName) throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException {
@@ -884,6 +895,8 @@ public class MethodUtils {
* @throws InvocationTargetException wraps an exception thrown by the method invoked
* @throws IllegalAccessException if the requested method is not accessible via reflection
* @throws NullPointerException if the object or method name are {@code null}
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object invokeMethod(final Object object, final String methodName,
Object... args) throws NoSuchMethodException,
@@ -909,6 +922,8 @@ public class MethodUtils {
* @throws NoSuchMethodException if there is no such accessible method
* @throws InvocationTargetException wraps an exception thrown by the method invoked
* @throws IllegalAccessException if the requested method is not accessible via reflection
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object invokeMethod(final Object object, final String methodName,
final Object[] args, final Class<?>[] parameterTypes)
@@ -935,10 +950,10 @@ public class MethodUtils {
* @param args use these arguments - treat {@code null} as empty array
* @return The value returned by the invoked method
* @throws NoSuchMethodException if there is no such accessible method
- * @throws InvocationTargetException wraps an exception thrown by the
- * method invoked
- * @throws IllegalAccessException if the requested method is not accessible
- * via reflection
+ * @throws InvocationTargetException wraps an exception thrown by the method invoked
+ * @throws IllegalAccessException if the requested method is not accessible via reflection
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object invokeStaticMethod(final Class<?> cls, final String methodName,
Object... args) throws NoSuchMethodException,
@@ -962,10 +977,10 @@ public class MethodUtils {
* @param parameterTypes match these parameters - treat {@code null} as empty array
* @return The value returned by the invoked method
* @throws NoSuchMethodException if there is no such accessible method
- * @throws InvocationTargetException wraps an exception thrown by the
- * method invoked
- * @throws IllegalAccessException if the requested method is not accessible
- * via reflection
+ * @throws InvocationTargetException wraps an exception thrown by the method invoked
+ * @throws IllegalAccessException if the requested method is not accessible via reflection
+ * @throws SecurityException if an underlying accessible object's method denies the request.
+ * @see SecurityManager#checkPermission
*/
public static Object invokeStaticMethod(final Class<?> cls, final String methodName,
Object[] args, Class<?>[] parameterTypes)
(commons-lang) 01/02: Add some tests that use reflection
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit b62fc004d1a60757090ba9a177202eed0db57cc0
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Dec 9 07:24:17 2024 -0500
Add some tests that use reflection
Still need to find a way to check for memory retention beyond what
AbstractLangTest.after() does
---
.../lang3/builder/ReflectionDiffBuilder.java | 1 -
.../org/apache/commons/lang3/AbstractLangTest.java | 1 +
.../EqualsBuilderReflectJreImplementationTest.java | 22 ++++++--
.../HashCodeBuilderAndEqualsBuilderTest.java | 10 ++++
.../lang3/builder/ReflectionDiffBuilderTest.java | 18 +++++++
.../commons/lang3/builder/TestClassBuilder.java | 60 ++++++++++++++++++++++
6 files changed, 108 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java b/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java
index cef3b2892..8e4d0970f 100644
--- a/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java
+++ b/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java
@@ -212,7 +212,6 @@ public class ReflectionDiffBuilder<T> implements Builder<DiffResult<T>> {
if (getLeft().equals(getRight())) {
return diffBuilder.build();
}
-
appendFields(getLeft().getClass());
return diffBuilder.build();
}
diff --git a/src/test/java/org/apache/commons/lang3/AbstractLangTest.java b/src/test/java/org/apache/commons/lang3/AbstractLangTest.java
index ca9ff1b49..b6d46a158 100644
--- a/src/test/java/org/apache/commons/lang3/AbstractLangTest.java
+++ b/src/test/java/org/apache/commons/lang3/AbstractLangTest.java
@@ -32,6 +32,7 @@ public class AbstractLangTest {
@AfterEach
public void after() {
assertTrue(ToStringStyle.getRegistry().isEmpty(), "Expected null, actual: " + ToStringStyle.getRegistry());
+ // TODO Do more to make sure memory is not retained, maybe like Log4j checks for it.
}
}
diff --git a/src/test/java/org/apache/commons/lang3/builder/EqualsBuilderReflectJreImplementationTest.java b/src/test/java/org/apache/commons/lang3/builder/EqualsBuilderReflectJreImplementationTest.java
index a8001d377..d5223e1be 100644
--- a/src/test/java/org/apache/commons/lang3/builder/EqualsBuilderReflectJreImplementationTest.java
+++ b/src/test/java/org/apache/commons/lang3/builder/EqualsBuilderReflectJreImplementationTest.java
@@ -42,10 +42,13 @@ import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalField;
import java.time.temporal.TemporalUnit;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.function.Supplier;
import org.apache.commons.lang3.AbstractLangTest;
+import org.apache.commons.lang3.stream.IntStreams;
import org.junit.jupiter.api.Test;
/**
@@ -90,13 +93,14 @@ public class EqualsBuilderReflectJreImplementationTest extends AbstractLangTest
}
}
- static class MyClass {
+ static class MyClass implements Cloneable {
private final MyCharSequence charSequence;
private final MyTemporal temporal;
private final MyTemporalAccessor temporalAccessor;
private final MyTemporalAmount temporalAmount;
private final Object[] objects;
+ private final List<Supplier<?>> list = new ArrayList<>();
MyClass(final MyCharSequence charSequence, final MyTemporal temporal, final MyTemporalAccessor temporalAccessor,
final MyTemporalAmount temporalAmount) {
@@ -117,6 +121,7 @@ public class EqualsBuilderReflectJreImplementationTest extends AbstractLangTest
localDate, HijrahDate.from(localDate), JapaneseDate.from(localDate), MinguoDate.from(localDate), ThaiBuddhistDate.from(localDate),
localDate, localTime, localDateTime, offsetDateTime, OffsetTime.of(localTime, zoneOffset), Year.of(value), YearMonth.of(value, value),
ZonedDateTime.of(localDateTime, zoneOffset), zoneOffset, ZoneId.of(zoneOffset.getId()) };
+ IntStreams.range(100).forEach(i -> list.add(() -> charSequence));
}
@Override
@@ -178,6 +183,7 @@ public class EqualsBuilderReflectJreImplementationTest extends AbstractLangTest
}
}
+
static class MyTemporalAccessor implements TemporalAccessor {
private final String string;
@@ -256,9 +262,9 @@ public class EqualsBuilderReflectJreImplementationTest extends AbstractLangTest
@Test
public void testRecursive() {
- final MyClass o1 = new MyClass(new MyCharSequence("1"), new MyTemporal("2"), new MyTemporalAccessor("3"), new MyTemporalAmount("4"));
+ final MyClass o1 = new MyClass(new MyCharSequence("1"), new MyTemporal("2"), new MyTemporalAccessor("3"), new MyTemporalAmount("4"));
// This gives you different instances of MyTemporalAccessor for 1 (and 2) that should be equals by reflection.
- final MyClass o1Bis = new MyClass(new MyCharSequence("1"), new MyTemporal("2"), new MyTemporalAccessor("3"), new MyTemporalAmount("4"));
+ final MyClass o1Bis = new MyClass(new MyCharSequence("1"), new MyTemporal("2"), new MyTemporalAccessor("3"), new MyTemporalAmount("4"));
final MyClass o2 = new MyClass(new MyCharSequence("5"), new MyTemporal("6"), new MyTemporalAccessor("7"), new MyTemporalAmount("8"));
final MyClass o2Bis = new MyClass(new MyCharSequence("5"), new MyTemporal("6"), new MyTemporalAccessor("7"), new MyTemporalAmount("8"));
// MyTemporal
@@ -276,4 +282,14 @@ public class EqualsBuilderReflectJreImplementationTest extends AbstractLangTest
assertFalse(new EqualsBuilder().setTestRecursive(true).append(o2, o1).isEquals());
}
+ @Test
+ public void testRetention() throws Exception {
+ // The following should not retain memory.
+ for (int i = 0; i < Integer.getInteger("testRetention", 10_000); i++) {
+ final Class<?> clazz = TestClassBuilder.defineSimpleClass(getClass().getPackage().getName(), i);
+ assertTrue(new EqualsBuilder().setTestRecursive(true).append(clazz.newInstance(), clazz.newInstance()).isEquals());
+ }
+ // some retention is checked in super's after().
+ }
+
}
diff --git a/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderAndEqualsBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderAndEqualsBuilderTest.java
index 5fa03eb38..8de3b010f 100644
--- a/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderAndEqualsBuilderTest.java
+++ b/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderAndEqualsBuilderTest.java
@@ -110,6 +110,7 @@ public class HashCodeBuilderAndEqualsBuilderTest extends AbstractLangTest {
new SubAllTransientFixture(2, 'c', "Test", (short) 2, "Same"),
new SubAllTransientFixture(2, 'c', "Test", (short) 2, "Same"),
testTransients);
+
}
@Test
@@ -133,4 +134,13 @@ public class HashCodeBuilderAndEqualsBuilderTest extends AbstractLangTest {
testInteger(true);
}
+ @Test
+ public void testRetention() throws Exception {
+ // The following should not retain memory.
+ for (int i = 0; i < Integer.getInteger("testRecursive", 10_000); i++) {
+ final Class<?> clazz = TestClassBuilder.defineSimpleClass(getClass().getPackage().getName(), i);
+ assertEqualsAndHashCodeContract(clazz.newInstance(), clazz.newInstance(), false);
+ }
+ }
+
}
diff --git a/src/test/java/org/apache/commons/lang3/builder/ReflectionDiffBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/ReflectionDiffBuilderTest.java
index 04b37e7be..86286627a 100644
--- a/src/test/java/org/apache/commons/lang3/builder/ReflectionDiffBuilderTest.java
+++ b/src/test/java/org/apache/commons/lang3/builder/ReflectionDiffBuilderTest.java
@@ -37,6 +37,7 @@ public class ReflectionDiffBuilderTest extends AbstractLangTest {
@SuppressWarnings("unused")
private static class TypeTestClass implements Diffable<TypeTestClass> {
+
private static int staticField;
private final ToStringStyle style = SHORT_STYLE;
private final boolean booleanField = true;
@@ -159,6 +160,7 @@ public class ReflectionDiffBuilderTest extends AbstractLangTest {
final String[] excludeFieldNames = reflectionDiffBuilder.getExcludeFieldNames();
assertNotNull(excludeFieldNames);
assertEquals(0, excludeFieldNames.length);
+ assertNotNull(reflectionDiffBuilder.build());
}
@Test
@@ -171,6 +173,7 @@ public class ReflectionDiffBuilderTest extends AbstractLangTest {
final String[] excludeFieldNames = reflectionDiffBuilder.getExcludeFieldNames();
assertNotNull(excludeFieldNames);
assertEquals(0, excludeFieldNames.length);
+ assertNotNull(reflectionDiffBuilder.build());
}
@Test
@@ -189,6 +192,7 @@ public class ReflectionDiffBuilderTest extends AbstractLangTest {
assertNotNull(excludeFieldNames);
assertEquals(1, excludeFieldNames.length);
assertEquals("charField", excludeFieldNames[0]);
+ assertNotNull(reflectionDiffBuilder.build());
}
@Test
@@ -202,6 +206,7 @@ public class ReflectionDiffBuilderTest extends AbstractLangTest {
assertNotNull(excludeFieldNames);
assertEquals(1, excludeFieldNames.length);
assertEquals("charField", excludeFieldNames[0]);
+ assertNotNull(reflectionDiffBuilder.build());
}
@Test
@@ -212,6 +217,19 @@ public class ReflectionDiffBuilderTest extends AbstractLangTest {
assertEquals(0, firstObject.diffDeprecated(secondObject).getNumberOfDiffs());
}
+ @Test
+ public void testRetention() throws Exception {
+ // The following should not retain memory.
+ for (int i = 0; i < Integer.getInteger("testRecursive", 10_000); i++) {
+ final Class<?> clazz = TestClassBuilder.defineSimpleClass(getClass().getPackage().getName(), i);
+ final Object firstObject = clazz.newInstance();
+ final Object secondObject = clazz.newInstance();
+ final ReflectionDiffBuilder<Object> reflectionDiffBuilder = new ReflectionDiffBuilder<>(firstObject, secondObject, SHORT_STYLE);
+ assertNotNull(reflectionDiffBuilder.build());
+ }
+ }
+
+
@Test
public void testNoDifferencesDiffExcludeAnnotatedField() {
final TypeTestClass firstObject = new TypeTestClass();
diff --git a/src/test/java/org/apache/commons/lang3/builder/TestClassBuilder.java b/src/test/java/org/apache/commons/lang3/builder/TestClassBuilder.java
new file mode 100644
index 000000000..09760d938
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/builder/TestClassBuilder.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.lang3.builder;
+
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Opcodes;
+
+/**
+ * Builds classes dynamically for tests.
+ */
+public class TestClassBuilder {
+
+ /**
+ * Extends {@link ClassLoader} to make {@link ClassLoader#defineClass(String, byte[])} public.
+ */
+ static final class DynamicClassLoader extends ClassLoader {
+ public Class<?> defineClass(final String name, final byte[] b) {
+ return defineClass(name, b, 0, b.length);
+ }
+ }
+
+ /**
+ * Defines the simplest possible class.
+ *
+ * @param name The class name.
+ * @return The new class.
+ */
+ static Class<?> defineSimpleClass(final String name) {
+ final ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
+ classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object", new String[] {});
+ final MethodVisitor ctor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
+ ctor.visitCode();
+ ctor.visitVarInsn(Opcodes.ALOAD, 0);
+ ctor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
+ ctor.visitInsn(Opcodes.RETURN);
+ ctor.visitMaxs(1, 1);
+ return new DynamicClassLoader().defineClass(name.replace('/', '.'), classWriter.toByteArray());
+ }
+
+ static Class<?> defineSimpleClass(final String packageName, final int i) {
+ return defineSimpleClass(packageName.replace('.', '/') + "/C" + i);
+ }
+
+}