1 18 package org.objectweb.medor.expression.parser.lib; 19 20 import org.objectweb.medor.expression.api.Expression; 21 import org.objectweb.medor.expression.api.Operator; 22 import org.objectweb.medor.expression.api.MalformedExpressionException; 23 import org.objectweb.medor.expression.lib.Plus; 24 import org.objectweb.medor.expression.lib.Concat; 25 import org.objectweb.medor.expression.lib.TypeConverter; 26 import org.objectweb.jorm.type.api.PTypeSpace; 27 28 34 public class ReplaceStringPlusByConcat { 35 36 37 42 public Expression rewrite(Expression e) throws MalformedExpressionException { 43 if (e instanceof Operator) { 44 Operator op = (Operator) e; 45 for(int i=op.getOperandNumber()-1; i>=0; i--) { 46 op.setExpression(i, rewrite(op.getExpression(i))); 47 } 48 if (op instanceof Plus) { 49 Expression left = op.getExpression(0); 50 Expression right = op.getExpression(1); 51 if (left.getType() == PTypeSpace.STRING 52 || right.getType() == PTypeSpace.STRING) { 53 return new Concat( 54 convertToString(left), 55 convertToString(right)); 56 } 57 } 58 } 59 return e; 60 } 61 62 65 private Expression convertToString(Expression e) throws MalformedExpressionException { 66 return (e.getType() == PTypeSpace.STRING 67 ? e 68 : new TypeConverter(e, PTypeSpace.STRING)); 69 } 70 71 } 72 | Popular Tags |