Search in sources :

Example 6 with GeneratedVariableElement

use of com.google.devtools.j2objc.types.GeneratedVariableElement in project j2objc by google.

the class MethodTranslator method visitForEachStatement.

@Override
public TreeNode visitForEachStatement(ForEachStatement node, Void data) {
    EnhancedForStatement forStatement = new EnhancedForStatement();
    AstType astType = node.getVariableType();
    Type type = (Type) astType.acceptVisitor(this, null);
    String varName = node.getVariableName();
    GeneratedVariableElement elem = GeneratedVariableElement.newLocalVar(varName, type.getTypeMirror(), executableElement);
    elem.addModifiers(node.getVariableModifiers());
    localVariableTable.put(varName, elem);
    return forStatement.setParameter(new SingleVariableDeclaration(elem)).setExpression((Expression) node.getInExpression().acceptVisitor(this, null)).setBody((Statement) node.getEmbeddedStatement().acceptVisitor(this, null));
}
Also used : AstType(com.strobel.decompiler.languages.java.ast.AstType) ComposedType(com.strobel.decompiler.languages.java.ast.ComposedType) AstType(com.strobel.decompiler.languages.java.ast.AstType) 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) WildcardType(com.strobel.decompiler.languages.java.ast.WildcardType) ExecutableType(javax.lang.model.type.ExecutableType) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) SuperReferenceExpression(com.strobel.decompiler.languages.java.ast.SuperReferenceExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) AnonymousObjectCreationExpression(com.strobel.decompiler.languages.java.ast.AnonymousObjectCreationExpression) Expression(com.google.devtools.j2objc.ast.Expression) TypeReferenceExpression(com.strobel.decompiler.languages.java.ast.TypeReferenceExpression) UnaryOperatorExpression(com.strobel.decompiler.languages.java.ast.UnaryOperatorExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) BinaryOperatorExpression(com.strobel.decompiler.languages.java.ast.BinaryOperatorExpression) ClassOfExpression(com.strobel.decompiler.languages.java.ast.ClassOfExpression) ThisReferenceExpression(com.strobel.decompiler.languages.java.ast.ThisReferenceExpression) AssignmentExpression(com.strobel.decompiler.languages.java.ast.AssignmentExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) PrimitiveExpression(com.strobel.decompiler.languages.java.ast.PrimitiveExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) MemberReferenceExpression(com.strobel.decompiler.languages.java.ast.MemberReferenceExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ObjectCreationExpression(com.strobel.decompiler.languages.java.ast.ObjectCreationExpression) InvocationExpression(com.strobel.decompiler.languages.java.ast.InvocationExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) ArrayCreationExpression(com.strobel.decompiler.languages.java.ast.ArrayCreationExpression) MethodGroupExpression(com.strobel.decompiler.languages.java.ast.MethodGroupExpression) IdentifierExpression(com.strobel.decompiler.languages.java.ast.IdentifierExpression) IndexerExpression(com.strobel.decompiler.languages.java.ast.IndexerExpression) NullReferenceExpression(com.strobel.decompiler.languages.java.ast.NullReferenceExpression) ArrayInitializerExpression(com.strobel.decompiler.languages.java.ast.ArrayInitializerExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement)

Example 7 with GeneratedVariableElement

use of com.google.devtools.j2objc.types.GeneratedVariableElement in project j2objc by google.

the class AnnotationRewriter method addEqualsMethod.

private void addEqualsMethod(AnnotationTypeDeclaration node) {
    TypeElement typeElement = node.getTypeElement();
    GeneratedExecutableElement equalsElement = GeneratedExecutableElement.newMethodWithSelector("isEqual:", typeUtil.getBoolean(), typeElement);
    GeneratedVariableElement paramElement = GeneratedVariableElement.newParameter("obj", typeUtil.getJavaObject().asType(), equalsElement);
    equalsElement.addParameter(paramElement);
    NativeStatement stmt = new NativeStatement("return JreAnnotationEquals(self, obj);");
    node.addBodyDeclaration(new MethodDeclaration(equalsElement).addParameter(new SingleVariableDeclaration(paramElement)).setBody(new Block().addStatement(stmt)).setModifiers(java.lang.reflect.Modifier.PUBLIC));
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) Block(com.google.devtools.j2objc.ast.Block)

Example 8 with GeneratedVariableElement

use of com.google.devtools.j2objc.types.GeneratedVariableElement in project j2objc by google.

the class EnumRewriter method addSimpleNonArcInitialization.

private void addSimpleNonArcInitialization(EnumDeclaration node) {
    List<EnumConstantDeclaration> constants = node.getEnumConstants();
    List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
    stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
    stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", constants.size())));
    stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
    VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
    stmts.add(new VariableDeclarationStatement(localEnum, null));
    // Create a local array of enum names only if reflection is stripped but
    // enum constants are not. For non-stripped classes, enum names are now
    // retrieved from metadata, to avoid duplicates.
    boolean useNamesArray = options.stripReflection() && !options.stripEnumConstants();
    if (useNamesArray) {
        StringBuilder sb = new StringBuilder("id names[] = {\n  ");
        for (EnumConstantDeclaration constant : constants) {
            sb.append("@\"" + ElementUtil.getName(constant.getVariableElement()) + "\", ");
        }
        sb.append("\n};");
        stmts.add(new NativeStatement(sb.toString()));
    }
    TypeMirror intType = typeUtil.getInt();
    GeneratedVariableElement loopCounterElement = GeneratedVariableElement.newLocalVar("i", intType, TreeUtil.getEnclosingElement(node));
    VariableDeclarationExpression loopCounter = new VariableDeclarationExpression().setType(Type.newType(loopCounterElement.asType())).addFragment(new VariableDeclarationFragment(loopCounterElement, TreeUtil.newLiteral(0, typeUtil)));
    Expression loopTest = new InfixExpression().setOperator(InfixExpression.Operator.LESS).setTypeMirror(intType).addOperand(new SimpleName(loopCounterElement)).addOperand(TreeUtil.newLiteral(constants.size(), typeUtil));
    Expression loopUpdater = new PostfixExpression(loopCounterElement, PostfixExpression.Operator.INCREMENT);
    Block loopBody = new Block();
    stmts.add(new ForStatement().addInitializer(loopCounter).setExpression(loopTest).addUpdater(loopUpdater).setBody(loopBody));
    String enumClassName = nameTable.getFullName(node.getTypeElement());
    loopBody.addStatement(new NativeStatement("((void)(" + enumClassName + "_values_[i] = e = objc_constructInstance(self, (void *)ptr)), ptr += objSize);"));
    if (useNamesArray) {
        loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, names[i], i);"));
    } else if (options.stripEnumConstants()) {
        String statementText = enumClassName + "_initWithNSString_withInt_(e, " + ENUM_NAME_STRIPPED + ", i);";
        loopBody.addStatement(new NativeStatement(statementText));
    } else {
        loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, JreEnumConstantName(" + enumClassName + "_class_(), i), i);"));
    }
}
Also used : GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) TypeMirror(javax.lang.model.type.TypeMirror) CastExpression(com.google.devtools.j2objc.ast.CastExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) ForStatement(com.google.devtools.j2objc.ast.ForStatement)

Aggregations

GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)8 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)5 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)5 Block (com.google.devtools.j2objc.ast.Block)4 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)4 Expression (com.google.devtools.j2objc.ast.Expression)3 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)3 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)3 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)3 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)3 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)3 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)3 ArrayType (com.google.devtools.j2objc.ast.ArrayType)2 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)2 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)2 SimpleType (com.google.devtools.j2objc.ast.SimpleType)2 Type (com.google.devtools.j2objc.ast.Type)2 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)2 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)2 AnonymousObjectCreationExpression (com.strobel.decompiler.languages.java.ast.AnonymousObjectCreationExpression)2