Search in sources :

Example 41 with VariableDeclarationFragment

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

the class JavaTextSelection method resolveInVariableInitializer.

public boolean resolveInVariableInitializer() {
    if (fInVariableInitializerRequested)
        return fInVariableInitializer;
    fInVariableInitializerRequested = true;
    resolveSelectedNodes();
    ASTNode node = getStartNode();
    ASTNode last = null;
    while (node != null) {
        int nodeType = node.getNodeType();
        if (node instanceof AbstractTypeDeclaration) {
            fInVariableInitializer = false;
            break;
        } else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
            fInVariableInitializer = false;
            break;
        } else if (nodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT && ((VariableDeclarationFragment) node).getInitializer() == last) {
            fInVariableInitializer = true;
            break;
        } else if (nodeType == ASTNode.SINGLE_VARIABLE_DECLARATION && ((SingleVariableDeclaration) node).getInitializer() == last) {
            fInVariableInitializer = true;
            break;
        } else if (nodeType == ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION && ((AnnotationTypeMemberDeclaration) node).getDefault() == last) {
            fInVariableInitializer = true;
            break;
        }
        last = node;
        node = node.getParent();
    }
    return fInVariableInitializer;
}
Also used : AnnotationTypeMemberDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 42 with VariableDeclarationFragment

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

the class TryWithResourceRefactoring method newResource.

private VariableDeclarationExpression newResource(List<Statement> tryStmts, VariableDeclarationStatement previousDeclStmt, VariableDeclarationFragment previousDeclFragment, List<ASTNode> nodesToRemove) {
    final ASTBuilder b = ctx.getASTBuilder();
    final VariableDeclarationFragment fragment = newFragment(tryStmts, previousDeclFragment, nodesToRemove);
    return fragment != null ? b.declareExpr(b.move(previousDeclStmt.getType()), fragment) : null;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 43 with VariableDeclarationFragment

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

the class RemoveUnnecessaryCastRefactoring method canRemoveCast.

private boolean canRemoveCast(CastExpression node) {
    final ASTNode parent = node.getParent();
    switch(parent.getNodeType()) {
        case RETURN_STATEMENT:
            final MethodDeclaration md = getAncestor(parent, MethodDeclaration.class);
            return isAssignmentCompatible(node.getExpression(), md.getReturnType2());
        case ASSIGNMENT:
            final Assignment as = (Assignment) parent;
            return isAssignmentCompatible(node.getExpression(), as) || isConstantExpressionAssignmentConversion(node);
        case VARIABLE_DECLARATION_FRAGMENT:
            final VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
            return isAssignmentCompatible(node.getExpression(), resolveTypeBinding(vdf)) || isConstantExpressionAssignmentConversion(node);
        case INFIX_EXPRESSION:
            final InfixExpression ie = (InfixExpression) parent;
            final Expression lo = ie.getLeftOperand();
            final Expression ro = ie.getRightOperand();
            if (node.equals(lo)) {
                return (isStringConcat(ie) || isAssignmentCompatible(node.getExpression(), ro)) && !isPrimitiveTypeNarrowing(node) && !hasOperator(ie, DIVIDE) && !hasOperator(ie, PLUS) && !hasOperator(ie, MINUS);
            } else {
                final boolean integralDivision = isIntegralDivision(ie);
                return ((isNotRefactored(lo) && isStringConcat(ie)) || (!integralDivision && isAssignmentCompatibleInInfixExpression(node, ie)) || (integralDivision && canRemoveCastInIntegralDivision(node, ie))) && !isPrimitiveTypeNarrowing(node) && !isIntegralDividedByFloatingPoint(node, ie);
            }
    }
    return false;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) Expression(org.eclipse.jdt.core.dom.Expression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 44 with VariableDeclarationFragment

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

the class RemoveFieldsDefaultValuesRefactoring method visit.

@Override
public boolean visit(FieldDeclaration node) {
    if (!canRemoveFieldDefaultValue(node)) {
        return VISIT_SUBTREE;
    }
    final ITypeBinding fieldType = node.getType().resolveBinding();
    if (fieldType == null || isFinal(node.getModifiers())) {
        return VISIT_SUBTREE;
    }
    boolean visitSubtree = VISIT_SUBTREE;
    for (VariableDeclarationFragment vdf : fragments(node)) {
        final Expression initializer = vdf.getInitializer();
        if (initializer != null) {
            if (!fieldType.isPrimitive() && isNullLiteral(initializer)) {
                this.ctx.getRefactorings().remove(initializer);
                visitSubtree = DO_NOT_VISIT_SUBTREE;
            } else if (fieldType.isPrimitive() && isPrimitiveLiteral(initializer) && isPrimitiveDefaultValue(initializer.resolveConstantExpressionValue())) {
                this.ctx.getRefactorings().remove(initializer);
                visitSubtree = DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return visitSubtree;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 45 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final MethodDeclaration it) {
    Javadoc _javadoc = it.getJavadoc();
    boolean _tripleNotEquals = (_javadoc != null);
    if (_tripleNotEquals) {
        it.getJavadoc().accept(this);
    }
    final Function1<ASTNode, StringBuffer> _function = (ASTNode node) -> {
        StringBuffer _xifexpression = null;
        if ((node instanceof MethodDeclaration)) {
            StringBuffer _xifexpression_1 = null;
            boolean _isConstructor = ((MethodDeclaration) node).isConstructor();
            boolean _not = (!_isConstructor);
            if (_not) {
                StringBuffer _xifexpression_2 = null;
                boolean _isOverrideMethod = this._aSTFlattenerUtils.isOverrideMethod(((MethodDeclaration) node));
                if (_isOverrideMethod) {
                    _xifexpression_2 = this.appendToBuffer("override ");
                } else {
                    _xifexpression_2 = this.appendToBuffer("def ");
                }
                _xifexpression_1 = _xifexpression_2;
            }
            _xifexpression = _xifexpression_1;
        }
        return _xifexpression;
    };
    final Function1<ASTNode, StringBuffer> afterAnnotationProcessingCallback = _function;
    this.appendModifiers(it, it.modifiers(), afterAnnotationProcessingCallback);
    boolean _isPackageVisibility = this._aSTFlattenerUtils.isPackageVisibility(Iterables.<Modifier>filter(it.modifiers(), Modifier.class));
    if (_isPackageVisibility) {
        ASTNode _parent = it.getParent();
        if ((_parent instanceof TypeDeclaration)) {
            ASTNode _parent_1 = it.getParent();
            boolean _isInterface = ((TypeDeclaration) _parent_1).isInterface();
            boolean _not = (!_isInterface);
            if (_not) {
                this.appendToBuffer("package ");
            }
        }
    }
    boolean _isConstructor = it.isConstructor();
    if (_isConstructor) {
        this.appendToBuffer(" new");
    }
    boolean _isEmpty = it.typeParameters().isEmpty();
    boolean _not_1 = (!_isEmpty);
    if (_not_1) {
        boolean _isConstructor_1 = it.isConstructor();
        if (_isConstructor_1) {
            this.addProblem(it, "Type parameters for constructors are not supported");
        }
        this.appendTypeParameters(it.typeParameters());
    }
    boolean _isConstructor_2 = it.isConstructor();
    boolean _not_2 = (!_isConstructor_2);
    if (_not_2) {
        Type _returnType2 = it.getReturnType2();
        boolean _tripleNotEquals_1 = (_returnType2 != null);
        if (_tripleNotEquals_1) {
            it.getReturnType2().accept(this);
        } else {
            this.appendToBuffer("void");
        }
        this.appendSpaceToBuffer();
        it.getName().accept(this);
    }
    this.appendToBuffer("(");
    final Consumer<SingleVariableDeclaration> _function_1 = (SingleVariableDeclaration p) -> {
        Boolean _isAssignedInBody = this._aSTFlattenerUtils.isAssignedInBody(it.getBody(), p.getName());
        if ((_isAssignedInBody).booleanValue()) {
            if ((it.isConstructor() && (!it.getBody().statements().isEmpty()))) {
                final Expression firstInBody = IterableExtensions.<Expression>head(this._aSTFlattenerUtils.findAssignmentsInBlock(it.getBody(), p));
                if ((firstInBody != null)) {
                    ConstructorInvocation _findParentOfType = this._aSTFlattenerUtils.<ConstructorInvocation>findParentOfType(firstInBody, ConstructorInvocation.class);
                    boolean _tripleNotEquals_2 = (_findParentOfType != null);
                    if (_tripleNotEquals_2) {
                        this.addProblem(p, "Final parameter modified in constructor call");
                    } else {
                        SuperConstructorInvocation _findParentOfType_1 = this._aSTFlattenerUtils.<SuperConstructorInvocation>findParentOfType(firstInBody, SuperConstructorInvocation.class);
                        boolean _tripleNotEquals_3 = (_findParentOfType_1 != null);
                        if (_tripleNotEquals_3) {
                            this.addProblem(p, "Final parameter modified in super constructor call");
                        }
                    }
                }
            }
            final VariableDeclarationFragment varFrag = p.getAST().newVariableDeclarationFragment();
            varFrag.setName(p.getAST().newSimpleName(p.getName().toString()));
            AST _aST = p.getAST();
            SimpleName _name = p.getName();
            String _plus = (_name + "_finalParam_");
            p.setName(_aST.newSimpleName(_plus));
            varFrag.setInitializer(p.getAST().newSimpleName(p.getName().toString()));
            final VariableDeclarationStatement varDecl = p.getAST().newVariableDeclarationStatement(varFrag);
            ASTNode _createInstance = p.getAST().createInstance(SimpleType.class);
            final Type typeCopy = ((Type) _createInstance);
            varDecl.setType(typeCopy);
            it.getBody().statements().add(0, varDecl);
        }
    };
    ListExtensions.<SingleVariableDeclaration>reverseView(it.parameters()).forEach(_function_1);
    this.visitAllSeparatedByComma(it.parameters());
    this.appendToBuffer(")");
    this.appendExtraDimensions(it.getExtraDimensions());
    List<? extends ASTNode> throwsTypes = CollectionLiterals.<ASTNode>newArrayList();
    boolean _java8orHigher = this.java8orHigher();
    boolean _not_3 = (!_java8orHigher);
    if (_not_3) {
        throwsTypes = it.thrownExceptions();
    } else {
        throwsTypes = this._aSTFlattenerUtils.genericChildListProperty(it, "thrownExceptionTypes");
    }
    boolean _isEmpty_1 = throwsTypes.isEmpty();
    boolean _not_4 = (!_isEmpty_1);
    if (_not_4) {
        this.appendToBuffer(" throws ");
        this.visitAllSeparatedByComma(throwsTypes);
    }
    this.appendSpaceToBuffer();
    Block _body = it.getBody();
    boolean _tripleNotEquals_2 = (_body != null);
    if (_tripleNotEquals_2) {
        it.getBody().accept(this);
    } else {
        this.appendLineWrapToBuffer();
    }
    return false;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Javadoc(org.eclipse.jdt.core.dom.Javadoc) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration)

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