- Timestamp:
- 08/12/2005 07:01:46 PM (21 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 3 edited
-
src/ws/fugue88/jpath/BrokenGraphException.java (added)
-
src/ws/fugue88/jpath/Context.java (modified) (5 diffs)
-
src/ws/fugue88/jpath/ContextFactory.java (modified) (2 diffs)
-
testsrc/ws/fugue88/jpath/ContextTest.java (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/ws/fugue88/jpath/Context.java
r2 r4 3 3 * 4 4 * $Log$ 5 * Revision 1.2 2005/08/12 19:01:46 dsowen 6 * Feature: can automatically fill missing parts of a graph. 7 * 5 8 * Revision 1.1 2005/08/03 00:35:29 dsowen 6 9 * Initial commit. … … 26 29 27 30 public Accessor getTargetAccessor(final Path path) 28 throws InvocationTargetException, NoSuchPropertyException 31 throws BrokenGraphException, IllegalAccessException, 32 InstantiationException, InvocationTargetException, 33 NoSuchPropertyException 29 34 { 30 35 Context current = this; … … 32 37 for(Iterator i = path.iterator(); i.hasNext();) { 33 38 accessor = current.getTargetAccessor((PathPart)i.next()); 34 current = new Context(accessor.getValue(), _binder); 39 Object obj = accessor.getValue(); 40 if(obj == null) { 41 if(_modifying && i.hasNext()) { 42 obj = createBlank(accessor.getType()); 43 accessor.setValue(obj); 44 } else { 45 throw new BrokenGraphException(); 46 } 47 } 48 current = new Context(obj, _binder, _modifying); 35 49 } 36 50 return accessor; 37 51 } 38 52 39 public Object getTarget(final Path path) throws InvocationTargetException, 53 public Object getTarget(final Path path) throws IllegalAccessException, 54 InstantiationException, InvocationTargetException, 40 55 NoSuchPropertyException 41 56 { 42 return getTargetAccessor(path).getValue(); 57 try { 58 return getTargetAccessor(path).getValue(); 59 } catch(BrokenGraphException e) { 60 return null; 61 } 43 62 } 44 63 45 public Context navigate(final Path path) throws InvocationTargetException, 64 public Context navigate(final Path path) throws IllegalAccessException, 65 InstantiationException, InvocationTargetException, 46 66 NoSuchPropertyException 47 67 { 48 68 Object child = getTarget(path); 49 return new Context(child, _binder );69 return new Context(child, _binder, _modifying); 50 70 } 51 71 … … 55 75 } 56 76 57 Context(final Object root, final Binder binder )77 Context(final Object root, final Binder binder, final boolean modifying) 58 78 { 79 // NPEs 80 root.getClass(); 81 binder.getClass(); 82 59 83 _root = root; 60 84 _binder = binder; 85 _modifying = modifying; 61 86 } 62 87 … … 83 108 } 84 109 110 private Object createBlank(final Class type) throws IllegalAccessException, 111 InstantiationException 112 { 113 return type.newInstance(); 114 } 115 85 116 private final Object _root; 86 117 private final Binder _binder; 118 private final boolean _modifying; 87 119 } -
trunk/src/ws/fugue88/jpath/ContextFactory.java
r2 r4 3 3 * 4 4 * $Log$ 5 * Revision 1.2 2005/08/12 19:01:46 dsowen 6 * Feature: can automatically fill missing parts of a graph. 7 * 5 8 * Revision 1.1 2005/08/03 00:35:29 dsowen 6 9 * Initial commit. … … 16 19 public Context createContext(final Object root) 17 20 { 18 return new Context(root, _binder); 21 return new Context(root, _binder, false); 22 } 23 24 public Context createModifyingContext(final Object root) 25 { 26 return new Context(root, _binder, true); 19 27 } 20 28 -
trunk/testsrc/ws/fugue88/jpath/ContextTest.java
r2 r4 3 3 * 4 4 * $Log$ 5 * Revision 1.2 2005/08/12 19:01:46 dsowen 6 * Feature: can automatically fill missing parts of a graph. 7 * 5 8 * Revision 1.1 2005/08/03 00:35:29 dsowen 6 9 * Initial commit. … … 101 104 assertEquals("done", a.b.get("c")); 102 105 } 106 107 public void testEarlyNull() throws Exception 108 { 109 class A { 110 111 String s; 112 } 113 class B { 114 115 A a; 116 } 117 B b = new B(); 118 119 ContextFactory factory = new ContextFactory(); 120 Context context = factory.createContext(b); 121 assertNull(context.getTarget(new Path("/a/s"))); 122 assertNull(b.a); 123 } 124 125 static class A { 126 127 String s; 128 } 129 130 public void testAutoGraph() throws Exception 131 { 132 class B { 133 134 A a; 135 } 136 B b = new B(); 137 138 ContextFactory factory = new ContextFactory(); 139 Context context = factory.createModifyingContext(b); 140 assertNull(context.getTarget(new Path("/a/s"))); 141 assertNotNull(b.a); 142 assertNull(b.a.s); 143 } 103 144 }
Note: See TracChangeset
for help on using the changeset viewer.
