1 package org.jicengine.element.impl; 2 3 import org.jicengine.operation.OperationException; 4 import org.jicengine.operation.Context; 5 import org.jicengine.operation.Operation; 6 import org.jicengine.element.*; 7 import java.util.*; 8 9 18 19 public class ArrayElementCompiler extends ElementCompiler { 20 21 26 public class ArrayConstructor implements Operation { 27 private int size = 0; 28 29 public void increaseSize() 30 { 31 this.size++; 32 } 33 34 public int getSize() 35 { 36 return this.size; 37 } 38 39 public boolean needsParameters() 40 { 41 return false; 42 } 43 44 public boolean needsParameter(String name) 45 { 46 return false; 47 } 48 49 public Object execute(Context context) throws OperationException 50 { 51 Class componentType = getElement().getInstanceClass().getComponentType(); 52 Object arrayObject = java.lang.reflect.Array.newInstance(componentType, this.size); 53 return arrayObject; 54 } 55 56 public String toString() 57 { 58 return "{array creator}"; 59 } 60 } 61 62 63 64 private int index = 0; 65 66 public ArrayElementCompiler(String name, Location location) throws ElementException 67 { 68 super(name, location); 69 70 getElement().setConstructor(new ArrayConstructor()); 71 72 } 73 74 public void elementInitialized() throws ElementException 75 { 76 super.elementInitialized(); 77 if( !getElement().getInstanceClass().isArray() ){ 78 throw new ElementException("Class '" + getElement().getInstanceClass().getName() + "' is not an array.", getName(), getLocation()); 79 } 80 } 81 82 protected ActionElement handleLooseVariableElement(VariableElement child) throws ElementException 83 { 84 ArrayConstructor constructor = (ArrayConstructor) getElement().getConstructor(); 86 87 constructor.increaseSize(); 88 int size = constructor.getSize(); 89 int index = size-1; 90 Operation action = new StoreToArrayOperation(index); 91 return new WrapperActionElement(child, child.getLocation(), action); 92 } 93 94 public class StoreToArrayOperation implements Operation { 95 private int index; 96 public StoreToArrayOperation(int index) 97 { 98 this.index = index; 99 } 100 public boolean needsParameter(String name) 101 { 102 return name.equals(Element.VARIABLE_NAME_PARENT_INSTANCE) || name.equals(Element.VARIABLE_NAME_ELEMENT_INSTANCE); 103 } 104 public boolean needsParameters() 105 { 106 return true; 107 } 108 109 public Object execute(Context context) throws OperationException 110 { 111 Object array = context.getObject(Element.VARIABLE_NAME_PARENT_INSTANCE); 112 Object element = context.getObject(Element.VARIABLE_NAME_ELEMENT_INSTANCE); 113 114 try { 115 java.lang.reflect.Array.set(array, this.index, element); 116 } catch (IllegalArgumentException e){ 117 throw new OperationException("[array-position " + this.index + "]: failed to store value '" + element + " (class '" + element.getClass().getName() + "')", e); 118 } 119 return null; 120 } 121 122 public String toString() 123 { 124 return "java.lang.reflect.Array.set(" + Element.VARIABLE_NAME_PARENT_INSTANCE + ", " + this.index + ", " + Element.VARIABLE_NAME_ELEMENT_INSTANCE + ")"; 125 } 126 } 127 } 128 | Popular Tags |