|
1 | 1 | /******************************************************************************* |
2 | | - * Copyright (c) 2021 Fabrice TIERCELIN and others. |
| 2 | + * Copyright (c) 2021, 2025 Fabrice TIERCELIN and others. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials |
5 | 5 | * are made available under the terms of the Eclipse Public License 2.0 |
|
26 | 26 | import org.eclipse.jdt.core.dom.ASTVisitor; |
27 | 27 | import org.eclipse.jdt.core.dom.CompilationUnit; |
28 | 28 | import org.eclipse.jdt.core.dom.Expression; |
| 29 | +import org.eclipse.jdt.core.dom.IMethodBinding; |
| 30 | +import org.eclipse.jdt.core.dom.ITypeBinding; |
29 | 31 | import org.eclipse.jdt.core.dom.InfixExpression; |
30 | 32 | import org.eclipse.jdt.core.dom.MethodInvocation; |
31 | 33 | import org.eclipse.jdt.core.dom.ThisExpression; |
@@ -55,30 +57,64 @@ public boolean visit(final InfixExpression visited) { |
55 | 57 | if (orderedCondition != null |
56 | 58 | && Arrays.asList(InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS).contains(orderedCondition.getOperator())) { |
57 | 59 | MethodInvocation comparisonMI= orderedCondition.getFirstOperand(); |
58 | | - Long literalValue= ASTNodes.getIntegerLiteral(orderedCondition.getSecondOperand()); |
59 | | - |
60 | | - if (literalValue != null |
61 | | - && literalValue.compareTo(0L) != 0 |
62 | | - && comparisonMI.getExpression() != null |
63 | | - && !ASTNodes.is(comparisonMI.getExpression(), ThisExpression.class)) { |
64 | | - if (literalValue.compareTo(0L) < 0) { |
65 | | - if (InfixExpression.Operator.EQUALS.equals(orderedCondition.getOperator())) { |
66 | | - fResult.add(new StandardComparisonFixOperation(visited, comparisonMI, InfixExpression.Operator.LESS)); |
67 | | - } else { |
68 | | - fResult.add(new StandardComparisonFixOperation(visited, comparisonMI, InfixExpression.Operator.GREATER_EQUALS)); |
| 60 | + if (comparisonMI != null) { |
| 61 | + IMethodBinding comparisonMethodBinding= comparisonMI.resolveMethodBinding(); |
| 62 | + if (comparisonMethodBinding != null) { |
| 63 | + IMethodBinding methodDeclaration= comparisonMethodBinding.getMethodDeclaration(); |
| 64 | + ITypeBinding declaringClass= methodDeclaration.getDeclaringClass(); |
| 65 | + if (declaringClass != null) { |
| 66 | + boolean knownComparison= false; |
| 67 | + if (comparisonMethodBinding.getName().equals("compareTo") && isFromClass(declaringClass, "java.lang.Comparable") //$NON-NLS-1$ //$NON-NLS-2$ |
| 68 | + || comparisonMethodBinding.getName().equals("compareToIgnoreCase") && isFromClass(declaringClass, "java.lang.String") //$NON-NLS-1$ //$NON-NLS-2$ |
| 69 | + || comparisonMethodBinding.getName().equals("compare") && isFromClass(declaringClass, "java.util.Comparator")) { //$NON-NLS-1$ //$NON-NLS-2$ |
| 70 | + knownComparison= true; |
| 71 | + } |
| 72 | + if (knownComparison) { |
| 73 | + Long literalValue= ASTNodes.getIntegerLiteral(orderedCondition.getSecondOperand()); |
| 74 | + |
| 75 | + if (literalValue != null |
| 76 | + && literalValue.compareTo(0L) != 0 |
| 77 | + && comparisonMI.getExpression() != null |
| 78 | + && !ASTNodes.is(comparisonMI.getExpression(), ThisExpression.class)) { |
| 79 | + if (literalValue.compareTo(0L) < 0) { |
| 80 | + if (InfixExpression.Operator.EQUALS.equals(orderedCondition.getOperator())) { |
| 81 | + fResult.add(new StandardComparisonFixOperation(visited, comparisonMI, InfixExpression.Operator.LESS)); |
| 82 | + } else { |
| 83 | + fResult.add(new StandardComparisonFixOperation(visited, comparisonMI, InfixExpression.Operator.GREATER_EQUALS)); |
| 84 | + } |
| 85 | + } else if (InfixExpression.Operator.EQUALS.equals(orderedCondition.getOperator())) { |
| 86 | + fResult.add(new StandardComparisonFixOperation(visited, comparisonMI, InfixExpression.Operator.GREATER)); |
| 87 | + } else { |
| 88 | + fResult.add(new StandardComparisonFixOperation(visited, comparisonMI, InfixExpression.Operator.LESS_EQUALS)); |
| 89 | + } |
| 90 | + |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
69 | 94 | } |
70 | | - } else if (InfixExpression.Operator.EQUALS.equals(orderedCondition.getOperator())) { |
71 | | - fResult.add(new StandardComparisonFixOperation(visited, comparisonMI, InfixExpression.Operator.GREATER)); |
72 | | - } else { |
73 | | - fResult.add(new StandardComparisonFixOperation(visited, comparisonMI, InfixExpression.Operator.LESS_EQUALS)); |
74 | 95 | } |
75 | | - |
76 | | - return false; |
77 | 96 | } |
78 | 97 | } |
79 | 98 |
|
80 | 99 | return true; |
81 | 100 | } |
| 101 | + |
| 102 | + private boolean isFromClass(ITypeBinding declaringClass, String name) { |
| 103 | + if (declaringClass == null) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + if (declaringClass.getErasure().getQualifiedName().equals(name)) { |
| 107 | + return true; |
| 108 | + } |
| 109 | + ITypeBinding[] interfaces= declaringClass.getInterfaces(); |
| 110 | + for (ITypeBinding anInterface : interfaces) { |
| 111 | + if (isFromClass(anInterface, name)) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + } |
| 115 | + ITypeBinding superClass= declaringClass.getSuperclass(); |
| 116 | + return isFromClass(superClass, name); |
| 117 | + } |
82 | 118 | } |
83 | 119 |
|
84 | 120 | public static class StandardComparisonFixOperation extends CompilationUnitRewriteOperation { |
|
0 commit comments