Search in sources :

Example 11 with SimpleName

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

the class JavaCloneWriter method endVisit.

@Override
public void endVisit(TypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    VariableElement originalVar = GeneratedVariableElement.newParameter("original", type.asType(), null);
    List<Statement> adjustments = getFieldAdjustments(node, originalVar);
    if (adjustments.isEmpty()) {
        return;
    }
    TypeMirror voidType = typeUtil.getVoid();
    ExecutableElement javaCloneElement = GeneratedExecutableElement.newMethodWithSelector(JAVA_CLONE_METHOD, voidType, type).addParameter(originalVar);
    MethodDeclaration declaration = new MethodDeclaration(javaCloneElement);
    declaration.setHasDeclaration(false);
    node.addBodyDeclaration(declaration);
    declaration.addParameter(new SingleVariableDeclaration(originalVar));
    Block body = new Block();
    declaration.setBody(body);
    List<Statement> statements = body.getStatements();
    ExecutableElement javaCloneSuperElement = GeneratedExecutableElement.newMethodWithSelector(JAVA_CLONE_METHOD, voidType, typeUtil.getJavaObject());
    SuperMethodInvocation superCall = new SuperMethodInvocation(new ExecutablePair(javaCloneSuperElement));
    superCall.addArgument(new SimpleName(originalVar));
    statements.add(new ExpressionStatement(superCall));
    statements.addAll(adjustments);
}
Also used : ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Statement(com.google.devtools.j2objc.ast.Statement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 12 with SimpleName

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

the class JavaToIOSMethodTranslator method visit.

@Override
public boolean visit(ClassInstanceCreation node) {
    // translate any embedded method invocations
    if (node.getExpression() != null) {
        node.getExpression().accept(this);
    }
    for (Expression e : node.getArguments()) {
        e.accept(this);
    }
    if (node.getAnonymousClassDeclaration() != null) {
        node.getAnonymousClassDeclaration().accept(this);
    }
    ExecutableElement method = node.getExecutableElement();
    String key = Mappings.getMethodKey(method, typeUtil);
    String selector = Mappings.STRING_CONSTRUCTOR_TO_METHOD_MAPPINGS.get(key);
    if (selector != null) {
        assert !node.hasRetainedResult();
        if (key.equals("java.lang.String.<init>(Ljava/lang/String;)V")) {
            // Special case: replace new String(constant) to constant (avoid clang warning).
            Expression arg = node.getArgument(0);
            if (arg instanceof StringLiteral) {
                node.replaceWith(arg.copy());
                return false;
            }
        }
        ExecutableElement newElement = GeneratedExecutableElement.newMappedMethod(selector, method);
        MethodInvocation newInvocation = new MethodInvocation(new ExecutablePair(newElement), new SimpleName(ElementUtil.getDeclaringClass(method)));
        TreeUtil.copyList(node.getArguments(), newInvocation.getArguments());
        node.replaceWith(newInvocation);
    }
    return true;
}
Also used : StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) Expression(com.google.devtools.j2objc.ast.Expression) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 13 with SimpleName

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

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

the class OuterReferenceResolverTest method testAnonymousClassCreatesLocalClassWithCaptures.

public void testAnonymousClassCreatesLocalClassWithCaptures() {
    resolveSource("Test", "class Test { Runnable test(final Object o) { " + "class Local { public void foo() { o.toString(); } } " + "return new Runnable() { public void run() { new Local(); } }; } }");
    TypeDeclaration runnableNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(2);
    assertTrue(ElementUtil.isAnonymous(runnableNode.getTypeElement()));
    List<VariableElement> innerFields = Lists.newArrayList(captureInfo.getCaptureFields(runnableNode.getTypeElement()));
    assertEquals(1, innerFields.size());
    assertEquals("val$o", ElementUtil.getName(innerFields.get(0)));
    ClassInstanceCreation creationNode = (ClassInstanceCreation) nodesByType.get(Kind.CLASS_INSTANCE_CREATION).get(1);
    List<Expression> captureArgs = creationNode.getCaptureArgs();
    assertEquals(1, captureArgs.size());
    Expression captureArg = captureArgs.get(0);
    assertTrue(captureArg instanceof SimpleName);
    VariableElement captureVar = TreeUtil.getVariableElement(captureArg);
    assertNotNull(captureVar);
    assertEquals("val$o", ElementUtil.getName(captureVar));
}
Also used : ClassInstanceCreation(com.google.devtools.j2objc.ast.ClassInstanceCreation) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Example 15 with SimpleName

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

the class OuterReferenceResolverTest method testInheritedOuterMethod.

public void testInheritedOuterMethod() {
    resolveSource("Test", "class Test { class A { void foo() {} } class B extends A { " + "class Inner { void test() { foo(); } } } }");
    TypeDeclaration aNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(1);
    TypeDeclaration bNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(2);
    TypeDeclaration innerNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(3);
    assertFalse(captureInfo.needsOuterReference(aNode.getTypeElement()));
    assertFalse(captureInfo.needsOuterReference(bNode.getTypeElement()));
    assertTrue(captureInfo.needsOuterReference(innerNode.getTypeElement()));
    // B will need an outer reference to Test so it can initialize its
    // superclass A.
    Expression bSuperOuter = bNode.getSuperOuter();
    assertTrue(bSuperOuter instanceof SimpleName);
    assertEquals("outer$", ElementUtil.getName(TreeUtil.getVariableElement(bSuperOuter)));
    // foo() call will need to get to B's scope to call the inherited method.
    MethodInvocation fooCall = (MethodInvocation) nodesByType.get(Kind.METHOD_INVOCATION).get(0);
    Expression expr = fooCall.getExpression();
    assertTrue(expr instanceof SimpleName);
    VariableElement fooReceiver = TreeUtil.getVariableElement(expr);
    assertNotNull(fooReceiver);
    assertEquals("Test.B", fooReceiver.asType().toString());
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) VariableElement(javax.lang.model.element.VariableElement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Aggregations

SimpleName (com.google.devtools.j2objc.ast.SimpleName)48 VariableElement (javax.lang.model.element.VariableElement)26 Expression (com.google.devtools.j2objc.ast.Expression)25 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)16 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)16 TypeMirror (javax.lang.model.type.TypeMirror)15 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)14 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)14 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)13 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)13 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)12 Block (com.google.devtools.j2objc.ast.Block)11 Statement (com.google.devtools.j2objc.ast.Statement)11 ExecutableElement (javax.lang.model.element.ExecutableElement)11 TypeElement (javax.lang.model.element.TypeElement)10 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)9 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)9 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)9 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)8 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)8