Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap calls to setAccessible in try-catch #178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -49,19 +49,25 @@ protected JsonBindingException couldNotSerialize(Throwable e) {

public static class MethodAccessor extends PropertyAccessor {
protected final Method _getter;
protected boolean accessible = true;

public MethodAccessor(String name, Method getter, Type type, Class<?> concreteClass) {
super(name, type, getter.getDeclaringClass(), concreteClass, getter.getAnnotations(), getter.getModifiers());
this._getter = getter;
if (!_getter.isAccessible()) {
_getter.setAccessible(true);
try{
_getter.setAccessible(true);
}
catch(Exception e){
accessible = false;
}
}
}

@Override
public Object access(final Object target) {
try {
return _getter.invoke(target);
return accessible ? _getter.invoke(target) : null;
} catch (IllegalArgumentException e) {
throw couldNotAccess(e);
} catch (IllegalAccessException e) {
@@ -84,19 +90,25 @@ int priority() {

public static class FieldAccessor extends PropertyAccessor {
protected final Field _field;
protected boolean accessible = true;

public FieldAccessor(String name, Field field, Type type, Class<?> concreteClass) {
super(name, type, field.getDeclaringClass(), concreteClass, field.getAnnotations(), field.getModifiers());
this._field = field;
if (!_field.isAccessible()) {
_field.setAccessible(true);
try{
_field.setAccessible(true);
}
catch(Exception e){
accessible = false;
}
}
}

@Override
public Object access(final Object target) {
try {
return _field.get(target);
return accessible ? _field.get(target) : null;
} catch (IllegalArgumentException e) {
throw couldNotAccess(e);
} catch (IllegalAccessException e) {
Original file line number Diff line number Diff line change
@@ -52,19 +52,27 @@ protected JsonBindingException couldNotDeserialize(Throwable e) {

public static class MethodMutator extends PropertyMutator {
protected final Method _setter;
protected boolean accessible = true;

public MethodMutator(String name, Method setter, Type type, Class<?> concreteClass) {
super(name, type, setter.getDeclaringClass(), concreteClass, setter.getAnnotations(), setter.getModifiers());
this._setter = setter;
if (!_setter.isAccessible()) {
_setter.setAccessible(true);
try{
_setter.setAccessible(true);
}
catch(Exception e){
accessible = false;
}
}
}

@Override
public void mutate(Object target, Object value) {
try {
_setter.invoke(target, value);
if(accessible){
_setter.invoke(target, value);
}
} catch (IllegalArgumentException e) {
throw couldNotMutate(e);
} catch (IllegalAccessException e) {
@@ -87,19 +95,27 @@ public int priority() {

public static class FieldMutator extends PropertyMutator {
protected final Field _field;
protected boolean accessible = true;

public FieldMutator(String name, Field field, Type type, Class<?> concreteClass) {
super(name, type, field.getDeclaringClass(), concreteClass, field.getAnnotations(), field.getModifiers());
this._field = field;
if (!_field.isAccessible()) {
_field.setAccessible(true);
try{
_field.setAccessible(true);
}
catch(Exception e){
accessible = false;
}
}
}

@Override
public void mutate(Object target, Object value) {
try {
_field.set(target, value);
if(accessible){
_field.set(target, value);
}
} catch (IllegalArgumentException e) {
throw couldNotMutate(e);
} catch (IllegalAccessException e) {