cast a Object to a Variable Name

@param o Object to cast
@return casted Variable Name
@throws PageException
325855
public static String toVariableName(Object o) throws PageException {
	String str = toString(o);
	if (!Decision.isVariableName(str)) throw new ExpressionException("can't cast [" + str + "] to variable name value");
	return str;
    }
431985
public static Object callWithNamedValues(PageContext pc, Object coll, Collection.Key methodName, Struct args, short type, String strType) throws PageException {
	Map<Key, FunctionLibFunction> members = getMembers(pc, type);
	FunctionLibFunction member = members.get(methodName);

	if (member != null) {
	    List<FunctionLibFunctionArg> _args = member.getArg();
	    FunctionLibFunctionArg arg;
	    if (args.size() < _args.size()) {
		Object val;
		ArrayList<Ref> refs = new ArrayList<Ref>();
		arg = _args.get(0);
		refs.add(new Casting(arg.getTypeAsString(), arg.getType(), new LFunctionValue(new LString(arg.getName()), coll)));
		for (int y = 1; y < _args.size(); y++) {
		    arg = _args.get(y);

		    // match by name
		    val = args.get(arg.getName(), null);

		    // match by alias
		    if (val == null) {
			String alias = arg.getAlias();
			if (!StringUtil.isEmpty(alias, true)) {
			    String[] aliases = lucee.runtime.type.util.ListUtil.trimItems(lucee.runtime.type.util.ListUtil.listToStringArray(alias, ','));
			    for (int x = 0; x < aliases.length; x++) {
				val = args.get(aliases[x], null);
				if (val != null) break;
			    }
			}
		    }

		    if (val == null) {
			if (arg.getRequired()) {
			    String[] names = member.getMemberNames();
			    String n = ArrayUtil.isEmpty(names) ? "" : names[0];
			    throw new ExpressionException("missing required argument [" + arg.getName() + "] for member function call [" + n + "]");
			}
		    }
		    else {
			refs.add(new Casting(arg.getTypeAsString(), arg.getType(), new LFunctionValue(new LString(arg.getName()), val)));
			// refs.add(new LFunctionValue(new LString(arg.getName()),new
			// Casting(pc,arg.getTypeAsString(),arg.getType(),val)));
		    }

		}
		return new BIFCall(coll, member, refs.toArray(new Ref[refs.size()])).getValue(pc);
	    }

	}
	throw new ExpressionException("No matching function member [" + methodName + "] for call with named arguments found, available function members are ["
		+ lucee.runtime.type.util.ListUtil.sort(CollectionUtil.getKeyList(members.keySet().iterator(), ","), "textnocase", "asc", ",") + "]");
    }
1281150
public static String toVariableName(Object o, String defaultValue) {
	String str = toString(o, null);

	if (str == null || !Decision.isVariableName(str)) return defaultValue;
	return str;
    }
1137032
public static short toShortValue(Object o) throws PageException {
	if (o instanceof Short) return ((Short) o).shortValue();
	else if (o instanceof CharSequence) return (short) toDoubleValue(o.toString());
	else if (o instanceof Character) return (short) (((Character) o).charValue());
	else if (o instanceof Boolean) return (short) ((((Boolean) o).booleanValue()) ? 1 : 0);
	else if (o instanceof Number) return (((Number) o).shortValue());
	else if (o instanceof Castable) return (short) ((Castable) o).castToDoubleValue();
	else if (o instanceof ObjectWrap) return toShortValue(((ObjectWrap) o).getEmbededObject());

	throw new CasterException(o, "short");
    }
45407
public static String toTypeName(Object o) {
	if (o == null) return "null";
	else if (o instanceof String) return "string";
	else if (o instanceof Boolean) return "boolean";
	else if (o instanceof Number) return "int";
	else if (o instanceof Array) return "array";
	else if (o instanceof Component) return "component";
	else if (o instanceof Struct) return "struct";
	else if (o instanceof Query) return "query";
	else if (o instanceof DateTime) return "datetime";
	else if (o instanceof byte[]) return "binary";
	else if (o instanceof ObjectWrap) {
	    return toTypeName(((ObjectWrap) o).getEmbededObject(null));
	}

	Class clazz = o.getClass();
	String className = clazz.getName();
	if (className.startsWith("java.lang.")) {
	    return className.substring(10);
	}
	return toClassName(clazz);
    }
871512
public static Object OPAQUE(Object o) {
	if (o == null) return null;

	try {

	    byte[] bytes = Caster.toBytes(Reflector.callMethod(o, "getBytes", ZERO_ARGS), null);
	    return new String(bytes, "UTF-8");
	}
	catch (Exception e) {
	    // print.printST(e);
	}

	return o;
    }
18418
public static Object get(PageContext pc, Object coll, int[] types, Key[] keys, Object[][] args, Object defaultValue) throws PageException {
	if (coll == null) return defaultValue;
	int to = keys.length - 1;
	VariableUtilImpl vu = (VariableUtilImpl) pc.getVariableUtil();
	for (int i = 0; i <= to; i++) {
	    switch (types[i]) {
	    case TYPE_DATA:
		coll = vu.getCollection(pc, coll, keys[i], Null.NULL);
		break;
	    case TYPE_UDF_ARGS:
		coll = vu.callFunctionWithoutNamedValues(pc, coll, keys[i], args[i], false, Null.NULL);
		break;
	    case TYPE_UDF_NAMED_ARGS:
		coll = vu.callFunctionWithNamedValues(pc, coll, keys[i], args[i], false, Null.NULL);
		break;
	    case TYPE_BIF:
		coll = null;// TODO
		break;
	    }

	    if (coll == Null.NULL || (coll == null && i < to)) return defaultValue;
	}
	return coll;
    }
1117468
String serialize(Object o) {
        String body;
        try {
            body = new String(JsonSerializer.serialize(o));
        } catch (IOException e) {
            throw new ServiceException(ErrorCode.HTTP_CLIENT_ERROR,
                    ErrorCode.HTTP_CLIENT_ERROR.getMessageTemplate(),
                    "serialize failed.");
        }
        return body;
    }
721150
public static Object setVariable(PageContext pc, String var, Object value) throws PageException {
	StringList list = parse(pc, new ParserString(var), false);
	if (list == null) throw new InterpreterException("invalid variable name declaration [" + var + "]");

	if (list.size() == 1) {
	    return pc.undefinedScope().set(list.next(), value);
	}

	// min 2 elements
	int scope = scopeString2Int(pc.ignoreScopes(), list.next());
	Object coll;
	if (scope == Scope.SCOPE_UNDEFINED) {
	    coll = pc.touch(pc.undefinedScope(), KeyImpl.init(list.current()));
	}
	else {
	    coll = VariableInterpreter.scope(pc, scope, true);
	    // coll=pc.scope(scope);
	}

	while (list.hasNextNext()) {
	    coll = pc.touch(coll, KeyImpl.init(list.next()));
	}
	return pc.set(coll, KeyImpl.init(list.next()), value);
    }
1244537
private static Object toQuery(Object obj) throws PageException {
	if (obj instanceof Struct) {
	    Struct sct = (Struct) obj;
	    Key[] keys = CollectionUtil.keys(sct);

	    // Columns
	    Key[] columns = null;
	    if (contains(keys, KeyConstants._COLUMNS)) columns = toColumns(sct.get(KeyConstants._COLUMNS, null));
	    else if (contains(keys, KeyConstants._COLUMNLIST)) columns = toColumnlist(sct.get(KeyConstants._COLUMNLIST, null));

	    // rowcount
	    int rowcount = -1;
	    if (contains(keys, ROWCOUNT)) rowcount = toRowCount(sct.get(ROWCOUNT, null));
	    else if (contains(keys, KeyConstants._RECORDCOUNT)) rowcount = toRowCount(sct.get(KeyConstants._RECORDCOUNT, null));

	    if (columns != null) {
		if (keys.length == 2 && contains(keys, KeyConstants._DATA)) {

		    Array[] data = toData(sct.get(KeyConstants._DATA, null), columns);
		    if (data != null) {
			return new QueryImpl(columns, data, "query");
		    }
		}

		else if (keys.length == 3 && rowcount != -1 && contains(keys, KeyConstants._DATA)) {
		    Array[] data = toData(sct.get(KeyConstants._DATA, null), columns, rowcount);
		    if (data != null) {
			return new QueryImpl(columns, data, "query");
		    }
		}
	    }
	    return toQuery(sct, keys);
	}
	/*
	 * else if(obj instanceof Query) { return toQuery((Query) obj); }
	 */
	else if (obj instanceof Collection) {
	    Collection coll = (Collection) obj;
	    return toQuery(coll, CollectionUtil.keys(coll));
	}

	return obj;

    }
482684
private static <T> T cast(Class<T> clazz, Object o) {
        if (! clazz.isInstance(o))
            throw new IllegalArgumentException(o.toString());
        return clazz.cast(o);
    }
1228190
public static NodeList toNodeList(Object o) throws PageException {
	// print.ln("nodeList:"+o);
	if (o instanceof NodeList) {
	    return (NodeList) o;
	}
	else if (o instanceof ObjectWrap) {
	    return toNodeList(((ObjectWrap) o).getEmbededObject());
	}
	throw new CasterException(o, "NodeList");
    }
949976
public static Attr toAttr(Document doc, Object o) throws PageException {
	if (o instanceof Attr) return (Attr) o;
	if (o instanceof Struct && ((Struct) o).size() == 1) {
	    Struct sct = (Struct) o;
	    Entry<Key, Object> e = sct.entryIterator().next();
	    Attr attr = doc.createAttribute(e.getKey().getString());
	    attr.setValue(Caster.toString(e.getValue()));
	    return attr;
	}

	throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Attribute");
    }
54080
public com.google.protobuf.ByteString
      getVariableNameBytes() {
    java.lang.Object ref = variableName_;
    if (ref instanceof java.lang.String) {
      com.google.protobuf.ByteString b = 
          com.google.protobuf.ByteString.copyFromUtf8(
              (java.lang.String) ref);
      variableName_ = b;
      return b;
    } else {
      return (com.google.protobuf.ByteString) ref;
    }
  }
1187490
public static <T> T prop(JavaScriptObject o, Object id, Class<? extends T> type) {
    return o == null ? null : o.<JsCache> cast().get(id, type);
  }
410137
@Deprecated
    public static Node toNode(Object o) throws PageException {
	if (o instanceof XMLStruct) return ((XMLStruct) o).toNode();
	if (o instanceof Node) return (Node) o;
	throw new CasterException(o, "node");
    }
428397
@SuppressWarnings("null")
	public static String valueOf(@Nullable Object o) {
		return String.valueOf(String.valueOf(o));
	}