Search in sources :

Example 81 with Block

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

the class IntroduceFactoryRefactoring method createFactoryMethod.

/**
	 * Creates and returns a new MethodDeclaration that represents the factory method to be used in
	 * place of direct calls to the constructor in question.
	 * 
	 * @param ast An AST used as a factory for various AST nodes
	 * @param ctorBinding binding for the constructor being wrapped
	 * @param unitRewriter the ASTRewrite to be used
	 * @return the new method declaration
	 * @throws CoreException if an exception occurs while accessing its corresponding resource
	 */
private MethodDeclaration createFactoryMethod(AST ast, IMethodBinding ctorBinding, ASTRewrite unitRewriter) throws CoreException {
    MethodDeclaration newMethod = ast.newMethodDeclaration();
    SimpleName newMethodName = ast.newSimpleName(fNewMethodName);
    ClassInstanceCreation newCtorCall = ast.newClassInstanceCreation();
    ReturnStatement ret = ast.newReturnStatement();
    Block body = ast.newBlock();
    List<Statement> stmts = body.statements();
    String retTypeName = ctorBinding.getName();
    createFactoryMethodSignature(ast, newMethod);
    newMethod.setName(newMethodName);
    newMethod.setBody(body);
    ITypeBinding declaringClass = fCtorBinding.getDeclaringClass();
    ITypeBinding[] ctorOwnerTypeParameters = declaringClass.getTypeParameters();
    setMethodReturnType(newMethod, retTypeName, ctorOwnerTypeParameters, ast);
    newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.STATIC | Modifier.PUBLIC));
    setCtorTypeArguments(newCtorCall, retTypeName, ctorOwnerTypeParameters, ast);
    createFactoryMethodConstructorArgs(ast, newCtorCall);
    if (Modifier.isAbstract(declaringClass.getModifiers())) {
        AnonymousClassDeclaration decl = ast.newAnonymousClassDeclaration();
        IMethodBinding[] unimplementedMethods = getUnimplementedMethods(declaringClass);
        CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(fCUHandle.getJavaProject());
        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fFactoryCU, decl.getStartPosition(), fImportRewriter);
        for (int i = 0; i < unimplementedMethods.length; i++) {
            IMethodBinding unImplementedMethod = unimplementedMethods[i];
            MethodDeclaration newMethodDecl = StubUtility2.createImplementationStub(fCUHandle, unitRewriter, fImportRewriter, context, unImplementedMethod, unImplementedMethod.getDeclaringClass().getName(), settings, false);
            decl.bodyDeclarations().add(newMethodDecl);
        }
        newCtorCall.setAnonymousClassDeclaration(decl);
    }
    ret.setExpression(newCtorCall);
    stmts.add(ret);
    return newMethod;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 82 with Block

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

the class SourceAnalyzer method initialize.

public void initialize() {
    Block body = fDeclaration.getBody();
    // first collect the static imports. This is necessary to not mark
    // static imported fields and methods as implicit visible.
    fTypesToImport = new ArrayList<SimpleName>();
    fStaticsToImport = new ArrayList<SimpleName>();
    ImportReferencesCollector.collect(body, fTypeRoot.getJavaProject(), null, fTypesToImport, fStaticsToImport);
    // Now collect implicit references and name references
    body.accept(new UpdateCollector());
    int numberOfLocals = LocalVariableIndex.perform(fDeclaration);
    FlowContext context = new FlowContext(0, numberOfLocals + 1);
    context.setConsiderAccessMode(true);
    context.setComputeMode(FlowContext.MERGE);
    InOutFlowAnalyzer flowAnalyzer = new InOutFlowAnalyzer(context);
    FlowInfo info = flowAnalyzer.perform(getStatements());
    for (Iterator<SingleVariableDeclaration> iter = fDeclaration.parameters().iterator(); iter.hasNext(); ) {
        SingleVariableDeclaration element = iter.next();
        IVariableBinding binding = element.resolveBinding();
        ParameterData data = (ParameterData) element.getProperty(ParameterData.PROPERTY);
        data.setAccessMode(info.getAccessMode(context, binding));
    }
}
Also used : FlowInfo(org.eclipse.jdt.internal.corext.refactoring.code.flow.FlowInfo) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Block(org.eclipse.jdt.core.dom.Block) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) FlowContext(org.eclipse.jdt.internal.corext.refactoring.code.flow.FlowContext) InOutFlowAnalyzer(org.eclipse.jdt.internal.corext.refactoring.code.flow.InOutFlowAnalyzer)

Example 83 with Block

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

the class SourceProvider method isSingleControlStatementWithoutBlock.

private boolean isSingleControlStatementWithoutBlock() {
    List<Statement> statements = fDeclaration.getBody().statements();
    int size = statements.size();
    if (size != 1)
        return false;
    Statement statement = statements.get(size - 1);
    int nodeType = statement.getNodeType();
    if (nodeType == ASTNode.IF_STATEMENT) {
        IfStatement ifStatement = (IfStatement) statement;
        return !(ifStatement.getThenStatement() instanceof Block) && !(ifStatement.getElseStatement() instanceof Block);
    } else if (nodeType == ASTNode.FOR_STATEMENT) {
        return !(((ForStatement) statement).getBody() instanceof Block);
    } else if (nodeType == ASTNode.WHILE_STATEMENT) {
        return !(((WhileStatement) statement).getBody() instanceof Block);
    }
    return false;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) Block(org.eclipse.jdt.core.dom.Block) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement)

Example 84 with Block

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

the class DelegateMethodCreator method createDelegateMethodBody.

private Block createDelegateMethodBody(final MethodDeclaration declaration) {
    Assert.isNotNull(declaration);
    MethodDeclaration old = (MethodDeclaration) getDeclaration();
    List<Expression> arguments;
    Statement call;
    if (old.isConstructor()) {
        ConstructorInvocation invocation = getAst().newConstructorInvocation();
        arguments = invocation.arguments();
        call = invocation;
        fDelegateInvocation = invocation;
    } else {
        MethodInvocation invocation = getAst().newMethodInvocation();
        invocation.setName(getAst().newSimpleName(getNewElementName()));
        invocation.setExpression(getAccess());
        arguments = invocation.arguments();
        call = createMethodInvocation(declaration, invocation);
        fDelegateInvocation = invocation;
    }
    createArguments(declaration, arguments, true);
    final Block body = getAst().newBlock();
    body.statements().add(call);
    return body;
}
Also used : ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) Expression(org.eclipse.jdt.core.dom.Expression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 85 with Block

use of org.eclipse.jdt.core.dom.Block 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)

Aggregations

Block (org.eclipse.jdt.core.dom.Block)105 ASTNode (org.eclipse.jdt.core.dom.ASTNode)61 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)45 AST (org.eclipse.jdt.core.dom.AST)44 Statement (org.eclipse.jdt.core.dom.Statement)42 Expression (org.eclipse.jdt.core.dom.Expression)39 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)38 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)37 ForStatement (org.eclipse.jdt.core.dom.ForStatement)35 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)34 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)30 IfStatement (org.eclipse.jdt.core.dom.IfStatement)30 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)28 DoStatement (org.eclipse.jdt.core.dom.DoStatement)26 Type (org.eclipse.jdt.core.dom.Type)26 SimpleName (org.eclipse.jdt.core.dom.SimpleName)22 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)21 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)19 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)18