use of javax.lang.model.type.ArrayType in project bazel by bazelbuild.
the class TreeBuilder method buildArrayAccess.
/**
* Builds an AST Tree to dereference an array.
*
* @param array the array to dereference
* @param index the index at which to dereference
* @return a Tree representing the dereference
*/
public ArrayAccessTree buildArrayAccess(ExpressionTree array, ExpressionTree index) {
ArrayType arrayType = (ArrayType) InternalUtils.typeOf(array);
JCTree.JCArrayAccess access = maker.Indexed((JCTree.JCExpression) array, (JCTree.JCExpression) index);
access.setType((Type) arrayType.getComponentType());
return access;
}
use of javax.lang.model.type.ArrayType in project LoganSquare by bluelinelabs.
the class Type method typeFor.
public static Type typeFor(TypeMirror typeMirror, TypeMirror typeConverterType, Elements elements, Types types) {
TypeMirror genericClassTypeMirror = types.erasure(typeMirror);
boolean hasTypeConverter = typeConverterType != null && !typeConverterType.toString().equals("void");
Type type;
if (!hasTypeConverter && typeMirror instanceof ArrayType) {
TypeMirror arrayTypeMirror = ((ArrayType) typeMirror).getComponentType();
type = new ArrayCollectionType(Type.typeFor(arrayTypeMirror, null, elements, types));
} else if (!hasTypeConverter && !genericClassTypeMirror.toString().equals(typeMirror.toString())) {
type = CollectionType.collectionTypeFor(typeMirror, genericClassTypeMirror, elements, types);
if (type == null) {
if (typeMirror.toString().contains("?")) {
throw new RuntimeException("Generic types with wildcards are currently not supported by LoganSquare.");
}
try {
type = new ParameterizedTypeField(TypeName.get(typeMirror));
} catch (Exception ignored) {
}
}
} else {
type = FieldType.fieldTypeFor(typeMirror, typeConverterType, elements, types);
}
return type;
}
use of javax.lang.model.type.ArrayType in project j2objc by google.
the class EnhancedForRewriter method handleArrayIteration.
private void handleArrayIteration(EnhancedForStatement node) {
Expression expression = node.getExpression();
ArrayType expressionType = (ArrayType) expression.getTypeMirror();
VariableElement loopVariable = node.getParameter().getVariableElement();
TypeMirror componentType = expressionType.getComponentType();
TypeElement iosArrayType = typeUtil.getIosArray(componentType);
TypeMirror bufferType = new PointerType(componentType);
VariableElement arrayVariable = GeneratedVariableElement.newLocalVar("a__", expressionType, null);
VariableElement bufferVariable = GeneratedVariableElement.newLocalVar("b__", bufferType, null).setTypeQualifiers("const*");
VariableElement endVariable = GeneratedVariableElement.newLocalVar("e__", bufferType, null).setTypeQualifiers("const*");
VariableElement bufferField = GeneratedVariableElement.newField("buffer", bufferType, iosArrayType).addModifiers(Modifier.PUBLIC);
VariableElement sizeField = GeneratedVariableElement.newField("size", typeUtil.getInt(), iosArrayType).addModifiers(Modifier.PUBLIC);
VariableDeclarationStatement arrayDecl = new VariableDeclarationStatement(arrayVariable, TreeUtil.remove(expression));
FieldAccess bufferAccess = new FieldAccess(bufferField, new SimpleName(arrayVariable));
VariableDeclarationStatement bufferDecl = new VariableDeclarationStatement(bufferVariable, bufferAccess);
InfixExpression endInit = new InfixExpression(bufferType, InfixExpression.Operator.PLUS, new SimpleName(bufferVariable), new FieldAccess(sizeField, new SimpleName(arrayVariable)));
VariableDeclarationStatement endDecl = new VariableDeclarationStatement(endVariable, endInit);
WhileStatement loop = new WhileStatement();
loop.setExpression(new InfixExpression(typeUtil.getBoolean(), InfixExpression.Operator.LESS, new SimpleName(bufferVariable), new SimpleName(endVariable)));
Block newLoopBody = makeBlock(TreeUtil.remove(node.getBody()));
loop.setBody(newLoopBody);
newLoopBody.addStatement(0, new VariableDeclarationStatement(loopVariable, new PrefixExpression(componentType, PrefixExpression.Operator.DEREFERENCE, new PostfixExpression(bufferVariable, PostfixExpression.Operator.INCREMENT))));
Block block = new Block();
List<Statement> stmts = block.getStatements();
stmts.add(arrayDecl);
stmts.add(bufferDecl);
stmts.add(endDecl);
stmts.add(loop);
replaceLoop(node, block, loop);
}
use of javax.lang.model.type.ArrayType in project j2objc by google.
the class TranslationUtil method createAnnotationValue.
public Expression createAnnotationValue(TypeMirror type, AnnotationValue aValue) {
Object value = aValue.getValue();
if (value == null) {
return new NullLiteral(typeUtil.getNull());
} else if (value instanceof VariableElement) {
return new SimpleName((VariableElement) value);
} else if (TypeUtil.isArray(type)) {
assert value instanceof List;
ArrayType arrayType = (ArrayType) type;
@SuppressWarnings("unchecked") List<? extends AnnotationValue> list = (List<? extends AnnotationValue>) value;
List<Expression> generatedValues = new ArrayList<>();
for (AnnotationValue elem : list) {
generatedValues.add(createAnnotationValue(arrayType.getComponentType(), elem));
}
return createObjectArray(generatedValues, arrayType);
} else if (TypeUtil.isAnnotation(type)) {
assert value instanceof AnnotationMirror;
return createAnnotation((AnnotationMirror) value);
} else if (value instanceof TypeMirror) {
return new TypeLiteral((TypeMirror) value, typeUtil);
} else {
// Boolean, Character, Number, String
return TreeUtil.newLiteral(value, typeUtil);
}
}
use of javax.lang.model.type.ArrayType 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);
}
}
}
Aggregations