use of com.google.devtools.j2objc.ast.ArrayInitializer in project j2objc by google.
the class SwitchRewriter method fixStringValue.
private void fixStringValue(SwitchStatement node) {
Expression expr = node.getExpression();
TypeMirror type = expr.getTypeMirror();
if (!typeUtil.isString(type)) {
return;
}
ArrayType arrayType = typeUtil.getArrayType(type);
ArrayInitializer arrayInit = new ArrayInitializer(arrayType);
int idx = 0;
for (Statement stmt : node.getStatements()) {
if (stmt instanceof SwitchCase) {
SwitchCase caseStmt = (SwitchCase) stmt;
if (!caseStmt.isDefault()) {
arrayInit.addExpression(TreeUtil.remove(caseStmt.getExpression()));
caseStmt.setExpression(NumberLiteral.newIntLiteral(idx++, typeUtil));
}
}
}
TypeMirror intType = typeUtil.getInt();
FunctionElement indexOfFunc = new FunctionElement("JreIndexOfStr", intType, null).addParameters(type, arrayType, intType);
FunctionInvocation invocation = new FunctionInvocation(indexOfFunc, intType);
invocation.addArgument(TreeUtil.remove(expr)).addArgument(arrayInit).addArgument(NumberLiteral.newIntLiteral(idx, typeUtil));
node.setExpression(invocation);
}
use of com.google.devtools.j2objc.ast.ArrayInitializer in project j2objc by google.
the class TranslationUtil method createObjectArray.
public Expression createObjectArray(List<Expression> expressions, ArrayType arrayType) {
if (expressions.isEmpty()) {
return new ArrayCreation(arrayType, typeUtil, 0);
}
ArrayCreation creation = new ArrayCreation(arrayType, typeUtil);
ArrayInitializer initializer = new ArrayInitializer(arrayType);
initializer.getExpressions().addAll(expressions);
creation.setInitializer(initializer);
return creation;
}
use of com.google.devtools.j2objc.ast.ArrayInitializer in project j2objc by google.
the class VarargsRewriter method rewriteVarargs.
private void rewriteVarargs(ExecutableElement method, TypeMirror varargsType, List<Expression> args) {
if (varargsType == null) {
return;
}
varargsType = typeUtil.erasure(varargsType);
int numRegularParams = method.getParameters().size() - 1;
List<Expression> varargs = args.subList(numRegularParams, args.size());
List<Expression> varargsCopy = Lists.newArrayList(varargs);
varargs.clear();
if (varargsCopy.isEmpty()) {
args.add(new ArrayCreation(typeUtil.getArrayType(varargsType), typeUtil, 0));
} else {
ArrayInitializer newInit = new ArrayInitializer(typeUtil.getArrayType(varargsType));
newInit.getExpressions().addAll(varargsCopy);
args.add(new ArrayCreation(newInit));
}
}
Aggregations