use of com.google.devtools.j2objc.ast.ArrayInitializer in project j2objc by google.
the class ArrayRewriter method createInvocation.
private MethodInvocation createInvocation(ArrayCreation node) {
ArrayType arrayType = node.getTypeMirror();
boolean retainedResult = node.hasRetainedResult() || options.useARC();
ArrayInitializer initializer = node.getInitializer();
if (initializer != null) {
return newInitializedArrayInvocation(arrayType, initializer.getExpressions(), retainedResult);
} else {
List<Expression> dimensions = node.getDimensions();
if (dimensions.size() == 1) {
return newSingleDimensionArrayInvocation(arrayType, dimensions.get(0), retainedResult);
} else {
return newMultiDimensionArrayInvocation(arrayType, dimensions, retainedResult);
}
}
}
use of com.google.devtools.j2objc.ast.ArrayInitializer in project j2objc by google.
the class TreeConverter method convertNewArray.
private TreeNode convertNewArray(JCTree.JCNewArray node) {
ArrayCreation newNode = new ArrayCreation();
List<Expression> dimensions = new ArrayList<>();
for (JCTree.JCExpression dimension : node.getDimensions()) {
dimensions.add((Expression) convert(dimension));
}
javax.lang.model.type.ArrayType type = (javax.lang.model.type.ArrayType) node.type;
if (node.getInitializers() != null) {
ArrayInitializer initializers = new ArrayInitializer(type);
for (JCTree.JCExpression initializer : node.getInitializers()) {
initializers.addExpression((Expression) convert(initializer));
}
newNode.setInitializer(initializers);
}
return newNode.setType((ArrayType) new ArrayType(type).setPosition(getPosition(node))).setDimensions(dimensions);
}
use of com.google.devtools.j2objc.ast.ArrayInitializer in project j2objc by google.
the class ArrayRewriter method newInitializedArrayInvocation.
private MethodInvocation newInitializedArrayInvocation(ArrayType arrayType, List<Expression> elements, boolean retainedResult) {
TypeMirror componentType = arrayType.getComponentType();
TypeElement iosArrayElement = typeUtil.getIosArray(componentType);
GeneratedExecutableElement methodElement = GeneratedExecutableElement.newMethodWithSelector(getInitializeSelector(componentType, retainedResult), iosArrayElement.asType(), iosArrayElement).addModifiers(Modifier.PUBLIC, Modifier.STATIC);
methodElement.addParameter(GeneratedVariableElement.newParameter("values", new PointerType(componentType), methodElement));
methodElement.addParameter(GeneratedVariableElement.newParameter("count", typeUtil.getInt(), methodElement));
if (!componentType.getKind().isPrimitive()) {
methodElement.addParameter(GeneratedVariableElement.newParameter("type", TypeUtil.IOS_CLASS.asType(), methodElement));
}
MethodInvocation invocation = new MethodInvocation(new ExecutablePair(methodElement), arrayType, new SimpleName(iosArrayElement));
// Create the array initializer and add it as the first parameter.
ArrayInitializer arrayInit = new ArrayInitializer(arrayType);
for (Expression element : elements) {
arrayInit.addExpression(element.copy());
}
invocation.addArgument(arrayInit);
// Add the array size parameter.
invocation.addArgument(NumberLiteral.newIntLiteral(arrayInit.getExpressions().size(), typeUtil));
// Add the type argument for object arrays.
if (!componentType.getKind().isPrimitive()) {
invocation.addArgument(new TypeLiteral(componentType, typeUtil));
}
return invocation;
}
use of com.google.devtools.j2objc.ast.ArrayInitializer in project j2objc by google.
the class ArrayRewriter method newMultiDimensionArrayInvocation.
private MethodInvocation newMultiDimensionArrayInvocation(ArrayType arrayType, List<Expression> dimensions, boolean retainedResult) {
assert dimensions.size() > 1;
TypeMirror componentType = arrayType;
for (int i = 0; i < dimensions.size(); i++) {
assert TypeUtil.isArray(componentType);
componentType = ((ArrayType) componentType).getComponentType();
}
TypeElement iosArrayElement = typeUtil.getIosArray(componentType);
ExecutableElement methodElement = getMultiDimensionMethod(componentType, iosArrayElement, retainedResult);
MethodInvocation invocation = new MethodInvocation(new ExecutablePair(methodElement), arrayType, new SimpleName(iosArrayElement));
// Add the dimension count argument.
invocation.addArgument(NumberLiteral.newIntLiteral(dimensions.size(), typeUtil));
// Create the dimensions array.
ArrayInitializer dimensionsArg = new ArrayInitializer(typeUtil.getArrayType(typeUtil.getInt()));
for (Expression e : dimensions) {
dimensionsArg.addExpression(e.copy());
}
invocation.addArgument(dimensionsArg);
if (!componentType.getKind().isPrimitive()) {
invocation.addArgument(new TypeLiteral(componentType, typeUtil));
}
return invocation;
}
use of com.google.devtools.j2objc.ast.ArrayInitializer in project j2objc by google.
the class TreeConverter method convertArrayInitializer.
private static TreeNode convertArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer node) {
List<Expression> expressions = new ArrayList<>();
for (Object expression : node.expressions()) {
expressions.add((Expression) convert(expression));
}
ArrayInitializer newNode = new ArrayInitializer();
convertExpression(node, newNode);
return newNode.setTypeMirror((JdtArrayType) BindingConverter.getType(node.resolveTypeBinding())).setExpressions(expressions);
}
Aggregations