Search in sources :

Example 6 with VariableDeclarationFragment

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

the class AnonymousClassConverterTest method testMethodVarInAnonymousClass.

public void testMethodVarInAnonymousClass() throws IOException {
    String source = "class Test { " + "  boolean debug;" + "  void foo() { " + "    if (true) {" + "      if (debug) {" + "        final Integer i = 1;" + "        Runnable r = new Runnable() { " + "          public void run() { int j = i + 1; } }; }}}}";
    // Verify method var in r1.run() isn't mistakenly made a field in r1.
    CompilationUnit unit = translateType("Test", source);
    NameTable nameTable = unit.getEnv().nameTable();
    List<AbstractTypeDeclaration> types = unit.getTypes();
    AbstractTypeDeclaration r1 = types.get(1);
    assertEquals("Test_1", nameTable.getFullName(r1.getTypeElement()));
    boolean found = false;
    for (VariableDeclarationFragment var : TreeUtil.getAllFields(r1)) {
        if (ElementUtil.getName(var.getVariableElement()).equals("val$i")) {
            found = true;
        }
    }
    assertTrue("required field not found", found);
    // Verify method var is passed to constructor.
    String translation = generateFromUnit(unit, "Test.m");
    assertTranslation(translation, "r = create_Test_1_initWithJavaLangInteger_(i)");
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) NameTable(com.google.devtools.j2objc.util.NameTable) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 7 with VariableDeclarationFragment

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

the class TreeConverter method convertVariableExpression.

private VariableDeclarationExpression convertVariableExpression(JCTree.JCVariableDecl node) {
    VarSymbol var = node.sym;
    boolean isVarargs = (node.sym.flags() & Flags.VARARGS) > 0;
    Type newType = convertType(var.asType(), getPosition(node), isVarargs);
    VariableDeclarationFragment fragment = new VariableDeclarationFragment();
    fragment.setVariableElement(var).setInitializer((Expression) convert(node.getInitializer()));
    return new VariableDeclarationExpression().setType(newType).addFragment(fragment);
}
Also used : ParameterizedType(com.google.devtools.j2objc.ast.ParameterizedType) Type(com.google.devtools.j2objc.ast.Type) SimpleType(com.google.devtools.j2objc.ast.SimpleType) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(com.google.devtools.j2objc.ast.ArrayType) PrimitiveType(com.google.devtools.j2objc.ast.PrimitiveType) UnionType(com.google.devtools.j2objc.ast.UnionType) ExecutableType(javax.lang.model.type.ExecutableType) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 8 with VariableDeclarationFragment

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

the class TypeDeclarationGenerator method printProperties.

protected void printProperties() {
    Iterable<VariableDeclarationFragment> fields = getAllFields();
    for (VariableDeclarationFragment fragment : fields) {
        FieldDeclaration fieldDecl = (FieldDeclaration) fragment.getParent();
        VariableElement varElement = fragment.getVariableElement();
        PropertyAnnotation property = (PropertyAnnotation) TreeUtil.getAnnotation(Property.class, fieldDecl.getAnnotations());
        if (property != null) {
            print("@property ");
            TypeMirror varType = varElement.asType();
            String propertyName = nameTable.getVariableBaseName(varElement);
            // Add default getter/setter here, as each fragment needs its own attributes
            // to support its unique accessors.
            Set<String> attributes = property.getPropertyAttributes();
            TypeElement declaringClass = ElementUtil.getDeclaringClass(varElement);
            if (property.getGetter() == null) {
                ExecutableElement getter = findGetterMethod(propertyName, varType, declaringClass);
                if (getter != null) {
                    attributes.add("getter=" + NameTable.getMethodName(getter));
                    if (!ElementUtil.isSynchronized(getter)) {
                        attributes.add("nonatomic");
                    }
                }
            }
            if (property.getSetter() == null) {
                ExecutableElement setter = findSetterMethod(propertyName, declaringClass);
                if (setter != null) {
                    attributes.add("setter=" + NameTable.getMethodName(setter));
                    if (!ElementUtil.isSynchronized(setter)) {
                        attributes.add("nonatomic");
                    }
                }
            }
            if (ElementUtil.isStatic(varElement)) {
                attributes.add("class");
            } else if (attributes.contains("class")) {
                ErrorUtil.error(fragment, "Only static fields can be translated to class properties");
            }
            if (attributes.contains("class") && !options.staticAccessorMethods()) {
                // Class property accessors must be present, as they are not synthesized by runtime.
                ErrorUtil.error(fragment, "Class properties require either a --swift-friendly or" + " --static-accessor-methods flag");
            }
            if (options.nullability()) {
                if (ElementUtil.hasNullableAnnotation(varElement)) {
                    attributes.add("nullable");
                } else if (ElementUtil.isNonnull(varElement, parametersNonnullByDefault)) {
                    attributes.add("nonnull");
                } else if (!attributes.contains("null_unspecified")) {
                    attributes.add("null_resettable");
                }
            }
            if (!attributes.isEmpty()) {
                print('(');
                print(PropertyAnnotation.toAttributeString(attributes));
                print(") ");
            }
            String objcType = nameTable.getObjCType(varType);
            print(objcType);
            if (!objcType.endsWith("*")) {
                print(' ');
            }
            println(propertyName + ";");
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Property(com.google.j2objc.annotations.Property) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration) PropertyAnnotation(com.google.devtools.j2objc.ast.PropertyAnnotation)

Example 9 with VariableDeclarationFragment

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

the class TypeDeclarationGenerator method printFieldSetters.

protected void printFieldSetters() {
    Iterable<VariableDeclarationFragment> fields = Iterables.filter(getInstanceFields(), NEEDS_SETTER);
    if (Iterables.isEmpty(fields)) {
        return;
    }
    newline();
    for (VariableDeclarationFragment fragment : fields) {
        VariableElement var = fragment.getVariableElement();
        String typeStr = nameTable.getObjCType(var.asType());
        if (typeStr.contains(",")) {
            typeStr = "J2OBJC_ARG(" + typeStr + ')';
        }
        String fieldName = nameTable.getVariableShortName(var);
        String isVolatile = ElementUtil.isVolatile(var) ? "_VOLATILE" : "";
        println(UnicodeUtils.format("J2OBJC%s_FIELD_SETTER(%s, %s, %s)", isVolatile, typeName, fieldName, typeStr));
    }
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableElement(javax.lang.model.element.VariableElement)

Example 10 with VariableDeclarationFragment

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

the class TypeImplementationGenerator method printStaticVars.

private void printStaticVars() {
    Iterable<VariableDeclarationFragment> fields = Iterables.filter(getStaticFields(), NEEDS_DEFINITION);
    if (Iterables.isEmpty(fields)) {
        return;
    }
    newline();
    for (VariableDeclarationFragment fragment : fields) {
        VariableElement varElement = fragment.getVariableElement();
        Expression initializer = fragment.getInitializer();
        String name = nameTable.getVariableQualifiedName(varElement);
        String objcType = getDeclarationType(varElement);
        objcType += objcType.endsWith("*") ? "" : " ";
        if (initializer != null) {
            String cast = !varElement.asType().getKind().isPrimitive() && ElementUtil.isVolatile(varElement) ? "(void *)" : "";
            printf("%s%s = %s%s;\n", objcType, name, cast, generateExpression(initializer));
        } else {
            printf("%s%s;\n", objcType, name);
        }
    }
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)24 VariableElement (javax.lang.model.element.VariableElement)11 FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)6 Statement (com.google.devtools.j2objc.ast.Statement)5 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)5 TypeMirror (javax.lang.model.type.TypeMirror)5 Block (com.google.devtools.j2objc.ast.Block)4 Expression (com.google.devtools.j2objc.ast.Expression)4 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)4 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)3 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)3 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)3 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)3 ForStatement (com.google.devtools.j2objc.ast.ForStatement)3 NameTable (com.google.devtools.j2objc.util.NameTable)3 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 EnumDeclaration (com.google.devtools.j2objc.ast.EnumDeclaration)2 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)2 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)2