1 17 18 package javax.el; 19 20 import java.beans.FeatureDescriptor ; 21 import java.lang.reflect.Array ; 22 import java.util.Arrays ; 23 import java.util.Iterator ; 24 25 public class ArrayELResolver extends ELResolver { 26 27 private final boolean readOnly; 28 29 public ArrayELResolver() { 30 this.readOnly = false; 31 } 32 33 public ArrayELResolver(boolean readOnly) { 34 this.readOnly = readOnly; 35 } 36 37 public Object getValue(ELContext context, Object base, Object property) 38 throws NullPointerException , PropertyNotFoundException, ELException { 39 if (context == null) { 40 throw new NullPointerException (); 41 } 42 43 if (base != null && base.getClass().isArray()) { 44 context.setPropertyResolved(true); 45 int idx = coerce(property); 46 if (idx < 0 || idx >= Array.getLength(base)) { 47 return null; 48 } else { 49 return Array.get(base, idx); 50 } 51 } 52 53 return null; 54 } 55 56 public Class <?> getType(ELContext context, Object base, Object property) 57 throws NullPointerException , PropertyNotFoundException, ELException { 58 if (context == null) { 59 throw new NullPointerException (); 60 } 61 62 if (base != null && base.getClass().isArray()) { 63 context.setPropertyResolved(true); 64 int idx = coerce(property); 65 checkBounds(base, idx); 66 return base.getClass().getComponentType(); 67 } 68 69 return null; 70 } 71 72 public void setValue(ELContext context, Object base, Object property, 73 Object value) throws NullPointerException , 74 PropertyNotFoundException, PropertyNotWritableException, 75 ELException { 76 if (context == null) { 77 throw new NullPointerException (); 78 } 79 80 if (base != null && base.getClass().isArray()) { 81 context.setPropertyResolved(true); 82 83 if (this.readOnly) { 84 throw new PropertyNotWritableException(message(context, 85 "resolverNotWriteable", new Object [] { base.getClass() 86 .getName() })); 87 } 88 89 int idx = coerce(property); 90 checkBounds(base, idx); 91 Array.set(base, idx, value); 92 } 93 } 94 95 public boolean isReadOnly(ELContext context, Object base, Object property) 96 throws NullPointerException , PropertyNotFoundException, ELException { 97 if (context == null) { 98 throw new NullPointerException (); 99 } 100 101 if (base != null && base.getClass().isArray()) { 102 context.setPropertyResolved(true); 103 int idx = coerce(property); 104 checkBounds(base, idx); 105 } 106 107 return this.readOnly; 108 } 109 110 public Iterator <FeatureDescriptor > getFeatureDescriptors(ELContext context, Object base) { 111 if (base != null && base.getClass().isArray()) { 112 FeatureDescriptor [] descs = new FeatureDescriptor [Array.getLength(base)]; 113 for (int i = 0; i < descs.length; i++) { 114 descs[i] = new FeatureDescriptor (); 115 descs[i].setDisplayName("["+i+"]"); 116 descs[i].setExpert(false); 117 descs[i].setHidden(false); 118 descs[i].setName(""+i); 119 descs[i].setPreferred(true); 120 descs[i].setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.FALSE); 121 descs[i].setValue(TYPE, Integer .class); 122 } 123 return Arrays.asList(descs).iterator(); 124 } 125 return null; 126 } 127 128 public Class <?> getCommonPropertyType(ELContext context, Object base) { 129 if (base != null && base.getClass().isArray()) { 130 return Integer .class; 131 } 132 return null; 133 } 134 135 private final static void checkBounds(Object base, int idx) { 136 if (idx < 0 || idx >= Array.getLength(base)) { 137 throw new PropertyNotFoundException( 138 new ArrayIndexOutOfBoundsException (idx).getMessage()); 139 } 140 } 141 142 private final static int coerce(Object property) { 143 if (property instanceof Number ) { 144 return ((Number ) property).intValue(); 145 } 146 if (property instanceof Character ) { 147 return ((Character ) property).charValue(); 148 } 149 if (property instanceof Boolean ) { 150 return (((Boolean ) property).booleanValue() ? 1 : 0); 151 } 152 if (property instanceof String ) { 153 return Integer.parseInt((String ) property); 154 } 155 throw new IllegalArgumentException (property != null ? property 156 .toString() : "null"); 157 } 158 159 } 160 | Popular Tags |