Search in sources :

Example 91 with VariableElement

use of javax.lang.model.element.VariableElement 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 92 with VariableElement

use of javax.lang.model.element.VariableElement in project j2objc by google.

the class AbstractMethodRewriter method newReturnTypeNarrowingDeclaration.

private MethodDeclaration newReturnTypeNarrowingDeclaration(String selector, ExecutablePair method, TypeElement declaringClass) {
    GeneratedExecutableElement element = GeneratedExecutableElement.newMethodWithSelector(selector, method.type().getReturnType(), declaringClass).addModifiers(ElementUtil.getVisibilityModifiers(method.element())).addModifiers(Modifier.ABSTRACT);
    MethodDeclaration decl = new MethodDeclaration(element);
    if (!declaringClass.getKind().isInterface()) {
        unit.setHasIncompleteImplementation();
    }
    int argCount = 0;
    for (TypeMirror paramType : method.type().getParameterTypes()) {
        VariableElement param = GeneratedVariableElement.newParameter("arg" + argCount++, paramType, element);
        element.addParameter(param);
        decl.addParameter(new SingleVariableDeclaration(param));
    }
    return decl;
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) TypeMirror(javax.lang.model.type.TypeMirror) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 93 with VariableElement

use of javax.lang.model.element.VariableElement in project j2objc by google.

the class AnnotationRewriter method endVisit.

@Override
public void endVisit(AnnotationTypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    if (!ElementUtil.isRuntimeAnnotation(type)) {
        return;
    }
    List<AnnotationTypeMemberDeclaration> members = TreeUtil.getAnnotationMembers(node);
    List<BodyDeclaration> bodyDecls = node.getBodyDeclarations();
    Map<ExecutableElement, VariableElement> fieldElements = createMemberFields(node, members);
    addMemberProperties(node, members, fieldElements);
    addDefaultAccessors(node, members);
    bodyDecls.add(createAnnotationTypeMethod(type));
    bodyDecls.add(createDescriptionMethod(type));
    addConstructor(node, fieldElements);
}
Also used : AnnotationTypeMemberDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 94 with VariableElement

use of javax.lang.model.element.VariableElement in project j2objc by google.

the class AnnotationRewriter method createMemberFields.

// Create an instance field for each member.
private Map<ExecutableElement, VariableElement> createMemberFields(AnnotationTypeDeclaration node, List<AnnotationTypeMemberDeclaration> members) {
    TypeElement type = node.getTypeElement();
    Map<ExecutableElement, VariableElement> fieldElements = new HashMap<>();
    for (AnnotationTypeMemberDeclaration member : members) {
        ExecutableElement memberElement = member.getExecutableElement();
        String propName = NameTable.getAnnotationPropertyName(memberElement);
        VariableElement field = GeneratedVariableElement.newField(propName, memberElement.getReturnType(), type);
        node.addBodyDeclaration(new FieldDeclaration(field, null));
        fieldElements.put(memberElement, field);
    }
    return fieldElements;
}
Also used : AnnotationTypeMemberDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 95 with VariableElement

use of javax.lang.model.element.VariableElement 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)

Aggregations

VariableElement (javax.lang.model.element.VariableElement)248 ExecutableElement (javax.lang.model.element.ExecutableElement)75 TypeElement (javax.lang.model.element.TypeElement)75 TypeMirror (javax.lang.model.type.TypeMirror)72 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)35 Element (javax.lang.model.element.Element)35 Expression (com.google.devtools.j2objc.ast.Expression)30 SimpleName (com.google.devtools.j2objc.ast.SimpleName)28 JVar (com.helger.jcodemodel.JVar)25 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)23 ArrayList (java.util.ArrayList)23 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)16 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)16 JInvocation (com.helger.jcodemodel.JInvocation)16 DeclaredType (javax.lang.model.type.DeclaredType)16 JBlock (com.helger.jcodemodel.JBlock)15 AbstractJClass (com.helger.jcodemodel.AbstractJClass)14 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)13 Elements (javax.lang.model.util.Elements)13 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)12