Changeset 78 for java/main/src/main/java/com/framsticks/params
- Timestamp:
- 01/09/13 00:09:10 (11 years ago)
- Location:
- java/main/src/main/java/com/framsticks/params
- Files:
-
- 1 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/params/AccessInterface.java
r77 r78 80 80 81 81 /** 82 * Load values from single line String.83 *84 * @param line85 * the line with values86 * @return the list of not severe exceptions which occurred while loading87 * @throws Exception88 * the severe exception occurred while loading89 */90 List<Exception> load2(String line) throws Exception;91 92 /**93 82 * Removes all the properties values. 94 83 */ … … 107 96 FramsClass getFramsClass(); 108 97 98 109 99 } -
java/main/src/main/java/com/framsticks/params/FramsClass.java
r77 r78 5 5 import com.framsticks.params.types.FloatParam; 6 6 import com.framsticks.params.types.StringParam; 7 import com.framsticks.parsers.FileSource; 8 import com.framsticks.parsers.Loaders; 7 9 import com.framsticks.util.Casting; 8 10 import org.apache.log4j.Logger; 9 11 10 12 import javax.lang.model.element.TypeElement; 13 import java.io.InputStream; 11 14 import java.lang.reflect.*; 12 15 import java.util.*; … … 58 61 59 62 public Collection<Param> getParamEntries() { 60 return param EntryMap.values();63 return paramList; 61 64 } 62 65 … … 214 217 } 215 218 } 219 if (rawType.equals(List.class)) { 220 Type containedType = p.getActualTypeArguments()[0]; 221 if (containedType instanceof Class) { 222 return "l " + ((Class) containedType).getCanonicalName(); 223 } 224 } 216 225 return null; 217 226 } … … 234 243 public static final String GENERATE_HELP_PREFIX = "automatically generated from: "; 235 244 236 237 238 public static class Constructor { 245 public static FramsClass readFromStream(InputStream stream) { 246 return Loaders.loadFramsClass(new FileSource(stream)); 247 } 248 249 public static class Constructor { 239 250 protected final FramsClass result; 240 251 protected Class currentClass; … … 249 260 currentClass = currentClass.getSuperclass(); 250 261 } 262 currentClass = src; 251 263 } 252 264 … … 254 266 return result; 255 267 } 268 269 public Constructor allFields() { 270 for (Field f : currentClass.getFields()) { 271 field(f.getName()); 272 } 273 return this; 274 } 256 275 257 276 public Constructor method(String name, Class<?> ... arguments) { -
java/main/src/main/java/com/framsticks/params/ListAccess.java
r77 r78 73 73 } 74 74 75 @Override76 public List<Exception> load2(String line) throws Exception {77 return null;78 }79 80 75 public AccessInterface getElementAccess() { 81 76 return elementAccess; -
java/main/src/main/java/com/framsticks/params/Param.java
r77 r78 109 109 } catch (ClassCastException e) { 110 110 throw new ClassCastException("property \"" + name 111 + "\" getType is \"" + value.getClass().getName()111 + "\" type is \"" + value.getClass().getName() 112 112 + "\", not \"" + type.getName() + "\""); 113 113 } -
java/main/src/main/java/com/framsticks/params/PropertiesAccess.java
r77 r78 20 20 @Override 21 21 public Map<String, Object> createAccessee() { 22 return PropertiesAccess.createPropertiesMap(); 23 } 24 25 public static Map<String, Object> createPropertiesMap() { 22 26 return new HashMap<String, Object>(); 23 27 } 24 25 28 26 29 public PropertiesAccess(FramsClass framsClass) { … … 83 86 84 87 85 86 87 88 } -
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 } -
java/main/src/main/java/com/framsticks/params/SimpleAbstractAccess.java
r77 r78 38 38 this.framsClass = framsClass; 39 39 } 40 41 42 43 44 40 /** 45 41 * Simple String key, value class. 46 42 */ 47 p rivatestatic class Entry {48 49 String key;50 String value;51 52 Entry(String key, String value) {43 public static class Entry { 44 45 public final String key; 46 public final String value; 47 48 public Entry(String key, String value) { 53 49 this.key = key; 54 50 this.value = value; … … 331 327 332 328 Entry entry; 333 334 329 while ((entry = readEntry(source)) != null) { 335 330 Param param = getParam(entry.key); … … 351 346 } 352 347 353 @Override 354 public List<Exception> load2(String line) throws Exception { 355 this.clearValues(); 356 357 // list of not terminable exceptions that occured 358 List<Exception> exceptions = new ArrayList<Exception>(); 359 360 int indexOfColon = line.indexOf(':'); 361 if (indexOfColon < 0) 362 indexOfColon = line.length(); 363 String classId = line.substring(0, indexOfColon).trim(); 364 if (!getId().equals(classId)) 365 throw new Exception( 366 "Inappropriate getId of param interface (class), specified \"" 367 + classId + "\" while should be \"" + getId() 368 + "\""); 369 String parameters; 370 if (indexOfColon == line.length()) 371 parameters = ""; 372 else 373 parameters = line.substring(indexOfColon + 1); 374 375 // tokenize 376 boolean doubleQuotes = false; 377 char previousChar = ','; 378 List<Entry> result = new ArrayList<Entry>(); 379 StringBuilder stringBuilder = new StringBuilder(); 380 String key = ""; 381 if (parameters.trim().length() > 0) { 382 for (char currentChar : parameters.toCharArray()) { 383 if (!doubleQuotes && currentChar == '=' && "".equals(key)) { 384 key = stringBuilder.toString(); 385 stringBuilder = new StringBuilder(); 386 } else if (!doubleQuotes && currentChar == ',') { 387 if (previousChar == ',') { 388 result.add(new Entry(key.trim(), null)); 389 } else { 390 result.add(new Entry(key.trim(), stringBuilder 391 .toString().trim())); 392 } 393 stringBuilder = new StringBuilder(); 394 key = ""; 395 } else if (currentChar == '"') { 396 if (previousChar == '\\') { 397 stringBuilder.deleteCharAt(stringBuilder.length() - 1); 398 stringBuilder.append(currentChar); 399 } else 400 doubleQuotes = !doubleQuotes; 401 } else { 402 stringBuilder.append(currentChar); 403 } 404 405 previousChar = currentChar; 406 } 407 408 String last = stringBuilder.toString().trim(); 409 // if (last.length() > 0 || previousChar == '\"' 410 // || previousChar == ':') 411 result.add(new Entry(key.trim(), last)); 412 413 if (doubleQuotes) 414 throw new Exception( 415 "Double quotes expected while end of line met"); 416 } 417 418 // if successfully parsed set all necessary values 419 420 //TODO: name omitting 421 Param currentParam = null; 422 boolean illegallyOmittedName = false; 423 for (Entry pair : result) { 424 try { 425 426 if (pair.key != null && !"".equals(pair.key)) { 427 Param param = getParam(pair.key); 428 if (param == null) { 429 illegallyOmittedName = true; 430 throw new Exception("No parameter with such id: " 431 + pair.key); 432 } else { 433 currentParam = param; 434 illegallyOmittedName = false; 435 } 436 } else if (illegallyOmittedName 437 || (currentParam.getFlags() & Flags.CANOMITNAME) == 0) { 438 throw new Exception( 439 "Parameter with offset: " 440 + currentParam 441 + " is not set, " 442 + "because it's definition or definition of one of the previous params " 443 + "does not contain flag, which allow to skip the getName (flag 1024)"); 444 } 445 446 if (pair.value != null) { 447 int setFlag = this.set(currentParam, pair.value); 448 if ((setFlag & Flags.PSET_HITMIN) != 0) { 449 exceptions.add(createBoundaryHitException(currentParam, pair.value, Flags.PSET_HITMIN)); 450 } 451 452 if ((setFlag & Flags.PSET_HITMAX) != 0) { 453 exceptions.add(createBoundaryHitException(currentParam, pair.value, Flags.PSET_HITMAX)); 454 } 455 456 if ((setFlag & Flags.PSET_RONLY) != 0) { 457 throw (new Exception( 458 "Tried to set a read-only attribute \"" 459 + currentParam.getId() 460 + "\" in class \"" + getId() + "\"")); 461 } 462 } 463 464 } catch (Exception e) { 465 exceptions.add(e); 466 //} finally { 467 // currentProperty++; 468 } 469 } 470 return exceptions; 471 } 472 473 private Exception createBoundaryHitException(Param param, String value, int flag) { 474 boolean minimum = (flag & Flags.PSET_HITMIN) != 0; 475 String boundary = (minimum ? param.getMin(Object.class) : param.getMax(Object.class)).toString(); 476 String name = (minimum ? "minimum" : "maximum"); 477 return new Exception("Tried to set attribute \"" 478 + param.getId() 479 + "\" in class \"" 480 + getId() 481 + "\" to value which exceeds " + name + " (" 482 + value 483 + "), truncated to: " 484 + boundary); 485 } 486 348 349 487 350 protected abstract <T> void internalSet(Param param, T value); 488 351 -
java/main/src/main/java/com/framsticks/params/Util.java
r77 r78 1 1 package com.framsticks.params; 2 3 import java.util.ArrayList; 4 import java.util.List; 2 5 3 6 /** … … 14 17 return result.toString(); 15 18 } 19 public static List<Object> stripAccessInterface(List<AccessInterface> accesses) { 20 List<Object> result = new ArrayList<Object>(); 21 for (AccessInterface a : accesses) { 22 result.add(a.getSelected()); 23 } 24 return result; 25 } 26 public static int copyParams(AccessInterface to, AccessInterface from) { 27 int copied = 0; 28 for (Param f : from.getParams()) { 29 Param t = from.getParam(f.getId()); 30 if (t == null) { 31 continue; 32 } 33 if (to.getClass() != f.getClass()) { 34 continue; 35 } 36 to.set(t, from.get(f, Object.class)); 37 ++copied; 38 } 39 return copied; 40 } 16 41 }
Note: See TracChangeset
for help on using the changeset viewer.