Search in sources :

Example 16 with ArrayType

use of javax.lang.model.type.ArrayType in project j2objc by google.

the class ArrayRewriter method maybeRewriteArrayLength.

private void maybeRewriteArrayLength(Expression node, SimpleName name, Expression expr) {
    TypeMirror exprType = expr.getTypeMirror();
    if (name.getIdentifier().equals("length") && TypeUtil.isArray(exprType)) {
        VariableElement sizeField = GeneratedVariableElement.newField("size", typeUtil.getInt(), typeUtil.getIosArray(((ArrayType) exprType).getComponentType()));
        node.replaceWith(new FieldAccess(sizeField, TreeUtil.remove(expr)));
    }
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess)

Example 17 with ArrayType

use of javax.lang.model.type.ArrayType 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);
}
Also used : ArrayType(javax.lang.model.type.ArrayType) FunctionElement(com.google.devtools.j2objc.types.FunctionElement) SwitchCase(com.google.devtools.j2objc.ast.SwitchCase) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer)

Example 18 with ArrayType

use of javax.lang.model.type.ArrayType in project j2objc by google.

the class TypeUtil method getDimensions.

public static int getDimensions(ArrayType arrayType) {
    int dimCount = 0;
    TypeMirror t = arrayType;
    while (t.getKind().equals(TypeKind.ARRAY)) {
        dimCount++;
        t = (((ArrayType) t).getComponentType());
    }
    return dimCount;
}
Also used : ArrayType(javax.lang.model.type.ArrayType) GeneratedArrayType(com.google.devtools.j2objc.types.GeneratedArrayType) AbstractTypeMirror(com.google.devtools.j2objc.types.AbstractTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror)

Example 19 with ArrayType

use of javax.lang.model.type.ArrayType in project immutables by immutables.

the class TypeStringProvider method caseType.

void caseType(TypeMirror type) {
    if (ended) {
        // to prevent additional recursive effects when using workaround
        return;
    }
    switch(type.getKind()) {
        case ERROR:
            unresolvedTypeHasOccured = true;
        //$FALL-THROUGH$
        case DECLARED:
            DeclaredType declaredType = (DeclaredType) type;
            appendResolved(declaredType);
            appendTypeArguments(type, declaredType);
            break;
        case ARRAY:
            TypeMirror componentType = ((ArrayType) type).getComponentType();
            int mark = buffer.length();
            caseType(componentType);
            cutTypeArgument(type, mark);
            buffer.append('[').append(']');
            break;
        case WILDCARD:
            WildcardType wildcard = (WildcardType) type;
            @Nullable TypeMirror extendsBound = wildcard.getExtendsBound();
            @Nullable TypeMirror superBound = wildcard.getSuperBound();
            if (extendsBound != null) {
                buffer.append("? extends ");
                caseType(extendsBound);
            } else if (superBound != null) {
                buffer.append("? super ");
                caseType(superBound);
            } else {
                buffer.append('?');
            }
            break;
        case TYPEVAR:
            if (allowedTypevars.length != 0) {
                TypeVariable typeVariable = (TypeVariable) type;
                String var = typeVariable.toString();
                int indexOfVar = Arrays.asList(allowedTypevars).indexOf(var);
                if (indexOfVar >= 0) {
                    if (typevarArguments != null) {
                        buffer.append(typevarArguments[indexOfVar]);
                    } else {
                        hasTypeVariables = true;
                        buffer.append(var);
                    }
                    break;
                }
            // If we don't have such parameter we consider this is the quirk
            // that was witnessed in Eclipse, we let the code below deal with it.
            }
            // ended flag
            if (tryToUseSourceAsAWorkaround()) {
                ended = true;
                break;
            }
            reporter.withElement(element).error("It is a compiler/annotation processing bug to receive type variable '%s' here." + " To avoid it — do not use not yet generated types in %s attribute", type, element.getSimpleName());
            // just append as toString whatever we have
            buffer.append(type);
            break;
        default:
            buffer.append(type);
    }
}
Also used : ArrayType(javax.lang.model.type.ArrayType) WildcardType(javax.lang.model.type.WildcardType) TypeMirror(javax.lang.model.type.TypeMirror) TypeVariable(javax.lang.model.type.TypeVariable) Nullable(javax.annotation.Nullable) DeclaredType(javax.lang.model.type.DeclaredType)

Example 20 with ArrayType

use of javax.lang.model.type.ArrayType in project immutables by immutables.

the class ValueAttribute method introspectType.

@Override
protected void introspectType() {
    TypeMirror typeMirror = returnType;
    // Special case for primitive Optional, may become a pattern for specialized types
    if (typeKind.isOptionalSpecializedJdk()) {
        typeParameters = ImmutableList.of(optionalSpecializedType());
        // no delegation to introspect further
        return;
    }
    if (isContainerType()) {
        if (typeMirror.getKind() == TypeKind.DECLARED || typeMirror.getKind() == TypeKind.ERROR) {
            DeclaredType declaredType = (DeclaredType) typeMirror;
            List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
            if (!typeArguments.isEmpty()) {
                final TypeMirror typeArgument = typeArguments.get(0);
                if (isSetType() && protoclass().environment().hasOrdinalModule()) {
                    this.generateOrdinalValueSet = new TypeIntrospectionBase() {

                        @Override
                        protected TypeMirror internalTypeMirror() {
                            return typeArgument;
                        }

                        @Override
                        protected TypeHierarchyCollector collectTypeHierarchy(TypeMirror typeMirror) {
                            TypeHierarchyCollector collector = containingType.createTypeHierarchyCollector(reporter, element);
                            collector.collectFrom(typeMirror);
                            return collector;
                        }
                    }.isOrdinalValue();
                }
                if (isMapType()) {
                    TypeMirror typeSecondArgument = typeArguments.get(1);
                    if (typeSecondArgument.getKind() == TypeKind.DECLARED) {
                        TypeElement typeElement = (TypeElement) ((DeclaredType) typeSecondArgument).asElement();
                        this.containedSecondaryTypeElement = typeElement;
                    }
                }
                typeMirror = typeArgument;
            }
        }
    } else if (isArrayType()) {
        arrayComponent = ((ArrayType) typeMirror).getComponentType();
        typeMirror = arrayComponent;
    }
    if (typeMirror.getKind() == TypeKind.DECLARED) {
        TypeElement typeElement = (TypeElement) ((DeclaredType) typeMirror).asElement();
        this.containedTypeElement = typeElement;
    }
    introspectTypeMirror(typeMirror);
    introspectSupertypes();
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeHierarchyCollector(org.immutables.generator.TypeHierarchyCollector) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType)

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