Search in sources :

Example 56 with QualifiedName

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

the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method isRefactorComparisonToRefactor.

private boolean isRefactorComparisonToRefactor(final AtomicReference<Expression> criteria, final AtomicBoolean isForward, final SimpleName name1, final SimpleName name2, final Expression expr1, final Expression expr2) {
    MethodInvocation method1 = ASTNodes.as(expr1, MethodInvocation.class);
    MethodInvocation method2 = ASTNodes.as(expr2, MethodInvocation.class);
    QualifiedName field1 = ASTNodes.as(expr1, QualifiedName.class);
    QualifiedName field2 = ASTNodes.as(expr2, QualifiedName.class);
    if (method1 != null && Utils.isEmpty(method1.arguments()) && method2 != null && Utils.isEmpty(method2.arguments())) {
        String methodName1 = method1.getName().getIdentifier();
        String methodName2 = method2.getName().getIdentifier();
        SimpleName objectExpr1 = ASTNodes.as(method1.getExpression(), SimpleName.class);
        SimpleName objectExpr2 = ASTNodes.as(method2.getExpression(), SimpleName.class);
        if (Utils.equalNotNull(methodName1, methodName2) && objectExpr1 != null && objectExpr2 != null) {
            if (ASTNodes.isSameVariable(objectExpr1, name1) && ASTNodes.isSameVariable(objectExpr2, name2)) {
                criteria.set(method1);
                return true;
            }
            if (ASTNodes.isSameVariable(objectExpr1, name2) && ASTNodes.isSameVariable(objectExpr2, name1)) {
                criteria.set(method1);
                isForward.lazySet(!isForward.get());
                return true;
            }
        }
    } else if (field1 != null && field2 != null) {
        SimpleName fieldName1 = field1.getName();
        SimpleName fieldName2 = field2.getName();
        SimpleName objectExpr1 = ASTNodes.as(field1.getQualifier(), SimpleName.class);
        SimpleName objectExpr2 = ASTNodes.as(field2.getQualifier(), SimpleName.class);
        if (ASTNodes.isSameVariable(fieldName1, fieldName2) && objectExpr1 != null && objectExpr2 != null) {
            if (ASTNodes.isSameVariable(objectExpr1, name1) && ASTNodes.isSameVariable(objectExpr2, name2)) {
                criteria.set(field1);
                return true;
            }
            if (ASTNodes.isSameVariable(objectExpr1, name2) && ASTNodes.isSameVariable(objectExpr2, name1)) {
                criteria.set(field1);
                isForward.lazySet(!isForward.get());
                return true;
            }
        }
    }
    return false;
}
Also used : QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 57 with QualifiedName

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

the class NamedMethodRatherThanLogLevelParameterCleanUp method visit.

@Override
public boolean visit(final MethodInvocation visited) {
    if (ASTNodes.usesGivenSignature(visited, Logger.class.getCanonicalName(), "log", Level.class.getCanonicalName(), String.class.getCanonicalName())) {
        // $NON-NLS-1$
        List<Expression> args = visited.arguments();
        if (args != null && args.size() == 2) {
            QualifiedName levelType = ASTNodes.as(args.get(0), QualifiedName.class);
            if (levelType != null) {
                String methodName;
                if (ASTNodes.isField(levelType, Level.class.getCanonicalName(), "SEVERE")) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    methodName = "severe";
                } else if (ASTNodes.isField(levelType, Level.class.getCanonicalName(), "WARNING")) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    methodName = "warning";
                } else if (ASTNodes.isField(levelType, Level.class.getCanonicalName(), "INFO")) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    methodName = "info";
                } else if (ASTNodes.isField(levelType, Level.class.getCanonicalName(), "FINE")) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    methodName = "fine";
                } else if (ASTNodes.isField(levelType, Level.class.getCanonicalName(), "FINER")) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    methodName = "finer";
                } else if (ASTNodes.isField(levelType, Level.class.getCanonicalName(), "FINEST")) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    methodName = "finest";
                } else {
                    return true;
                }
                replaceLevelByMethodName(visited, methodName);
                return false;
            }
        }
    }
    return true;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Level(java.util.logging.Level) Logger(java.util.logging.Logger)

Example 58 with QualifiedName

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

the class RemoveUnneededThisExpressionCleanUp method thisExpressionRefersToEnclosingType.

private static boolean thisExpressionRefersToEnclosingType(final Name thisQualifierName, final ASTNode node) {
    if (thisQualifierName == null) {
        return true;
    }
    ASTNode enclosingType = ASTNodes.getEnclosingType(node);
    if (enclosingType instanceof AnonymousClassDeclaration) {
        return false;
    }
    AbstractTypeDeclaration ancestor = (AbstractTypeDeclaration) enclosingType;
    if (thisQualifierName instanceof SimpleName) {
        return ASTNodes.isEqual((SimpleName) thisQualifierName, ancestor.getName());
    }
    if (thisQualifierName instanceof QualifiedName) {
        QualifiedName qualifiedName = (QualifiedName) thisQualifierName;
        return ASTNodes.isEqual(qualifiedName.getName(), ancestor.getName()) && thisExpressionRefersToEnclosingType(qualifiedName.getQualifier(), ancestor);
    }
    throw new NotImplementedException(thisQualifierName);
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) NotImplementedException(org.autorefactor.util.NotImplementedException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 59 with QualifiedName

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

the class AbstractPrimitiveRatherThanWrapperCleanUp method isNotNull.

private boolean isNotNull(final Expression expression) {
    if (expression instanceof ParenthesizedExpression) {
        ParenthesizedExpression parenthesizedExpression = (ParenthesizedExpression) expression;
        return isNotNull(parenthesizedExpression.getExpression());
    }
    if (expression instanceof ConditionalExpression) {
        ConditionalExpression prefixExpression = (ConditionalExpression) expression;
        return isNotNull(prefixExpression.getThenExpression()) && isNotNull(prefixExpression.getElseExpression());
    }
    if (getLiteralClass().equals(expression.getClass())) {
        return true;
    }
    if (expression instanceof QualifiedName) {
        QualifiedName qualifiedName = (QualifiedName) expression;
        return ASTNodes.hasType(qualifiedName.getQualifier(), getWrapperFullyQualifiedName()) && (ASTNodes.isField(qualifiedName, getWrapperFullyQualifiedName(), getSafeInConstants()) || ASTNodes.isField(qualifiedName, getPrimitiveTypeName(), getSafeInConstants()));
    }
    if (expression instanceof InfixExpression) {
        InfixExpression infixExpression = (InfixExpression) expression;
        return getInfixInSafeOperators().contains(infixExpression.getOperator());
    }
    if (expression instanceof PrefixExpression) {
        PrefixExpression prefixExpression = (PrefixExpression) expression;
        return getPrefixInSafeOperators().contains(prefixExpression.getOperator());
    }
    if (expression instanceof PostfixExpression) {
        PostfixExpression postfixExpression = (PostfixExpression) expression;
        return getPostfixInSafeOperators().contains(postfixExpression.getOperator());
    }
    if (expression instanceof CastExpression) {
        CastExpression castExpression = (CastExpression) expression;
        return ASTNodes.hasType(castExpression.getType().resolveBinding(), getPrimitiveTypeName()) || ASTNodes.hasType(castExpression.getType().resolveBinding(), getWrapperFullyQualifiedName()) && isNotNull(castExpression.getExpression());
    }
    if (expression instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) expression;
        return // $NON-NLS-1$
        ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", getPrimitiveTypeName()) || getParsingMethodName(getWrapperFullyQualifiedName()) != null && (// $NON-NLS-1$
        ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", String.class.getCanonicalName()) || // $NON-NLS-1$
        ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", String.class.getCanonicalName(), int.class.getSimpleName()));
    }
    if (expression instanceof ClassInstanceCreation) {
        ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) expression;
        List<Expression> classInstanceCreationArguments = classInstanceCreation.arguments();
        if (classInstanceCreationArguments.size() == 1) {
            Expression arg0 = classInstanceCreationArguments.get(0);
            return ASTNodes.hasType(arg0, String.class.getCanonicalName());
        }
    }
    return false;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 60 with QualifiedName

use of org.eclipse.jdt.core.dom.QualifiedName in project sts4 by spring-projects.

the class WebfluxMethodFinder method visit.

@Override
public boolean visit(MethodInvocation node) {
    boolean visitChildren = true;
    if (node != this.root) {
        IMethodBinding methodBinding = node.resolveMethodBinding();
        try {
            if (WebfluxUtils.REQUEST_PREDICATES_TYPE.equals(methodBinding.getDeclaringClass().getBinaryName())) {
                String name = methodBinding.getName();
                if (name != null && WebfluxUtils.REQUEST_PREDICATE_HTTPMETHOD_METHODS.contains(name)) {
                    Range range = doc.toRange(node.getStartPosition(), node.getLength());
                    methods.add(new WebfluxRouteElement(name, range));
                } else if (name != null && WebfluxUtils.REQUEST_PREDICATE_METHOD_METHOD.equals(name)) {
                    QualifiedName qualifiedName = WebfluxUtils.extractQualifiedNameArgument(node);
                    if (qualifiedName.getName() != null) {
                        Range range = doc.toRange(qualifiedName.getStartPosition(), qualifiedName.getLength());
                        methods.add(new WebfluxRouteElement(qualifiedName.getName().toString(), range));
                    }
                }
            }
        } catch (BadLocationException e) {
        // ignore
        }
        if (WebfluxUtils.isRouteMethodInvocation(methodBinding)) {
            visitChildren = false;
        }
    }
    return visitChildren;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Range(org.eclipse.lsp4j.Range) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Aggregations

QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)68 SimpleName (org.eclipse.jdt.core.dom.SimpleName)47 ASTNode (org.eclipse.jdt.core.dom.ASTNode)36 Name (org.eclipse.jdt.core.dom.Name)26 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)22 Expression (org.eclipse.jdt.core.dom.Expression)20 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)18 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)17 IBinding (org.eclipse.jdt.core.dom.IBinding)16 SimpleType (org.eclipse.jdt.core.dom.SimpleType)16 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)14 ArrayList (java.util.ArrayList)13 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)12 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)10 Type (org.eclipse.jdt.core.dom.Type)10 CastExpression (org.eclipse.jdt.core.dom.CastExpression)9 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)9 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)9 IType (org.eclipse.jdt.core.IType)8 Assignment (org.eclipse.jdt.core.dom.Assignment)8