Search in sources :

Example 46 with Expression

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

the class UnusedCodeFix method hasSideEffect.

private static boolean hasSideEffect(SimpleName reference) {
    ASTNode parent = reference.getParent();
    while (parent instanceof QualifiedName) {
        parent = parent.getParent();
    }
    if (parent instanceof FieldAccess) {
        parent = parent.getParent();
    }
    ASTNode node = null;
    int nameParentType = parent.getNodeType();
    if (nameParentType == ASTNode.ASSIGNMENT) {
        Assignment assignment = (Assignment) parent;
        node = assignment.getRightHandSide();
    } else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
        SingleVariableDeclaration decl = (SingleVariableDeclaration) parent;
        node = decl.getInitializer();
        if (node == null)
            return false;
    } else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
        node = parent;
    } else {
        return false;
    }
    ArrayList<Expression> sideEffects = new ArrayList<Expression>();
    node.accept(new SideEffectFinder(sideEffects));
    return sideEffects.size() > 0;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) 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) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 47 with Expression

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

the class Checks method isEnumCase.

public static boolean isEnumCase(ASTNode node) {
    if (node instanceof SwitchCase) {
        final SwitchCase caze = (SwitchCase) node;
        final Expression expression = caze.getExpression();
        if (expression instanceof Name) {
            final Name name = (Name) expression;
            final IBinding binding = name.resolveBinding();
            if (binding instanceof IVariableBinding) {
                IVariableBinding variableBinding = (IVariableBinding) binding;
                return variableBinding.isEnumConstant();
            }
        }
    }
    return false;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) Expression(org.eclipse.jdt.core.dom.Expression) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Name(org.eclipse.jdt.core.dom.Name)

Example 48 with Expression

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

the class AccessAnalyzer method visit.

@Override
public boolean visit(PrefixExpression node) {
    Expression operand = node.getOperand();
    if (!considerBinding(resolveBinding(operand), operand))
        return true;
    PrefixExpression.Operator operator = node.getOperator();
    if (operator != PrefixExpression.Operator.INCREMENT && operator != PrefixExpression.Operator.DECREMENT)
        return true;
    checkParent(node);
    fRewriter.replace(node, createInvocation(node.getAST(), node.getOperand(), node.getOperator().toString()), createGroupDescription(PREFIX_ACCESS));
    return false;
}
Also used : PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 49 with Expression

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

the class ChangeSignatureProcessor method isValidExpression.

public static boolean isValidExpression(String string) {
    String trimmed = string.trim();
    if (//speed up for a common case //$NON-NLS-1$
    "".equals(trimmed))
        return false;
    StringBuffer cuBuff = new StringBuffer();
    cuBuff.append(CONST_CLASS_DECL).append(//$NON-NLS-1$
    "Object").append(CONST_ASSIGN);
    int offset = cuBuff.length();
    cuBuff.append(trimmed).append(CONST_CLOSE);
    ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    p.setSource(cuBuff.toString().toCharArray());
    CompilationUnit cu = (CompilationUnit) p.createAST(null);
    Selection selection = Selection.createFromStartLength(offset, trimmed.length());
    SelectionAnalyzer analyzer = new SelectionAnalyzer(selection, false);
    cu.accept(analyzer);
    ASTNode selected = analyzer.getFirstSelectedNode();
    return (selected instanceof Expression) && trimmed.equals(cuBuff.substring(cu.getExtendedStartPosition(selected), cu.getExtendedStartPosition(selected) + cu.getExtendedLength(selected)));
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) Selection(org.eclipse.jdt.internal.corext.dom.Selection) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTParser(org.eclipse.jdt.core.dom.ASTParser) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)

Example 50 with Expression

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

the class ChangeSignatureProcessor method isValidVarargsExpression.

public static boolean isValidVarargsExpression(String string) {
    String trimmed = string.trim();
    if (//speed up for a common case //$NON-NLS-1$
    "".equals(trimmed))
        return true;
    StringBuffer cuBuff = new StringBuffer();
    //$NON-NLS-1$
    cuBuff.append("class A{ {m(");
    int offset = cuBuff.length();
    cuBuff.append(trimmed).append(//$NON-NLS-1$
    ");}}");
    ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    p.setSource(cuBuff.toString().toCharArray());
    CompilationUnit cu = (CompilationUnit) p.createAST(null);
    Selection selection = Selection.createFromStartLength(offset, trimmed.length());
    SelectionAnalyzer analyzer = new SelectionAnalyzer(selection, false);
    cu.accept(analyzer);
    ASTNode[] selectedNodes = analyzer.getSelectedNodes();
    if (selectedNodes.length == 0)
        return false;
    for (int i = 0; i < selectedNodes.length; i++) {
        if (!(selectedNodes[i] instanceof Expression))
            return false;
    }
    return true;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) Selection(org.eclipse.jdt.internal.corext.dom.Selection) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTParser(org.eclipse.jdt.core.dom.ASTParser) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)

Aggregations

Expression (org.eclipse.jdt.core.dom.Expression)552 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)263 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)213 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)187 CastExpression (org.eclipse.jdt.core.dom.CastExpression)185 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)135 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)126 ASTNode (org.eclipse.jdt.core.dom.ASTNode)125 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)122 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)121 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)112 AST (org.eclipse.jdt.core.dom.AST)101 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)95 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)88 SimpleName (org.eclipse.jdt.core.dom.SimpleName)83 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)76 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)73 Type (org.eclipse.jdt.core.dom.Type)70 ArrayList (java.util.ArrayList)69 Block (org.eclipse.jdt.core.dom.Block)63