Search in sources :

Example 16 with SimpleName

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

the class OuterReferenceResolverTest method testNestedLocalClassesWithNestedCreations.

public void testNestedLocalClassesWithNestedCreations() {
    // This test is particularly tricky for OuterReferenceResolver because A captures variable i,
    // but that is not known until after A's creation. A's creation occurs within B, which requires
    // B to have an outer field in order to access A's capturing field for i. B's creation therefore
    // requires the outer field to be passed as an outer argument.
    // Because of the cascading effects of the statements in this test and the order in which they
    // occur, we would need to do three passes over the code to resolve B's creation successfuly.
    resolveSource("Test", "class Test { void test(int i) { class A { " + "void foo() { class B { void bar() { new B(); new A(); } } } " + "int other() { return i; } } } }");
    ClassInstanceCreation bCreate = (ClassInstanceCreation) nodesByType.get(Kind.CLASS_INSTANCE_CREATION).get(0);
    Expression outerArg = bCreate.getExpression();
    assertTrue(outerArg instanceof SimpleName);
    VariableElement var = TreeUtil.getVariableElement(outerArg);
    assertNotNull(var);
    assertEquals("this$0", ElementUtil.getName(var));
}
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)

Example 17 with SimpleName

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

the class TreeConverter method convertExpressionMethodReference.

private static TreeNode convertExpressionMethodReference(org.eclipse.jdt.core.dom.ExpressionMethodReference node) {
    IMethodBinding methodBinding = node.resolveMethodBinding();
    ExpressionMethodReference newNode = new ExpressionMethodReference();
    Expression expression = (Expression) convert(node.getExpression());
    boolean consumesFirstParam = !BindingUtil.isStatic(methodBinding) && expression instanceof Name && !ElementUtil.isVariable(((Name) expression).getElement());
    convertMethodReference(node, newNode, methodBinding, consumesFirstParam);
    return newNode.setName((SimpleName) TreeConverter.convert(node.getName())).setExpression(expression);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ExpressionMethodReference(com.google.devtools.j2objc.ast.ExpressionMethodReference) 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) SimpleName(com.google.devtools.j2objc.ast.SimpleName) SimpleName(com.google.devtools.j2objc.ast.SimpleName) Name(com.google.devtools.j2objc.ast.Name) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName)

Example 18 with SimpleName

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

the class TreeConverter method convertTypeMethodReference.

private static TreeNode convertTypeMethodReference(org.eclipse.jdt.core.dom.TypeMethodReference node) {
    TypeMethodReference newNode = new TypeMethodReference();
    IMethodBinding methodBinding = node.resolveMethodBinding();
    if (node.getType().resolveBinding().isArray()) {
        // JDT does not provide the correct method on array types, so we find it from
        // java.lang.Object.
        IMethodBinding functionalInterface = getFunctionalInterface(node.resolveTypeBinding());
        methodBinding = getObjectMethod(node.getAST(), node.getName().getIdentifier(), functionalInterface.getParameterTypes().length - 1);
    }
    convertMethodReference(node, newNode, methodBinding, !BindingUtil.isStatic(methodBinding));
    return newNode.setName((SimpleName) TreeConverter.convert(node.getName())).setType((Type) TreeConverter.convert(node.getType()));
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SimpleName(com.google.devtools.j2objc.ast.SimpleName) TypeMethodReference(com.google.devtools.j2objc.ast.TypeMethodReference)

Example 19 with SimpleName

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

the class CastResolver method endVisit.

/**
   * Adds a cast check to compareTo methods. This helps Comparable types behave
   * well in sorted collections which rely on Java's runtime type checking.
   */
@Override
public void endVisit(MethodDeclaration node) {
    ExecutableElement element = node.getExecutableElement();
    if (!ElementUtil.getName(element).equals("compareTo") || node.getBody() == null) {
        return;
    }
    DeclaredType comparableType = typeUtil.findSupertype(ElementUtil.getDeclaringClass(element).asType(), "java.lang.Comparable");
    if (comparableType == null) {
        return;
    }
    List<? extends TypeMirror> typeArguments = comparableType.getTypeArguments();
    List<? extends VariableElement> parameters = element.getParameters();
    if (typeArguments.size() != 1 || parameters.size() != 1 || !typeArguments.get(0).equals(parameters.get(0).asType())) {
        return;
    }
    VariableElement param = node.getParameter(0).getVariableElement();
    FunctionInvocation castCheck = createCastCheck(typeArguments.get(0), new SimpleName(param));
    if (castCheck != null) {
        node.getBody().addStatement(0, new ExpressionStatement(castCheck));
    }
}
Also used : FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableElement(javax.lang.model.element.VariableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 20 with SimpleName

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

the class TreeConverter method convertSimpleName.

private static TreeNode convertSimpleName(org.eclipse.jdt.core.dom.SimpleName node) {
    SimpleName newNode = new SimpleName();
    convertName(node, newNode);
    boolean isConstructor = node.resolveBinding() instanceof IMethodBinding && ((IMethodBinding) node.resolveBinding()).isConstructor();
    return newNode.setIdentifier(isConstructor ? "<init>" : node.getIdentifier()).setTypeMirror(BindingConverter.getType(node.resolveTypeBinding()));
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SimpleName(com.google.devtools.j2objc.ast.SimpleName)

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