001 package railo.transformer.bytecode.expression.var; 002 003 import java.util.ArrayList; 004 import java.util.Iterator; 005 import java.util.List; 006 007 import org.objectweb.asm.Type; 008 009 import railo.runtime.type.Scope; 010 import railo.runtime.type.scope.ScopeFactory; 011 import railo.transformer.bytecode.BytecodeContext; 012 import railo.transformer.bytecode.BytecodeException; 013 import railo.transformer.bytecode.Literal; 014 import railo.transformer.bytecode.expression.ExprString; 015 import railo.transformer.bytecode.expression.Expression; 016 import railo.transformer.bytecode.expression.ExpressionBase; 017 import railo.transformer.bytecode.literal.LitString; 018 019 public final class VariableString extends ExpressionBase implements ExprString { 020 021 private Expression expr; 022 023 public VariableString(Expression expr) { 024 super(expr.getLine()); 025 this.expr=expr; 026 } 027 028 public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException { 029 return translateVariableToExprString(expr).writeOut(bc, mode); 030 } 031 032 public static ExprString toExprString(Expression expr) { 033 if(expr instanceof ExprString) return (ExprString) expr; 034 return new VariableString(expr); 035 } 036 037 public static ExprString translateVariableToExprString(Expression expr) throws BytecodeException { 038 if(expr instanceof ExprString) return (ExprString) expr; 039 return LitString.toExprString(translateVariableToString(expr), expr.getLine()); 040 } 041 042 private static String translateVariableToString(Expression expr) throws BytecodeException { 043 if(!(expr instanceof Variable)) throw new BytecodeException("can't translate value to a string",expr.getLine()); 044 return variableToString((Variable) expr); 045 } 046 047 048 public static String variableToString(Variable var) throws BytecodeException { 049 return railo.runtime.type.List.arrayToList(variableToStringArray(var),"."); 050 } 051 public static String[] variableToStringArray(Variable var) throws BytecodeException { 052 List members = var.getMembers(); 053 054 List<String> arr=new ArrayList<String>(); 055 if(var.getScope()!=Scope.SCOPE_UNDEFINED)arr.add(ScopeFactory.toStringScope(var.getScope(),"undefined")); 056 Iterator it = members.iterator(); 057 DataMember dm; 058 Expression n; 059 Literal l; 060 while(it.hasNext()) { 061 Object o = it.next(); 062 if(!(o instanceof DataMember)) throw new BytecodeException("can't translate Variable to a String",var.getLine()); 063 dm=(DataMember) o; 064 n=dm.getName(); 065 if(n instanceof Literal) { 066 l=(Literal) n; 067 arr.add(l.getString()); 068 } 069 else throw new BytecodeException("argument name must be a constant value",var.getLine()); 070 } 071 return arr.toArray(new String[arr.size()]); 072 } 073 074 public String castToString() throws BytecodeException{ 075 return translateVariableToString(expr); 076 } 077 }