Search in sources :

Example 1 with SuperConstructorInvocation

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

the class StubUtility2 method createConstructorStub.

public static MethodDeclaration createConstructorStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, ITypeBinding typeBinding, IMethodBinding superConstructor, IVariableBinding[] variableBindings, int modifiers, 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(typeBinding.getName()));
    decl.setConstructor(true);
    List<SingleVariableDeclaration> parameters = decl.parameters();
    if (superConstructor != null) {
        createTypeParameters(imports, context, ast, superConstructor, decl);
        createParameters(unit.getJavaProject(), imports, context, ast, superConstructor, null, decl);
        createThrownExceptions(decl, superConstructor, imports, context, ast);
    }
    Block body = ast.newBlock();
    decl.setBody(body);
    String delimiter = StubUtility.getLineDelimiterUsed(unit);
    if (superConstructor != null) {
        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()));
        }
        body.statements().add(invocation);
    }
    List<String> prohibited = new ArrayList<String>();
    for (final Iterator<SingleVariableDeclaration> iterator = parameters.iterator(); iterator.hasNext(); ) prohibited.add(iterator.next().getName().getIdentifier());
    String param = null;
    List<String> list = new ArrayList<String>(prohibited);
    String[] excluded = null;
    for (int i = 0; i < variableBindings.length; i++) {
        SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
        var.setType(imports.addImport(variableBindings[i].getType(), ast, context));
        excluded = new String[list.size()];
        list.toArray(excluded);
        param = suggestParameterName(unit, variableBindings[i], excluded);
        list.add(param);
        var.setName(ast.newSimpleName(param));
        parameters.add(var);
    }
    list = new ArrayList<String>(prohibited);
    for (int i = 0; i < variableBindings.length; i++) {
        excluded = new String[list.size()];
        list.toArray(excluded);
        final String paramName = suggestParameterName(unit, variableBindings[i], excluded);
        list.add(paramName);
        final String fieldName = variableBindings[i].getName();
        Expression expression = null;
        if (paramName.equals(fieldName) || settings.useKeywordThis) {
            FieldAccess access = ast.newFieldAccess();
            access.setExpression(ast.newThisExpression());
            access.setName(ast.newSimpleName(fieldName));
            expression = access;
        } else
            expression = ast.newSimpleName(fieldName);
        Assignment assignment = ast.newAssignment();
        assignment.setLeftHandSide(expression);
        assignment.setRightHandSide(ast.newSimpleName(paramName));
        assignment.setOperator(Assignment.Operator.ASSIGN);
        body.statements().add(ast.newExpressionStatement(assignment));
    }
    if (settings != null && settings.createComments) {
        String string = CodeGeneration.getMethodComment(unit, typeBinding.getName(), decl, superConstructor, 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) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Assignment(org.eclipse.jdt.core.dom.Assignment) Expression(org.eclipse.jdt.core.dom.Expression) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 2 with SuperConstructorInvocation

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

the class ConstructorFromSuperclassProposal method addEnclosingInstanceAccess.

private SuperConstructorInvocation addEnclosingInstanceAccess(ASTRewrite rewrite, ImportRewriteContext importRewriteContext, List<SingleVariableDeclaration> parameters, String[] paramNames, ITypeBinding enclosingInstance) {
    AST ast = rewrite.getAST();
    SuperConstructorInvocation invocation = ast.newSuperConstructorInvocation();
    SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
    var.setType(getImportRewrite().addImport(enclosingInstance, ast, importRewriteContext));
    String[] enclosingArgNames = StubUtility.getArgumentNameSuggestions(getCompilationUnit().getJavaProject(), enclosingInstance.getTypeDeclaration().getName(), 0, paramNames);
    String firstName = enclosingArgNames[0];
    var.setName(ast.newSimpleName(firstName));
    parameters.add(var);
    Name enclosing = ast.newSimpleName(firstName);
    invocation.setExpression(enclosing);
    //$NON-NLS-1$
    String key = "arg_name_" + firstName;
    addLinkedPosition(rewrite.track(enclosing), false, key);
    for (int i = 0; i < enclosingArgNames.length; i++) {
        // alternative names
        addLinkedPositionProposal(key, enclosingArgNames[i], null);
    }
    return invocation;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) Name(org.eclipse.jdt.core.dom.Name)

Example 3 with SuperConstructorInvocation

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

the class ConstructorFromSuperclassProposal method createNewMethodDeclaration.

private MethodDeclaration createNewMethodDeclaration(AST ast, IMethodBinding binding, ASTRewrite rewrite, ImportRewriteContext importRewriteContext, CodeGenerationSettings commentSettings) throws CoreException {
    String name = fTypeNode.getName().getIdentifier();
    MethodDeclaration decl = ast.newMethodDeclaration();
    decl.setConstructor(true);
    decl.setName(ast.newSimpleName(name));
    Block body = ast.newBlock();
    decl.setBody(body);
    SuperConstructorInvocation invocation = null;
    List<SingleVariableDeclaration> parameters = decl.parameters();
    String[] paramNames = getArgumentNames(binding);
    ITypeBinding enclosingInstance = getEnclosingInstance();
    if (enclosingInstance != null) {
        invocation = addEnclosingInstanceAccess(rewrite, importRewriteContext, parameters, paramNames, enclosingInstance);
    }
    if (binding == null) {
        decl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
    } else {
        decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, binding.getModifiers()));
        ITypeBinding[] params = binding.getParameterTypes();
        for (int i = 0; i < params.length; i++) {
            SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
            var.setType(getImportRewrite().addImport(params[i], ast, importRewriteContext));
            var.setName(ast.newSimpleName(paramNames[i]));
            parameters.add(var);
        }
        List<Type> thrownExceptions = decl.thrownExceptionTypes();
        ITypeBinding[] excTypes = binding.getExceptionTypes();
        for (int i = 0; i < excTypes.length; i++) {
            Type excType = getImportRewrite().addImport(excTypes[i], ast, importRewriteContext);
            thrownExceptions.add(excType);
        }
        if (invocation == null) {
            invocation = ast.newSuperConstructorInvocation();
        }
        List<Expression> arguments = invocation.arguments();
        for (int i = 0; i < paramNames.length; i++) {
            Name argument = ast.newSimpleName(paramNames[i]);
            arguments.add(argument);
            //$NON-NLS-1$
            addLinkedPosition(rewrite.track(argument), false, "arg_name_" + paramNames[i]);
        }
    }
    String bodyStatement = (invocation == null) ? "" : ASTNodes.asFormattedString(invocation, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true));
    //$NON-NLS-1$
    String placeHolder = CodeGeneration.getMethodBodyContent(getCompilationUnit(), name, name, true, bodyStatement, String.valueOf('\n'));
    if (placeHolder != null) {
        ASTNode todoNode = rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
        body.statements().add(todoNode);
    }
    if (commentSettings != null) {
        String string = CodeGeneration.getMethodComment(getCompilationUnit(), name, decl, null, String.valueOf('\n'));
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }
    return decl;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Name(org.eclipse.jdt.core.dom.Name) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Example 4 with SuperConstructorInvocation

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

the class ASTNodeSearchUtil method getAstNode.

public static ASTNode getAstNode(CompilationUnit cuNode, int start, int length) {
    SelectionAnalyzer analyzer = new SelectionAnalyzer(Selection.createFromStartLength(start, length), true);
    cuNode.accept(analyzer);
    //XXX workaround for jdt core feature 23527
    ASTNode node = analyzer.getFirstSelectedNode();
    if (node == null && analyzer.getLastCoveringNode() instanceof SuperConstructorInvocation)
        node = analyzer.getLastCoveringNode().getParent();
    else if (node == null && analyzer.getLastCoveringNode() instanceof ConstructorInvocation)
        node = analyzer.getLastCoveringNode().getParent();
    if (node == null)
        return null;
    ASTNode parentNode = node.getParent();
    if (parentNode instanceof MethodDeclaration) {
        MethodDeclaration md = (MethodDeclaration) parentNode;
        if (!(node instanceof SimpleName) && md.isConstructor() && md.getBody() != null && md.getBody().statements().size() > 0 && (md.getBody().statements().get(0) instanceof ConstructorInvocation || md.getBody().statements().get(0) instanceof SuperConstructorInvocation) && ((ASTNode) md.getBody().statements().get(0)).getLength() == length + 1)
            return (ASTNode) md.getBody().statements().get(0);
    }
    if (parentNode instanceof SuperConstructorInvocation) {
        if (parentNode.getLength() == length + 1)
            return parentNode;
    }
    if (parentNode instanceof ConstructorInvocation) {
        if (parentNode.getLength() == length + 1)
            return parentNode;
    }
    return node;
}
Also used : SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Example 5 with SuperConstructorInvocation

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

the class ChangeSignatureProcessor method addExplicitSuperConstructorCall.

private void addExplicitSuperConstructorCall(MethodDeclaration constructor, CompilationUnitRewrite cuRewrite) {
    SuperConstructorInvocation superCall = constructor.getAST().newSuperConstructorInvocation();
    addArgumentsToNewSuperConstructorCall(superCall, cuRewrite);
    String msg = RefactoringCoreMessages.ChangeSignatureRefactoring_add_super_call;
    TextEditGroup description = cuRewrite.createGroupDescription(msg);
    cuRewrite.getASTRewrite().getListRewrite(constructor.getBody(), Block.STATEMENTS_PROPERTY).insertFirst(superCall, description);
}
Also used : SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

SuperConstructorInvocation (org.eclipse.jdt.core.dom.SuperConstructorInvocation)18 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)10 ASTNode (org.eclipse.jdt.core.dom.ASTNode)9 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)8 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)7 ConstructorInvocation (org.eclipse.jdt.core.dom.ConstructorInvocation)7 Expression (org.eclipse.jdt.core.dom.Expression)7 AST (org.eclipse.jdt.core.dom.AST)6 Block (org.eclipse.jdt.core.dom.Block)6 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)6 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 Javadoc (org.eclipse.jdt.core.dom.Javadoc)5 SimpleName (org.eclipse.jdt.core.dom.SimpleName)5 ArrayList (java.util.ArrayList)4 CastExpression (org.eclipse.jdt.core.dom.CastExpression)4 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 Assignment (org.eclipse.jdt.core.dom.Assignment)3 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)3 CoreException (org.eclipse.core.runtime.CoreException)2