Search in sources :

Example 21 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project che by eclipse.

the class DelegateMethodCreator method createReturnStatement.

/**
	 * Creates a new return statement for the method invocation.
	 *
	 * @param invocation the method invocation to create a return statement for
	 * @return the corresponding statement
	 */
private ReturnStatement createReturnStatement(final MethodInvocation invocation) {
    Assert.isNotNull(invocation);
    final ReturnStatement statement = invocation.getAST().newReturnStatement();
    statement.setExpression(invocation);
    return statement;
}
Also used : ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement)

Example 22 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project che by eclipse.

the class StubUtility2 method createDelegationStub.

public static MethodDeclaration createDelegationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding delegate, IVariableBinding delegatingField, CodeGenerationSettings settings) throws CoreException {
    Assert.isNotNull(delegate);
    Assert.isNotNull(delegatingField);
    Assert.isNotNull(settings);
    AST ast = rewrite.getAST();
    MethodDeclaration decl = ast.newMethodDeclaration();
    decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, delegate.getModifiers() & ~Modifier.SYNCHRONIZED & ~Modifier.ABSTRACT & ~Modifier.NATIVE));
    decl.setName(ast.newSimpleName(delegate.getName()));
    decl.setConstructor(false);
    createTypeParameters(imports, context, ast, delegate, decl);
    decl.setReturnType2(imports.addImport(delegate.getReturnType(), ast, context));
    List<SingleVariableDeclaration> params = createParameters(unit.getJavaProject(), imports, context, ast, delegate, null, decl);
    createThrownExceptions(decl, delegate, imports, context, ast);
    Block body = ast.newBlock();
    decl.setBody(body);
    String delimiter = StubUtility.getLineDelimiterUsed(unit);
    Statement statement = null;
    MethodInvocation invocation = ast.newMethodInvocation();
    invocation.setName(ast.newSimpleName(delegate.getName()));
    List<Expression> arguments = invocation.arguments();
    for (int i = 0; i < params.size(); i++) arguments.add(ast.newSimpleName(params.get(i).getName().getIdentifier()));
    if (settings.useKeywordThis) {
        FieldAccess access = ast.newFieldAccess();
        access.setExpression(ast.newThisExpression());
        access.setName(ast.newSimpleName(delegatingField.getName()));
        invocation.setExpression(access);
    } else
        invocation.setExpression(ast.newSimpleName(delegatingField.getName()));
    if (delegate.getReturnType().isPrimitive() && delegate.getReturnType().getName().equals("void")) {
        //$NON-NLS-1$
        statement = ast.newExpressionStatement(invocation);
    } else {
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(invocation);
        statement = returnStatement;
    }
    body.statements().add(statement);
    ITypeBinding declaringType = delegatingField.getDeclaringClass();
    if (declaringType == null) {
        // can be null for
        return decl;
    }
    String qualifiedName = declaringType.getQualifiedName();
    IPackageBinding packageBinding = declaringType.getPackage();
    if (packageBinding != null) {
        if (packageBinding.getName().length() > 0 && qualifiedName.startsWith(packageBinding.getName()))
            qualifiedName = qualifiedName.substring(packageBinding.getName().length());
    }
    if (settings.createComments) {
        /*
			 * TODO: have API for delegate method comments This is an inlined
			 * version of
			 * {@link CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, IMethodBinding, String)}
			 */
        delegate = delegate.getMethodDeclaration();
        String declaringClassQualifiedName = delegate.getDeclaringClass().getQualifiedName();
        String linkToMethodName = delegate.getName();
        String[] parameterTypesQualifiedNames = StubUtility.getParameterTypeNamesForSeeTag(delegate);
        String string = StubUtility.getMethodComment(unit, qualifiedName, decl, delegate.isDeprecated(), linkToMethodName, declaringClassQualifiedName, parameterTypesQualifiedNames, true, delimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }
    return decl;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Javadoc(org.eclipse.jdt.core.dom.Javadoc) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 23 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project che by eclipse.

the class StubUtility2 method createConstructorStub.

/* This method should work with all AST levels. */
public static MethodDeclaration createConstructorStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding binding, String type, int modifiers, boolean omitSuperForDefConst, boolean todo, CodeGenerationSettings settings) throws CoreException {
    AST ast = rewrite.getAST();
    MethodDeclaration decl = ast.newMethodDeclaration();
    decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, modifiers & ~Modifier.ABSTRACT & ~Modifier.NATIVE));
    decl.setName(ast.newSimpleName(type));
    decl.setConstructor(true);
    createTypeParameters(imports, context, ast, binding, decl);
    List<SingleVariableDeclaration> parameters = createParameters(unit.getJavaProject(), imports, context, ast, binding, null, decl);
    createThrownExceptions(decl, binding, imports, context, ast);
    Block body = ast.newBlock();
    decl.setBody(body);
    String delimiter = StubUtility.getLineDelimiterUsed(unit);
    //$NON-NLS-1$
    String bodyStatement = "";
    if (!omitSuperForDefConst || !parameters.isEmpty()) {
        SuperConstructorInvocation invocation = ast.newSuperConstructorInvocation();
        SingleVariableDeclaration varDecl = null;
        for (Iterator<SingleVariableDeclaration> iterator = parameters.iterator(); iterator.hasNext(); ) {
            varDecl = iterator.next();
            invocation.arguments().add(ast.newSimpleName(varDecl.getName().getIdentifier()));
        }
        bodyStatement = ASTNodes.asFormattedString(invocation, 0, delimiter, unit.getJavaProject().getOptions(true));
    }
    if (todo) {
        String placeHolder = CodeGeneration.getMethodBodyContent(unit, type, binding.getName(), true, bodyStatement, delimiter);
        if (placeHolder != null) {
            ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
            body.statements().add(todoNode);
        }
    } else {
        ReturnStatement statementNode = (ReturnStatement) rewrite.createStringPlaceholder(bodyStatement, ASTNode.RETURN_STATEMENT);
        body.statements().add(statementNode);
    }
    if (settings != null && settings.createComments) {
        String string = CodeGeneration.getMethodComment(unit, type, decl, binding, delimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }
    return decl;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) Javadoc(org.eclipse.jdt.core.dom.Javadoc) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Example 24 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project che by eclipse.

the class StubUtility2 method createImplementationStub.

public static MethodDeclaration createImplementationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding binding, String[] parameterNames, String type, CodeGenerationSettings settings, boolean inInterface) throws CoreException {
    Assert.isNotNull(imports);
    Assert.isNotNull(rewrite);
    AST ast = rewrite.getAST();
    MethodDeclaration decl = ast.newMethodDeclaration();
    decl.modifiers().addAll(getImplementationModifiers(ast, binding, inInterface, imports, context));
    decl.setName(ast.newSimpleName(binding.getName()));
    decl.setConstructor(false);
    ITypeBinding bindingReturnType = binding.getReturnType();
    if (bindingReturnType.isWildcardType()) {
        ITypeBinding bound = bindingReturnType.getBound();
        bindingReturnType = (bound != null) ? bound : bindingReturnType.getErasure();
    }
    IJavaProject javaProject = unit.getJavaProject();
    if (JavaModelUtil.is50OrHigher(javaProject)) {
        createTypeParameters(imports, context, ast, binding, decl);
    } else {
        bindingReturnType = bindingReturnType.getErasure();
    }
    decl.setReturnType2(imports.addImport(bindingReturnType, ast, context));
    List<SingleVariableDeclaration> parameters = createParameters(javaProject, imports, context, ast, binding, parameterNames, decl);
    createThrownExceptions(decl, binding, imports, context, ast);
    String delimiter = unit.findRecommendedLineSeparator();
    int modifiers = binding.getModifiers();
    if (!(inInterface && Modifier.isAbstract(modifiers))) {
        // generate a method body
        Map<String, String> options = javaProject.getOptions(true);
        Block body = ast.newBlock();
        decl.setBody(body);
        //$NON-NLS-1$
        String bodyStatement = "";
        if (Modifier.isAbstract(modifiers)) {
            Expression expression = ASTNodeFactory.newDefaultExpression(ast, decl.getReturnType2(), decl.getExtraDimensions());
            if (expression != null) {
                ReturnStatement returnStatement = ast.newReturnStatement();
                returnStatement.setExpression(expression);
                bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, options);
            }
        } else {
            SuperMethodInvocation invocation = ast.newSuperMethodInvocation();
            ITypeBinding declaringType = binding.getDeclaringClass();
            if (declaringType.isInterface()) {
                String qualifier = imports.addImport(declaringType.getErasure(), context);
                Name name = ASTNodeFactory.newName(ast, qualifier);
                invocation.setQualifier(name);
            }
            invocation.setName(ast.newSimpleName(binding.getName()));
            SingleVariableDeclaration varDecl = null;
            for (Iterator<SingleVariableDeclaration> iterator = parameters.iterator(); iterator.hasNext(); ) {
                varDecl = iterator.next();
                invocation.arguments().add(ast.newSimpleName(varDecl.getName().getIdentifier()));
            }
            Expression expression = invocation;
            Type returnType = decl.getReturnType2();
            if (returnType instanceof PrimitiveType && ((PrimitiveType) returnType).getPrimitiveTypeCode().equals(PrimitiveType.VOID)) {
                bodyStatement = ASTNodes.asFormattedString(ast.newExpressionStatement(expression), 0, delimiter, options);
            } else {
                ReturnStatement returnStatement = ast.newReturnStatement();
                returnStatement.setExpression(expression);
                bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, options);
            }
        }
        String placeHolder = CodeGeneration.getMethodBodyContent(unit, type, binding.getName(), false, bodyStatement, delimiter);
        if (placeHolder != null) {
            ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
            body.statements().add(todoNode);
        }
    }
    if (settings != null && settings.createComments) {
        String string = CodeGeneration.getMethodComment(unit, type, decl, binding, delimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }
    if (settings != null && settings.overrideAnnotation && JavaModelUtil.is50OrHigher(javaProject)) {
        addOverrideAnnotation(javaProject, rewrite, decl, binding);
    }
    return decl;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Name(org.eclipse.jdt.core.dom.Name) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) IJavaProject(org.eclipse.jdt.core.IJavaProject) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType)

Example 25 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project che by eclipse.

the class AbstractMethodCorrectionProposal method getStub.

private MethodDeclaration getStub(ASTRewrite rewrite, ASTNode targetTypeDecl) throws CoreException {
    AST ast = targetTypeDecl.getAST();
    MethodDeclaration decl = ast.newMethodDeclaration();
    SimpleName newNameNode = getNewName(rewrite);
    decl.setConstructor(isConstructor());
    addNewModifiers(rewrite, targetTypeDecl, decl.modifiers());
    ArrayList<String> takenNames = new ArrayList<String>();
    addNewTypeParameters(rewrite, takenNames, decl.typeParameters());
    decl.setName(newNameNode);
    IVariableBinding[] declaredFields = fSenderBinding.getDeclaredFields();
    for (int i = 0; i < declaredFields.length; i++) {
        // avoid to take parameter names that are equal to field names
        takenNames.add(declaredFields[i].getName());
    }
    //$NON-NLS-1$
    String bodyStatement = "";
    if (!isConstructor()) {
        Type returnType = getNewMethodType(rewrite);
        decl.setReturnType2(returnType);
        boolean isVoid = returnType instanceof PrimitiveType && PrimitiveType.VOID.equals(((PrimitiveType) returnType).getPrimitiveTypeCode());
        if (!fSenderBinding.isInterface() && !isVoid) {
            ReturnStatement returnStatement = ast.newReturnStatement();
            returnStatement.setExpression(ASTNodeFactory.newDefaultExpression(ast, returnType, 0));
            bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true));
        }
    }
    addNewParameters(rewrite, takenNames, decl.parameters());
    addNewExceptions(rewrite, decl.thrownExceptionTypes());
    Block body = null;
    if (!fSenderBinding.isInterface()) {
        body = ast.newBlock();
        String placeHolder = CodeGeneration.getMethodBodyContent(getCompilationUnit(), fSenderBinding.getName(), newNameNode.getIdentifier(), isConstructor(), bodyStatement, String.valueOf('\n'));
        if (placeHolder != null) {
            ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
            body.statements().add(todoNode);
        }
    }
    decl.setBody(body);
    CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(getCompilationUnit().getJavaProject());
    if (settings.createComments && !fSenderBinding.isAnonymous()) {
        String string = CodeGeneration.getMethodComment(getCompilationUnit(), fSenderBinding.getName(), decl, null, String.valueOf('\n'));
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }
    return decl;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType)

Aggregations

ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)34 Block (org.eclipse.jdt.core.dom.Block)24 AST (org.eclipse.jdt.core.dom.AST)23 Expression (org.eclipse.jdt.core.dom.Expression)22 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)21 ASTNode (org.eclipse.jdt.core.dom.ASTNode)20 Type (org.eclipse.jdt.core.dom.Type)19 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)18 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)14 Statement (org.eclipse.jdt.core.dom.Statement)12 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)11 SimpleName (org.eclipse.jdt.core.dom.SimpleName)11 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)11 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)10 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)9 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)9 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)9 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)8 DoStatement (org.eclipse.jdt.core.dom.DoStatement)8 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)8