- Timestamp:
- 01/09/13 00:09:10 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/params/ReflectionAccess.java
r77 r78 3 3 import java.lang.reflect.Field; 4 4 import java.lang.reflect.InvocationTargetException; 5 import java.lang.reflect.Modifier; 6 import java.lang.reflect.Type; 5 7 import java.util.List; 6 8 … … 43 45 String id = param.getId(); 44 46 try { 45 return type.cast( object.getClass().getField(id).get(object));47 return type.cast(reflectedClass.getField(id).get(object)); 46 48 } catch (NoSuchFieldException ignored) { 47 49 } 48 50 try { 49 return type.cast( object.getClass().getMethod(accessorName(true, id)).invoke(object));51 return type.cast(reflectedClass.getMethod(accessorName(true, id)).invoke(object)); 50 52 } catch (NoSuchMethodException ex) { 51 53 //ex.printStackTrace(); … … 68 70 } 69 71 70 private <T> void setValue(Param param, T val ) {72 private <T> void setValue(Param param, T value) { 71 73 if (object == null) { 72 74 return; … … 75 77 String id = param.getId(); 76 78 try { 77 object.getClass().getField(id).set(object, val); 79 Field f = reflectedClass.getField(id); 80 Class t = f.getType(); 81 if (Modifier.isFinal(f.getModifiers())) { 82 return; 83 } 84 if (value != null || (!t.isPrimitive())) { 85 f.set(object, value); 86 } 78 87 return; 79 88 } catch (NoSuchFieldException ignored) { 80 89 } 81 90 try { 82 object.getClass().getMethod(accessorName(false, id), new Class[]{val.getClass()}).invoke(object, val); 83 } catch (InvocationTargetException e) { 84 //e.printStackTrace(); 91 reflectedClass.getMethod(accessorName(false, id), new Class[]{param.getStorageType()}).invoke(object, value); 92 } catch (InvocationTargetException ignored) { 85 93 } catch (NoSuchMethodException ignored) { 86 94 } 87 } catch (IllegalAccessException ex) { 88 LOGGER.warn("illegal access error occurred while trying to access returnedObject"); 95 } catch (Exception ex) { 89 96 ex.printStackTrace(); 90 97 } … … 103 110 resetErrors(); 104 111 105 Field[] fields;106 112 try { 107 // TODO: conceptually invalid - should clean only fields that are specified in FramsClass 108 fields = object.getClass().getFields(); 109 110 for (Field field : fields) { 111 field.set(object, null); 113 for (Param p : framsClass.getParamEntries()) { 114 setValue(p, p.getDef(Object.class)); 112 115 } 113 } catch (IllegalAccessException ex) { 114 LOGGER.warn("IllegalAccessException thrown while trying to reset object values"); 116 } catch (IllegalArgumentException ex) { 115 117 ex.printStackTrace(); 116 118 } … … 125 127 @Override 126 128 public void select(Object object) { 129 assert object == null || reflectedClass.isInstance(object); 127 130 this.object = object; 128 131 } … … 166 169 } 167 170 LOGGER.fatal("failed to create reflected object of class " + reflectedClass.getCanonicalName() + " for frams type " + framsClass.getId()); 168 169 171 return null; 170 172 }
Note: See TracChangeset
for help on using the changeset viewer.