Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
Always setAccessible
Browse files Browse the repository at this point in the history
  • Loading branch information
alta189 committed May 13, 2012
1 parent 31f252a commit e3ebddf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
Expand Up @@ -41,6 +41,7 @@ public static IdRegistration getId(Class<?> clazz) throws TableRegistrationExcep
// Check if id defaults to 0 // Check if id defaults to 0
try { try {
Object o = new EmptyInjector().newInstance(clazz); Object o = new EmptyInjector().newInstance(clazz);
field.setAccessible(true);
int id = ((Number)field.get(o)).intValue(); int id = ((Number)field.get(o)).intValue();
if (id != 0) if (id != 0)
throw new TableRegistrationException("The id does not default to 0"); throw new TableRegistrationException("The id does not default to 0");
Expand Down
Expand Up @@ -65,6 +65,7 @@ public static boolean validClass(Class<?> clazz) {
public static Object deserialize(Class<?> clazz, String data) { public static Object deserialize(Class<?> clazz, String data) {
try { try {
Method deserialize = clazz.getDeclaredMethod("deserialize", String.class); Method deserialize = clazz.getDeclaredMethod("deserialize", String.class);
deserialize.setAccessible(true);
return deserialize.invoke(null, data); return deserialize.invoke(null, data);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
throw new SerializeException("Could not deserialize data", e.getCause()); throw new SerializeException("Could not deserialize data", e.getCause());
Expand All @@ -78,6 +79,7 @@ public static Object deserialize(Class<?> clazz, String data) {
public static String serialize(Class<?> clazz, Object object) { public static String serialize(Class<?> clazz, Object object) {
try { try {
Method serialize = clazz.getDeclaredMethod("serialize"); Method serialize = clazz.getDeclaredMethod("serialize");
serialize.setAccessible(true);
return (String) serialize.invoke(object); return (String) serialize.invoke(object);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
throw new SerializeException("Could not serialize Class '" + clazz.getCanonicalName() + "'", e.getCause()); throw new SerializeException("Could not serialize Class '" + clazz.getCanonicalName() + "'", e.getCause());
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/alta189/simplesave/internal/TableUtils.java
Expand Up @@ -25,6 +25,7 @@ public static int getIdValue(TableRegistration table, Object o) {
IdRegistration idRegistration = table.getId(); IdRegistration idRegistration = table.getId();
try { try {
Field field = o.getClass().getDeclaredField(idRegistration.getName()); Field field = o.getClass().getDeclaredField(idRegistration.getName());
field.setAccessible(true);
return ((Number)field.get(o)).intValue(); return ((Number)field.get(o)).intValue();
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
Expand All @@ -36,6 +37,7 @@ public static int getIdValue(TableRegistration table, Object o) {
public static Integer getValueAsInteger(FieldRegistration fieldRegistration, Object o) { public static Integer getValueAsInteger(FieldRegistration fieldRegistration, Object o) {
try { try {
Field field = o.getClass().getDeclaredField(fieldRegistration.getName()); Field field = o.getClass().getDeclaredField(fieldRegistration.getName());
field.setAccessible(true);
return field.getInt(o); return field.getInt(o);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
Expand All @@ -45,6 +47,7 @@ public static Integer getValueAsInteger(FieldRegistration fieldRegistration, Obj
public static Long getValueAsLong(FieldRegistration fieldRegistration, Object o) { public static Long getValueAsLong(FieldRegistration fieldRegistration, Object o) {
try { try {
Field field = o.getClass().getDeclaredField(fieldRegistration.getName()); Field field = o.getClass().getDeclaredField(fieldRegistration.getName());
field.setAccessible(true);
return field.getLong(o); return field.getLong(o);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
Expand All @@ -54,6 +57,7 @@ public static Long getValueAsLong(FieldRegistration fieldRegistration, Object o)
public static Double getValueAsDouble(FieldRegistration fieldRegistration, Object o) { public static Double getValueAsDouble(FieldRegistration fieldRegistration, Object o) {
try { try {
Field field = o.getClass().getDeclaredField(fieldRegistration.getName()); Field field = o.getClass().getDeclaredField(fieldRegistration.getName());
field.setAccessible(true);
return field.getDouble(o); return field.getDouble(o);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
Expand All @@ -63,6 +67,7 @@ public static Double getValueAsDouble(FieldRegistration fieldRegistration, Objec
public static String getValueAsString(FieldRegistration fieldRegistration, Object o) { public static String getValueAsString(FieldRegistration fieldRegistration, Object o) {
try { try {
Field field = o.getClass().getDeclaredField(fieldRegistration.getName()); Field field = o.getClass().getDeclaredField(fieldRegistration.getName());
field.setAccessible(true);
return (String) field.get(o); return (String) field.get(o);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
Expand All @@ -72,6 +77,7 @@ public static String getValueAsString(FieldRegistration fieldRegistration, Objec
public static Boolean getValueAsBoolean(FieldRegistration fieldRegistration, Object o) { public static Boolean getValueAsBoolean(FieldRegistration fieldRegistration, Object o) {
try { try {
Field field = o.getClass().getDeclaredField(fieldRegistration.getName()); Field field = o.getClass().getDeclaredField(fieldRegistration.getName());
field.setAccessible(true);
return field.getBoolean(o); return field.getBoolean(o);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
Expand All @@ -81,6 +87,7 @@ public static Boolean getValueAsBoolean(FieldRegistration fieldRegistration, Obj
public static Short getValueAsShort(FieldRegistration fieldRegistration, Object o) { public static Short getValueAsShort(FieldRegistration fieldRegistration, Object o) {
try { try {
Field field = o.getClass().getDeclaredField(fieldRegistration.getName()); Field field = o.getClass().getDeclaredField(fieldRegistration.getName());
field.setAccessible(true);
return field.getShort(o); return field.getShort(o);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
Expand All @@ -90,6 +97,7 @@ public static Short getValueAsShort(FieldRegistration fieldRegistration, Object
public static Float getValueAsFloat(FieldRegistration fieldRegistration, Object o) { public static Float getValueAsFloat(FieldRegistration fieldRegistration, Object o) {
try { try {
Field field = o.getClass().getDeclaredField(fieldRegistration.getName()); Field field = o.getClass().getDeclaredField(fieldRegistration.getName());
field.setAccessible(true);
return field.getFloat(o); return field.getFloat(o);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
Expand All @@ -99,6 +107,7 @@ public static Float getValueAsFloat(FieldRegistration fieldRegistration, Object
public static Byte getValueAsByte(FieldRegistration fieldRegistration, Object o) { public static Byte getValueAsByte(FieldRegistration fieldRegistration, Object o) {
try { try {
Field field = o.getClass().getDeclaredField(fieldRegistration.getName()); Field field = o.getClass().getDeclaredField(fieldRegistration.getName());
field.setAccessible(true);
return field.getByte(o); return field.getByte(o);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
Expand All @@ -108,6 +117,7 @@ public static Byte getValueAsByte(FieldRegistration fieldRegistration, Object o)
public static String serializeField(FieldRegistration fieldRegistration, Object tableObject) { public static String serializeField(FieldRegistration fieldRegistration, Object tableObject) {
try { try {
Field field = tableObject.getClass().getDeclaredField(fieldRegistration.getName()); Field field = tableObject.getClass().getDeclaredField(fieldRegistration.getName());
field.setAccessible(true);
Object o = field.get(tableObject); Object o = field.get(tableObject);
return SerializedClassBuilder.serialize(fieldRegistration.getClass(), o); return SerializedClassBuilder.serialize(fieldRegistration.getClass(), o);
} catch (Exception e) { } catch (Exception e) {
Expand Down

0 comments on commit e3ebddf

Please sign in to comment.