Search in sources :

Example 81 with VariableElement

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

the class JavaToIOSMethodTranslator method addCopyWithZoneMethod.

private void addCopyWithZoneMethod(TypeDeclaration node) {
    // Create copyWithZone: method.
    GeneratedExecutableElement copyElement = GeneratedExecutableElement.newMethodWithSelector("copyWithZone:", TypeUtil.ID_TYPE, node.getTypeElement());
    MethodDeclaration copyDecl = new MethodDeclaration(copyElement);
    copyDecl.setHasDeclaration(false);
    // Add NSZone *zone parameter.
    VariableElement zoneParam = GeneratedVariableElement.newParameter("zone", NSZONE_TYPE, copyElement);
    copyElement.addParameter(zoneParam);
    copyDecl.addParameter(new SingleVariableDeclaration(zoneParam));
    Block block = new Block();
    copyDecl.setBody(block);
    ExecutableElement cloneElement = ElementUtil.findMethod(typeUtil.getJavaObject(), "clone");
    MethodInvocation invocation = new MethodInvocation(new ExecutablePair(cloneElement), null);
    if (options.useReferenceCounting()) {
        invocation = new MethodInvocation(new ExecutablePair(RETAIN_METHOD), invocation);
    }
    block.addStatement(new ReturnStatement(invocation));
    node.addBodyDeclaration(copyDecl);
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) Block(com.google.devtools.j2objc.ast.Block) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 82 with VariableElement

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

the class SuperMethodInvocationRewriter method getSuperFunctionSignature.

private static String getSuperFunctionSignature(ExecutableElement method) {
    StringBuilder signature = new StringBuilder(NameTable.getPrimitiveObjCType(method.getReturnType()));
    signature.append(" (*%s)(id, SEL");
    for (VariableElement param : method.getParameters()) {
        signature.append(", ").append(NameTable.getPrimitiveObjCType(param.asType()));
    }
    signature.append(")");
    return signature.toString();
}
Also used : VariableElement(javax.lang.model.element.VariableElement)

Example 83 with VariableElement

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

the class UnsequencedExpressionRewriter method getUnsequencedAccesses.

private List<VariableAccess> getUnsequencedAccesses() {
    if (!hasModification) {
        return Collections.emptyList();
    }
    ListMultimap<VariableElement, VariableAccess> accessesByVar = MultimapBuilder.hashKeys().arrayListValues().build();
    for (VariableAccess access : orderedAccesses) {
        accessesByVar.put(access.variable, access);
    }
    Set<VariableAccess> unsequencedAccesses = Sets.newHashSet();
    for (VariableElement var : accessesByVar.keySet()) {
        findUnsequenced(accessesByVar.get(var), unsequencedAccesses);
    }
    List<VariableAccess> orderedUnsequencedAccesses = Lists.newArrayListWithCapacity(unsequencedAccesses.size());
    for (VariableAccess access : orderedAccesses) {
        if (unsequencedAccesses.contains(access)) {
            orderedUnsequencedAccesses.add(access);
        }
    }
    return orderedUnsequencedAccesses;
}
Also used : VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 84 with VariableElement

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

the class VariableRenamer method collectAndRenameFields.

private void collectAndRenameFields(TypeElement type, Set<VariableElement> fields) {
    if (type == null) {
        return;
    }
    collectAndRenameFields(ElementUtil.getSuperclass(type), fields);
    if (!renamedTypes.contains(type)) {
        renamedTypes.add(type);
        Set<String> superFieldNames = new HashSet<>();
        for (VariableElement superField : fields) {
            superFieldNames.add(superField.getSimpleName().toString());
        }
        // Look for methods that might conflict with a static variable when functionized.
        Set<String> staticMethodNames = new HashSet<>();
        for (ExecutableElement method : ElementUtil.getExecutables(type)) {
            if (method.getParameters().size() == 0) {
                staticMethodNames.add(nameTable.getFunctionName(method));
            }
        }
        for (VariableElement field : ElementUtil.getDeclaredFields(type)) {
            String fieldName = field.getSimpleName().toString();
            if (ElementUtil.isGlobalVar(field)) {
                if (staticMethodNames.contains(fieldName)) {
                    while (staticMethodNames.contains(fieldName)) {
                        fieldName += "_";
                    }
                    nameTable.setVariableName(field, fieldName);
                }
            } else if (!ElementUtil.isStatic(field) && superFieldNames.contains(fieldName)) {
                fieldName += "_" + type.getSimpleName();
                nameTable.setVariableName(field, fieldName);
            }
        }
    }
    for (VariableElement field : ElementUtil.getDeclaredFields(type)) {
        if (!ElementUtil.isStatic(field)) {
            fields.add(field);
        }
    }
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) HashSet(java.util.HashSet)

Example 85 with VariableElement

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

the class VariableRenamer method pushType.

private void pushType(TypeElement type) {
    Set<VariableElement> fields = new HashSet<>();
    collectAndRenameFields(type, fields);
    Set<String> fullFieldNames = new HashSet<>();
    for (VariableElement field : fields) {
        fullFieldNames.add(nameTable.getVariableShortName(field));
    }
    fieldNameStack.add(fullFieldNames);
}
Also used : VariableElement(javax.lang.model.element.VariableElement) HashSet(java.util.HashSet)

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