Search in sources :

Example 1 with TypeLiteral

use of com.google.devtools.j2objc.ast.TypeLiteral in project j2objc by google.

the class OcniExtractor method endVisit.

@Override
public void endVisit(MethodDeclaration node) {
    int modifiers = node.getModifiers();
    if (Modifier.isNative(modifiers)) {
        NativeStatement nativeStmt = extractNativeStatement(node);
        if (nativeStmt != null) {
            Block body = new Block();
            body.addStatement(nativeStmt);
            node.setBody(body);
            node.removeModifiers(Modifier.NATIVE);
        }
    }
    if (Modifier.isSynchronized(modifiers)) {
        TypeElement declaringClass = ElementUtil.getDeclaringClass(node.getExecutableElement());
        SynchronizedStatement syncStmt = new SynchronizedStatement(Modifier.isStatic(modifiers) ? new TypeLiteral(declaringClass.asType(), typeUtil) : new ThisExpression(declaringClass.asType()));
        syncStmt.setBody(TreeUtil.remove(node.getBody()));
        Block newBody = new Block();
        newBody.addStatement(syncStmt);
        node.setBody(newBody);
        node.removeModifiers(Modifier.SYNCHRONIZED);
    }
}
Also used : ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) TypeElement(javax.lang.model.element.TypeElement) Block(com.google.devtools.j2objc.ast.Block) SynchronizedStatement(com.google.devtools.j2objc.ast.SynchronizedStatement)

Example 2 with TypeLiteral

use of com.google.devtools.j2objc.ast.TypeLiteral 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 3 with TypeLiteral

use of com.google.devtools.j2objc.ast.TypeLiteral in project j2objc by google.

the class ArrayRewriter method newSingleDimensionArrayInvocation.

private MethodInvocation newSingleDimensionArrayInvocation(ArrayType arrayType, Expression dimensionExpr, boolean retainedResult) {
    TypeMirror componentType = arrayType.getComponentType();
    TypeElement iosArrayElement = typeUtil.getIosArray(componentType);
    boolean isPrimitive = componentType.getKind().isPrimitive();
    String selector = (retainedResult ? "newArray" : "array") + "WithLength:" + (isPrimitive ? "" : "type:");
    GeneratedExecutableElement methodElement = GeneratedExecutableElement.newMethodWithSelector(selector, iosArrayElement.asType(), iosArrayElement).addModifiers(Modifier.PUBLIC, Modifier.STATIC);
    methodElement.addParameter(GeneratedVariableElement.newParameter("length", typeUtil.getInt(), methodElement));
    if (!isPrimitive) {
        methodElement.addParameter(GeneratedVariableElement.newParameter("type", TypeUtil.IOS_CLASS.asType(), methodElement));
    }
    MethodInvocation invocation = new MethodInvocation(new ExecutablePair(methodElement), arrayType, new SimpleName(iosArrayElement));
    // Add the array length argument.
    invocation.addArgument(dimensionExpr.copy());
    // Add the type argument for object arrays.
    if (!isPrimitive) {
        invocation.addArgument(new TypeLiteral(componentType, typeUtil));
    }
    return invocation;
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 4 with TypeLiteral

use of com.google.devtools.j2objc.ast.TypeLiteral in project j2objc by google.

the class TreeConverter method convertFieldAccess.

private TreeNode convertFieldAccess(JCTree.JCFieldAccess node) {
    String fieldName = node.name.toString();
    SourcePosition pos = getPosition(node);
    JCTree.JCExpression selected = node.getExpression();
    if (fieldName.equals("this")) {
        return new ThisExpression().setQualifier((Name) convert(selected)).setTypeMirror(node.sym.asType());
    }
    if ("super".equals(getMemberName(selected))) {
        SuperFieldAccess newNode = new SuperFieldAccess().setVariableElement((VariableElement) node.sym).setName(convertSimpleName(node.sym, node.type, pos));
        if (selected.getKind() == Kind.MEMBER_SELECT) {
            newNode.setQualifier((Name) convert(((JCTree.JCFieldAccess) selected).getExpression()));
        }
        return newNode;
    }
    if (node.getIdentifier().toString().equals("class")) {
        return new TypeLiteral(node.type).setType((Type) convertType(selected.type, pos, false).setPosition(getPosition(node)));
    }
    if (selected.getKind() == Kind.IDENTIFIER && (!node.sym.getKind().isField() || ElementUtil.isConstant((VariableElement) node.sym))) {
        if (selected.toString().equals("this")) {
            // Just return the constant.
            return new SimpleName(node.sym);
        }
        JCIdent ident = (JCTree.JCIdent) selected;
        return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier(convertSimpleName(ident.sym, ident.type, pos)).setElement(node.sym);
    }
    if (selected.getKind() == Kind.MEMBER_SELECT) {
        TreeNode newSelected = convertFieldAccess((JCTree.JCFieldAccess) selected).setPosition(pos);
        if (newSelected.getKind() == TreeNode.Kind.QUALIFIED_NAME) {
            return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((QualifiedName) newSelected).setElement(node.sym);
        }
    }
    if (ElementUtil.isConstant((VariableElement) node.sym) && ElementUtil.isStatic(node.sym) && !(selected.getKind() == Kind.METHOD_INVOCATION)) {
        return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((Name) convert(selected)).setElement(node.sym);
    }
    return new FieldAccess().setVariableElement((VariableElement) node.sym).setExpression((Expression) convert(selected)).setName(convertSimpleName(node.sym, node.type, pos).setTypeMirror(node.type));
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) SimpleName(com.google.devtools.j2objc.ast.SimpleName) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) JCTree(com.sun.tools.javac.tree.JCTree) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess) VariableElement(javax.lang.model.element.VariableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) Name(com.google.devtools.j2objc.ast.Name) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionalExpression(com.google.devtools.j2objc.ast.FunctionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) TreeNode(com.google.devtools.j2objc.ast.TreeNode) SourcePosition(com.google.devtools.j2objc.ast.SourcePosition) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess)

Example 5 with TypeLiteral

use of com.google.devtools.j2objc.ast.TypeLiteral in project j2objc by google.

the class CastResolver method createCastCheck.

private FunctionInvocation createCastCheck(TypeMirror type, Expression expr) {
    type = typeUtil.erasure(type);
    TypeMirror idType = TypeUtil.ID_TYPE;
    if (TypeUtil.isInterface(type) || isObjectArray(type)) {
        // Interfaces and object arrays requre a isInstance call.
        FunctionElement element = new FunctionElement("cast_check", idType, null).addParameters(idType, TypeUtil.IOS_CLASS.asType());
        FunctionInvocation invocation = new FunctionInvocation(element, idType);
        invocation.addArgument(TreeUtil.remove(expr));
        invocation.addArgument(new TypeLiteral(type, typeUtil));
        return invocation;
    } else if (TypeUtil.isArray(type) || TypeUtil.isDeclaredType(type)) {
        // Primitive array and non-interface type casts are checked using Objective-C's
        // isKindOfClass:.
        TypeElement objcClass = typeUtil.getObjcClass(type);
        FunctionElement checkFunction = new FunctionElement("cast_chk", idType, null).addParameters(idType, idType);
        FunctionInvocation invocation = new FunctionInvocation(checkFunction, idType);
        invocation.addArgument(TreeUtil.remove(expr));
        ExecutableElement classElement = GeneratedExecutableElement.newMethodWithSelector("class", idType, objcClass).addModifiers(Modifier.STATIC);
        MethodInvocation classInvocation = new MethodInvocation(new ExecutablePair(classElement), new SimpleName(objcClass));
        invocation.addArgument(classInvocation);
        return invocation;
    }
    return null;
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Aggregations

TypeLiteral (com.google.devtools.j2objc.ast.TypeLiteral)10 SimpleName (com.google.devtools.j2objc.ast.SimpleName)6 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)6 TypeMirror (javax.lang.model.type.TypeMirror)6 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)5 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)5 TypeElement (javax.lang.model.element.TypeElement)5 Expression (com.google.devtools.j2objc.ast.Expression)4 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)4 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)2 Block (com.google.devtools.j2objc.ast.Block)2 CastExpression (com.google.devtools.j2objc.ast.CastExpression)2 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)2 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)2 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)2 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)2 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)2 VariableElement (javax.lang.model.element.VariableElement)2