mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
(no message)
This commit is contained in:
+5
-26
@@ -1,20 +1,18 @@
|
||||
package com.siyeh.ig.classlayout;
|
||||
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.siyeh.ig.*;
|
||||
import com.siyeh.ig.fixes.MakeProtectedFix;
|
||||
import com.siyeh.ig.ui.SingleCheckboxOptionsPanel;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class NonProtectedConstructorInAbstractClassInspection extends MethodInspection {
|
||||
public boolean m_ignoreNonPublicClasses = false;
|
||||
private static final Logger s_logger =
|
||||
Logger.getInstance("NonProtectedConstructorInAbstractClassInspection");
|
||||
private final MakeProtectedFix fix = new MakeProtectedFix();
|
||||
|
||||
public String getDisplayName() {
|
||||
@@ -42,25 +40,6 @@ public class NonProtectedConstructorInAbstractClassInspection extends MethodInsp
|
||||
return fix;
|
||||
}
|
||||
|
||||
private static class MakeProtectedFix extends InspectionGadgetsFix {
|
||||
public String getName() {
|
||||
return "Make 'protected'";
|
||||
}
|
||||
|
||||
public void applyFix(Project project, ProblemDescriptor descriptor) {
|
||||
final PsiElement constructorIdentifier = descriptor.getPsiElement();
|
||||
try {
|
||||
final PsiMethod constructor = (PsiMethod) constructorIdentifier.getParent();
|
||||
final PsiModifierList modifiers = constructor.getModifierList();
|
||||
modifiers.setModifierProperty(PsiModifier.PUBLIC, false);
|
||||
modifiers.setModifierProperty(PsiModifier.PRIVATE, false);
|
||||
modifiers.setModifierProperty(PsiModifier.PROTECTED, true);
|
||||
} catch (IncorrectOperationException e) {
|
||||
s_logger.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class NonProtectedConstructorInAbstractClassVisitor extends BaseInspectionVisitor {
|
||||
private NonProtectedConstructorInAbstractClassVisitor(BaseInspection inspection,
|
||||
InspectionManager inspectionManager, boolean isOnTheFly) {
|
||||
|
||||
+3
-2
@@ -6,6 +6,7 @@ import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.ExpressionInspection;
|
||||
import com.siyeh.ig.GroupNames;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class AssignmentToCatchBlockParameterInspection extends ExpressionInspection {
|
||||
|
||||
@@ -32,10 +33,10 @@ public class AssignmentToCatchBlockParameterInspection extends ExpressionInspect
|
||||
|
||||
public void visitAssignmentExpression(PsiAssignmentExpression expression) {
|
||||
super.visitAssignmentExpression(expression);
|
||||
final PsiExpression lhs = expression.getLExpression();
|
||||
if (lhs == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(expression)){
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = expression.getLExpression();
|
||||
if (!(lhs instanceof PsiReferenceExpression)) {
|
||||
return;
|
||||
}
|
||||
|
||||
+10
-6
@@ -2,11 +2,13 @@ package com.siyeh.ig.confusing;
|
||||
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.ExpressionInspection;
|
||||
import com.siyeh.ig.GroupNames;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class AssignmentToForLoopParameterInspection extends ExpressionInspection {
|
||||
|
||||
@@ -33,10 +35,10 @@ public class AssignmentToForLoopParameterInspection extends ExpressionInspection
|
||||
|
||||
public void visitAssignmentExpression(PsiAssignmentExpression expression) {
|
||||
super.visitAssignmentExpression(expression);
|
||||
final PsiExpression lhs = expression.getLExpression();
|
||||
if (lhs == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(expression)){
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = expression.getLExpression();
|
||||
checkForForLoopParam(lhs);
|
||||
checkForForeachLoopParam(lhs);
|
||||
}
|
||||
@@ -47,8 +49,9 @@ public class AssignmentToForLoopParameterInspection extends ExpressionInspection
|
||||
if (sign == null) {
|
||||
return;
|
||||
}
|
||||
if (!sign.getTokenType().equals(JavaTokenType.PLUSPLUS) &&
|
||||
!sign.getTokenType().equals(JavaTokenType.MINUSMINUS)) {
|
||||
final IElementType tokenType = sign.getTokenType();
|
||||
if (!tokenType.equals(JavaTokenType.PLUSPLUS) &&
|
||||
!tokenType.equals(JavaTokenType.MINUSMINUS)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression operand = expression.getOperand();
|
||||
@@ -64,8 +67,9 @@ public class AssignmentToForLoopParameterInspection extends ExpressionInspection
|
||||
if (sign == null) {
|
||||
return;
|
||||
}
|
||||
if (!sign.getTokenType().equals(JavaTokenType.PLUSPLUS) &&
|
||||
!sign.getTokenType().equals(JavaTokenType.MINUSMINUS)) {
|
||||
final IElementType tokenType = sign.getTokenType();
|
||||
if (!tokenType.equals(JavaTokenType.PLUSPLUS) &&
|
||||
!tokenType.equals(JavaTokenType.MINUSMINUS)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression operand = expression.getOperand();
|
||||
|
||||
+3
-2
@@ -7,6 +7,7 @@ import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.ExpressionInspection;
|
||||
import com.siyeh.ig.GroupNames;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class AssignmentToMethodParameterInspection extends ExpressionInspection {
|
||||
|
||||
@@ -33,10 +34,10 @@ public class AssignmentToMethodParameterInspection extends ExpressionInspection
|
||||
|
||||
public void visitAssignmentExpression(PsiAssignmentExpression expression) {
|
||||
super.visitAssignmentExpression(expression);
|
||||
final PsiExpression lhs = expression.getLExpression();
|
||||
if (lhs == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(expression)){
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = expression.getLExpression();
|
||||
checkForMethodParam(lhs);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.ExpressionInspection;
|
||||
import com.siyeh.ig.GroupNames;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class ChainedEqualityInspection extends ExpressionInspection {
|
||||
|
||||
@@ -33,11 +34,14 @@ public class ChainedEqualityInspection extends ExpressionInspection {
|
||||
|
||||
public void visitBinaryExpression(PsiBinaryExpression expression) {
|
||||
super.visitBinaryExpression(expression);
|
||||
if(!WellFormednessUtils.isWellFormed(expression)){
|
||||
return;
|
||||
}
|
||||
if (!isEqualityComparison(expression)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = expression.getLOperand();
|
||||
if (lhs == null || !(lhs instanceof PsiBinaryExpression)) {
|
||||
if (!(lhs instanceof PsiBinaryExpression)) {
|
||||
return;
|
||||
}
|
||||
if (!isEqualityComparison((PsiBinaryExpression) lhs)) {
|
||||
|
||||
+1
-3
@@ -3,7 +3,6 @@ package com.siyeh.ig.confusing;
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.psi.PsiAssignmentExpression;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiExpressionList;
|
||||
import com.intellij.psi.PsiExpressionStatement;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
@@ -35,8 +34,7 @@ public class NestedAssignmentInspection extends ExpressionInspection {
|
||||
|
||||
public void visitAssignmentExpression(PsiAssignmentExpression expression) {
|
||||
super.visitAssignmentExpression(expression);
|
||||
if (expression.getParent() instanceof PsiExpressionStatement ||
|
||||
expression.getParent() instanceof PsiExpressionList) {
|
||||
if (expression.getParent() instanceof PsiExpressionStatement) {
|
||||
return;
|
||||
}
|
||||
registerError(expression);
|
||||
|
||||
+3
-9
@@ -6,6 +6,7 @@ import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.GroupNames;
|
||||
import com.siyeh.ig.StatementInspection;
|
||||
import com.siyeh.ig.psiutils.SwitchUtils;
|
||||
import com.siyeh.ig.ui.SingleIntegerFieldOptionsPanel;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -62,19 +63,12 @@ public class SwitchStatementDensityInspection extends StatementInspection {
|
||||
}
|
||||
|
||||
private static double calculateDensity(PsiSwitchStatement statement) {
|
||||
int branches = 0;
|
||||
final PsiCodeBlock body = statement.getBody();
|
||||
final PsiStatement[] statements = body.getStatements();
|
||||
for (int i = 0; i < statements.length; i++) {
|
||||
final PsiStatement child = statements[i];
|
||||
if (child instanceof PsiSwitchLabelStatement) {
|
||||
branches++;
|
||||
}
|
||||
}
|
||||
final int numBranches = SwitchUtils.calculateBranchCount(statement);
|
||||
final StatementCountVisitor visitor = new StatementCountVisitor();
|
||||
body.accept(visitor);
|
||||
final int numStatements = visitor.getNumStatements();
|
||||
return (double) branches / (double) numStatements;
|
||||
return (double) numBranches / (double) numStatements;
|
||||
}
|
||||
|
||||
private static class StatementCountVisitor extends PsiRecursiveElementVisitor {
|
||||
|
||||
+6
-20
@@ -6,6 +6,7 @@ import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.GroupNames;
|
||||
import com.siyeh.ig.StatementInspection;
|
||||
import com.siyeh.ig.psiutils.SwitchUtils;
|
||||
import com.siyeh.ig.ui.SingleIntegerFieldOptionsPanel;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -33,17 +34,9 @@ public class SwitchStatementWithTooFewBranchesInspection extends StatementInspec
|
||||
}
|
||||
|
||||
protected String buildErrorString(PsiElement location) {
|
||||
int branches = 0;
|
||||
final PsiSwitchStatement statement = (PsiSwitchStatement) location.getParent();
|
||||
final PsiCodeBlock body = statement.getBody();
|
||||
final PsiStatement[] statements = body.getStatements();
|
||||
for (int i = 0; i < statements.length; i++) {
|
||||
final PsiStatement child = statements[i];
|
||||
if (child instanceof PsiSwitchLabelStatement) {
|
||||
branches++;
|
||||
}
|
||||
}
|
||||
return "'#ref' has too few branches (" + branches + "), and should probably be replaced by an 'if' statement #loc";
|
||||
final int numBranches = SwitchUtils.calculateBranchCount(statement);
|
||||
return "'#ref' has too few branches (" + numBranches + "), and should probably be replaced by an 'if' statement #loc";
|
||||
}
|
||||
|
||||
public BaseInspectionVisitor createVisitor(InspectionManager inspectionManager, boolean onTheFly) {
|
||||
@@ -56,23 +49,16 @@ public class SwitchStatementWithTooFewBranchesInspection extends StatementInspec
|
||||
}
|
||||
|
||||
public void visitSwitchStatement(PsiSwitchStatement statement) {
|
||||
int branches = 0;
|
||||
final PsiCodeBlock body = statement.getBody();
|
||||
if (body == null) {
|
||||
return;
|
||||
}
|
||||
final PsiStatement[] statements = body.getStatements();
|
||||
for (int i = 0; i < statements.length; i++) {
|
||||
final PsiStatement child = statements[i];
|
||||
if (child instanceof PsiSwitchLabelStatement) {
|
||||
branches++;
|
||||
}
|
||||
}
|
||||
if (branches >= getLimit()) {
|
||||
final int numBranches = SwitchUtils.calculateBranchCount(statement);
|
||||
if (numBranches >= getLimit()) {
|
||||
return;
|
||||
}
|
||||
registerStatementError(statement);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-19
@@ -6,6 +6,7 @@ import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.GroupNames;
|
||||
import com.siyeh.ig.StatementInspection;
|
||||
import com.siyeh.ig.psiutils.SwitchUtils;
|
||||
import com.siyeh.ig.ui.SingleIntegerFieldOptionsPanel;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -33,17 +34,9 @@ public class SwitchStatementWithTooManyBranchesInspection extends StatementInspe
|
||||
}
|
||||
|
||||
protected String buildErrorString(PsiElement location) {
|
||||
int branches = 0;
|
||||
final PsiSwitchStatement statement = (PsiSwitchStatement) location.getParent();
|
||||
final PsiCodeBlock body = statement.getBody();
|
||||
final PsiStatement[] statements = body.getStatements();
|
||||
for (int i = 0; i < statements.length; i++) {
|
||||
final PsiStatement child = statements[i];
|
||||
if (child instanceof PsiSwitchLabelStatement) {
|
||||
branches++;
|
||||
}
|
||||
}
|
||||
return "'#ref' has too many branches (" + branches + ") #loc";
|
||||
final int numBranches = SwitchUtils.calculateBranchCount(statement);
|
||||
return "'#ref' has too many branches (" + numBranches + ") #loc";
|
||||
}
|
||||
|
||||
public BaseInspectionVisitor createVisitor(InspectionManager inspectionManager, boolean onTheFly) {
|
||||
@@ -56,19 +49,12 @@ public class SwitchStatementWithTooManyBranchesInspection extends StatementInspe
|
||||
}
|
||||
|
||||
public void visitSwitchStatement(PsiSwitchStatement statement) {
|
||||
int branches = 0;
|
||||
final PsiCodeBlock body = statement.getBody();
|
||||
if (body == null) {
|
||||
return;
|
||||
}
|
||||
final PsiStatement[] statements = body.getStatements();
|
||||
for (int i = 0; i < statements.length; i++) {
|
||||
final PsiStatement child = statements[i];
|
||||
if (child instanceof PsiSwitchLabelStatement) {
|
||||
branches++;
|
||||
}
|
||||
}
|
||||
if (branches <= getLimit()) {
|
||||
final int numBranches = SwitchUtils.calculateBranchCount(statement);
|
||||
if (numBranches <= getLimit()) {
|
||||
return;
|
||||
}
|
||||
registerStatementError(statement);
|
||||
|
||||
+36
-27
@@ -2,23 +2,26 @@ package com.siyeh.ig.encapsulation;
|
||||
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.ExpressionInspection;
|
||||
import com.siyeh.ig.GroupNames;
|
||||
import com.siyeh.ig.psiutils.CollectionUtils;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class AssignmentToCollectionFieldFromParameterInspection extends ExpressionInspection {
|
||||
public class AssignmentToCollectionFieldFromParameterInspection
|
||||
extends ExpressionInspection{
|
||||
|
||||
public String getDisplayName() {
|
||||
public String getDisplayName(){
|
||||
return "Assignment to Collection or array field from parameter";
|
||||
}
|
||||
|
||||
public String getGroupDisplayName() {
|
||||
public String getGroupDisplayName(){
|
||||
return GroupNames.ENCAPSULATION_GROUP_NAME;
|
||||
}
|
||||
|
||||
public String buildErrorString(PsiElement location) {
|
||||
public String buildErrorString(PsiElement location){
|
||||
final PsiAssignmentExpression assignment = (PsiAssignmentExpression) location.getParent();
|
||||
final PsiExpression lhs = assignment.getLExpression();
|
||||
final PsiExpression rhs = assignment.getRExpression();
|
||||
@@ -26,51 +29,57 @@ public class AssignmentToCollectionFieldFromParameterInspection extends Expressi
|
||||
|
||||
final PsiField field = (PsiField) element;
|
||||
final PsiType type = field.getType();
|
||||
if (type.getArrayDimensions() > 0) {
|
||||
return "assignment to array field #ref from parameter " + rhs.getText() + "#loc";
|
||||
} else {
|
||||
return "assignment to Collection field #ref from parameter " + rhs.getText() + "#loc";
|
||||
if(type.getArrayDimensions() > 0){
|
||||
return "assignment to array field #ref from parameter " +
|
||||
rhs.getText() +
|
||||
"#loc";
|
||||
} else{
|
||||
return "assignment to Collection field #ref from parameter " +
|
||||
rhs.getText() +
|
||||
"#loc";
|
||||
}
|
||||
}
|
||||
|
||||
public BaseInspectionVisitor createVisitor(InspectionManager inspectionManager, boolean onTheFly) {
|
||||
return new AssignmentToCollectionFieldFromParameterVisitor(this, inspectionManager, onTheFly);
|
||||
public BaseInspectionVisitor createVisitor(
|
||||
InspectionManager inspectionManager, boolean onTheFly){
|
||||
return new AssignmentToCollectionFieldFromParameterVisitor(this,
|
||||
inspectionManager,
|
||||
onTheFly);
|
||||
}
|
||||
|
||||
private static class AssignmentToCollectionFieldFromParameterVisitor extends BaseInspectionVisitor {
|
||||
private AssignmentToCollectionFieldFromParameterVisitor(BaseInspection inspection,
|
||||
InspectionManager inspectionManager, boolean isOnTheFly) {
|
||||
private static class AssignmentToCollectionFieldFromParameterVisitor
|
||||
extends BaseInspectionVisitor{
|
||||
private AssignmentToCollectionFieldFromParameterVisitor(
|
||||
BaseInspection inspection,
|
||||
InspectionManager inspectionManager, boolean isOnTheFly){
|
||||
super(inspection, inspectionManager, isOnTheFly);
|
||||
}
|
||||
|
||||
public void visitAssignmentExpression(PsiAssignmentExpression expression) {
|
||||
public void visitAssignmentExpression(
|
||||
PsiAssignmentExpression expression){
|
||||
super.visitAssignmentExpression(expression);
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
if (sign == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(expression)){
|
||||
return;
|
||||
}
|
||||
if (!(sign.getTokenType() == JavaTokenType.EQ)) {
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
|
||||
final IElementType tokenType = sign.getTokenType();
|
||||
if(!tokenType.equals(JavaTokenType.EQ)){
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = expression.getLExpression();
|
||||
if (lhs == null) {
|
||||
return;
|
||||
}
|
||||
if (!CollectionUtils.isArrayOrCollectionField(lhs)) {
|
||||
if(!CollectionUtils.isArrayOrCollectionField(lhs)){
|
||||
return;
|
||||
}
|
||||
final PsiExpression rhs = expression.getRExpression();
|
||||
if (rhs == null) {
|
||||
return;
|
||||
}
|
||||
if (!(rhs instanceof PsiReferenceExpression)) {
|
||||
if(!(rhs instanceof PsiReferenceExpression)){
|
||||
return;
|
||||
}
|
||||
final PsiElement element = ((PsiReference) rhs).resolve();
|
||||
if (!(element instanceof PsiParameter)) {
|
||||
if(!(element instanceof PsiParameter)){
|
||||
return;
|
||||
}
|
||||
if (!(element.getParent() instanceof PsiParameterList)) {
|
||||
if(!(element.getParent() instanceof PsiParameterList)){
|
||||
return;
|
||||
}
|
||||
registerError(lhs);
|
||||
|
||||
+3
-8
@@ -7,6 +7,7 @@ import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.ExpressionInspection;
|
||||
import com.siyeh.ig.GroupNames;
|
||||
import com.siyeh.ig.psiutils.TypeUtils;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class AssignmentToDateFieldFromParameterInspection extends ExpressionInspection {
|
||||
|
||||
@@ -41,25 +42,19 @@ public class AssignmentToDateFieldFromParameterInspection extends ExpressionInsp
|
||||
|
||||
public void visitAssignmentExpression(PsiAssignmentExpression expression) {
|
||||
super.visitAssignmentExpression(expression);
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
if (sign == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(expression)){
|
||||
return;
|
||||
}
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
if (!sign.getTokenType().equals(JavaTokenType.EQ)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = expression.getLExpression();
|
||||
if (lhs == null) {
|
||||
return;
|
||||
}
|
||||
if (!TypeUtils.expressionHasTypeOrSubtype("java.util.Date", lhs)
|
||||
&& !TypeUtils.expressionHasTypeOrSubtype("java.util.Calendar", lhs)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression rhs = expression.getRExpression();
|
||||
if (rhs == null) {
|
||||
return;
|
||||
}
|
||||
if (!(rhs instanceof PsiReferenceExpression)) {
|
||||
return;
|
||||
}
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ import com.intellij.psi.PsiModifier;
|
||||
import com.siyeh.ig.*;
|
||||
import com.siyeh.ig.fixes.EncapsulateVariableFix;
|
||||
|
||||
public class PackageVisibleFieldInspection extends ClassInspection {
|
||||
public class PackageVisibleFieldInspection extends FieldInspection {
|
||||
private final EncapsulateVariableFix fix = new EncapsulateVariableFix();
|
||||
|
||||
public String getDisplayName() {
|
||||
|
||||
+4
-3
@@ -5,6 +5,7 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.siyeh.ig.*;
|
||||
import com.siyeh.ig.psiutils.ClassUtils;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class ThisEscapedInConstructorInspection extends ClassInspection {
|
||||
|
||||
@@ -79,14 +80,14 @@ public class ThisEscapedInConstructorInspection extends ClassInspection {
|
||||
}
|
||||
|
||||
public void visitAssignmentExpression(PsiAssignmentExpression assignment) {
|
||||
|
||||
super.visitAssignmentExpression(assignment);
|
||||
|
||||
if(!WellFormednessUtils.isWellFormed(assignment)){
|
||||
return;
|
||||
}
|
||||
final boolean isInInitialization = checkForInitialization(assignment);
|
||||
if (!isInInitialization) {
|
||||
return;
|
||||
}
|
||||
|
||||
final PsiExpression psiExpression = getLastRightExpression(assignment);
|
||||
|
||||
if (psiExpression == null ||
|
||||
|
||||
+13
-24
@@ -1,30 +1,19 @@
|
||||
package com.siyeh.ig.internationalization;
|
||||
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.PsiBinaryExpression;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.ExpressionInspection;
|
||||
import com.siyeh.ig.GroupNames;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import com.siyeh.ig.psiutils.ComparisonUtils;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class CharacterComparisonInspection extends ExpressionInspection {
|
||||
|
||||
private static final Set s_comparisonOperators = new HashSet(4);
|
||||
|
||||
static {
|
||||
s_comparisonOperators.add(">");
|
||||
s_comparisonOperators.add("<");
|
||||
s_comparisonOperators.add(">=");
|
||||
s_comparisonOperators.add("<=");
|
||||
}
|
||||
|
||||
private static boolean isComparison(String operator) {
|
||||
return s_comparisonOperators.contains(operator);
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return "Character comparison";
|
||||
}
|
||||
@@ -48,22 +37,22 @@ public class CharacterComparisonInspection extends ExpressionInspection {
|
||||
|
||||
public void visitBinaryExpression(PsiBinaryExpression expression) {
|
||||
super.visitBinaryExpression(expression);
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
if (sign == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(expression)){
|
||||
return;
|
||||
}
|
||||
final String operand = sign.getText();
|
||||
if (!isComparison(operand)) {
|
||||
if(!ComparisonUtils.isComparison(expression)){
|
||||
return;
|
||||
}
|
||||
if(ComparisonUtils.isEqualityComparison(expression)){
|
||||
return;
|
||||
}
|
||||
|
||||
final PsiExpression lhs = expression.getLOperand();
|
||||
if (!isCharacter(lhs)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression rhs = expression.getROperand();
|
||||
if (rhs == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final PsiType rhsType = rhs.getType();
|
||||
if (rhsType == null) {
|
||||
return;
|
||||
|
||||
+8
-9
@@ -7,6 +7,7 @@ import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.ExpressionInspection;
|
||||
import com.siyeh.ig.GroupNames;
|
||||
import com.siyeh.ig.psiutils.TypeUtils;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class StringConcatenationInspection extends ExpressionInspection {
|
||||
|
||||
@@ -33,17 +34,17 @@ public class StringConcatenationInspection extends ExpressionInspection {
|
||||
|
||||
public void visitBinaryExpression(PsiBinaryExpression expression) {
|
||||
super.visitBinaryExpression(expression);
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
if (sign == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(expression))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!(sign.getTokenType() == JavaTokenType.PLUS)) {
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
|
||||
if (!sign.getTokenType().equals(JavaTokenType.PLUS)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = expression.getLOperand();
|
||||
if (lhs == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final PsiType lhsType = lhs.getType();
|
||||
if (TypeUtils.isJavaLangString(lhsType)) {
|
||||
registerError(sign);
|
||||
@@ -51,9 +52,7 @@ public class StringConcatenationInspection extends ExpressionInspection {
|
||||
}
|
||||
|
||||
final PsiExpression rhs = expression.getROperand();
|
||||
if (rhs == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final PsiType rhsType = rhs.getType();
|
||||
if (TypeUtils.isJavaLangString(rhsType)) {
|
||||
registerError(sign);
|
||||
|
||||
@@ -12,7 +12,6 @@ import javax.swing.event.DocumentEvent;
|
||||
import javax.swing.event.DocumentListener;
|
||||
import javax.swing.text.Document;
|
||||
import java.awt.*;
|
||||
import java.text.NumberFormat;
|
||||
|
||||
public class ClassWithoutLoggerInspection extends ClassInspection {
|
||||
|
||||
@@ -34,11 +33,6 @@ public class ClassWithoutLoggerInspection extends ClassInspection {
|
||||
final JLabel classNameLabel = new JLabel("Logger class name:");
|
||||
classNameLabel.setHorizontalAlignment(SwingConstants.TRAILING);
|
||||
|
||||
final NumberFormat numberFormat = NumberFormat.getIntegerInstance();
|
||||
numberFormat.setParseIntegerOnly(true);
|
||||
numberFormat.setMinimumIntegerDigits(1);
|
||||
numberFormat.setMaximumIntegerDigits(2);
|
||||
|
||||
final JTextField loggerClassNameField = new JTextField();
|
||||
final Font panelFont = panel.getFont();
|
||||
loggerClassNameField.setFont(panelFont);
|
||||
|
||||
@@ -31,11 +31,6 @@ public class NonStaticFinalLoggerInspection extends ClassInspection {
|
||||
final JLabel classNameLabel = new JLabel("Logger class name:");
|
||||
classNameLabel.setHorizontalAlignment(SwingConstants.TRAILING);
|
||||
|
||||
final NumberFormat numberFormat = NumberFormat.getIntegerInstance();
|
||||
numberFormat.setParseIntegerOnly(true);
|
||||
numberFormat.setMinimumIntegerDigits(1);
|
||||
numberFormat.setMaximumIntegerDigits(2);
|
||||
|
||||
final JTextField loggerClassNameField = new JTextField();
|
||||
final Font panelFont = panel.getFont();
|
||||
loggerClassNameField.setFont(panelFont);
|
||||
|
||||
+31
-27
@@ -10,71 +10,75 @@ import com.siyeh.ig.VariableInspection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class ObsoleteCollectionInspection extends VariableInspection {
|
||||
public class ObsoleteCollectionInspection extends VariableInspection{
|
||||
|
||||
private static final Set s_obsoleteCollectionTypes = new HashSet(2);
|
||||
|
||||
static {
|
||||
static{
|
||||
s_obsoleteCollectionTypes.add("java.util.Vector");
|
||||
s_obsoleteCollectionTypes.add("java.util.Hashtable");
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
public String getDisplayName(){
|
||||
return "Use of obsolete collection type";
|
||||
}
|
||||
|
||||
public String getGroupDisplayName() {
|
||||
public String getGroupDisplayName(){
|
||||
return GroupNames.MATURITY_GROUP_NAME;
|
||||
}
|
||||
|
||||
public String buildErrorString(PsiElement location) {
|
||||
public String buildErrorString(PsiElement location){
|
||||
return "Obsolete collection type #ref used #loc";
|
||||
}
|
||||
|
||||
public BaseInspectionVisitor createVisitor(InspectionManager inspectionManager, boolean onTheFly) {
|
||||
public BaseInspectionVisitor createVisitor(
|
||||
InspectionManager inspectionManager, boolean onTheFly){
|
||||
return new ObsoleteCollectionVisitor(this, inspectionManager, onTheFly);
|
||||
}
|
||||
|
||||
private static class ObsoleteCollectionVisitor extends BaseInspectionVisitor {
|
||||
private ObsoleteCollectionVisitor(BaseInspection inspection, InspectionManager inspectionManager, boolean isOnTheFly) {
|
||||
private static class ObsoleteCollectionVisitor
|
||||
extends BaseInspectionVisitor{
|
||||
private ObsoleteCollectionVisitor(BaseInspection inspection,
|
||||
InspectionManager inspectionManager,
|
||||
boolean isOnTheFly){
|
||||
super(inspection, inspectionManager, isOnTheFly);
|
||||
}
|
||||
|
||||
public void visitVariable(PsiVariable variable) {
|
||||
public void visitVariable(PsiVariable variable){
|
||||
super.visitVariable(variable);
|
||||
final PsiType type = variable.getType();
|
||||
if (type == null) {
|
||||
return;
|
||||
}
|
||||
String typeName = type.getCanonicalText();
|
||||
if (typeName.indexOf('<') > 0) {
|
||||
typeName = typeName.substring(0, typeName.indexOf('<'));
|
||||
}
|
||||
if (!s_obsoleteCollectionTypes.contains(typeName)) {
|
||||
if(!isObsoleteCollectionType(type)){
|
||||
return;
|
||||
}
|
||||
final PsiTypeElement typeElement = variable.getTypeElement();
|
||||
registerError(typeElement);
|
||||
}
|
||||
|
||||
public void visitNewExpression(PsiNewExpression newExpression) {
|
||||
public void visitNewExpression(PsiNewExpression newExpression){
|
||||
super.visitNewExpression(newExpression);
|
||||
final PsiType type = newExpression.getType();
|
||||
if (type == null) {
|
||||
return;
|
||||
}
|
||||
String typeName = type.getCanonicalText();
|
||||
if(typeName.indexOf('<')>0)
|
||||
{
|
||||
typeName = typeName.substring(0, typeName.indexOf('<'));
|
||||
}
|
||||
if (!s_obsoleteCollectionTypes.contains(typeName)) {
|
||||
if(!isObsoleteCollectionType(type)){
|
||||
return;
|
||||
}
|
||||
final PsiJavaCodeReferenceElement classNameElement = newExpression.getClassReference();
|
||||
registerError(classNameElement);
|
||||
}
|
||||
|
||||
private static boolean isObsoleteCollectionType(PsiType type){
|
||||
if(type == null){
|
||||
return false;
|
||||
}
|
||||
|
||||
String typeName = type.getCanonicalText();
|
||||
if(typeName.indexOf('<') > 0){
|
||||
typeName = typeName.substring(0, typeName.indexOf('<'));
|
||||
}
|
||||
if(!s_obsoleteCollectionTypes.contains(typeName)){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.siyeh.ig.methodmetrics;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
|
||||
class NegationCountVisitor extends PsiRecursiveElementVisitor {
|
||||
private int m_count = 0;
|
||||
@@ -22,7 +23,8 @@ class NegationCountVisitor extends PsiRecursiveElementVisitor {
|
||||
if (sign == null) {
|
||||
return;
|
||||
}
|
||||
if (!(sign.getTokenType() != JavaTokenType.NE)) {
|
||||
final IElementType tokenType = sign.getTokenType();
|
||||
if (tokenType.equals(JavaTokenType.NE)) {
|
||||
m_count++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,10 +59,6 @@ public class ClassNamingConventionInspection extends ConventionInspection {
|
||||
if (!aClass.isPhysical()) {
|
||||
return super.checkClass(aClass, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkClass(aClass, mgr, isOnTheFly);
|
||||
}
|
||||
|
||||
final BaseInspectionVisitor visitor = createVisitor(mgr, isOnTheFly);
|
||||
aClass.accept(visitor);
|
||||
return visitor.getErrors();
|
||||
|
||||
+1
-3
@@ -63,9 +63,7 @@ public class ConstantNamingConventionInspection extends ConventionInspection {
|
||||
if (!containingClass.isPhysical()) {
|
||||
return super.checkField(field, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkField(field, mgr, isOnTheFly);
|
||||
}
|
||||
|
||||
final BaseInspectionVisitor visitor = createVisitor(mgr, isOnTheFly);
|
||||
field.accept(visitor);
|
||||
return visitor.getErrors();
|
||||
|
||||
@@ -26,9 +26,6 @@ public class DollarSignInNameInspection extends BaseInspection {
|
||||
}
|
||||
|
||||
public ProblemDescriptor[] checkClass(PsiClass aClass, InspectionManager mgr, boolean isOnTheFly) {
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkClass(aClass, mgr, isOnTheFly);
|
||||
}
|
||||
if (aClass instanceof PsiAnonymousClass) {
|
||||
return super.checkClass(aClass, mgr, isOnTheFly);
|
||||
}
|
||||
@@ -45,10 +42,6 @@ public class DollarSignInNameInspection extends BaseInspection {
|
||||
if (!containingClass.isPhysical()) {
|
||||
return super.checkMethod(method, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkMethod(method, mgr, isOnTheFly);
|
||||
}
|
||||
|
||||
if (containingClass instanceof PsiAnonymousClass) {
|
||||
return super.checkClass(containingClass, mgr, isOnTheFly);
|
||||
}
|
||||
@@ -68,9 +61,6 @@ public class DollarSignInNameInspection extends BaseInspection {
|
||||
if (containingClass instanceof PsiAnonymousClass) {
|
||||
return super.checkClass(containingClass, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkField(field, mgr, isOnTheFly);
|
||||
}
|
||||
final BaseInspectionVisitor visitor = createVisitor(mgr, isOnTheFly);
|
||||
field.accept(visitor);
|
||||
return visitor.getErrors();
|
||||
|
||||
-3
@@ -64,9 +64,6 @@ public class InstanceMethodNamingConventionInspection extends ConventionInspecti
|
||||
if (!containingClass.isPhysical()) {
|
||||
return super.checkMethod(method, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkMethod(method, mgr, isOnTheFly);
|
||||
}
|
||||
final BaseInspectionVisitor visitor = createVisitor(mgr, isOnTheFly);
|
||||
method.accept(visitor);
|
||||
return visitor.getErrors();
|
||||
|
||||
-3
@@ -62,9 +62,6 @@ public class InstanceVariableNamingConventionInspection extends ConventionInspec
|
||||
if (!containingClass.isPhysical()) {
|
||||
return super.checkField(field, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkField(field, mgr, isOnTheFly);
|
||||
}
|
||||
final BaseInspectionVisitor visitor = createVisitor(mgr, isOnTheFly);
|
||||
field.accept(visitor);
|
||||
return visitor.getErrors();
|
||||
|
||||
-3
@@ -60,9 +60,6 @@ public class InterfaceNamingConventionInspection extends ConventionInspection {
|
||||
if (!aClass.isPhysical()) {
|
||||
return super.checkClass(aClass, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkClass(aClass, mgr, isOnTheFly);
|
||||
}
|
||||
final BaseInspectionVisitor visitor = createVisitor(mgr, isOnTheFly);
|
||||
aClass.accept(visitor);
|
||||
|
||||
|
||||
-3
@@ -77,9 +77,6 @@ public class LocalVariableNamingConventionInspection extends ConventionInspectio
|
||||
if (!containingClass.isPhysical()) {
|
||||
return super.checkMethod(method, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkMethod(method, mgr, isOnTheFly);
|
||||
}
|
||||
final BaseInspectionVisitor visitor = createVisitor(mgr, isOnTheFly);
|
||||
method.accept(visitor);
|
||||
return visitor.getErrors();
|
||||
|
||||
-3
@@ -63,9 +63,6 @@ public class ParameterNamingConventionInspection extends ConventionInspection {
|
||||
if (!containingClass.isPhysical()) {
|
||||
return super.checkMethod(method, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkMethod(method, mgr, isOnTheFly);
|
||||
}
|
||||
final BaseInspectionVisitor visitor = createVisitor(mgr, isOnTheFly);
|
||||
method.accept(visitor);
|
||||
return visitor.getErrors();
|
||||
|
||||
-3
@@ -62,9 +62,6 @@ public class StaticMethodNamingConventionInspection extends ConventionInspection
|
||||
if (!containingClass.isPhysical()) {
|
||||
return super.checkMethod(method, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkMethod(method, mgr, isOnTheFly);
|
||||
}
|
||||
final BaseInspectionVisitor visitor = createVisitor(mgr, isOnTheFly);
|
||||
method.accept(visitor);
|
||||
return visitor.getErrors();
|
||||
|
||||
-3
@@ -63,9 +63,6 @@ public class StaticVariableNamingConventionInspection extends ConventionInspecti
|
||||
if (!containingClass.isPhysical()) {
|
||||
return super.checkField(field, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkField(field, mgr, isOnTheFly);
|
||||
}
|
||||
final BaseInspectionVisitor visitor = createVisitor(mgr, isOnTheFly);
|
||||
field.accept(visitor);
|
||||
return visitor.getErrors();
|
||||
|
||||
+19
-8
@@ -4,8 +4,10 @@ import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.siyeh.ig.*;
|
||||
import com.siyeh.ig.psiutils.ClassUtils;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class MultiplyOrDivideByPowerOfTwoInspection extends ExpressionInspection {
|
||||
private final MultiplyByPowerOfTwoFix fix = new MultiplyByPowerOfTwoFix();
|
||||
@@ -33,7 +35,8 @@ public class MultiplyOrDivideByPowerOfTwoInspection extends ExpressionInspection
|
||||
final PsiJavaToken sign = exp.getOperationSign();
|
||||
lhs = exp.getLExpression();
|
||||
rhs = exp.getRExpression();
|
||||
if (sign.getTokenType().equals(JavaTokenType.ASTERISKEQ)) {
|
||||
final IElementType tokenType = sign.getTokenType();
|
||||
if (tokenType.equals(JavaTokenType.ASTERISKEQ)) {
|
||||
operator = "<<=";
|
||||
} else {
|
||||
operator = ">>=";
|
||||
@@ -43,7 +46,8 @@ public class MultiplyOrDivideByPowerOfTwoInspection extends ExpressionInspection
|
||||
final PsiJavaToken sign = exp.getOperationSign();
|
||||
lhs = exp.getLOperand();
|
||||
rhs = exp.getROperand();
|
||||
if (!(sign.getTokenType() != JavaTokenType.ASTERISK)) {
|
||||
final IElementType tokenType = sign.getTokenType();
|
||||
if (tokenType.equals(JavaTokenType.ASTERISK)) {
|
||||
operator = "<<";
|
||||
} else {
|
||||
operator = ">>";
|
||||
@@ -80,12 +84,15 @@ public class MultiplyOrDivideByPowerOfTwoInspection extends ExpressionInspection
|
||||
|
||||
public void visitBinaryExpression(PsiBinaryExpression expression) {
|
||||
super.visitBinaryExpression(expression);
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
if (sign == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(expression))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!sign.getTokenType().equals(JavaTokenType.ASTERISK) &&
|
||||
!sign.getTokenType().equals(JavaTokenType.DIV)) {
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
|
||||
final IElementType tokenType = sign.getTokenType();
|
||||
if (!tokenType.equals(JavaTokenType.ASTERISK) &&
|
||||
!tokenType.equals(JavaTokenType.DIV)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression rhs = expression.getROperand();
|
||||
@@ -104,9 +111,13 @@ public class MultiplyOrDivideByPowerOfTwoInspection extends ExpressionInspection
|
||||
|
||||
public void visitAssignmentExpression(PsiAssignmentExpression expression) {
|
||||
super.visitAssignmentExpression(expression);
|
||||
if(!WellFormednessUtils.isWellFormed(expression)){
|
||||
return;
|
||||
}
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
if (!sign.getTokenType().equals(JavaTokenType.ASTERISKEQ) &&
|
||||
!sign.getTokenType().equals(JavaTokenType.DIVEQ)) {
|
||||
final IElementType tokenType = sign.getTokenType();
|
||||
if (!tokenType.equals(JavaTokenType.ASTERISKEQ) &&
|
||||
!tokenType.equals(JavaTokenType.DIVEQ)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression rhs = expression.getRExpression();
|
||||
|
||||
+14
-10
@@ -2,12 +2,14 @@ package com.siyeh.ig.performance;
|
||||
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.ExpressionInspection;
|
||||
import com.siyeh.ig.GroupNames;
|
||||
import com.siyeh.ig.psiutils.ControlFlowUtils;
|
||||
import com.siyeh.ig.psiutils.TypeUtils;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
import com.siyeh.ig.ui.SingleCheckboxOptionsPanel;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -47,11 +49,14 @@ public class StringConcatenationInLoopsInspection extends ExpressionInspection {
|
||||
|
||||
public void visitBinaryExpression(PsiBinaryExpression expression) {
|
||||
super.visitBinaryExpression(expression);
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
if (sign == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(expression))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!sign.getTokenType().equals(JavaTokenType.PLUS)) {
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
|
||||
final IElementType tokenType = sign.getTokenType();
|
||||
if (!tokenType.equals(JavaTokenType.PLUS)) {
|
||||
return;
|
||||
}
|
||||
final PsiType type = expression.getType();
|
||||
@@ -79,18 +84,17 @@ public class StringConcatenationInLoopsInspection extends ExpressionInspection {
|
||||
|
||||
public void visitAssignmentExpression(PsiAssignmentExpression expression) {
|
||||
super.visitAssignmentExpression(expression);
|
||||
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
if (sign == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(expression)){
|
||||
return;
|
||||
}
|
||||
if (!sign.getTokenType().equals(JavaTokenType.PLUSEQ)) {
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
|
||||
final IElementType tokenType = sign.getTokenType();
|
||||
if (!tokenType.equals(JavaTokenType.PLUSEQ)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = expression.getLExpression();
|
||||
if (lhs == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final PsiType type = lhs.getType();
|
||||
if (type == null) {
|
||||
return;
|
||||
|
||||
@@ -2,162 +2,168 @@ package com.siyeh.ig.psiutils;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
|
||||
public class ExpectedTypeUtils {
|
||||
public class ExpectedTypeUtils{
|
||||
|
||||
private ExpectedTypeUtils() {
|
||||
private ExpectedTypeUtils(){
|
||||
super();
|
||||
}
|
||||
|
||||
public static PsiType findExpectedType(PsiExpression exp) {
|
||||
public static PsiType findExpectedType(PsiExpression exp){
|
||||
PsiElement context = exp.getParent();
|
||||
PsiExpression wrappedExp = exp;
|
||||
final PsiManager manager = exp.getManager();
|
||||
while (context instanceof PsiParenthesizedExpression) {
|
||||
while(context instanceof PsiParenthesizedExpression){
|
||||
wrappedExp = (PsiExpression) context;
|
||||
context = context.getParent();
|
||||
}
|
||||
if (context instanceof PsiVariable) {
|
||||
if(context instanceof PsiVariable){
|
||||
final PsiVariable psiVariable = (PsiVariable) context;
|
||||
return psiVariable.getType();
|
||||
} else if (context instanceof PsiReferenceExpression) {
|
||||
} else if(context instanceof PsiReferenceExpression){
|
||||
final PsiReferenceExpression ref = (PsiReferenceExpression) context;
|
||||
final PsiElement parent = ref.getParent();
|
||||
if (parent instanceof PsiMethodCallExpression) {
|
||||
if(parent instanceof PsiMethodCallExpression){
|
||||
final PsiMethod psiMethod = ((PsiMethodCallExpression) parent).resolveMethod();
|
||||
if (psiMethod == null) {
|
||||
if(psiMethod == null){
|
||||
return null;
|
||||
}
|
||||
final PsiClass aClass = psiMethod.getContainingClass();
|
||||
final PsiElementFactory factory = manager.getElementFactory();
|
||||
return factory.createType(aClass);
|
||||
} else if (parent instanceof PsiReferenceExpression) {
|
||||
} else if(parent instanceof PsiReferenceExpression){
|
||||
final PsiElement elt = ((PsiReferenceExpression) parent).resolve();
|
||||
if (elt instanceof PsiField) {
|
||||
if(elt instanceof PsiField){
|
||||
final PsiClass aClass = ((PsiField) elt).getContainingClass();
|
||||
final PsiElementFactory factory = manager.getElementFactory();
|
||||
return factory.createType(aClass);
|
||||
} else {
|
||||
} else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (context instanceof PsiArrayInitializerExpression) {
|
||||
} else if(context instanceof PsiArrayInitializerExpression){
|
||||
final PsiArrayInitializerExpression initializer = (PsiArrayInitializerExpression) context;
|
||||
final PsiArrayType arrayType = (PsiArrayType) initializer.getType();
|
||||
if(arrayType!=null)
|
||||
{
|
||||
if(arrayType != null){
|
||||
return arrayType.getComponentType();
|
||||
}
|
||||
}else if (context instanceof PsiArrayAccessExpression) {
|
||||
} else if(context instanceof PsiArrayAccessExpression){
|
||||
final PsiArrayAccessExpression accessExpression = (PsiArrayAccessExpression) context;
|
||||
if(accessExpression.getIndexExpression().equals(wrappedExp))
|
||||
{
|
||||
if(accessExpression.getIndexExpression().equals(wrappedExp)){
|
||||
return PsiType.INT;
|
||||
}
|
||||
} else if (context instanceof PsiAssignmentExpression) {
|
||||
} else if(context instanceof PsiAssignmentExpression){
|
||||
final PsiAssignmentExpression assignment = (PsiAssignmentExpression) context;
|
||||
final PsiExpression rExpression = assignment.getRExpression();
|
||||
if (rExpression != null) {
|
||||
if (rExpression.equals(wrappedExp)) {
|
||||
if(rExpression != null){
|
||||
if(rExpression.equals(wrappedExp)){
|
||||
final PsiExpression lExpression = assignment.getLExpression();
|
||||
PsiType lType = lExpression.getType();
|
||||
if (lType == null) return null;
|
||||
if(lType == null){
|
||||
return null;
|
||||
}
|
||||
// e.g. String += any type
|
||||
if (TypeUtils.isJavaLangString(lType) && JavaTokenType.PLUSEQ.equals(assignment.getOperationSign().getTokenType())) {
|
||||
return rExpression.getType();
|
||||
if(TypeUtils.isJavaLangString(lType) &&
|
||||
JavaTokenType.PLUSEQ.equals(
|
||||
assignment.getOperationSign().getTokenType())){
|
||||
return rExpression.getType();
|
||||
}
|
||||
return lType;
|
||||
}
|
||||
}
|
||||
} else if (context instanceof PsiDeclarationStatement) {
|
||||
} else if(context instanceof PsiDeclarationStatement){
|
||||
final PsiDeclarationStatement assignment = (PsiDeclarationStatement) context;
|
||||
final PsiElement[] declaredElements = assignment.getDeclaredElements();
|
||||
for (int i = 0; i < declaredElements.length; i++) {
|
||||
if (declaredElements[i] instanceof PsiVariable) {
|
||||
for(int i = 0; i < declaredElements.length; i++){
|
||||
if(declaredElements[i] instanceof PsiVariable){
|
||||
final PsiVariable declaredElement = (PsiVariable) declaredElements[i];
|
||||
final PsiExpression initializer = declaredElement.getInitializer();
|
||||
if (wrappedExp.equals(initializer)) {
|
||||
if(wrappedExp.equals(initializer)){
|
||||
return declaredElement.getType();
|
||||
}
|
||||
}
|
||||
}
|
||||
}else if (context instanceof PsiField) {
|
||||
} else if(context instanceof PsiField){
|
||||
final PsiField field = (PsiField) context;
|
||||
final PsiExpression initializer = field.getInitializer();
|
||||
if (wrappedExp.equals(initializer)) {
|
||||
if(wrappedExp.equals(initializer)){
|
||||
return field.getType();
|
||||
}
|
||||
} else if (context instanceof PsiBinaryExpression) {
|
||||
} else if(context instanceof PsiBinaryExpression){
|
||||
final PsiBinaryExpression binaryExp = (PsiBinaryExpression) context;
|
||||
final PsiJavaToken sign = binaryExp.getOperationSign();
|
||||
final IElementType tokenType = sign.getTokenType();
|
||||
final PsiType type = binaryExp.getType();
|
||||
if (TypeUtils.isJavaLangString(type)) {
|
||||
if(TypeUtils.isJavaLangString(type)){
|
||||
return null;
|
||||
}
|
||||
if (isArithmeticOperation(tokenType)) {
|
||||
if(isArithmeticOperation(tokenType)){
|
||||
return type;
|
||||
} else if (isEqualityOperation(tokenType)) {
|
||||
} else if(isEqualityOperation(tokenType)){
|
||||
final PsiExpression lhs = binaryExp.getLOperand();
|
||||
if (lhs == null) {
|
||||
if(lhs == null){
|
||||
return null;
|
||||
}
|
||||
final PsiType lhsType = lhs.getType();
|
||||
if (ClassUtils.isPrimitive(lhsType)) {
|
||||
if(ClassUtils.isPrimitive(lhsType)){
|
||||
return lhsType;
|
||||
}
|
||||
final PsiExpression rhs = binaryExp.getROperand();
|
||||
if (rhs == null) {
|
||||
if(rhs == null){
|
||||
return null;
|
||||
}
|
||||
final PsiType rhsType = rhs.getType();
|
||||
if (ClassUtils.isPrimitive(rhsType)) {
|
||||
if(ClassUtils.isPrimitive(rhsType)){
|
||||
return rhsType;
|
||||
}
|
||||
return null;
|
||||
} else {
|
||||
} else{
|
||||
return null;
|
||||
}
|
||||
} else if (context instanceof PsiPrefixExpression) {
|
||||
} else if(context instanceof PsiPrefixExpression){
|
||||
final PsiPrefixExpression prefixExp = (PsiPrefixExpression) context;
|
||||
return prefixExp.getType();
|
||||
} else if (context instanceof PsiPostfixExpression) {
|
||||
} else if(context instanceof PsiPostfixExpression){
|
||||
final PsiPostfixExpression postfixExp = (PsiPostfixExpression) context;
|
||||
return postfixExp.getType();
|
||||
} else if (context instanceof PsiConditionalExpression) {
|
||||
} else if(context instanceof PsiConditionalExpression){
|
||||
final PsiConditionalExpression conditional = (PsiConditionalExpression) context;
|
||||
final PsiExpression condition = conditional.getCondition();
|
||||
if (condition.equals(wrappedExp)) {
|
||||
if(condition.equals(wrappedExp)){
|
||||
return PsiType.BOOLEAN;
|
||||
}
|
||||
return conditional.getType();
|
||||
} else if (context instanceof PsiExpressionList) {
|
||||
} else if(context instanceof PsiExpressionList){
|
||||
final PsiExpressionList expList = (PsiExpressionList) context;
|
||||
final PsiMethod method = ExpectedTypeUtils.findCalledMethod(expList);
|
||||
if (method == null) {
|
||||
if(method == null){
|
||||
return null;
|
||||
}
|
||||
final int parameterPosition = ExpectedTypeUtils.getParameterPosition(expList, wrappedExp);
|
||||
return ExpectedTypeUtils.getTypeOfParemeter(method, parameterPosition);
|
||||
} else if (context instanceof PsiReturnStatement) {
|
||||
final int parameterPosition =
|
||||
ExpectedTypeUtils.getParameterPosition(expList, wrappedExp);
|
||||
return ExpectedTypeUtils.getTypeOfParemeter(method,
|
||||
parameterPosition);
|
||||
} else if(context instanceof PsiReturnStatement){
|
||||
final PsiReturnStatement psiReturnStatement = (PsiReturnStatement) context;
|
||||
final PsiMethod method = ExpectedTypeUtils.findEnclosingPsiMethod(psiReturnStatement);
|
||||
if (method == null) {
|
||||
final PsiMethod method = (PsiMethod) PsiTreeUtil.getParentOfType(
|
||||
psiReturnStatement, PsiMethod.class);
|
||||
if(method == null){
|
||||
return null;
|
||||
}
|
||||
return method.getReturnType();
|
||||
} else if (context instanceof PsiWhileStatement) {
|
||||
} else if(context instanceof PsiWhileStatement){
|
||||
return PsiType.BOOLEAN;
|
||||
} else if (context instanceof PsiDoWhileStatement) {
|
||||
} else if(context instanceof PsiDoWhileStatement){
|
||||
return PsiType.BOOLEAN;
|
||||
} else if (context instanceof PsiForStatement) {
|
||||
} else if(context instanceof PsiForStatement){
|
||||
return PsiType.BOOLEAN;
|
||||
} else if (context instanceof PsiIfStatement) {
|
||||
} else if(context instanceof PsiIfStatement){
|
||||
return PsiType.BOOLEAN;
|
||||
} else if (context instanceof PsiSynchronizedStatement) {
|
||||
} else if(context instanceof PsiSynchronizedStatement){
|
||||
final Project project = manager.getProject();
|
||||
final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
|
||||
return PsiClassType.getJavaLangObject(manager, scope);
|
||||
@@ -165,7 +171,7 @@ public class ExpectedTypeUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean isArithmeticOperation(IElementType sign) {
|
||||
private static boolean isArithmeticOperation(IElementType sign){
|
||||
return sign.equals(JavaTokenType.PLUS)
|
||||
|| sign.equals(JavaTokenType.MINUS)
|
||||
|| sign.equals(JavaTokenType.ASTERISK)
|
||||
@@ -173,64 +179,54 @@ public class ExpectedTypeUtils {
|
||||
sign.equals(JavaTokenType.PERC);
|
||||
}
|
||||
|
||||
|
||||
private static boolean isEqualityOperation(IElementType sign) {
|
||||
private static boolean isEqualityOperation(IElementType sign){
|
||||
return sign.equals(JavaTokenType.EQEQ)
|
||||
|| sign.equals(JavaTokenType.NE);
|
||||
}
|
||||
|
||||
private static int getParameterPosition(PsiExpressionList expressionList, PsiExpression exp) {
|
||||
private static int getParameterPosition(PsiExpressionList expressionList,
|
||||
PsiExpression exp){
|
||||
final PsiExpression[] expressions = expressionList.getExpressions();
|
||||
for (int i = 0; i < expressions.length; i++) {
|
||||
if (expressions[i].equals(exp)) {
|
||||
for(int i = 0; i < expressions.length; i++){
|
||||
if(expressions[i].equals(exp)){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static PsiType getTypeOfParemeter(PsiMethod psiMethod, int parameterPosition) {
|
||||
private static PsiType getTypeOfParemeter(PsiMethod psiMethod,
|
||||
int parameterPosition){
|
||||
final PsiParameterList paramList = psiMethod.getParameterList();
|
||||
final PsiParameter[] parameters = paramList.getParameters();
|
||||
if(parameterPosition < 0) {
|
||||
if(parameterPosition < 0){
|
||||
return null;
|
||||
}
|
||||
if (parameterPosition >= parameters.length) {
|
||||
if(parameterPosition >= parameters.length){
|
||||
final int lastParamPosition = parameters.length - 1;
|
||||
if(lastParamPosition<0)
|
||||
return null;
|
||||
final PsiParameter lastParameter = parameters[lastParamPosition];
|
||||
if(lastParameter.isVarArgs())
|
||||
{
|
||||
return ((PsiArrayType)lastParameter.getType()).getComponentType();
|
||||
if(lastParamPosition < 0){
|
||||
return null;
|
||||
}
|
||||
final PsiParameter lastParameter = parameters[lastParamPosition];
|
||||
if(lastParameter.isVarArgs()){
|
||||
return ((PsiArrayType) lastParameter.getType()).getComponentType();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
final PsiParameter param = parameters[parameterPosition];
|
||||
if(param.isVarArgs())
|
||||
{
|
||||
if(param.isVarArgs()){
|
||||
return ((PsiArrayType) param.getType()).getComponentType();
|
||||
}
|
||||
return param.getType();
|
||||
}
|
||||
|
||||
private static PsiMethod findEnclosingPsiMethod(PsiElement psiElement) {
|
||||
PsiElement currentPsiElement = psiElement;
|
||||
while (currentPsiElement != null) {
|
||||
currentPsiElement = currentPsiElement.getParent();
|
||||
if (currentPsiElement instanceof PsiMethod) {
|
||||
return (PsiMethod) currentPsiElement;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static PsiMethod findCalledMethod(PsiExpressionList expList) {
|
||||
private static PsiMethod findCalledMethod(PsiExpressionList expList){
|
||||
final PsiElement parent = expList.getParent();
|
||||
if (parent instanceof PsiMethodCallExpression) {
|
||||
if(parent instanceof PsiMethodCallExpression){
|
||||
final PsiMethodCallExpression methodCall = (PsiMethodCallExpression) parent;
|
||||
return methodCall.resolveMethod();
|
||||
} else if (parent instanceof PsiNewExpression) {
|
||||
} else if(parent instanceof PsiNewExpression){
|
||||
final PsiNewExpression psiNewExpression = (PsiNewExpression) parent;
|
||||
return psiNewExpression.resolveMethod();
|
||||
}
|
||||
|
||||
@@ -6,41 +6,41 @@ import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
|
||||
public class SerializationUtils {
|
||||
public class SerializationUtils{
|
||||
private static final String SERIALIZABLE_CLASS_NAME = "java.io.Serializable";
|
||||
private static final String EXTERNALIZABLE_CLASS_NAME = "java.io.Externalizable";
|
||||
|
||||
private SerializationUtils() {
|
||||
private SerializationUtils(){
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
public static boolean isSerializable(PsiClass aClass) {
|
||||
public static boolean isSerializable(PsiClass aClass){
|
||||
final PsiManager manager = aClass.getManager();
|
||||
final Project project = manager.getProject();
|
||||
final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
|
||||
final PsiClass serializable = manager.findClass(SERIALIZABLE_CLASS_NAME, scope);
|
||||
final PsiClass serializable = manager.findClass(
|
||||
SERIALIZABLE_CLASS_NAME, scope);
|
||||
return InheritanceUtil.isInheritorOrSelf(aClass, serializable, true);
|
||||
}
|
||||
|
||||
public static boolean isExternalizable(PsiClass aClass) {
|
||||
public static boolean isExternalizable(PsiClass aClass){
|
||||
final PsiManager manager = aClass.getManager();
|
||||
final Project project = manager.getProject();
|
||||
final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
|
||||
final PsiClass serializable = manager.findClass(EXTERNALIZABLE_CLASS_NAME, scope);
|
||||
final PsiClass serializable = manager.findClass(
|
||||
EXTERNALIZABLE_CLASS_NAME, scope);
|
||||
return InheritanceUtil.isInheritorOrSelf(aClass, serializable, true);
|
||||
}
|
||||
|
||||
|
||||
public static boolean isDirectlySerializable(PsiClass aClass) {
|
||||
public static boolean isDirectlySerializable(PsiClass aClass){
|
||||
final PsiReferenceList implementsList = aClass.getImplementsList();
|
||||
if (implementsList != null) {
|
||||
if(implementsList != null){
|
||||
final PsiJavaCodeReferenceElement[] interfaces = implementsList.getReferenceElements();
|
||||
for (int i = 0; i < interfaces.length; i++) {
|
||||
for(int i = 0; i < interfaces.length; i++){
|
||||
final PsiClass implemented = (PsiClass) interfaces[i].resolve();
|
||||
if (implemented != null) {
|
||||
if(implemented != null){
|
||||
final String name = implemented.getQualifiedName();
|
||||
if (SERIALIZABLE_CLASS_NAME.equals(name)) {
|
||||
if(SERIALIZABLE_CLASS_NAME.equals(name)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -49,90 +49,112 @@ public class SerializationUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isReadObject(PsiMethod method) {
|
||||
public static boolean hasReadObject(PsiClass aClass){
|
||||
final PsiMethod[] methods = aClass.getMethods();
|
||||
for(int i = 0; i < methods.length; i++){
|
||||
final PsiMethod method = methods[i];
|
||||
if(isReadObject(method)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean hasWriteObject(PsiClass aClass){
|
||||
final PsiMethod[] methods = aClass.getMethods();
|
||||
for(int i = 0; i < methods.length; i++){
|
||||
final PsiMethod method = methods[i];
|
||||
if(isWriteObject(method)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isReadObject(PsiMethod method){
|
||||
final String methodName = method.getName();
|
||||
if (!"readObject".equals(methodName)) {
|
||||
if(!"readObject".equals(methodName)){
|
||||
return false;
|
||||
}
|
||||
final PsiParameterList parameterList = method.getParameterList();
|
||||
final PsiParameter[] parameters = parameterList.getParameters();
|
||||
if (parameters.length != 1) {
|
||||
if(parameters.length != 1){
|
||||
return false;
|
||||
}
|
||||
final PsiType argType = parameters[0].getType();
|
||||
if (!TypeUtils.typeEquals("java.io.ObjectInputStream", argType)) {
|
||||
if(!TypeUtils.typeEquals("java.io.ObjectInputStream", argType)){
|
||||
return false;
|
||||
}
|
||||
final PsiType returnType = method.getReturnType();
|
||||
if (!TypeUtils.typeEquals("void", returnType)) {
|
||||
if(!TypeUtils.typeEquals("void", returnType)){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isWriteObject(PsiMethod method) {
|
||||
public static boolean isWriteObject(PsiMethod method){
|
||||
final String methodName = method.getName();
|
||||
if (!"writeObject".equals(methodName)) {
|
||||
if(!"writeObject".equals(methodName)){
|
||||
return false;
|
||||
}
|
||||
final PsiParameterList parameterList = method.getParameterList();
|
||||
final PsiParameter[] parameters = parameterList.getParameters();
|
||||
if (parameters.length != 1) {
|
||||
if(parameters.length != 1){
|
||||
return false;
|
||||
}
|
||||
final PsiType argType = parameters[0].getType();
|
||||
if (!TypeUtils.typeEquals("java.io.ObjectOutputStream", argType)) {
|
||||
if(!TypeUtils.typeEquals("java.io.ObjectOutputStream", argType)){
|
||||
return false;
|
||||
}
|
||||
final PsiType returnType = method.getReturnType();
|
||||
if (!TypeUtils.typeEquals("void", returnType)) {
|
||||
if(!TypeUtils.typeEquals("void", returnType)){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isReadResolve(PsiMethod method) {
|
||||
public static boolean isReadResolve(PsiMethod method){
|
||||
final String methodName = method.getName();
|
||||
if (!"readResolve".equals(methodName)) {
|
||||
if(!"readResolve".equals(methodName)){
|
||||
return false;
|
||||
}
|
||||
final PsiParameterList parameterList = method.getParameterList();
|
||||
final PsiParameter[] parameters = parameterList.getParameters();
|
||||
if (parameters.length != 0) {
|
||||
if(parameters.length != 0){
|
||||
return false;
|
||||
}
|
||||
final PsiType returnType = method.getReturnType();
|
||||
if (!TypeUtils.isJavaLangObject(returnType)) {
|
||||
if(!TypeUtils.isJavaLangObject(returnType)){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isWriteReplace(PsiMethod method) {
|
||||
public static boolean isWriteReplace(PsiMethod method){
|
||||
final String methodName = method.getName();
|
||||
if (!"writeReplace".equals(methodName)) {
|
||||
if(!"writeReplace".equals(methodName)){
|
||||
return false;
|
||||
}
|
||||
final PsiParameterList parameterList = method.getParameterList();
|
||||
final PsiParameter[] parameters = parameterList.getParameters();
|
||||
if (parameters.length != 0) {
|
||||
if(parameters.length != 0){
|
||||
return false;
|
||||
}
|
||||
final PsiType returnType = method.getReturnType();
|
||||
if (!TypeUtils.isJavaLangObject(returnType)) {
|
||||
if(!TypeUtils.isJavaLangObject(returnType)){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean typeIsSerializable(PsiType type) {
|
||||
public static boolean typeIsSerializable(PsiType type){
|
||||
final PsiType componentType = type.getDeepComponentType();
|
||||
if (TypeConversionUtil.isPrimitiveAndNotNull(componentType)) {
|
||||
if(TypeConversionUtil.isPrimitiveAndNotNull(componentType)){
|
||||
return true;
|
||||
}
|
||||
final PsiType[] superTypes = componentType.getSuperTypes();
|
||||
for (int i = 0; i < superTypes.length; i++) {
|
||||
if (TypeUtils.typeEquals(SERIALIZABLE_CLASS_NAME, superTypes[i])) {
|
||||
for(int i = 0; i < superTypes.length; i++){
|
||||
if(TypeUtils.typeEquals(SERIALIZABLE_CLASS_NAME, superTypes[i])){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.siyeh.ig.psiutils;
|
||||
|
||||
import com.intellij.psi.PsiBinaryExpression;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.PsiJavaToken;
|
||||
import com.intellij.psi.PsiAssignmentExpression;
|
||||
|
||||
public class WellFormednessUtils{
|
||||
private WellFormednessUtils(){
|
||||
super();
|
||||
}
|
||||
|
||||
public static boolean isWellFormed(PsiBinaryExpression expression)
|
||||
{
|
||||
final PsiExpression lhs = expression.getLOperand();
|
||||
if(lhs == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
final PsiExpression rhs = expression.getROperand();
|
||||
if(rhs == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
final PsiJavaToken operationSign = expression.getOperationSign();
|
||||
if(operationSign == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isWellFormed(PsiAssignmentExpression expression){
|
||||
final PsiExpression lhs = expression.getLExpression();
|
||||
if(lhs == null){
|
||||
return false;
|
||||
}
|
||||
final PsiExpression rhs = expression.getRExpression();
|
||||
if(rhs == null){
|
||||
return false;
|
||||
}
|
||||
final PsiJavaToken operationSign = expression.getOperationSign();
|
||||
if(operationSign == null){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+4
-23
@@ -3,7 +3,6 @@ package com.siyeh.ig.serialization;
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.ClassInspection;
|
||||
@@ -22,17 +21,8 @@ public class ExternalizableWithSerializationMethodsInspection extends ClassInspe
|
||||
|
||||
public String buildErrorString(PsiElement location) {
|
||||
final PsiClass aClass = (PsiClass) location.getParent();
|
||||
final PsiMethod[] methods = aClass.getMethods();
|
||||
boolean hasReadObject = false;
|
||||
boolean hasWriteObject = false;
|
||||
for (int i = 0; i < methods.length; i++) {
|
||||
final PsiMethod method = methods[i];
|
||||
if (SerializationUtils.isReadObject(method)) {
|
||||
hasReadObject = true;
|
||||
} else if (SerializationUtils.isWriteObject(method)) {
|
||||
hasWriteObject = true;
|
||||
}
|
||||
}
|
||||
final boolean hasReadObject = SerializationUtils.hasReadObject(aClass);
|
||||
final boolean hasWriteObject = SerializationUtils.hasWriteObject(aClass);
|
||||
if (hasReadObject && hasWriteObject) {
|
||||
return "Externalizable class #ref defines readObject() and writeObject() #loc";
|
||||
} else if (hasWriteObject) {
|
||||
@@ -59,17 +49,8 @@ public class ExternalizableWithSerializationMethodsInspection extends ClassInspe
|
||||
if (!SerializationUtils.isExternalizable(aClass)) {
|
||||
return;
|
||||
}
|
||||
final PsiMethod[] methods = aClass.getMethods();
|
||||
boolean hasReadObject = false;
|
||||
boolean hasWriteObject = false;
|
||||
for (int i = 0; i < methods.length; i++) {
|
||||
final PsiMethod method = methods[i];
|
||||
if (SerializationUtils.isReadObject(method)) {
|
||||
hasReadObject = true;
|
||||
} else if (SerializationUtils.isWriteObject(method)) {
|
||||
hasWriteObject = true;
|
||||
}
|
||||
}
|
||||
final boolean hasReadObject = SerializationUtils.hasReadObject(aClass);
|
||||
final boolean hasWriteObject = SerializationUtils.hasWriteObject(aClass);
|
||||
if (!hasWriteObject && !hasReadObject) {
|
||||
return;
|
||||
}
|
||||
|
||||
+5
-23
@@ -3,7 +3,6 @@ package com.siyeh.ig.serialization;
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.ClassInspection;
|
||||
@@ -22,17 +21,9 @@ public class NonSerializableWithSerializationMethodsInspection extends ClassInsp
|
||||
|
||||
public String buildErrorString(PsiElement location) {
|
||||
final PsiClass aClass = (PsiClass) location.getParent();
|
||||
final PsiMethod[] methods = aClass.getMethods();
|
||||
boolean hasReadObject = false;
|
||||
boolean hasWriteObject = false;
|
||||
for (int i = 0; i < methods.length; i++) {
|
||||
final PsiMethod method = methods[i];
|
||||
if (SerializationUtils.isReadObject(method)) {
|
||||
hasReadObject = true;
|
||||
} else if (SerializationUtils.isWriteObject(method)) {
|
||||
hasWriteObject = true;
|
||||
}
|
||||
}
|
||||
final boolean hasReadObject = SerializationUtils.hasReadObject(aClass);
|
||||
final boolean hasWriteObject = SerializationUtils.hasWriteObject(aClass);
|
||||
|
||||
if (hasReadObject && hasWriteObject) {
|
||||
return "Non-serializable class #ref defines readObject() and writeObject() #loc";
|
||||
} else if (hasWriteObject) {
|
||||
@@ -59,17 +50,8 @@ public class NonSerializableWithSerializationMethodsInspection extends ClassInsp
|
||||
if (SerializationUtils.isSerializable(aClass)) {
|
||||
return;
|
||||
}
|
||||
final PsiMethod[] methods = aClass.getMethods();
|
||||
boolean hasReadObject = false;
|
||||
boolean hasWriteObject = false;
|
||||
for (int i = 0; i < methods.length; i++) {
|
||||
final PsiMethod method = methods[i];
|
||||
if (SerializationUtils.isReadObject(method)) {
|
||||
hasReadObject = true;
|
||||
} else if (SerializationUtils.isWriteObject(method)) {
|
||||
hasWriteObject = true;
|
||||
}
|
||||
}
|
||||
final boolean hasReadObject = SerializationUtils.hasReadObject(aClass);
|
||||
final boolean hasWriteObject = SerializationUtils.hasWriteObject(aClass);
|
||||
if (!hasWriteObject && !hasReadObject) {
|
||||
return;
|
||||
}
|
||||
|
||||
+5
-25
@@ -1,16 +1,15 @@
|
||||
package com.siyeh.ig.serialization;
|
||||
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.siyeh.ig.*;
|
||||
import com.siyeh.ig.fixes.MakePrivateFix;
|
||||
import com.siyeh.ig.psiutils.SerializationUtils;
|
||||
|
||||
public class ReadObjectAndWriteObjectPrivateInspection extends MethodInspection {
|
||||
private static final Logger s_logger = Logger.getInstance("ReadObjectAndWriteObjectPrivateInspection");
|
||||
private final MakePrivateFix fix = new MakePrivateFix();
|
||||
|
||||
public String getDisplayName() {
|
||||
@@ -33,25 +32,6 @@ public class ReadObjectAndWriteObjectPrivateInspection extends MethodInspection
|
||||
return fix;
|
||||
}
|
||||
|
||||
private static class MakePrivateFix extends InspectionGadgetsFix {
|
||||
public String getName() {
|
||||
return "Make 'private'";
|
||||
}
|
||||
|
||||
public void applyFix(Project project, ProblemDescriptor descriptor) {
|
||||
try {
|
||||
final PsiElement methodNameToken = descriptor.getPsiElement();
|
||||
final PsiMethod method = (PsiMethod) methodNameToken.getParent();
|
||||
final PsiModifierList modifiers = method.getModifierList();
|
||||
modifiers.setModifierProperty(PsiModifier.PUBLIC, false);
|
||||
modifiers.setModifierProperty(PsiModifier.PROTECTED, false);
|
||||
modifiers.setModifierProperty(PsiModifier.PRIVATE, true);
|
||||
} catch (IncorrectOperationException e) {
|
||||
s_logger.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class ReadObjectWriteObjectPrivateVisitor extends BaseInspectionVisitor {
|
||||
private ReadObjectWriteObjectPrivateVisitor(BaseInspection inspection, InspectionManager inspectionManager, boolean isOnTheFly) {
|
||||
super(inspection, inspectionManager, isOnTheFly);
|
||||
|
||||
+5
-27
@@ -1,17 +1,15 @@
|
||||
package com.siyeh.ig.serialization;
|
||||
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.siyeh.ig.*;
|
||||
import com.siyeh.ig.fixes.MakeProtectedFix;
|
||||
import com.siyeh.ig.psiutils.SerializationUtils;
|
||||
|
||||
public class ReadResolveAndWriteReplaceProtectedInspection extends MethodInspection {
|
||||
private static final Logger s_logger =
|
||||
Logger.getInstance("ReadResolveAndWriteReplaceProtectedInspection ");
|
||||
private final MakeProtectedFix fix = new MakeProtectedFix();
|
||||
|
||||
public String getDisplayName() {
|
||||
@@ -35,26 +33,6 @@ public class ReadResolveAndWriteReplaceProtectedInspection extends MethodInspect
|
||||
return fix;
|
||||
}
|
||||
|
||||
private static class MakeProtectedFix extends InspectionGadgetsFix {
|
||||
public String getName() {
|
||||
return "Make 'protected'";
|
||||
}
|
||||
|
||||
public void applyFix(Project project, ProblemDescriptor descriptor) {
|
||||
try {
|
||||
final PsiElement methodNameToken = descriptor.getPsiElement();
|
||||
final PsiMethod method =
|
||||
(PsiMethod) methodNameToken.getParent();
|
||||
final PsiModifierList modifiers = method.getModifierList();
|
||||
modifiers.setModifierProperty(PsiModifier.PUBLIC, false);
|
||||
modifiers.setModifierProperty(PsiModifier.PRIVATE, false);
|
||||
modifiers.setModifierProperty(PsiModifier.PROTECTED, true);
|
||||
} catch (IncorrectOperationException e) {
|
||||
s_logger.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class ReadResolveWriteReplaceProtectedVisitor extends BaseInspectionVisitor {
|
||||
private ReadResolveWriteReplaceProtectedVisitor(BaseInspection inspection, InspectionManager inspectionManager, boolean isOnTheFly) {
|
||||
super(inspection, inspectionManager, isOnTheFly);
|
||||
|
||||
+5
-24
@@ -1,17 +1,18 @@
|
||||
package com.siyeh.ig.serialization;
|
||||
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.siyeh.ig.*;
|
||||
import com.siyeh.ig.fixes.AddSerialVersionUIDFix;
|
||||
import com.siyeh.ig.psiutils.SerializationUtils;
|
||||
import com.siyeh.ig.ui.SingleCheckboxOptionsPanel;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class SerializableHasSerialVersionUIDFieldInspection extends ClassInspection {
|
||||
|
||||
public boolean m_ignoreSerializableDueToInheritance = true;
|
||||
private final AddSerialVersionUIDFix fix = new AddSerialVersionUIDFix();
|
||||
|
||||
@@ -31,26 +32,6 @@ public class SerializableHasSerialVersionUIDFieldInspection extends ClassInspect
|
||||
return fix;
|
||||
}
|
||||
|
||||
private static class AddSerialVersionUIDFix extends InspectionGadgetsFix {
|
||||
public String getName() {
|
||||
return "Add serialVersionUIDField";
|
||||
}
|
||||
|
||||
public void applyFix(Project project, ProblemDescriptor problemDescriptor) {
|
||||
final PsiElement classIdentifier = problemDescriptor.getPsiElement();
|
||||
final PsiElement aClass = classIdentifier.getParent();
|
||||
try {
|
||||
final PsiManager psiManager = aClass.getManager();
|
||||
final PsiElementFactory elementFactory = psiManager.getElementFactory();
|
||||
final long serialVersionUID = 1;
|
||||
final PsiField field = elementFactory.createFieldFromText("private static final long serialVersionUID = "+ serialVersionUID+"L;", aClass);
|
||||
aClass.add(field);
|
||||
} catch (IncorrectOperationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JComponent createOptionsPanel() {
|
||||
return new SingleCheckboxOptionsPanel("Ignore classes serializable due to inheritance",
|
||||
this, "m_ignoreSerializableDueToInheritance");
|
||||
|
||||
+6
-23
@@ -3,7 +3,6 @@ package com.siyeh.ig.serialization;
|
||||
import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.ClassInspection;
|
||||
@@ -26,17 +25,9 @@ public class SerializableHasSerializationMethodsInspection extends ClassInspecti
|
||||
|
||||
public String buildErrorString(PsiElement location) {
|
||||
final PsiClass aClass = (PsiClass) location.getParent();
|
||||
final PsiMethod[] methods = aClass.getMethods();
|
||||
boolean hasReadObject = false;
|
||||
boolean hasWriteObject = false;
|
||||
for (int i = 0; i < methods.length; i++) {
|
||||
final PsiMethod method = methods[i];
|
||||
if (SerializationUtils.isReadObject(method)) {
|
||||
hasReadObject = true;
|
||||
} else if (SerializationUtils.isWriteObject(method)) {
|
||||
hasWriteObject = true;
|
||||
}
|
||||
}
|
||||
final boolean hasReadObject = SerializationUtils.hasReadObject(aClass);
|
||||
final boolean hasWriteObject = SerializationUtils.hasWriteObject(aClass);
|
||||
|
||||
if (!hasReadObject && !hasWriteObject) {
|
||||
return "#ref doesn't define readObject() or writeObject() #loc";
|
||||
} else if (hasReadObject) {
|
||||
@@ -74,17 +65,9 @@ public class SerializableHasSerializationMethodsInspection extends ClassInspecti
|
||||
return;
|
||||
}
|
||||
}
|
||||
final PsiMethod[] methods = aClass.getMethods();
|
||||
boolean hasReadObject = false;
|
||||
boolean hasWriteObject = false;
|
||||
for (int i = 0; i < methods.length; i++) {
|
||||
final PsiMethod method = methods[i];
|
||||
if (SerializationUtils.isReadObject(method)) {
|
||||
hasReadObject = true;
|
||||
} else if (SerializationUtils.isWriteObject(method)) {
|
||||
hasWriteObject = true;
|
||||
}
|
||||
}
|
||||
final boolean hasReadObject = SerializationUtils.hasReadObject(aClass);
|
||||
final boolean hasWriteObject = SerializationUtils.hasWriteObject(aClass);
|
||||
|
||||
if (hasWriteObject && hasReadObject) {
|
||||
return;
|
||||
}
|
||||
|
||||
+7
-4
@@ -5,10 +5,8 @@ import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.ClassInspection;
|
||||
import com.siyeh.ig.GroupNames;
|
||||
import com.siyeh.ig.*;
|
||||
import com.siyeh.ig.fixes.AddSerialVersionUIDFix;
|
||||
import com.siyeh.ig.psiutils.SerializationUtils;
|
||||
import com.siyeh.ig.ui.SingleCheckboxOptionsPanel;
|
||||
|
||||
@@ -16,6 +14,7 @@ import javax.swing.*;
|
||||
|
||||
public class SerializableInnerClassHasSerialVersionUIDFieldInspection extends ClassInspection {
|
||||
public boolean m_ignoreSerializableDueToInheritance = true;
|
||||
private final AddSerialVersionUIDFix fix = new AddSerialVersionUIDFix();
|
||||
|
||||
public String getDisplayName() {
|
||||
return "Serializable non-static inner class without serialVersionUID";
|
||||
@@ -28,6 +27,10 @@ public class SerializableInnerClassHasSerialVersionUIDFieldInspection extends Cl
|
||||
public String buildErrorString(PsiElement location) {
|
||||
return "Inner class #ref doesn't define a serialVersionUID field #loc";
|
||||
}
|
||||
|
||||
protected InspectionGadgetsFix buildFix(PsiElement location){
|
||||
return fix;
|
||||
}
|
||||
|
||||
public JComponent createOptionsPanel() {
|
||||
return new SingleCheckboxOptionsPanel("Ignore classes serializable due to inheritance",
|
||||
|
||||
+4
-11
@@ -10,6 +10,7 @@ import com.intellij.psi.PsiJavaToken;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.siyeh.ig.*;
|
||||
import com.siyeh.ig.psiutils.ComparisonUtils;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class ConstantOnLHSOfComparisonInspection extends ExpressionInspection {
|
||||
private final SwapComparisonFix fix = new SwapComparisonFix();
|
||||
@@ -62,28 +63,20 @@ public class ConstantOnLHSOfComparisonInspection extends ExpressionInspection {
|
||||
|
||||
public void visitBinaryExpression(PsiBinaryExpression expression) {
|
||||
super.visitBinaryExpression(expression);
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
if (sign == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(expression))
|
||||
{
|
||||
return;
|
||||
}
|
||||
final String operator = sign.getText();
|
||||
if (!ComparisonUtils.isComparison(operator)) {
|
||||
if (!ComparisonUtils.isComparison(expression)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = expression.getLOperand();
|
||||
if (lhs == null) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression rhs = expression.getROperand();
|
||||
if (rhs == null) {
|
||||
return;
|
||||
}
|
||||
if (!PsiUtil.isConstantExpression(lhs)
|
||||
|| PsiUtil.isConstantExpression(rhs)) {
|
||||
return;
|
||||
}
|
||||
registerError(expression);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-10
@@ -10,6 +10,7 @@ import com.intellij.psi.PsiJavaToken;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.siyeh.ig.*;
|
||||
import com.siyeh.ig.psiutils.ComparisonUtils;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class ConstantOnRHSOfComparisonInspection extends ExpressionInspection {
|
||||
private final SwapComparisonFix fix = new SwapComparisonFix();
|
||||
@@ -60,22 +61,14 @@ public class ConstantOnRHSOfComparisonInspection extends ExpressionInspection {
|
||||
|
||||
public void visitBinaryExpression(PsiBinaryExpression expression) {
|
||||
super.visitBinaryExpression(expression);
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
if (sign == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(expression)){
|
||||
return;
|
||||
}
|
||||
final String operator = sign.getText();
|
||||
if (!ComparisonUtils.isComparison(operator)) {
|
||||
if (!ComparisonUtils.isComparison(expression)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = expression.getLOperand();
|
||||
if (lhs == null) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression rhs = expression.getROperand();
|
||||
if (rhs == null) {
|
||||
return;
|
||||
}
|
||||
if (!PsiUtil.isConstantExpression(rhs) ||
|
||||
PsiUtil.isConstantExpression(lhs)) {
|
||||
return;
|
||||
|
||||
+4
-6
@@ -9,6 +9,7 @@ import com.intellij.psi.util.ConstantExpressionUtil;
|
||||
import com.intellij.psi.util.IsConstantExpressionVisitor;
|
||||
import com.siyeh.ig.*;
|
||||
import com.siyeh.ig.psiutils.TypeUtils;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class PointlessArithmeticExpressionInspection extends ExpressionInspection {
|
||||
private final PointlessArithmeticFix fix = new PointlessArithmeticFix();
|
||||
@@ -89,6 +90,9 @@ public class PointlessArithmeticExpressionInspection extends ExpressionInspectio
|
||||
|
||||
public void visitBinaryExpression(PsiBinaryExpression expression) {
|
||||
super.visitBinaryExpression(expression);
|
||||
if(!WellFormednessUtils.isWellFormed(expression)){
|
||||
return;
|
||||
}
|
||||
if (TypeUtils.expressionHasType("java.lang.String", expression)) {
|
||||
return;
|
||||
}
|
||||
@@ -135,9 +139,6 @@ public class PointlessArithmeticExpressionInspection extends ExpressionInspectio
|
||||
}
|
||||
|
||||
private static boolean isZero(PsiExpression expression) {
|
||||
if (expression == null) {
|
||||
return false;
|
||||
}
|
||||
final IsConstantExpressionVisitor visitor = new IsConstantExpressionVisitor();
|
||||
expression.accept(visitor);
|
||||
if (!visitor.isConstant()) {
|
||||
@@ -148,9 +149,6 @@ public class PointlessArithmeticExpressionInspection extends ExpressionInspectio
|
||||
}
|
||||
|
||||
private static boolean isOne(PsiExpression expression) {
|
||||
if (expression == null) {
|
||||
return false;
|
||||
}
|
||||
final IsConstantExpressionVisitor visitor =
|
||||
new IsConstantExpressionVisitor();
|
||||
expression.accept(visitor);
|
||||
|
||||
+4
-6
@@ -8,6 +8,7 @@ import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.IsConstantExpressionVisitor;
|
||||
import com.intellij.psi.util.ConstantExpressionUtil;
|
||||
import com.siyeh.ig.*;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class PointlessBooleanExpressionInspection extends ExpressionInspection {
|
||||
private final BooleanLiteralComparisonFix fix = new BooleanLiteralComparisonFix();
|
||||
@@ -116,13 +117,13 @@ public class PointlessBooleanExpressionInspection extends ExpressionInspection {
|
||||
public void visitClass(PsiClass aClass) {
|
||||
//to avoid drilldown
|
||||
}
|
||||
|
||||
|
||||
public void visitBinaryExpression(PsiBinaryExpression expression) {
|
||||
super.visitBinaryExpression(expression);
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
if (sign == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(expression)){
|
||||
return;
|
||||
}
|
||||
final PsiJavaToken sign = expression.getOperationSign();
|
||||
final PsiExpression rhs = expression.getROperand();
|
||||
if (rhs == null) {
|
||||
return;
|
||||
@@ -137,9 +138,6 @@ public class PointlessBooleanExpressionInspection extends ExpressionInspection {
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = expression.getLOperand();
|
||||
if (lhs == null) {
|
||||
return;
|
||||
}
|
||||
final PsiType lhsType = lhs.getType();
|
||||
if (lhsType == null) {
|
||||
return;
|
||||
|
||||
+59
-40
@@ -4,105 +4,124 @@ import com.intellij.codeInspection.InspectionManager;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.siyeh.ig.*;
|
||||
import com.siyeh.ig.psiutils.ExpressionEquivalenceChecker;
|
||||
import com.siyeh.ig.psiutils.SideEffectChecker;
|
||||
import com.siyeh.ig.psiutils.WellFormednessUtils;
|
||||
|
||||
public class ReplaceAssignmentWithOperatorAssignmentInspection extends ExpressionInspection {
|
||||
public class ReplaceAssignmentWithOperatorAssignmentInspection
|
||||
extends ExpressionInspection{
|
||||
|
||||
public String getDisplayName() {
|
||||
public String getDisplayName(){
|
||||
return "Assignment replaceable with operator assignment";
|
||||
}
|
||||
|
||||
public String getGroupDisplayName() {
|
||||
public String getGroupDisplayName(){
|
||||
return GroupNames.VERBOSE_GROUP_NAME;
|
||||
}
|
||||
|
||||
public String buildErrorString(PsiElement location) {
|
||||
public String buildErrorString(PsiElement location){
|
||||
return "#ref could be simplified to " +
|
||||
calculateReplacementExpression((PsiAssignmentExpression) location) + " #loc";
|
||||
calculateReplacementExpression(
|
||||
(PsiAssignmentExpression) location) +
|
||||
" #loc";
|
||||
}
|
||||
|
||||
private static String calculateReplacementExpression(PsiAssignmentExpression expression) {
|
||||
private static String calculateReplacementExpression(
|
||||
PsiAssignmentExpression expression){
|
||||
final PsiBinaryExpression rhs = (PsiBinaryExpression) expression.getRExpression();
|
||||
final PsiExpression lhs = expression.getLExpression();
|
||||
final PsiJavaToken sign = rhs.getOperationSign();
|
||||
final PsiExpression rhsRhs = rhs.getROperand();
|
||||
return lhs.getText() + ' ' + sign.getText() + "= " + rhsRhs.getText();
|
||||
String signText = sign.getText();
|
||||
if("&&".equals(signText)){
|
||||
signText = "&";
|
||||
} else if("||".equals(signText)){
|
||||
signText = "|";
|
||||
}
|
||||
return lhs.getText() + ' ' + signText + "= " + rhsRhs.getText();
|
||||
}
|
||||
|
||||
public BaseInspectionVisitor createVisitor(InspectionManager inspectionManager, boolean onTheFly) {
|
||||
return new ReplaceAssignmentWithOperatorAssignmentVisitor(this, inspectionManager, onTheFly);
|
||||
public BaseInspectionVisitor createVisitor(
|
||||
InspectionManager inspectionManager, boolean onTheFly){
|
||||
return new ReplaceAssignmentWithOperatorAssignmentVisitor(this,
|
||||
inspectionManager,
|
||||
onTheFly);
|
||||
}
|
||||
|
||||
public InspectionGadgetsFix buildFix(PsiElement location) {
|
||||
return new ReplaceAssignmentWithOperatorAssignmentFix((PsiAssignmentExpression) location);
|
||||
public InspectionGadgetsFix buildFix(PsiElement location){
|
||||
return new ReplaceAssignmentWithOperatorAssignmentFix(
|
||||
(PsiAssignmentExpression) location);
|
||||
}
|
||||
|
||||
private static class ReplaceAssignmentWithOperatorAssignmentFix extends InspectionGadgetsFix {
|
||||
private static class ReplaceAssignmentWithOperatorAssignmentFix
|
||||
extends InspectionGadgetsFix{
|
||||
private final String m_name;
|
||||
|
||||
private ReplaceAssignmentWithOperatorAssignmentFix(PsiAssignmentExpression expression) {
|
||||
private ReplaceAssignmentWithOperatorAssignmentFix(
|
||||
PsiAssignmentExpression expression){
|
||||
super();
|
||||
final PsiBinaryExpression rhs = (PsiBinaryExpression) expression.getRExpression();
|
||||
final PsiJavaToken sign = rhs.getOperationSign();
|
||||
m_name = "Replace = with " + sign.getText() + '=';
|
||||
String signText = sign.getText();
|
||||
if("&&".equals(signText)){
|
||||
signText = "&";
|
||||
} else if("||".equals(signText)){
|
||||
signText = "|";
|
||||
}
|
||||
m_name = "Replace = with " + signText + '=';
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
public String getName(){
|
||||
return m_name;
|
||||
}
|
||||
|
||||
public void applyFix(Project project, ProblemDescriptor descriptor) {
|
||||
public void applyFix(Project project, ProblemDescriptor descriptor){
|
||||
final PsiAssignmentExpression expression =
|
||||
(PsiAssignmentExpression) descriptor.getPsiElement();
|
||||
final String newExpression = calculateReplacementExpression(expression);
|
||||
final String newExpression =
|
||||
calculateReplacementExpression(expression);
|
||||
replaceExpression(project, expression, newExpression);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class ReplaceAssignmentWithOperatorAssignmentVisitor extends BaseInspectionVisitor {
|
||||
private ReplaceAssignmentWithOperatorAssignmentVisitor(BaseInspection inspection,
|
||||
InspectionManager inspectionManager, boolean isOnTheFly) {
|
||||
private static class ReplaceAssignmentWithOperatorAssignmentVisitor
|
||||
extends BaseInspectionVisitor{
|
||||
private ReplaceAssignmentWithOperatorAssignmentVisitor(
|
||||
BaseInspection inspection,
|
||||
InspectionManager inspectionManager, boolean isOnTheFly){
|
||||
super(inspection, inspectionManager, isOnTheFly);
|
||||
}
|
||||
|
||||
public void visitAssignmentExpression(PsiAssignmentExpression assignment) {
|
||||
public void visitAssignmentExpression(
|
||||
PsiAssignmentExpression assignment){
|
||||
super.visitAssignmentExpression(assignment);
|
||||
final PsiJavaToken sign = assignment.getOperationSign();
|
||||
if (sign == null) {
|
||||
if(!WellFormednessUtils.isWellFormed(assignment)){
|
||||
return;
|
||||
}
|
||||
if (!sign.getTokenType().equals(JavaTokenType.EQ)) {
|
||||
final PsiJavaToken sign = assignment.getOperationSign();
|
||||
final IElementType assignmentTokenType = sign.getTokenType();
|
||||
if(!assignmentTokenType.equals(JavaTokenType.EQ)){
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = assignment.getLExpression();
|
||||
final PsiExpression rhs = assignment.getRExpression();
|
||||
if (lhs == null || rhs == null) {
|
||||
return;
|
||||
}
|
||||
if (!(rhs instanceof PsiBinaryExpression)) {
|
||||
|
||||
if(!(rhs instanceof PsiBinaryExpression)){
|
||||
return;
|
||||
}
|
||||
final PsiBinaryExpression binaryRhs = (PsiBinaryExpression) rhs;
|
||||
final PsiJavaToken operatorSign = binaryRhs.getOperationSign();
|
||||
if (operatorSign.getTokenType().equals(JavaTokenType.OROR) ||
|
||||
operatorSign.getTokenType().equals(JavaTokenType.ANDAND)) {
|
||||
if(!WellFormednessUtils.isWellFormed(binaryRhs)){
|
||||
return;
|
||||
}
|
||||
final PsiExpression lOperand = binaryRhs.getLOperand();
|
||||
if (lOperand == null) {
|
||||
if(SideEffectChecker.mayHaveSideEffects(lhs)){
|
||||
return;
|
||||
}
|
||||
final PsiExpression rOperand = binaryRhs.getROperand();
|
||||
if (rOperand == null) {
|
||||
return;
|
||||
}
|
||||
if (SideEffectChecker.mayHaveSideEffects(lhs)) {
|
||||
return;
|
||||
}
|
||||
if (!ExpressionEquivalenceChecker.expressionsAreEquivalent(lhs, lOperand)) {
|
||||
if(!ExpressionEquivalenceChecker.expressionsAreEquivalent(lhs,
|
||||
lOperand)){
|
||||
return;
|
||||
}
|
||||
registerError(assignment);
|
||||
|
||||
-9
@@ -23,9 +23,6 @@ public class UnnecessaryInterfaceModifierInspection extends BaseInspection {
|
||||
if (!aClass.isPhysical()) {
|
||||
return super.checkClass(aClass, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkClass(aClass, mgr, isOnTheFly);
|
||||
}
|
||||
final BaseInspectionVisitor visitor = createVisitor(mgr, isOnTheFly);
|
||||
aClass.accept(visitor);
|
||||
|
||||
@@ -36,9 +33,6 @@ public class UnnecessaryInterfaceModifierInspection extends BaseInspection {
|
||||
if (!method.isPhysical()) {
|
||||
return super.checkMethod(method, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkMethod(method, mgr, isOnTheFly);
|
||||
}
|
||||
final BaseInspectionVisitor visitor = createVisitor(mgr, isOnTheFly);
|
||||
method.accept(visitor);
|
||||
return visitor.getErrors();
|
||||
@@ -48,9 +42,6 @@ public class UnnecessaryInterfaceModifierInspection extends BaseInspection {
|
||||
if (!field.isPhysical()) {
|
||||
return super.checkField(field, mgr, isOnTheFly);
|
||||
}
|
||||
if (isOnTheFly && !InspectionGadgetsPlugin.isEnabled()) {
|
||||
return super.checkField(field, mgr, isOnTheFly);
|
||||
}
|
||||
final BaseInspectionVisitor visitor = createVisitor(mgr, isOnTheFly);
|
||||
field.accept(visitor);
|
||||
return visitor.getErrors();
|
||||
|
||||
Reference in New Issue
Block a user