Search in sources :

Example 6 with Initializer

use of org.eclipse.jdt.core.dom.Initializer in project flux by eclipse.

the class QuickAssistProcessor method getConvertStringConcatenationProposals.

private static boolean getConvertStringConcatenationProposals(IInvocationContext context, Collection<ICommandAccess> resultingCollections) {
    ASTNode node = context.getCoveringNode();
    BodyDeclaration parentDecl = ASTResolving.findParentBodyDeclaration(node);
    if (!(parentDecl instanceof MethodDeclaration || parentDecl instanceof Initializer))
        return false;
    AST ast = node.getAST();
    //$NON-NLS-1$
    ITypeBinding stringBinding = ast.resolveWellKnownType("java.lang.String");
    if (node instanceof Expression && !(node instanceof InfixExpression)) {
        node = node.getParent();
    }
    if (node instanceof VariableDeclarationFragment) {
        node = ((VariableDeclarationFragment) node).getInitializer();
    } else if (node instanceof Assignment) {
        node = ((Assignment) node).getRightHandSide();
    }
    InfixExpression oldInfixExpression = null;
    while (node instanceof InfixExpression) {
        InfixExpression curr = (InfixExpression) node;
        if (curr.resolveTypeBinding() == stringBinding && curr.getOperator() == InfixExpression.Operator.PLUS) {
            // is a infix expression we can use
            oldInfixExpression = curr;
        } else {
            break;
        }
        node = node.getParent();
    }
    if (oldInfixExpression == null)
        return false;
    if (resultingCollections == null) {
        return true;
    }
    LinkedCorrectionProposal stringBufferProposal = getConvertToStringBufferProposal(context, ast, oldInfixExpression);
    resultingCollections.add(stringBufferProposal);
    ASTRewriteCorrectionProposal messageFormatProposal = getConvertToMessageFormatProposal(context, ast, oldInfixExpression);
    if (messageFormatProposal != null)
        resultingCollections.add(messageFormatProposal);
    return true;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) AST(org.eclipse.jdt.core.dom.AST) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer) Initializer(org.eclipse.jdt.core.dom.Initializer) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 7 with Initializer

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

the class AssignToVariableAssistProposal method doAddField.

private ASTRewrite doAddField() {
    boolean isParamToField = fNodeToAssign.getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION;
    ASTNode newTypeDecl = ASTResolving.findParentType(fNodeToAssign);
    if (newTypeDecl == null) {
        return null;
    }
    Expression expression = isParamToField ? ((SingleVariableDeclaration) fNodeToAssign).getName() : ((ExpressionStatement) fNodeToAssign).getExpression();
    AST ast = newTypeDecl.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    createImportRewrite((CompilationUnit) fNodeToAssign.getRoot());
    BodyDeclaration bodyDecl = ASTResolving.findParentBodyDeclaration(fNodeToAssign);
    Block body;
    if (bodyDecl instanceof MethodDeclaration) {
        body = ((MethodDeclaration) bodyDecl).getBody();
    } else if (bodyDecl instanceof Initializer) {
        body = ((Initializer) bodyDecl).getBody();
    } else {
        return null;
    }
    IJavaProject project = getCompilationUnit().getJavaProject();
    boolean isAnonymous = newTypeDecl.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION;
    boolean isStatic = Modifier.isStatic(bodyDecl.getModifiers()) && !isAnonymous;
    boolean isConstructorParam = isParamToField && fNodeToAssign.getParent() instanceof MethodDeclaration && ((MethodDeclaration) fNodeToAssign.getParent()).isConstructor();
    int modifiers = Modifier.PRIVATE;
    if (isStatic) {
        modifiers |= Modifier.STATIC;
    } else if (isConstructorParam) {
    //			String saveActionsKey= AbstractSaveParticipantPreferenceConfiguration.EDITOR_SAVE_PARTICIPANT_PREFIX + CleanUpPostSaveListener.POSTSAVELISTENER_ID;
    //			IScopeContext[] scopes= { InstanceScope.INSTANCE, new ProjectScope(project.getProject()) };
    //			boolean safeActionsEnabled= Platform.getPreferencesService().getBoolean(JavaPlugin.getPluginId(), saveActionsKey, false, scopes);
    }
    if (/*CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(
					CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.CLEANUP_ON_SAVE_ADDITIONAL_OPTIONS, project))
			&&*/
    CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL, project)) && CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS, project))) {
        int constructors = 0;
        if (newTypeDecl instanceof AbstractTypeDeclaration) {
            List<BodyDeclaration> bodyDeclarations = ((AbstractTypeDeclaration) newTypeDecl).bodyDeclarations();
            for (BodyDeclaration decl : bodyDeclarations) {
                if (decl instanceof MethodDeclaration && ((MethodDeclaration) decl).isConstructor()) {
                    constructors++;
                }
            }
        }
        if (constructors == 1) {
            modifiers |= Modifier.FINAL;
        }
    }
    VariableDeclarationFragment newDeclFrag = addFieldDeclaration(rewrite, newTypeDecl, modifiers, expression);
    String varName = newDeclFrag.getName().getIdentifier();
    Assignment assignment = ast.newAssignment();
    assignment.setRightHandSide((Expression) rewrite.createCopyTarget(expression));
    boolean needsThis = StubUtility.useThisForFieldAccess(project);
    if (isParamToField) {
        needsThis |= varName.equals(((SimpleName) expression).getIdentifier());
    }
    SimpleName accessName = ast.newSimpleName(varName);
    if (needsThis) {
        FieldAccess fieldAccess = ast.newFieldAccess();
        fieldAccess.setName(accessName);
        if (isStatic) {
            String typeName = ((AbstractTypeDeclaration) newTypeDecl).getName().getIdentifier();
            fieldAccess.setExpression(ast.newSimpleName(typeName));
        } else {
            fieldAccess.setExpression(ast.newThisExpression());
        }
        assignment.setLeftHandSide(fieldAccess);
    } else {
        assignment.setLeftHandSide(accessName);
    }
    ASTNode selectionNode;
    if (isParamToField) {
        // assign parameter to field
        ExpressionStatement statement = ast.newExpressionStatement(assignment);
        int insertIdx = findAssignmentInsertIndex(body.statements());
        rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY).insertAt(statement, insertIdx, null);
        selectionNode = statement;
    } else {
        if (needsSemicolon(expression)) {
            rewrite.replace(expression, ast.newExpressionStatement(assignment), null);
        } else {
            rewrite.replace(expression, assignment, null);
        }
        selectionNode = fNodeToAssign;
    }
    addLinkedPosition(rewrite.track(newDeclFrag.getName()), false, KEY_NAME);
    if (!isParamToField) {
        FieldDeclaration fieldDeclaration = (FieldDeclaration) newDeclFrag.getParent();
        addLinkedPosition(rewrite.track(fieldDeclaration.getType()), false, KEY_TYPE);
    }
    addLinkedPosition(rewrite.track(accessName), true, KEY_NAME);
    IVariableBinding variableBinding = newDeclFrag.resolveBinding();
    if (variableBinding != null) {
        SimpleName[] linkedNodes = LinkedNodeFinder.findByBinding(fNodeToAssign.getRoot(), variableBinding);
        for (int i = 0; i < linkedNodes.length; i++) {
            addLinkedPosition(rewrite.track(linkedNodes[i]), false, KEY_NAME);
        }
    }
    setEndPosition(rewrite.track(selectionNode));
    return rewrite;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) Assignment(org.eclipse.jdt.core.dom.Assignment) IJavaProject(org.eclipse.jdt.core.IJavaProject) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Initializer(org.eclipse.jdt.core.dom.Initializer) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 8 with Initializer

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

the class ScopeAnalyzer method addLocalDeclarations.

private boolean addLocalDeclarations(ASTNode node, int offset, int flags, IBindingRequestor requestor) {
    if (hasFlag(VARIABLES, flags) || hasFlag(TYPES, flags)) {
        BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
        if (declaration instanceof MethodDeclaration || declaration instanceof Initializer || declaration instanceof FieldDeclaration) {
            ScopeAnalyzerVisitor visitor = new ScopeAnalyzerVisitor(offset, flags, requestor);
            declaration.accept(visitor);
            return visitor.fBreak;
        }
    }
    return false;
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 9 with Initializer

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

the class ExtractMethodRefactoring method initializeDestinations.

private void initializeDestinations() {
    List<ASTNode> result = new ArrayList<ASTNode>();
    BodyDeclaration decl = fAnalyzer.getEnclosingBodyDeclaration();
    ASTNode current = ASTResolving.findParentType(decl.getParent());
    if (fAnalyzer.isValidDestination(current)) {
        result.add(current);
    }
    if (current != null && (decl instanceof MethodDeclaration || decl instanceof Initializer || decl instanceof FieldDeclaration)) {
        ITypeBinding binding = ASTNodes.getEnclosingType(current);
        ASTNode next = ASTResolving.findParentType(current.getParent());
        while (next != null && binding != null && binding.isNested()) {
            if (fAnalyzer.isValidDestination(next)) {
                result.add(next);
            }
            current = next;
            binding = ASTNodes.getEnclosingType(current);
            next = ASTResolving.findParentType(next.getParent());
        }
    }
    fDestinations = result.toArray(new ASTNode[result.size()]);
    fDestination = fDestinations[fDestinationIndex];
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 10 with Initializer

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

the class ExtractConstantRefactoring method computeConstantDeclarationLocation.

private void computeConstantDeclarationLocation() throws JavaModelException {
    if (isDeclarationLocationComputed())
        return;
    BodyDeclaration lastStaticDependency = null;
    Iterator<BodyDeclaration> decls = getContainingTypeDeclarationNode().bodyDeclarations().iterator();
    while (decls.hasNext()) {
        BodyDeclaration decl = decls.next();
        int modifiers;
        if (decl instanceof FieldDeclaration)
            modifiers = ((FieldDeclaration) decl).getModifiers();
        else if (decl instanceof Initializer)
            modifiers = ((Initializer) decl).getModifiers();
        else {
            continue;
        /* this declaration is not a field declaration
				              or initializer, so the placement of the constant
				              declaration relative to it does not matter */
        }
        if (Modifier.isStatic(modifiers) && depends(getSelectedExpression(), decl))
            lastStaticDependency = decl;
    }
    if (lastStaticDependency == null)
        fInsertFirst = true;
    else
        fToInsertAfter = lastStaticDependency;
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Aggregations

Initializer (org.eclipse.jdt.core.dom.Initializer)11 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)10 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)9 ASTNode (org.eclipse.jdt.core.dom.ASTNode)7 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)7 AST (org.eclipse.jdt.core.dom.AST)4 Block (org.eclipse.jdt.core.dom.Block)4 Expression (org.eclipse.jdt.core.dom.Expression)4 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)4 Assignment (org.eclipse.jdt.core.dom.Assignment)3 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 SimpleName (org.eclipse.jdt.core.dom.SimpleName)3 Statement (org.eclipse.jdt.core.dom.Statement)3 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)3 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)3 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)2 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)2 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)2 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)2 ForStatement (org.eclipse.jdt.core.dom.ForStatement)2