Search in sources :

Example 1 with FieldDeclaration

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

the class DeadCodeEliminator method isInlinableConstant.

private boolean isInlinableConstant(BodyDeclaration decl) {
    if (!(decl instanceof FieldDeclaration)) {
        return false;
    }
    int modifiers = decl.getModifiers();
    if (!Modifier.isStatic(modifiers) || !Modifier.isFinal(modifiers) || Modifier.isPrivate(modifiers)) {
        return false;
    }
    TypeMirror type = ((FieldDeclaration) decl).getTypeMirror();
    if (!(type.getKind().isPrimitive() || typeUtil.isString(type))) {
        return false;
    }
    // Only when every fragment has constant value do we say this is inlinable.
    for (VariableDeclarationFragment fragment : ((FieldDeclaration) decl).getFragments()) {
        if (fragment.getVariableElement().getConstantValue() == null) {
            return false;
        }
    }
    return true;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 2 with FieldDeclaration

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

the class DeadCodeEliminator method removeDeadFields.

/**
   * Deletes non-constant dead fields from a type's body declarations list.
   */
private void removeDeadFields(String clazz, List<BodyDeclaration> declarations) {
    Iterator<BodyDeclaration> declarationsIter = declarations.iterator();
    while (declarationsIter.hasNext()) {
        BodyDeclaration declaration = declarationsIter.next();
        if (declaration instanceof FieldDeclaration) {
            FieldDeclaration field = (FieldDeclaration) declaration;
            Iterator<VariableDeclarationFragment> fragmentsIter = field.getFragments().iterator();
            while (fragmentsIter.hasNext()) {
                VariableDeclarationFragment fragment = fragmentsIter.next();
                // Don't delete any constants because we can't detect their use.
                VariableElement var = fragment.getVariableElement();
                if (var.getConstantValue() == null && deadCodeMap.containsField(clazz, ElementUtil.getName(var))) {
                    fragmentsIter.remove();
                }
            }
            if (field.getFragments().isEmpty()) {
                declarationsIter.remove();
            }
        }
    }
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) VariableElement(javax.lang.model.element.VariableElement) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 3 with FieldDeclaration

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

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

the class TreeConverter method convertVariableDeclaration.

private TreeNode convertVariableDeclaration(JCTree.JCVariableDecl node) {
    VarSymbol var = node.sym;
    if (var.getKind() == ElementKind.FIELD) {
        FieldDeclaration newNode = new FieldDeclaration(var, (Expression) convert(node.getInitializer()));
        convertBodyDeclaration(node, node.getModifiers(), newNode, var);
        return newNode;
    }
    if (var.getKind() == ElementKind.LOCAL_VARIABLE) {
        return new VariableDeclarationStatement(var, (Expression) convert(node.getInitializer()));
    }
    if (var.getKind() == ElementKind.ENUM_CONSTANT) {
        EnumConstantDeclaration newNode = new EnumConstantDeclaration().setVariableElement(var);
        convertBodyDeclaration(node, node.getModifiers(), newNode, var);
        ClassInstanceCreation init = (ClassInstanceCreation) convert(node.getInitializer());
        TreeUtil.moveList(init.getArguments(), newNode.getArguments());
        if (init.getAnonymousClassDeclaration() != null) {
            newNode.setAnonymousClassDeclaration(TreeUtil.remove(init.getAnonymousClassDeclaration()));
        }
        return newNode.setExecutablePair(init.getExecutablePair()).setVarargsType(init.getVarargsType());
    }
    return convertSingleVariable(node);
}
Also used : ClassInstanceCreation(com.google.devtools.j2objc.ast.ClassInstanceCreation) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 5 with FieldDeclaration

use of com.google.devtools.j2objc.ast.FieldDeclaration 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)

Aggregations

FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)10 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)6 VariableElement (javax.lang.model.element.VariableElement)5 TypeElement (javax.lang.model.element.TypeElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 AnnotationTypeMemberDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration)1 ClassInstanceCreation (com.google.devtools.j2objc.ast.ClassInstanceCreation)1 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)1 PropertyAnnotation (com.google.devtools.j2objc.ast.PropertyAnnotation)1 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)1 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)1 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)1 Property (com.google.j2objc.annotations.Property)1 Weak (com.google.j2objc.annotations.Weak)1 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)1 HashMap (java.util.HashMap)1