Search in sources :

Example 31 with VariableDeclarationFragment

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

the class ExtractConstantRefactoring method canReplace.

// !! - like one in ExtractTempRefactoring
private static boolean canReplace(IASTFragment fragment) {
    ASTNode node = fragment.getAssociatedNode();
    ASTNode parent = node.getParent();
    if (parent instanceof VariableDeclarationFragment) {
        VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
        if (node.equals(vdf.getName()))
            return false;
    }
    if (parent instanceof ExpressionStatement)
        return false;
    if (parent instanceof SwitchCase) {
        if (node instanceof Name) {
            Name name = (Name) node;
            ITypeBinding typeBinding = name.resolveTypeBinding();
            if (typeBinding != null) {
                return !typeBinding.isEnum();
            }
        }
    }
    return true;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 32 with VariableDeclarationFragment

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

the class FullConstraintCreator method getConstraintsFromFragmentList.

private ITypeConstraint[] getConstraintsFromFragmentList(List<VariableDeclarationFragment> list, Type type) {
    int size = list.size();
    ConstraintVariable typeVariable = fConstraintVariableFactory.makeTypeVariable(type);
    List<ITypeConstraint> result = new ArrayList<ITypeConstraint>((size * (size - 1)) / 2);
    for (int i = 0; i < size; i++) {
        VariableDeclarationFragment fragment1 = list.get(i);
        SimpleName fragment1Name = fragment1.getName();
        result.addAll(Arrays.asList(fTypeConstraintFactory.createDefinesConstraint(fConstraintVariableFactory.makeExpressionOrTypeVariable(fragment1Name, getContext()), typeVariable)));
        for (int j = i + 1; j < size; j++) {
            VariableDeclarationFragment fragment2 = list.get(j);
            result.addAll(Arrays.asList(fTypeConstraintFactory.createEqualsConstraint(fConstraintVariableFactory.makeExpressionOrTypeVariable(fragment1Name, getContext()), fConstraintVariableFactory.makeExpressionOrTypeVariable(fragment2.getName(), getContext()))));
        }
    }
    return result.toArray(new ITypeConstraint[result.size()]);
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList)

Example 33 with VariableDeclarationFragment

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

the class RemoveDeclarationCorrectionProposal method removeVariableReferences.

/**
	 * Remove the field or variable declaration including the initializer.
	 * @param rewrite the ast rewrite
	 * @param reference the reference
	 */
private void removeVariableReferences(ASTRewrite rewrite, SimpleName reference) {
    ASTNode parent = reference.getParent();
    while (parent instanceof QualifiedName) {
        parent = parent.getParent();
    }
    if (parent instanceof FieldAccess) {
        parent = parent.getParent();
    }
    int nameParentType = parent.getNodeType();
    if (nameParentType == ASTNode.ASSIGNMENT) {
        Assignment assignment = (Assignment) parent;
        Expression rightHand = assignment.getRightHandSide();
        ASTNode assignParent = assignment.getParent();
        if (assignParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT && rightHand.getNodeType() != ASTNode.ASSIGNMENT) {
            removeVariableWithInitializer(rewrite, rightHand, assignParent);
        } else {
            rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), null);
        }
    } else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
        rewrite.remove(parent, null);
    } else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
        VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
        ASTNode varDecl = frag.getParent();
        List<VariableDeclarationFragment> fragments;
        if (varDecl instanceof VariableDeclarationExpression) {
            fragments = ((VariableDeclarationExpression) varDecl).fragments();
        } else if (varDecl instanceof FieldDeclaration) {
            fragments = ((FieldDeclaration) varDecl).fragments();
        } else {
            fragments = ((VariableDeclarationStatement) varDecl).fragments();
        }
        if (fragments.size() == 1) {
            rewrite.remove(varDecl, null);
        } else {
            // don't try to preserve
            rewrite.remove(frag, null);
        }
    }
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 34 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project generator by mybatis.

the class ExistingJavaFileVisitor method visit.

/**
     * Find the generated fields and delete them
     */
@Override
public boolean visit(FieldDeclaration node) {
    if (isGenerated(node)) {
        List<Annotation> annotations = retrieveAnnotations(node);
        if (!annotations.isEmpty()) {
            VariableDeclarationFragment variable = (VariableDeclarationFragment) node.fragments().get(0);
            fieldAnnotations.put(variable.getName().getIdentifier(), annotations);
        }
        node.delete();
    }
    return false;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) Annotation(org.eclipse.jdt.core.dom.Annotation)

Example 35 with VariableDeclarationFragment

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

the class InlineTempRefactoring method getModifiedInitializerSource.

private Expression getModifiedInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException {
    VariableDeclaration varDecl = getVariableDeclaration();
    Expression initializer = varDecl.getInitializer();
    ASTNode referenceContext = reference.getParent();
    if (Invocations.isResolvedTypeInferredFromExpectedType(initializer)) {
        if (!(referenceContext instanceof VariableDeclarationFragment || referenceContext instanceof SingleVariableDeclaration || referenceContext instanceof Assignment)) {
            ITypeBinding[] typeArguments = Invocations.getInferredTypeArguments(initializer);
            if (typeArguments != null) {
                String newSource = createParameterizedInvocation(initializer, typeArguments, rewrite);
                return (Expression) rewrite.getASTRewrite().createStringPlaceholder(newSource, initializer.getNodeType());
            }
        }
    }
    Expression copy = (Expression) rewrite.getASTRewrite().createCopyTarget(initializer);
    AST ast = rewrite.getAST();
    if (NecessaryParenthesesChecker.needsParentheses(initializer, reference.getParent(), reference.getLocationInParent())) {
        ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
        parenthesized.setExpression(copy);
        copy = parenthesized;
    }
    ITypeBinding explicitCast = ASTNodes.getExplicitCast(initializer, reference);
    if (explicitCast != null) {
        CastExpression cast = ast.newCastExpression();
        if (NecessaryParenthesesChecker.needsParentheses(copy, cast, CastExpression.EXPRESSION_PROPERTY)) {
            ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
            parenthesized.setExpression(copy);
            copy = parenthesized;
        }
        cast.setExpression(copy);
        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(reference, rewrite.getImportRewrite());
        cast.setType(rewrite.getImportRewrite().addImport(explicitCast, ast, context));
        copy = cast;
    } else if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(varDecl) > 0) {
        ArrayType newType = (ArrayType) ASTNodeFactory.newType(ast, varDecl);
        ArrayCreation newArrayCreation = ast.newArrayCreation();
        newArrayCreation.setType(newType);
        newArrayCreation.setInitializer((ArrayInitializer) copy);
        return newArrayCreation;
    }
    return copy;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayType(org.eclipse.jdt.core.dom.ArrayType) 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) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Aggregations

VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)101 ASTNode (org.eclipse.jdt.core.dom.ASTNode)44 AST (org.eclipse.jdt.core.dom.AST)38 Expression (org.eclipse.jdt.core.dom.Expression)33 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)33 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)30 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)28 Assignment (org.eclipse.jdt.core.dom.Assignment)25 SimpleName (org.eclipse.jdt.core.dom.SimpleName)25 Type (org.eclipse.jdt.core.dom.Type)25 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)22 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)21 Block (org.eclipse.jdt.core.dom.Block)17 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)16 ArrayList (java.util.ArrayList)15 CastExpression (org.eclipse.jdt.core.dom.CastExpression)15 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)15 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)15 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)15 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)14