|
| 1 | +package paintcomponents.java.lazy; |
| 2 | + |
| 3 | +import java.lang.reflect.InvocationTargetException; |
| 4 | +import java.lang.reflect.Method; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.NoSuchElementException; |
| 7 | + |
| 8 | +import paintcomponents.NoConnectingLineSegmentException; |
| 9 | +import paintcomponents.data.DataFromPoint; |
| 10 | +import paintcomponents.data.DataFromPointDataProvider; |
| 11 | +import paintcomponents.data.DataFromPointNoDataProviderException; |
| 12 | +import paintcomponents.data.DataFromPointProviderCannotProvideDataException; |
| 13 | +import paintcomponents.data.DataTextIOPaintComponent; |
| 14 | +import paintcomponents.data.DataToPoint; |
| 15 | + |
| 16 | +public class MethodPaintComponent extends DataTextIOPaintComponent |
| 17 | + implements DataFromPointDataProvider { |
| 18 | + |
| 19 | + private Method displayingMethod; |
| 20 | + |
| 21 | + public MethodPaintComponent(Method displayingMethod, int x, int y) { |
| 22 | + super(displayingMethod.toString(), x, y); |
| 23 | + this.displayingMethod = displayingMethod; |
| 24 | + init(); |
| 25 | + } |
| 26 | + /* |
| 27 | + * |
| 28 | + * |
| 29 | + * line 0 is signature |
| 30 | + * |
| 31 | + * |
| 32 | + * |
| 33 | + * line 1 is the operating instance |
| 34 | + * |
| 35 | + * |
| 36 | + * |
| 37 | + * parameters take place from line 2 to length+1 |
| 38 | + * |
| 39 | + * |
| 40 | + * |
| 41 | + * method's return value take line length+2 |
| 42 | + * |
| 43 | + * |
| 44 | + * |
| 45 | + * |
| 46 | + */ |
| 47 | + |
| 48 | + private void init() { |
| 49 | + |
| 50 | + // line 0 is signature |
| 51 | + // line 1 is the operating instance |
| 52 | + addToPoint(1); |
| 53 | + // parameters take place from line 2 to length+1 |
| 54 | + Class[] paramTypes = displayingMethod.getParameterTypes(); |
| 55 | + for (int i = 2; i < paramTypes.length + 2; i++) { |
| 56 | + addToPoint(i); |
| 57 | + } |
| 58 | + |
| 59 | + // method's return value take line length+2 |
| 60 | + addFromPoint(this, paramTypes.length + 2); |
| 61 | + |
| 62 | + // prepare String |
| 63 | + StringBuilder s = new StringBuilder(); |
| 64 | + s.append(this.displayingMethod.toString() + "\n"); |
| 65 | + s.append("<< Operating Instance :: " + this.displayingMethod.toString() |
| 66 | + + "\n"); |
| 67 | + for (int i = 0; i < paramTypes.length; i++) { |
| 68 | + s.append("arg" + i + " :: " + paramTypes[i].getName() + "\n"); |
| 69 | + } |
| 70 | + |
| 71 | + s.append("Return value" |
| 72 | + + this.displayingMethod.getReturnType().toString() + " >>>> " |
| 73 | + + "\n"); |
| 74 | + setDisplayingText(s.toString()); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public Object provideInformationToDataFromPoint( |
| 80 | + DataFromPoint dataFromPoint) { |
| 81 | + // TODO Auto-generated method stub |
| 82 | + |
| 83 | + // prepare argument list |
| 84 | + ArrayList<DataToPoint> toPoints = getToPoints(); |
| 85 | + |
| 86 | + Object operatingInstance = null; |
| 87 | + try { |
| 88 | + operatingInstance = toPoints.get(0).fetchData(); |
| 89 | + } catch (NoSuchElementException | NoConnectingLineSegmentException |
| 90 | + | DataFromPointNoDataProviderException |
| 91 | + | DataFromPointProviderCannotProvideDataException e1) { |
| 92 | + e1.printStackTrace(); |
| 93 | + // TODO Handle Exception |
| 94 | + // Note: a static method may not contain an valid instance |
| 95 | + // throw new IllegalStateException(); |
| 96 | + } |
| 97 | + |
| 98 | + // args takes toPoint 1 to size |
| 99 | + Object[] args = new Object[toPoints.size() - 1]; |
| 100 | + |
| 101 | + for (int i = 1; i < toPoints.size(); i++) { |
| 102 | + DataToPoint toPoint = toPoints.get(i); |
| 103 | + try { |
| 104 | + args[i] = toPoint.fetchData(); |
| 105 | + } catch (NoSuchElementException | NoConnectingLineSegmentException |
| 106 | + | DataFromPointNoDataProviderException |
| 107 | + | DataFromPointProviderCannotProvideDataException e) { |
| 108 | + e.printStackTrace(); |
| 109 | + // TODO Handle Exception |
| 110 | + // arguments must be valid |
| 111 | + throw new IllegalStateException(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + try { |
| 116 | + return this.displayingMethod.invoke(operatingInstance, args); |
| 117 | + } catch (IllegalAccessException | IllegalArgumentException |
| 118 | + | InvocationTargetException e) { |
| 119 | + // TODO Auto-generated catch block |
| 120 | + e.printStackTrace(); |
| 121 | + throw new IllegalStateException(); |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public boolean canProvideInformationToDataFromPoint( |
| 128 | + DataFromPoint dataFromPoint) { |
| 129 | + boolean isFirst = true; |
| 130 | + |
| 131 | + // this class is LAZY, don't care about states |
| 132 | + // skip the first toPoint if this method is static |
| 133 | + // TODO check if the method is static |
| 134 | + for (DataToPoint toPoint : getToPoints()) { |
| 135 | + if (isFirst) { |
| 136 | + isFirst = false; |
| 137 | + continue; |
| 138 | + } |
| 139 | + try { |
| 140 | + toPoint.fetchData(); |
| 141 | + } catch (Exception e) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + isFirst = false; |
| 145 | + |
| 146 | + } |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments