Example usage for org.eclipse.jdt.core.dom SuperMethodInvocation setName

List of usage examples for org.eclipse.jdt.core.dom SuperMethodInvocation setName

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom SuperMethodInvocation setName.

Prototype

public void setName(SimpleName name) 

Source Link

Document

Sets the name of the method invoked in this expression to the given name.

Usage

From source file:com.google.devtools.j2cpp.translate.DestructorGenerator.java

License:Open Source License

@SuppressWarnings("unchecked")
private void addReleaseStatements(MethodDeclaration method, List<IVariableBinding> fields) {
    // Find existing super.finalize(), if any.
    final boolean[] hasSuperFinalize = new boolean[1];
    method.accept(new ASTVisitor() {
        @Override/* w  ww . j  a v a  2 s  .co  m*/
        public void endVisit(SuperMethodInvocation node) {
            if (FINALIZE_METHOD.equals(node.getName().getIdentifier())) {
                hasSuperFinalize[0] = true;
            }
        }
    });

    List<Statement> statements = method.getBody().statements(); // safe by definition
    if (!statements.isEmpty() && statements.get(0) instanceof TryStatement) {
        TryStatement tryStatement = ((TryStatement) statements.get(0));
        if (tryStatement.getBody() != null) {
            statements = tryStatement.getBody().statements(); // safe by definition
        }
    }
    AST ast = method.getAST();
    int index = statements.size();
    for (IVariableBinding field : fields) {
        if (!field.getType().isPrimitive() && !Types.isWeakReference(field)) {
            Assignment assign = ast.newAssignment();
            SimpleName receiver = ast.newSimpleName(field.getName());
            Types.addBinding(receiver, field);
            assign.setLeftHandSide(receiver);
            assign.setRightHandSide(Types.newNullLiteral());
            Types.addBinding(assign, field.getDeclaringClass());
            ExpressionStatement stmt = ast.newExpressionStatement(assign);
            statements.add(index, stmt);
        }
    }
    if (Options.useReferenceCounting() && !hasSuperFinalize[0]) {
        SuperMethodInvocation call = ast.newSuperMethodInvocation();
        IMethodBinding methodBinding = Types.getMethodBinding(method);
        GeneratedMethodBinding binding = new GeneratedMethodBinding(destructorName, Modifier.PUBLIC,
                Types.mapTypeName("void"), methodBinding.getDeclaringClass(), false, false, true);
        Types.addBinding(call, binding);
        call.setName(ast.newSimpleName(destructorName));
        Types.addBinding(call.getName(), binding);
        ExpressionStatement stmt = ast.newExpressionStatement(call);
        statements.add(stmt);
    }
}

From source file:com.google.devtools.j2cpp.translate.JavaToIOSMethodTranslator.java

License:Open Source License

@Override
public boolean visit(SuperMethodInvocation node) {
    // translate any embedded method invocations
    @SuppressWarnings("unchecked")
    List<Expression> args = node.arguments(); // safe by definition
    for (Expression e : args) {
        e.accept(this);
    }/* www.  j  a  va  2s  .  c  o m*/

    IMethodBinding binding = Types.getMethodBinding(node);
    JavaMethod md = descriptions.get(binding);
    if (md != null) {
        String key = md.getKey();
        String value = methodMappings.get(key);
        if (value == null) {
            // Method has same name as a mapped method's, but it's ignored since
            // it doesn't override it.
            return super.visit(node);
        }
        IOSMethod iosMethod = new IOSMethod(value, binding, ast);
        node.setName(NameTable.unsafeSimpleName(iosMethod.getName(), ast));
        SimpleName name = node.getName();
        if (name.getIdentifier().equals(binding.getDeclaringClass().getName())
                || name.getIdentifier().equals(binding.getDeclaringClass().getQualifiedName())) {
            node.setName(NameTable.unsafeSimpleName(iosMethod.getDeclaringClass(), ast));
        }
        Types.addMappedIOSMethod(binding, iosMethod);
        IMethodBinding newBinding = iosMethod.resolveBinding();
        Types.addMappedInvocation(node, newBinding);
        Types.addBinding(node, newBinding);
        Types.addBinding(name, newBinding);
    } else {
        // Not mapped, check if it overrides a mapped method.
        for (IMethodBinding methodBinding : mappedMethods) {
            if (binding.overrides(methodBinding)) {
                JavaMethod desc = getDescription(methodBinding);
                String value = methodMappings.get(desc.getKey());
                if (value != null) {
                    IOSMethod iosMethod = new IOSMethod(value, binding, ast);
                    node.setName(NameTable.unsafeSimpleName(iosMethod.getName(), ast));
                    Types.addMappedIOSMethod(binding, iosMethod);
                    IMethodBinding newBinding = iosMethod.resolveBinding();
                    Types.addBinding(node, newBinding);
                    Types.addBinding(node.getName(), newBinding);
                }
            }
        }
    }
    return false;
}

From source file:com.google.devtools.j2cpp.translate.Rewriter.java

License:Open Source License

/**
 * Java interfaces that redeclare java.lang.Object's equals, hashCode, or
 * toString methods need a forwarding method if the implementing class
 * relies on java.lang.Object's implementation.  This is because NSObject
 * is declared as adhering to the NSObject protocol, but doesn't explicitly
 * declare these method in its interface.  This prevents gcc from finding
 * an implementation, so it issues a warning.
 *//* w ww  .  j a  va2s  .c o m*/
private void addForwardingMethod(AST ast, ITypeBinding typeBinding, IMethodBinding interfaceMethod,
        List<BodyDeclaration> decls) {
    Logger.getAnonymousLogger()
            .fine(String.format("adding %s to %s", interfaceMethod.getName(), typeBinding.getQualifiedName()));
    MethodDeclaration method = createInterfaceMethodBody(ast, typeBinding, interfaceMethod);

    // Add method body with single "super.method(parameters);" statement.
    Block body = ast.newBlock();
    method.setBody(body);
    SuperMethodInvocation superInvocation = ast.newSuperMethodInvocation();
    superInvocation.setName(NodeCopier.copySubtree(ast, method.getName()));

    @SuppressWarnings("unchecked")
    List<SingleVariableDeclaration> parameters = method.parameters(); // safe by design
    @SuppressWarnings("unchecked")
    List<Expression> args = superInvocation.arguments(); // safe by definition
    for (SingleVariableDeclaration param : parameters) {
        Expression arg = NodeCopier.copySubtree(ast, param.getName());
        args.add(arg);
    }
    Types.addBinding(superInvocation, Types.getMethodBinding(method));
    @SuppressWarnings("unchecked")
    List<Statement> stmts = body.statements(); // safe by definition
    ReturnStatement returnStmt = ast.newReturnStatement();
    returnStmt.setExpression(superInvocation);
    stmts.add(returnStmt);

    decls.add(method);
}

From source file:com.google.devtools.j2objc.translate.ASTFactory.java

License:Apache License

public static SuperMethodInvocation newSuperMethodInvocation(AST ast, IMethodBinding binding) {
    SuperMethodInvocation invocation = ast.newSuperMethodInvocation();
    invocation.setName(newSimpleName(ast, binding));
    Types.addBinding(invocation, binding);
    return invocation;
}

From source file:com.idega.eclipse.ejbwizards.IBOEntityCreator.java

License:Open Source License

private void createHomeImplementation(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage,
        String name) throws JavaModelException, MalformedTreeException, BadLocationException {
    iUnit.getBuffer().setContents("");
    String source = iUnit.getBuffer().getContents();
    Document document = new Document(source);

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(iUnit);/*w w w.  j  a va2s .co  m*/

    CompilationUnit unit = (CompilationUnit) parser.createAST(monitor);
    unit.recordModifications();

    AST ast = unit.getAST();

    // Package statement
    PackageDeclaration packageDeclaration = ast.newPackageDeclaration();
    unit.setPackage(packageDeclaration);
    packageDeclaration.setName(ast.newName(typePackage));

    // class declaration
    TypeDeclaration classType = getTypeDeclaration(ast, name + "HomeImpl", false, "IBOHomeImpl", null,
            getHomeImplImports());
    classType.superInterfaceTypes().add(ast.newSimpleType(ast.newSimpleName(name + "Home")));
    addHomeImplImport("com.idega.business.IBOHomeImpl");
    unit.types().add(classType);

    // create() method
    MethodDeclaration methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    if (this.isJDK1_5) {
        MarkerAnnotation annotation = ast.newMarkerAnnotation();
        annotation.setTypeName(ast.newSimpleName("Override"));
        methodConstructor.modifiers().add(annotation);
    }
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    if (this.isJDK1_5) {
        ParameterizedType returnType = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("Class")));
        returnType.typeArguments().add(ast.newSimpleType(ast.newSimpleName(name)));
        methodConstructor.setReturnType2(returnType);
    } else {
        methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName("Class")));
    }
    methodConstructor.setName(ast.newSimpleName("getBeanInterfaceClass"));

    classType.bodyDeclarations().add(methodConstructor);

    Block constructorBlock = ast.newBlock();
    methodConstructor.setBody(constructorBlock);

    TypeLiteral typeLiteral = ast.newTypeLiteral();
    typeLiteral.setType(ast.newSimpleType(ast.newSimpleName(name)));

    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(typeLiteral);
    constructorBlock.statements().add(returnStatement);

    // create() method
    methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
    methodConstructor.setName(ast.newSimpleName("create"));
    methodConstructor.thrownExceptions().add(ast.newName("CreateException"));
    addHomeImplImport("javax.ejb.CreateException");
    classType.bodyDeclarations().add(methodConstructor);

    constructorBlock = ast.newBlock();
    methodConstructor.setBody(constructorBlock);

    SuperMethodInvocation superMethodInvocation = ast.newSuperMethodInvocation();
    superMethodInvocation.setName(ast.newSimpleName("createIBO"));

    CastExpression ce = ast.newCastExpression();
    ce.setType(ast.newSimpleType(ast.newSimpleName(name)));
    ce.setExpression(superMethodInvocation);

    returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ce);
    constructorBlock.statements().add(returnStatement);

    writeImports(ast, unit, getHomeImplImports());
    commitChanges(iUnit, unit, document);
}

From source file:com.idega.eclipse.ejbwizards.IDOEntityCreator.java

License:Open Source License

private void createHomeImplementation(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage,
        String name) throws JavaModelException, MalformedTreeException, BadLocationException {
    iUnit.getBuffer().setContents("");
    String source = iUnit.getBuffer().getContents();
    Document document = new Document(source);

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(iUnit);//from ww w. j av a 2s  . com

    CompilationUnit unit = (CompilationUnit) parser.createAST(monitor);
    unit.recordModifications();

    AST ast = unit.getAST();

    // Package statement
    unit.setPackage(getPackageDeclaration(ast, typePackage));

    // class declaration
    TypeDeclaration classType = getTypeDeclaration(ast, name + "HomeImpl", false, "IDOFactory", null,
            getHomeImplImports());
    classType.superInterfaceTypes().add(ast.newSimpleType(ast.newSimpleName(name + "Home")));
    addHomeImplImport("com.idega.data.IDOFactory");
    unit.types().add(classType);

    // create() method
    MethodDeclaration methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    if (this.isJDK1_5) {
        MarkerAnnotation annotation = ast.newMarkerAnnotation();
        annotation.setTypeName(ast.newSimpleName("Override"));
        methodConstructor.modifiers().add(annotation);
    }
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    if (this.isJDK1_5) {
        ParameterizedType returnType = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("Class")));
        returnType.typeArguments().add(ast.newSimpleType(ast.newSimpleName(name + "Home")));
        methodConstructor.setReturnType2(returnType);
    } else {
        methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName("Class")));
    }
    methodConstructor.setName(ast.newSimpleName("getEntityInterfaceClass"));

    classType.bodyDeclarations().add(methodConstructor);

    Block constructorBlock = ast.newBlock();
    methodConstructor.setBody(constructorBlock);

    TypeLiteral typeLiteral = ast.newTypeLiteral();
    typeLiteral.setType(ast.newSimpleType(ast.newSimpleName(name)));

    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(typeLiteral);
    constructorBlock.statements().add(returnStatement);

    // create() method
    methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
    methodConstructor.setName(ast.newSimpleName("create"));
    methodConstructor.thrownExceptions().add(ast.newName("CreateException"));
    addHomeImplImport("javax.ejb.CreateException");
    classType.bodyDeclarations().add(methodConstructor);

    constructorBlock = ast.newBlock();
    methodConstructor.setBody(constructorBlock);

    SuperMethodInvocation superMethodInvocation = ast.newSuperMethodInvocation();
    superMethodInvocation.setName(ast.newSimpleName("createIDO"));

    CastExpression ce = ast.newCastExpression();
    ce.setType(ast.newSimpleType(ast.newSimpleName(name)));
    ce.setExpression(superMethodInvocation);

    returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ce);
    constructorBlock.statements().add(returnStatement);

    // findByPrimarKey(Object) method
    methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
    methodConstructor.setName(ast.newSimpleName("findByPrimaryKey"));
    methodConstructor.thrownExceptions().add(ast.newName("FinderException"));
    addHomeImplImport("javax.ejb.FinderException");
    classType.bodyDeclarations().add(methodConstructor);

    SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration();
    variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
    variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName("Object")));
    variableDeclaration.setName(ast.newSimpleName("pk"));
    methodConstructor.parameters().add(variableDeclaration);

    constructorBlock = ast.newBlock();
    methodConstructor.setBody(constructorBlock);

    superMethodInvocation = ast.newSuperMethodInvocation();
    superMethodInvocation.setName(ast.newSimpleName("findByPrimaryKeyIDO"));
    superMethodInvocation.arguments().add(ast.newSimpleName("pk"));

    ce = ast.newCastExpression();
    ce.setType(ast.newSimpleType(ast.newSimpleName(name)));
    ce.setExpression(superMethodInvocation);

    returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ce);
    constructorBlock.statements().add(returnStatement);

    if (this.isLegacyEntity) {
        // createLegacy() method
        methodConstructor = ast.newMethodDeclaration();
        methodConstructor.setConstructor(false);
        methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
        methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
        methodConstructor.setName(ast.newSimpleName("createLegacy"));
        classType.bodyDeclarations().add(methodConstructor);

        constructorBlock = ast.newBlock();
        methodConstructor.setBody(constructorBlock);

        TryStatement tryStatement = ast.newTryStatement();
        constructorBlock.statements().add(tryStatement);
        Block tryBlock = ast.newBlock();
        tryStatement.setBody(tryBlock);

        MethodInvocation mi = ast.newMethodInvocation();
        mi.setName(ast.newSimpleName("create"));

        returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(mi);
        tryBlock.statements().add(returnStatement);

        CatchClause catchClause = ast.newCatchClause();
        tryStatement.catchClauses().add(catchClause);
        variableDeclaration = ast.newSingleVariableDeclaration();
        variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
        variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("CreateException"))));
        variableDeclaration.setName(ast.newSimpleName("ce"));
        catchClause.setException(variableDeclaration);
        Block catchBlock = ast.newBlock();
        catchClause.setBody(catchBlock);

        ClassInstanceCreation cc = ast.newClassInstanceCreation();
        cc.setType(ast.newSimpleType(ast.newSimpleName("RuntimeException")));
        mi = ast.newMethodInvocation();
        mi.setExpression(ast.newSimpleName("ce"));
        mi.setName(ast.newSimpleName("getMessage"));
        cc.arguments().add(mi);

        ThrowStatement throwStatement = ast.newThrowStatement();
        throwStatement.setExpression(cc);
        catchBlock.statements().add(throwStatement);

        // findByPrimarKey(int) method
        methodConstructor = ast.newMethodDeclaration();
        methodConstructor.setConstructor(false);
        methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
        methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
        methodConstructor.setName(ast.newSimpleName("findByPrimaryKey"));
        methodConstructor.thrownExceptions().add(ast.newName("FinderException"));
        classType.bodyDeclarations().add(methodConstructor);

        variableDeclaration = ast.newSingleVariableDeclaration();
        variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
        variableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT));
        variableDeclaration.setName(ast.newSimpleName("id"));
        methodConstructor.parameters().add(variableDeclaration);

        constructorBlock = ast.newBlock();
        methodConstructor.setBody(constructorBlock);

        superMethodInvocation = ast.newSuperMethodInvocation();
        superMethodInvocation.setName(ast.newSimpleName("findByPrimaryKeyIDO"));
        superMethodInvocation.arguments().add(ast.newSimpleName("id"));

        ce = ast.newCastExpression();
        ce.setType(ast.newSimpleType(ast.newSimpleName(name)));
        ce.setExpression(superMethodInvocation);

        returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(ce);
        constructorBlock.statements().add(returnStatement);

        // findByPrimarKeyLegacy(int) method
        methodConstructor = ast.newMethodDeclaration();
        methodConstructor.setConstructor(false);
        methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
        methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
        methodConstructor.setName(ast.newSimpleName("findByPrimaryKeyLegacy"));
        methodConstructor.thrownExceptions().add(ast.newName("SQLException"));
        addHomeImplImport("java.sql.SQLException");
        classType.bodyDeclarations().add(methodConstructor);

        variableDeclaration = ast.newSingleVariableDeclaration();
        variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
        variableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT));
        variableDeclaration.setName(ast.newSimpleName("id"));
        methodConstructor.parameters().add(variableDeclaration);

        constructorBlock = ast.newBlock();
        methodConstructor.setBody(constructorBlock);

        tryStatement = ast.newTryStatement();
        constructorBlock.statements().add(tryStatement);
        tryBlock = ast.newBlock();
        tryStatement.setBody(tryBlock);

        mi = ast.newMethodInvocation();
        mi.setName(ast.newSimpleName("findByPrimaryKey"));
        mi.arguments().add(ast.newSimpleName("id"));

        returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(mi);
        tryBlock.statements().add(returnStatement);

        catchClause = ast.newCatchClause();
        tryStatement.catchClauses().add(catchClause);
        variableDeclaration = ast.newSingleVariableDeclaration();
        variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
        variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("FinderException"))));
        variableDeclaration.setName(ast.newSimpleName("fe"));
        catchClause.setException(variableDeclaration);
        catchBlock = ast.newBlock();
        catchClause.setBody(catchBlock);

        cc = ast.newClassInstanceCreation();
        cc.setType(ast.newSimpleType(ast.newSimpleName("SQLException")));
        mi = ast.newMethodInvocation();
        mi.setExpression(ast.newSimpleName("fe"));
        mi.setName(ast.newSimpleName("getMessage"));
        cc.arguments().add(mi);

        throwStatement = ast.newThrowStatement();
        throwStatement.setExpression(cc);
        catchBlock.statements().add(throwStatement);
    }

    MethodFilter[] validFilter = { new MethodFilter(WizardConstants.EJB_CREATE_START, MethodFilter.TYPE_PREFIX),
            new MethodFilter(WizardConstants.EJB_HOME_START, MethodFilter.TYPE_PREFIX),
            new MethodFilter(WizardConstants.EJB_FIND_START, MethodFilter.TYPE_PREFIX) };
    List methods = filterMethods(getType().getMethods(), validFilter, null);
    for (Iterator iter = methods.iterator(); iter.hasNext();) {
        IMethod method = (IMethod) iter.next();
        String fullMethodName = method.getElementName();
        String methodName = cutAwayEJBSuffix(fullMethodName);
        String[] parameterNames = method.getParameterNames();

        String returnType = method.getReturnType();
        if (fullMethodName.startsWith(WizardConstants.EJB_FIND_START)
                || fullMethodName.startsWith(WizardConstants.EJB_CREATE_START)) {
            if (!(Signature.getSimpleName(Signature.toString(method.getReturnType()))
                    .indexOf(Signature.getSimpleName("java.util.Collection")) != -1)
                    && !(Signature.getSimpleName(Signature.toString(method.getReturnType()))
                            .indexOf(Signature.getSimpleName("java.util.Set")) != -1)) {
                returnType = name;
            }
        }
        if (!returnType.equals(name)) {
            returnType = getReturnType(returnType);
        }

        methodConstructor = getMethodDeclaration(ast, method, methodName, returnType, getHomeImplImports(),
                false);
        classType.bodyDeclarations().add(methodConstructor);

        constructorBlock = ast.newBlock();
        methodConstructor.setBody(constructorBlock);

        constructorBlock.statements().add(getIDOCheckOutStatement(ast, getHomeImplImports()));

        if (fullMethodName.startsWith(WizardConstants.EJB_FIND_START)) {
            if (Signature.getSimpleName(Signature.toString(method.getReturnType()))
                    .indexOf(Signature.getSimpleName("java.util.Collection")) != -1) {
                constructorBlock.statements().add(
                        getDataCollectingStatement(ast, returnType, "ids", fullMethodName, parameterNames));
                constructorBlock.statements().add(getIDOCheckInStatement(ast));
                constructorBlock.statements()
                        .add(getObjectReturnStatement(ast, "getEntityCollectionForPrimaryKeys", "ids"));
            } else if (Signature.getSimpleName(Signature.toString(method.getReturnType()))
                    .indexOf(Signature.getSimpleName("java.util.Set")) != -1) {
                constructorBlock.statements().add(
                        getDataCollectingStatement(ast, returnType, "ids", fullMethodName, parameterNames));
                constructorBlock.statements().add(getIDOCheckInStatement(ast));
                constructorBlock.statements()
                        .add(getObjectReturnStatement(ast, "getEntitySetForPrimaryKeys", "ids"));
            } else {
                constructorBlock.statements()
                        .add(getDataCollectingStatement(ast, "Object", "pk", fullMethodName, parameterNames));
                constructorBlock.statements().add(getIDOCheckInStatement(ast));
                constructorBlock.statements().add(getObjectReturnStatement(ast, "findByPrimaryKey", "pk"));
            }
        } else if (fullMethodName.startsWith(WizardConstants.EJB_HOME_START)) {
            constructorBlock.statements().add(
                    getDataCollectingStatement(ast, returnType, "theReturn", fullMethodName, parameterNames));
            constructorBlock.statements().add(getIDOCheckInStatement(ast));
            constructorBlock.statements().add(getPrimitiveReturnStatement(ast, "theReturn"));
        } else if (fullMethodName.startsWith(WizardConstants.EJB_CREATE_START)) {
            constructorBlock.statements()
                    .add(getDataCollectingStatement(ast, "Object", "pk", fullMethodName, parameterNames));

            ce = ast.newCastExpression();
            ce.setType(ast.newSimpleType(ast.newSimpleName(getType().getTypeQualifiedName())));
            ce.setExpression(ast.newSimpleName("entity"));

            ParenthesizedExpression pe = ast.newParenthesizedExpression();
            pe.setExpression(ce);
            MethodInvocation mi = ast.newMethodInvocation();
            mi.setExpression(pe);
            mi.setName(ast.newSimpleName("ejbPostCreate"));
            constructorBlock.statements().add(ast.newExpressionStatement(mi));

            constructorBlock.statements().add(getIDOCheckInStatement(ast));

            TryStatement tryStatement = ast.newTryStatement();
            constructorBlock.statements().add(tryStatement);
            Block tryBlock = ast.newBlock();
            tryStatement.setBody(tryBlock);

            mi = ast.newMethodInvocation();
            mi.setName(ast.newSimpleName("findByPrimaryKey"));
            mi.arguments().add(ast.newSimpleName("pk"));

            returnStatement = ast.newReturnStatement();
            returnStatement.setExpression(mi);
            tryBlock.statements().add(returnStatement);

            CatchClause catchClause = ast.newCatchClause();
            tryStatement.catchClauses().add(catchClause);
            variableDeclaration = ast.newSingleVariableDeclaration();
            variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
            variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("FinderException"))));
            variableDeclaration.setName(ast.newSimpleName("fe"));
            catchClause.setException(variableDeclaration);
            Block catchBlock = ast.newBlock();
            catchClause.setBody(catchBlock);

            ClassInstanceCreation cc = ast.newClassInstanceCreation();
            cc.setType(ast.newSimpleType(ast.newSimpleName("IDOCreateException")));
            addHomeImplImport("com.idega.data.IDOCreateException");
            cc.arguments().add(ast.newSimpleName(("fe")));

            ThrowStatement throwStatement = ast.newThrowStatement();
            throwStatement.setExpression(cc);
            catchBlock.statements().add(throwStatement);

            catchClause = ast.newCatchClause();
            tryStatement.catchClauses().add(catchClause);
            variableDeclaration = ast.newSingleVariableDeclaration();
            variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
            variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("Exception"))));
            variableDeclaration.setName(ast.newSimpleName("e"));
            catchClause.setException(variableDeclaration);
            catchBlock = ast.newBlock();
            catchClause.setBody(catchBlock);

            cc = ast.newClassInstanceCreation();
            cc.setType(ast.newSimpleType(ast.newSimpleName("IDOCreateException")));
            cc.arguments().add(ast.newSimpleName(("e")));

            throwStatement = ast.newThrowStatement();
            throwStatement.setExpression(cc);
            catchBlock.statements().add(throwStatement);
        }
    }

    writeImports(ast, unit, getHomeImplImports());
    commitChanges(iUnit, unit, document);
}

From source file:com.motorola.studio.android.generatecode.AbstractCodeGenerator.java

License:Apache License

/**
 * Creates a method in the format super.$superMethodName($list_params);
 * @param superMethodName//from w w  w.jav  a 2s  . co  m
 * @param arguments null if not necessary or a list of arguments to pass for method
 * @return
 */
@SuppressWarnings("unchecked")
protected SuperMethodInvocation createSuperMethodInvocation(String superMethodName, List<String> arguments) {
    SuperMethodInvocation superInvoke = typeDeclaration.getAST().newSuperMethodInvocation();
    SimpleName onSaveStateName = typeDeclaration.getAST().newSimpleName(superMethodName);
    superInvoke.setName(onSaveStateName);
    if (arguments != null) {
        for (String a : arguments) {
            SimpleName arg = typeDeclaration.getAST().newSimpleName(a);
            superInvoke.arguments().add(arg);
        }
    }
    return superInvoke;
}

From source file:com.motorola.studio.android.model.java.ActivityClass.java

License:Apache License

/**
 * Adds a "super.method(arguments...)" statement inside the given method body
 * //from   w  w w.j a  va  2 s  .  com
 * @param method The method declaration
 */
@SuppressWarnings("unchecked")
private void addSuperInvocation(MethodDeclaration method) {
    SuperMethodInvocation superInv = ast.newSuperMethodInvocation();
    superInv.setName(ast.newSimpleName(method.getName().toString()));
    for (Object param : method.parameters()) {
        if (param instanceof SingleVariableDeclaration) {
            SingleVariableDeclaration vd = (SingleVariableDeclaration) param;
            String varName = vd.getName().toString();
            superInv.arguments().add(ast.newSimpleName(varName));
        }
    }
    method.getBody().statements().add(ast.newExpressionStatement(superInv));
}

From source file:de.ovgu.cide.export.physical.ahead.JakFeatureRefactorer.java

License:Open Source License

/**
 * creates a Super call (actually a super call that is replaced by Super
 * when printing the Jak file with the JakPrettyPrinter).
 * //from   w ww. j a va 2 s  .c  o m
 * @param currentMethod
 * @param ast
 * @param withReturn
 *            if true then the result of the super invocation is returned
 *            (return Super...), otherwise it is placed in a local variable
 *            called result
 * @param formal
 *            local variable to which the result (if any) should be
 *            assigned. if formal==null, then a new variable is created with
 *            the name result (ChK: is there a usecase for this?)
 * @return
 */
static Statement createSuperCall(MethodDeclaration currentMethod, AST ast, boolean withReturn, Formal formal) {
    Statement superCall;
    if (currentMethod.isConstructor()) {
        assert !withReturn;
        /**
         * super constructor calls are implicit in Jak. so there is no
         * statement to call a super constructor.
         */
        superCall = null;

    } else {
        SuperMethodInvocation superCallExpr = ast.newSuperMethodInvocation();
        SuperCallHelper.addSuperLayerCall(superCallExpr);
        superCallExpr.setName(ast.newSimpleName(currentMethod.getName().getIdentifier()));
        String types = "";
        for (Iterator<SingleVariableDeclaration> iter = currentMethod.parameters().iterator(); iter
                .hasNext();) {
            SingleVariableDeclaration param = iter.next();
            SimpleName v = ast.newSimpleName(param.getName().getIdentifier());
            superCallExpr.arguments().add(v);

            VariableDeclaration decl = LocalVariableHelper.findVariableDeclaration(param.getName());
            if (decl != null)
                LocalVariableHelper.addLocalVariableAccess(v, decl);

            types += getTypeString(param.getType());
            if (iter.hasNext())
                types += ", ";
        }
        SuperTypeHelper.cacheTypes(superCallExpr, types);
        if (RefactoringUtils.isVoid(currentMethod.getReturnType2())) {
            superCall = ast.newExpressionStatement(superCallExpr);
        } else {
            if (withReturn) {
                ReturnStatement returnStatement = ast.newReturnStatement();
                returnStatement.setExpression(superCallExpr);
                superCall = returnStatement;
            } else {
                if (formal == null) {
                    VariableDeclarationFragment returnVariable = ast.newVariableDeclarationFragment();
                    returnVariable.setName(ast.newSimpleName("result"));
                    VariableDeclarationStatement variableDecl = ast
                            .newVariableDeclarationStatement(returnVariable);
                    variableDecl.setType(
                            (Type) ASTNode.copySubtree(currentMethod.getAST(), currentMethod.getReturnType2()));
                    returnVariable.setInitializer(superCallExpr);
                    superCall = variableDecl;
                } else {
                    Assignment assign = ast.newAssignment();
                    SimpleName v = ast.newSimpleName(formal.name);
                    assign.setLeftHandSide(v);
                    LocalVariableHelper.addLocalVariableAccess(v, formal);
                    assign.setRightHandSide(superCallExpr);
                    superCall = ast.newExpressionStatement(assign);
                }

            }
        }
    }
    return superCall;
}

From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.CreateSuperCallResolution.java

License:Open Source License

protected SuperMethodInvocation createSuperMethodInvocation(ASTRewrite rewrite, MethodDeclaration method) {
    Assert.isNotNull(rewrite);/* ww  w .j av  a2 s  .  com*/
    Assert.isNotNull(method);

    AST ast = rewrite.getAST();
    SuperMethodInvocation invocation = ast.newSuperMethodInvocation();

    invocation.setName((SimpleName) rewrite.createCopyTarget(method.getName()));

    return invocation;
}