Search in sources :

Example 1 with NativeStatement

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

the class OcniExtractor method endVisit.

@Override
public void endVisit(MethodDeclaration node) {
    int modifiers = node.getModifiers();
    if (Modifier.isNative(modifiers)) {
        NativeStatement nativeStmt = extractNativeStatement(node);
        if (nativeStmt != null) {
            Block body = new Block();
            body.addStatement(nativeStmt);
            node.setBody(body);
            node.removeModifiers(Modifier.NATIVE);
        }
    }
    if (Modifier.isSynchronized(modifiers)) {
        TypeElement declaringClass = ElementUtil.getDeclaringClass(node.getExecutableElement());
        SynchronizedStatement syncStmt = new SynchronizedStatement(Modifier.isStatic(modifiers) ? new TypeLiteral(declaringClass.asType(), typeUtil) : new ThisExpression(declaringClass.asType()));
        syncStmt.setBody(TreeUtil.remove(node.getBody()));
        Block newBody = new Block();
        newBody.addStatement(syncStmt);
        node.setBody(newBody);
        node.removeModifiers(Modifier.SYNCHRONIZED);
    }
}
Also used : ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) TypeElement(javax.lang.model.element.TypeElement) Block(com.google.devtools.j2objc.ast.Block) SynchronizedStatement(com.google.devtools.j2objc.ast.SynchronizedStatement)

Example 2 with NativeStatement

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

the class AnnotationRewriter method addHashCodeMethod.

private void addHashCodeMethod(AnnotationTypeDeclaration node) {
    GeneratedExecutableElement element = GeneratedExecutableElement.newMethodWithSelector("hash", typeUtil.getInt(), node.getTypeElement());
    NativeStatement stmt = new NativeStatement("return JreAnnotationHashCode(self);");
    node.addBodyDeclaration(new MethodDeclaration(element).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) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) Block(com.google.devtools.j2objc.ast.Block)

Example 3 with NativeStatement

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

the class DestructorGenerator method addDeallocMethod.

private void addDeallocMethod(AbstractTypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    boolean hasFinalize = hasFinalizeMethod(type);
    List<Statement> releaseStatements = options.useARC() ? ImmutableList.of() : createReleaseStatements(node);
    MethodDeclaration onDeallocMethodDeclaration = getOnDeallocMethodDeclaration(node);
    if (releaseStatements.isEmpty() && !hasFinalize && onDeallocMethodDeclaration == null) {
        return;
    }
    ExecutableElement deallocElement = GeneratedExecutableElement.newMethodWithSelector(NameTable.DEALLOC_METHOD, typeUtil.getVoid(), type).addModifiers(Modifier.PUBLIC);
    MethodDeclaration deallocDecl = new MethodDeclaration(deallocElement);
    deallocDecl.setHasDeclaration(false);
    Block block = new Block();
    deallocDecl.setBody(block);
    List<Statement> stmts = block.getStatements();
    if (onDeallocMethodDeclaration != null) {
        stmts.add(new ExpressionStatement(new MethodInvocation(new ExecutablePair(onDeallocMethodDeclaration.getExecutableElement()), null)));
    }
    if (hasFinalize) {
        String clsName = nameTable.getFullName(type);
        stmts.add(new NativeStatement("JreCheckFinalize(self, [" + clsName + " class]);"));
    }
    stmts.addAll(releaseStatements);
    if (options.useReferenceCounting()) {
        stmts.add(new ExpressionStatement(new SuperMethodInvocation(new ExecutablePair(superDeallocElement))));
    }
    node.addBodyDeclaration(deallocDecl);
}
Also used : ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Statement(com.google.devtools.j2objc.ast.Statement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 4 with NativeStatement

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

the class EnumRewriter method addValuesMethod.

private void addValuesMethod(EnumDeclaration node) {
    TypeElement type = node.getTypeElement();
    ExecutableElement method = ElementUtil.findMethod(type, "values");
    assert method != null : "Can't find values method on enum type.";
    String typeName = nameTable.getFullName(type);
    MethodDeclaration methodDecl = new MethodDeclaration(method);
    Block body = new Block();
    methodDecl.setBody(body);
    body.addStatement(new NativeStatement(UnicodeUtils.format("  return [IOSObjectArray arrayWithObjects:%s_values_ count:%s type:%s_class_()];", typeName, node.getEnumConstants().size(), typeName)));
    node.addBodyDeclaration(methodDecl);
}
Also used : NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement) Block(com.google.devtools.j2objc.ast.Block)

Example 5 with NativeStatement

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

the class EnumRewriter method addValueOfMethod.

private void addValueOfMethod(EnumDeclaration node) {
    TypeElement type = node.getTypeElement();
    ExecutableElement method = ElementUtil.findMethod(type, "valueOf", "java.lang.String");
    assert method != null : "Can't find valueOf method on enum type.";
    String typeName = nameTable.getFullName(type);
    int numConstants = node.getEnumConstants().size();
    VariableElement nameParam = GeneratedVariableElement.newParameter("name", method.getParameters().get(0).asType(), method);
    MethodDeclaration methodDecl = new MethodDeclaration(method);
    methodDecl.addParameter(new SingleVariableDeclaration(nameParam));
    Block body = new Block();
    methodDecl.setBody(body);
    StringBuilder impl = new StringBuilder();
    if (options.stripEnumConstants()) {
        impl.append(UnicodeUtils.format("  @throw create_JavaLangError_initWithNSString_(@\"Enum.valueOf(String) " + "called on %s enum with stripped constant names\");", typeName));
    } else {
        if (numConstants > 0) {
            impl.append(UnicodeUtils.format("  for (int i = 0; i < %s; i++) {\n" + "    %s *e = %s_values_[i];\n" + "    if ([name isEqual:[e name]]) {\n" + "      return e;\n" + "    }\n" + "  }\n", numConstants, typeName, typeName));
        }
        impl.append("  @throw create_JavaLangIllegalArgumentException_initWithNSString_(name);\n" + "  return nil;");
    }
    body.addStatement(new NativeStatement(impl.toString()));
    node.addBodyDeclaration(methodDecl);
}
Also used : NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement) Block(com.google.devtools.j2objc.ast.Block) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Aggregations

NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)15 Block (com.google.devtools.j2objc.ast.Block)13 TypeElement (javax.lang.model.element.TypeElement)10 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)9 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)8 ExecutableElement (javax.lang.model.element.ExecutableElement)8 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)6 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)5 Statement (com.google.devtools.j2objc.ast.Statement)4 VariableElement (javax.lang.model.element.VariableElement)4 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)3 GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 CastExpression (com.google.devtools.j2objc.ast.CastExpression)2 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)2 ForStatement (com.google.devtools.j2objc.ast.ForStatement)2 FunctionDeclaration (com.google.devtools.j2objc.ast.FunctionDeclaration)2 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)2 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)2