Skip to content

Commit

Permalink
ReflectionPropagation refactor (#354)
Browse files Browse the repository at this point in the history
* Refactor GetValueCommand

Signed-off-by: Gyúróczki Gergő <gergonoorbi@gmail.com>

* Refactor SetValueCommand

Signed-off-by: Gyúróczki Gergő <gergonoorbi@gmail.com>
  • Loading branch information
Degubi authored and aguibert committed Oct 21, 2019
1 parent 2ff436e commit 06706b9
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 160 deletions.
28 changes: 0 additions & 28 deletions src/main/java/org/eclipse/yasson/internal/model/GetFromField.java

This file was deleted.

30 changes: 0 additions & 30 deletions src/main/java/org/eclipse/yasson/internal/model/GetFromGetter.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.eclipse.yasson.internal.model;

import javax.json.bind.JsonbException;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;

/**
* Wrapper for getting value from javabean property.
*
* @author Roman Grigoriadi
*/
abstract class GetValueCommand {
@FunctionalInterface
interface GetValueCommand {

/**
* Get a value with reflection on {@link java.lang.reflect.Field field} or {@link java.lang.reflect.Method getter}.
Expand All @@ -19,21 +18,5 @@ abstract class GetValueCommand {
* @throws InvocationTargetException if reflection fails.
* @return value
*/
abstract Object internalGetValue(Object object) throws IllegalAccessException, InvocationTargetException;

/**
* Get a value with reflection on {@link java.lang.reflect.Field field} or {@link java.lang.reflect.Method getter}.
*
* @param object object to invoke get value on, not null.
* @throws JsonbException if reflection fails.
* @return value
*/
final Object getValue(Object object) {
Objects.requireNonNull(object);
try {
return internalGetValue(object);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new JsonbException("Error getting value on: " + object, e);
}
}
}
Object getValue(Object object) throws IllegalAccessException, InvocationTargetException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
******************************************************************************/
package org.eclipse.yasson.internal.model;

import org.eclipse.yasson.internal.JsonbContext;

import javax.json.bind.config.PropertyVisibilityStrategy;
import javax.json.bind.JsonbException;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.lang.IllegalAccessException;
import java.util.Objects;

/**
* @author Roman Grigoriadi
Expand All @@ -36,12 +39,14 @@ public ReflectionPropagation(Property property, PropertyVisibilityStrategy strat
*/
@Override
protected void acceptMethod(Method method, OperationMode mode) {
Objects.requireNonNull(method);

switch (mode) {
case GET:
getValueCommand = new GetFromGetter(method);
getValueCommand = method::invoke;
break;
case SET:
setValueCommand = new SetWithSetter(method);
setValueCommand = method::invoke;
break;
default: throw new IllegalStateException("Unknown mode");
}
Expand All @@ -52,24 +57,52 @@ protected void acceptMethod(Method method, OperationMode mode) {
*/
@Override
protected void acceptField(Field field, OperationMode mode) {
Objects.requireNonNull(field);

switch (mode) {
case GET:
getValueCommand = new GetFromField(field);
getValueCommand = field::get;
break;
case SET:
setValueCommand = new SetWithField(field);
setValueCommand = field::set;
break;
default: throw new IllegalStateException("Unknown mode");
}
}

/**
* Sets a value with reflection on {@link java.lang.reflect.Field field} or {@link java.lang.reflect.Method setter}.
*
* @param object object to invoke set value on, not null.
* @param value object to be set, nullable.
* @throws JsonbException if reflection fails.
*/
@Override
void setValue(Object object, Object value) {
setValueCommand.setValue(object, value);
Objects.requireNonNull(object);

try {
setValueCommand.setValue(object, value);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new JsonbException("Error getting value on: " + object, e);
}
}

/**
* Get a value with reflection on {@link java.lang.reflect.Field field} or {@link java.lang.reflect.Method getter}.
*
* @param object object to invoke get value on, not null.
* @throws JsonbException if reflection fails.
* @return value
*/
@Override
Object getValue(Object object) {
return getValueCommand.getValue(object);
Objects.requireNonNull(object);

try {
return getValueCommand.getValue(object);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new JsonbException("Error getting value on: " + object, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
package org.eclipse.yasson.internal.model;

import javax.json.bind.JsonbException;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;

/**
* Wrapper for setting a value on javabean property.
*
* @author Roman Grigoriadi
*/
abstract class SetValueCommand {

abstract void internalSetValue(Object object, Object value) throws IllegalAccessException, InvocationTargetException;
@FunctionalInterface
interface SetValueCommand {

/**
* Sets a value with reflection on {@link java.lang.reflect.Field field} or {@link java.lang.reflect.Method setter}.
*
* @param object object to invoke set value on, not null.
* @param value object to be set, nullable.
* @throws JsonbException if reflection fails.
* @throws IllegalAccessException if reflection fails.
* @throws InvocationTargetException if reflection fails.
*/
final void setValue(Object object, Object value) {
Objects.requireNonNull(object);
try {
internalSetValue(object, value);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new JsonbException("Error getting value on: " + object, e);
}
}
void setValue(Object object, Object value) throws IllegalAccessException, InvocationTargetException;
}
30 changes: 0 additions & 30 deletions src/main/java/org/eclipse/yasson/internal/model/SetWithField.java

This file was deleted.

29 changes: 0 additions & 29 deletions src/main/java/org/eclipse/yasson/internal/model/SetWithSetter.java

This file was deleted.

0 comments on commit 06706b9

Please sign in to comment.