Search in sources :

Example 6 with ArrayType

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;
}
Also used : ArrayType(javax.lang.model.type.ArrayType) JCTree(com.sun.tools.javac.tree.JCTree)

Example 7 with ArrayType

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;
}
Also used : ArrayType(javax.lang.model.type.ArrayType) ArrayType(javax.lang.model.type.ArrayType) FieldType(com.bluelinelabs.logansquare.processor.type.field.FieldType) ArrayCollectionType(com.bluelinelabs.logansquare.processor.type.collection.ArrayCollectionType) CollectionType(com.bluelinelabs.logansquare.processor.type.collection.CollectionType) TypeMirror(javax.lang.model.type.TypeMirror) ParameterizedTypeField(com.bluelinelabs.logansquare.processor.type.field.ParameterizedTypeField) ArrayCollectionType(com.bluelinelabs.logansquare.processor.type.collection.ArrayCollectionType)

Example 8 with ArrayType

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);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) Statement(com.google.devtools.j2objc.ast.Statement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) PointerType(com.google.devtools.j2objc.types.PointerType) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) ArrayType(javax.lang.model.type.ArrayType) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) TypeMirror(javax.lang.model.type.TypeMirror) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess)

Example 9 with ArrayType

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);
    }
}
Also used : SimpleName(com.google.devtools.j2objc.ast.SimpleName) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) ArrayType(javax.lang.model.type.ArrayType) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) CastExpression(com.google.devtools.j2objc.ast.CastExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) NullLiteral(com.google.devtools.j2objc.ast.NullLiteral)

Example 10 with ArrayType

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);
        }
    }
}
Also used : ArrayType(javax.lang.model.type.ArrayType) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer)

Aggregations

ArrayType (javax.lang.model.type.ArrayType)24 TypeMirror (javax.lang.model.type.TypeMirror)18 DeclaredType (javax.lang.model.type.DeclaredType)11 TypeElement (javax.lang.model.element.TypeElement)9 Test (org.junit.Test)7 Expression (com.google.devtools.j2objc.ast.Expression)4 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)3 VariableElement (javax.lang.model.element.VariableElement)3 BindString (butterknife.BindString)2 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)2 FieldAccess (com.google.devtools.j2objc.ast.FieldAccess)2 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)2 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)2 SimpleName (com.google.devtools.j2objc.ast.SimpleName)2 Statement (com.google.devtools.j2objc.ast.Statement)2 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)2 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 PrimitiveType (javax.lang.model.type.PrimitiveType)2