1 23 24 package com.sun.el.parser; 25 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 29 import javax.el.ELException; 30 import javax.el.ELResolver; 31 import javax.el.MethodInfo; 32 import javax.el.PropertyNotFoundException; 33 34 import com.sun.el.lang.EvaluationContext; 35 import com.sun.el.util.MessageFactory; 36 import com.sun.el.util.ReflectionUtil; 37 38 42 public final class AstValue extends SimpleNode { 43 44 protected static class Target { 45 protected Object base; 46 47 protected Object property; 48 } 49 50 public AstValue(int id) { 51 super(id); 52 } 53 54 public Class getType(EvaluationContext ctx) throws ELException { 55 Target t = getTarget(ctx); 56 ctx.setPropertyResolved(false); 57 return ctx.getELResolver().getType(ctx, t.base, t.property); 58 } 59 60 private final Target getTarget(EvaluationContext ctx) throws ELException { 61 Object base = this.children[0].getValue(ctx); 63 64 if (base == null) { 66 throw new PropertyNotFoundException(MessageFactory.get( 67 "error.unreachable.base", this.children[0].getImage())); 68 } 69 70 Object property = null; 72 int propCount = this.jjtGetNumChildren() - 1; 73 int i = 1; 74 75 ELResolver resolver = ctx.getELResolver(); 77 if (propCount > 1) { 78 while (base != null && i < propCount) { 79 property = this.children[i].getValue(ctx); 80 ctx.setPropertyResolved(false); 81 base = resolver.getValue(ctx, base, property); 82 i++; 83 } 84 if (base == null || property == null) { 87 throw new PropertyNotFoundException(MessageFactory.get( 88 "error.unreachable.property", property)); 89 } 90 } 91 92 property = this.children[i].getValue(ctx); 93 94 if (property == null) { 95 throw new PropertyNotFoundException(MessageFactory.get( 96 "error.unreachable.property", this.children[i])); 97 } 98 99 Target t = new Target(); 100 t.base = base; 101 t.property = property; 102 return t; 103 } 104 105 public Object getValue(EvaluationContext ctx) throws ELException { 106 Object base = this.children[0].getValue(ctx); 107 int propCount = this.jjtGetNumChildren(); 108 int i = 1; 109 Object property = null; 110 ELResolver resolver = ctx.getELResolver(); 111 while (base != null && i < propCount) { 112 property = this.children[i].getValue(ctx); 113 if (property == null) { 114 return null; 115 } else { 116 ctx.setPropertyResolved(false); 117 base = resolver.getValue(ctx, base, property); 118 } 119 i++; 120 } 121 return base; 122 } 123 124 public boolean isReadOnly(EvaluationContext ctx) throws ELException { 125 Target t = getTarget(ctx); 126 ctx.setPropertyResolved(false); 127 return ctx.getELResolver().isReadOnly(ctx, t.base, t.property); 128 } 129 130 public void setValue(EvaluationContext ctx, Object value) 131 throws ELException { 132 Target t = getTarget(ctx); 133 ctx.setPropertyResolved(false); 134 ctx.getELResolver().setValue(ctx, t.base, t.property, value); 135 } 136 137 public MethodInfo getMethodInfo(EvaluationContext ctx, Class [] paramTypes) 138 throws ELException { 139 Target t = getTarget(ctx); 140 Method m = ReflectionUtil.getMethod(t.base, t.property, paramTypes); 141 return new MethodInfo(m.getName(), m.getReturnType(), m 142 .getParameterTypes()); 143 } 144 145 public Object invoke(EvaluationContext ctx, Class [] paramTypes, 146 Object [] paramValues) throws ELException { 147 Target t = getTarget(ctx); 148 Method m = ReflectionUtil.getMethod(t.base, t.property, paramTypes); 149 Object result = null; 150 try { 151 result = m.invoke(t.base, (Object []) paramValues); 152 } catch (IllegalAccessException iae) { 153 throw new ELException(iae); 154 } catch (InvocationTargetException ite) { 155 throw new ELException(ite.getCause()); 156 } 157 return result; 158 } 159 } 160 | Popular Tags |