Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Jan 18, 2024
1 parent a6bf574 commit c7d8d52
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 22 deletions.
60 changes: 52 additions & 8 deletions src/main/java/com/epam/reportportal/utils/reflect/Accessible.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,6 +56,10 @@ public void setValue(Object value) {
}
}

/**
* Return given field value.
*/
@Nullable
public Object getValue() {
try {
return this.f.get(this.bean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public void test_field_access_negative() {
}

public static Stream<Arguments> 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
Expand Down

0 comments on commit c7d8d52

Please sign in to comment.