diff --git a/src/main/java/com/epam/reportportal/utils/reflect/Accessible.java b/src/main/java/com/epam/reportportal/utils/reflect/Accessible.java index 1c00640b..749a0eda 100644 --- a/src/main/java/com/epam/reportportal/utils/reflect/Accessible.java +++ b/src/main/java/com/epam/reportportal/utils/reflect/Accessible.java @@ -17,37 +17,80 @@ package com.epam.reportportal.utils.reflect; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.lang.reflect.Field; import java.lang.reflect.Method; /** - * Representation of accessible method or field + * Utility class to decorate routine code of accessing fields and methods in complex objects. Supports access to private elements and + * inheritance. */ public class Accessible { private final Object object; - public Accessible(Object object) { + /** + * Create the decorator for given object. + * + * @param object an instance of object on which you need reflective access + */ + public Accessible(@Nonnull Object object) { this.object = object; } - public AccessibleMethod method(Method m) { + /** + * Create a decorator for an instance of accessible method. + * + * @param m method to access + * @return decorator instance + */ + @Nonnull + public AccessibleMethod method(@Nonnull Method m) { return new AccessibleMethod(object, m); } - public AccessibleMethod method(String m, Class... parameterTypes) throws NoSuchMethodException { + /** + * Create a decorator for an instance of accessible method. + * + * @param m method to access + * @param parameterTypes an array of specific parameters to distinguish the method + * @return decorator instance + * @throws NoSuchMethodException no such method found + */ + @Nonnull + public AccessibleMethod method(@Nonnull String m, @Nullable Class... parameterTypes) throws NoSuchMethodException { return new AccessibleMethod(object, getMethod(m, parameterTypes)); } - public AccessibleField field(Field f) { + /** + * Create a decorator for an instance of accessible field. + * + * @param f field to access + * @return decorator instance + */ + @Nonnull + public AccessibleField field(@Nonnull Field f) { return new AccessibleField(object, f); } - public AccessibleField field(String name) throws NoSuchFieldException { + /** + * Create a decorator for an instance of accessible field. + * + * @param name field to access + * @return decorator instance + */ + @Nonnull + public AccessibleField field(@Nonnull String name) throws NoSuchFieldException { return new AccessibleField(object, getField(name)); } - public static Accessible on(Object object) { + /** + * Create the decorator for given object. + * + * @param object an instance of object on which you need reflective access + */ + @Nonnull + public static Accessible on(@Nonnull Object object) { return new Accessible(object); } @@ -69,7 +112,8 @@ private Field getField(@Nonnull String fieldName) throws NoSuchFieldException { } } - private Method getMethod(String methodName, Class... parameterTypes) throws NoSuchMethodException { + @Nonnull + private Method getMethod(@Nonnull String methodName, @Nullable Class... parameterTypes) throws NoSuchMethodException { Class clazz = object.getClass(); try { return clazz.getMethod(methodName, parameterTypes); diff --git a/src/main/java/com/epam/reportportal/utils/reflect/AccessibleField.java b/src/main/java/com/epam/reportportal/utils/reflect/AccessibleField.java index cdaf3e9d..f4143ece 100644 --- a/src/main/java/com/epam/reportportal/utils/reflect/AccessibleField.java +++ b/src/main/java/com/epam/reportportal/utils/reflect/AccessibleField.java @@ -16,28 +16,34 @@ package com.epam.reportportal.utils.reflect; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.lang.reflect.Field; /** - * Setter and Getter for Accessible Field - * - * @author Andrei Varabyeu + * Utility class to decorate routine code of setting and getting a field thought Reflections. */ public class AccessibleField { private final Field f; private final Object bean; - AccessibleField(Object bean, Field f) { + AccessibleField(@Nonnull Object bean, @Nonnull Field f) { this.bean = bean; this.f = f; } + @Nonnull public Class getType() { return this.f.getType(); } - public void setValue(Object value) { + /** + * Set given field value. + * + * @param value value to set + */ + public void setValue(@Nullable Object value) { try { this.f.set(this.bean, value); } catch (IllegalAccessException accessException) { //NOSONAR @@ -50,6 +56,10 @@ public void setValue(Object value) { } } + /** + * Return given field value. + */ + @Nullable public Object getValue() { try { return this.f.get(this.bean); diff --git a/src/main/java/com/epam/reportportal/utils/reflect/AccessibleMethod.java b/src/main/java/com/epam/reportportal/utils/reflect/AccessibleMethod.java index f761a0fa..87374492 100644 --- a/src/main/java/com/epam/reportportal/utils/reflect/AccessibleMethod.java +++ b/src/main/java/com/epam/reportportal/utils/reflect/AccessibleMethod.java @@ -16,26 +16,31 @@ package com.epam.reportportal.utils.reflect; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** - * Accessible method implementation. Set accessibility == true for specified - * method and can invoke methods - * - * @author Andrei Varabyeu + * Utility class to decorate routine code of invoking a method thought Reflections. */ public class AccessibleMethod { private final Method method; private final Object bean; - AccessibleMethod(Object bean, Method method) { + AccessibleMethod(@Nonnull Object bean, @Nonnull Method method) { this.bean = bean; this.method = method; } - public Object invoke(Object... args) throws Throwable { + /** + * Set given method. + * + * @param args arguments to pass to the method + */ + @Nullable + public Object invoke(@Nullable Object... args) throws Throwable { try { return invoke(this.bean, this.method, args); } catch (IllegalAccessException accessException) { //NOSONAR @@ -48,7 +53,8 @@ public Object invoke(Object... args) throws Throwable { } } - private Object invoke(Object bean, Method m, Object... args) throws Throwable { + @Nullable + private Object invoke(@Nonnull Object bean, @Nonnull Method m, @Nullable Object... args) throws Throwable { try { return m.invoke(bean, args); } catch (IllegalArgumentException e) { diff --git a/src/test/java/com/epam/reportportal/utils/reflect/AccessibleTest.java b/src/test/java/com/epam/reportportal/utils/reflect/AccessibleTest.java index b641ba1e..36955496 100644 --- a/src/test/java/com/epam/reportportal/utils/reflect/AccessibleTest.java +++ b/src/test/java/com/epam/reportportal/utils/reflect/AccessibleTest.java @@ -88,8 +88,7 @@ public void test_field_access_negative() { } public static Stream methodData() throws NoSuchMethodException { - return Stream.of(Arguments.of( - "publicMethodNoParams", + return Stream.of(Arguments.of("publicMethodNoParams", AccessibleTest.class.getMethod("publicMethodNoParams"), null, PUBLIC_METHOD_NO_PARAMS_VALUE