Search in sources :

Example 96 with VariableElement

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

the class TreeConverter method createAnonymousConstructor.

private static MethodDeclaration createAnonymousConstructor(JdtExecutableElement constructorElem, IMethodBinding constructorBinding) {
    MethodDeclaration constructor = new MethodDeclaration(constructorElem);
    Block body = new Block();
    constructor.setBody(body);
    IMethodBinding superConstructorBinding = findSuperConstructor(constructorBinding);
    ExecutablePair superConstructor = new ExecutablePair(BindingConverter.getExecutableElement(superConstructorBinding), BindingConverter.getType(superConstructorBinding));
    SuperConstructorInvocation superCall = new SuperConstructorInvocation(superConstructor);
    body.addStatement(superCall);
    Iterator<? extends VariableElement> params = constructorElem.getParameters().iterator();
    if (constructorElem.hasSuperOuter()) {
        VariableElement param = params.next();
        constructor.addParameter(new SingleVariableDeclaration(param));
        superCall.setExpression(new SimpleName(param));
    }
    while (params.hasNext()) {
        VariableElement param = params.next();
        constructor.addParameter(new SingleVariableDeclaration(param));
        superCall.addArgument(new SimpleName(param));
    }
    assert constructor.getParameters().size() == constructorElem.getParameters().size();
    return constructor;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) SimpleName(com.google.devtools.j2objc.ast.SimpleName) Block(com.google.devtools.j2objc.ast.Block) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 97 with VariableElement

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

the class ComplexExpressionExtractor method handleNode.

private void handleNode(Expression node, Collection<Expression> children) {
    if (node.getParent() instanceof Statement) {
        return;
    }
    int depth = 0;
    for (Expression child : children) {
        Integer childDepth = depths.get(child);
        depth = Math.max(depth, childDepth != null ? childDepth : 1);
    }
    if (depth >= maxDepth) {
        VariableElement newVar = GeneratedVariableElement.newLocalVar("complex$" + count++, node.getTypeMirror(), currentMethod);
        Statement newStmt = new VariableDeclarationStatement(newVar, node.copy());
        assert currentStatement != null;
        TreeUtil.insertBefore(currentStatement, newStmt);
        node.replaceWith(new SimpleName(newVar));
    } else {
        depths.put(node, depth + 1);
    }
}
Also used : 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) DoStatement(com.google.devtools.j2objc.ast.DoStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 98 with VariableElement

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

the class JavadocConverter method visitParam.

@Override
public Void visitParam(ParamTree node, TagElement tag) {
    DCTree.DCIdentifier identifier = (DCTree.DCIdentifier) node.getName();
    if (identifier == null || node.isTypeParameter()) {
        return null;
    }
    List<? extends VariableElement> params = element instanceof ExecutableElement ? ((ExecutableElement) element).getParameters() : Collections.emptyList();
    tag.setTagName(TagElement.TAG_PARAM);
    String name = identifier.toString();
    VariableElement param = null;
    for (VariableElement p : params) {
        if (name.equals(p.getSimpleName().toString())) {
            param = p;
            break;
        }
    }
    // param will be null if the @param tag refers to a nonexistent parameter.
    TreeNode nameNode = param != null ? new SimpleName(param) : new SimpleName(name);
    setPos(identifier, nameNode);
    tag.addFragment(nameNode);
    scan(node.getDescription(), tag);
    int lastEnd = nameNode.getStartPosition();
    for (TreeNode fragment : tag.getFragments()) {
        // TODO(tball): remove and fix JavadocGenerator after javac switch.
        if (fragment.getKind() == TreeNode.Kind.TEXT_ELEMENT) {
            TextElement text = (TextElement) fragment;
            text.setText(" " + text.getText());
            text.setSourceRange(text.getStartPosition(), text.getLength() + 1);
        }
        int thisEnd = lastEnd + fragment.getLength();
        setPos(fragment, lastEnd, thisEnd);
        lastEnd = thisEnd;
    }
    setPos(tag, pos(node), endPos(node));
    tag.setLineNumber(nameNode.getLineNumber());
    return null;
}
Also used : TextElement(com.google.devtools.j2objc.ast.TextElement) TreeNode(com.google.devtools.j2objc.ast.TreeNode) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) DCTree(com.sun.tools.javac.tree.DCTree)

Example 99 with VariableElement

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

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

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