diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/ClassNewInstanceInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/ClassNewInstanceInspection.java index 6ff1384a6793..d882683a057b 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/ClassNewInstanceInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/ClassNewInstanceInspection.java @@ -28,6 +28,7 @@ import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.PsiReplacementUtil; +import com.siyeh.ig.psiutils.CommentTracker; import com.siyeh.ig.psiutils.TypeUtils; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NonNls; @@ -74,10 +75,8 @@ public class ClassNewInstanceInspection extends BaseInspection { if (!(parent instanceof PsiReferenceExpression)) { return; } - final PsiReferenceExpression methodExpression = - (PsiReferenceExpression)parent; - final PsiExpression qualifier = - methodExpression.getQualifierExpression(); + final PsiReferenceExpression methodExpression = (PsiReferenceExpression)parent; + final PsiExpression qualifier = methodExpression.getQualifierExpression(); if (qualifier == null) { return; } @@ -85,13 +84,10 @@ public class ClassNewInstanceInspection extends BaseInspection { if (!(grandParent instanceof PsiMethodCallExpression)) { return; } - final PsiElement parentOfType = - PsiTreeUtil.getParentOfType(element, PsiMethod.class, PsiTryStatement.class, PsiLambdaExpression.class); + final PsiElement parentOfType = PsiTreeUtil.getParentOfType(element, PsiMethod.class, PsiTryStatement.class, PsiLambdaExpression.class); if (parentOfType instanceof PsiTryStatement) { - final PsiTryStatement tryStatement = - (PsiTryStatement)parentOfType; - addCatchBlock(tryStatement, "java.lang.NoSuchMethodException", - "java.lang.reflect.InvocationTargetException"); + final PsiTryStatement tryStatement = (PsiTryStatement)parentOfType; + addCatchBlock(tryStatement, "java.lang.NoSuchMethodException", "java.lang.reflect.InvocationTargetException"); } else if (parentOfType instanceof PsiLambdaExpression) { final PsiMethod method = LambdaUtil.getFunctionalInterfaceMethod(parentOfType); @@ -102,72 +98,57 @@ public class ClassNewInstanceInspection extends BaseInspection { } else { final PsiMethod method = (PsiMethod)parentOfType; - addThrowsClause(method, "java.lang.NoSuchMethodException", - "java.lang.reflect.InvocationTargetException"); + addThrowsClause(method, "java.lang.NoSuchMethodException", "java.lang.reflect.InvocationTargetException"); } - final PsiMethodCallExpression methodCallExpression = - (PsiMethodCallExpression)grandParent; - @NonNls final String newExpression = qualifier.getText() + - ".getConstructor().newInstance()"; - PsiReplacementUtil.replaceExpression(methodCallExpression, newExpression); + final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent; + @NonNls final String newExpression = qualifier.getText() + ".getConstructor().newInstance()"; + PsiReplacementUtil.replaceExpression(methodCallExpression, newExpression, new CommentTracker()); } private static void addThrowsClause(PsiMethod method, String... exceptionNames) { final PsiReferenceList throwsList = method.getThrowsList(); - final PsiClassType[] referencedTypes = - throwsList.getReferencedTypes(); + final PsiClassType[] referencedTypes = throwsList.getReferencedTypes(); final Set presentExceptionNames = new HashSet<>(); for (PsiClassType referencedType : referencedTypes) { final String exceptionName = referencedType.getCanonicalText(); presentExceptionNames.add(exceptionName); } final Project project = method.getProject(); - final PsiElementFactory factory = - JavaPsiFacade.getElementFactory(project); - final JavaCodeStyleManager codeStyleManager = - JavaCodeStyleManager.getInstance(project); + final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); + final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); final GlobalSearchScope scope = method.getResolveScope(); for (String exceptionName : exceptionNames) { if (presentExceptionNames.contains(exceptionName)) { continue; } - final PsiJavaCodeReferenceElement throwsReference = - factory.createReferenceElementByFQClassName( - exceptionName, scope); + final PsiJavaCodeReferenceElement throwsReference = factory.createReferenceElementByFQClassName(exceptionName, scope); final PsiElement element = throwsList.add(throwsReference); codeStyleManager.shortenClassReferences(element); } } protected static void addCatchBlock(PsiTryStatement tryStatement, - String... exceptionNames) - throws IncorrectOperationException { + String... exceptionNames) throws IncorrectOperationException { final Project project = tryStatement.getProject(); - final PsiParameter[] parameters = - tryStatement.getCatchBlockParameters(); + final PsiParameter[] parameters = tryStatement.getCatchBlockParameters(); final Set presentExceptionNames = new HashSet<>(); for (PsiParameter parameter : parameters) { final PsiType type = parameter.getType(); final String exceptionName = type.getCanonicalText(); presentExceptionNames.add(exceptionName); } - final JavaCodeStyleManager codeStyleManager = - JavaCodeStyleManager.getInstance(project); + final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); final String name = codeStyleManager.suggestUniqueVariableName("e", tryStatement.getTryBlock(), false); - final PsiElementFactory factory = - JavaPsiFacade.getElementFactory(project); + final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); for (String exceptionName : exceptionNames) { if (presentExceptionNames.contains(exceptionName)) { continue; } - final PsiClassType type = (PsiClassType) - factory.createTypeFromText(exceptionName, tryStatement); - final PsiCatchSection section = - factory.createCatchSection(type, name, tryStatement); - final PsiCatchSection element = (PsiCatchSection) - tryStatement.add(section); + final PsiClassType type = (PsiClassType)factory.createTypeFromText(exceptionName, tryStatement); + final PsiCatchSection section = factory.createCatchSection(type, name, tryStatement); + final PsiCatchSection element = (PsiCatchSection)tryStatement.add(section); codeStyleManager.shortenClassReferences(element); } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/bugs/class_new_instance/Lambda.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/bugs/class_new_instance/Lambda.after.java index c28a6e1751b9..ddb1a927c841 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/bugs/class_new_instance/Lambda.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/bugs/class_new_instance/Lambda.after.java @@ -1,5 +1,6 @@ class Lambda {{ - XYZ xyz = () -> String.class.getConstructor().newInstance(); + XYZ xyz = () -> //end of line comment + String.class.getConstructor().newInstance(); }} interface XYZ { void m() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException; diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/bugs/class_new_instance/Lambda.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/bugs/class_new_instance/Lambda.java index 0be267d63e6a..1e2b31987d9d 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/bugs/class_new_instance/Lambda.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/bugs/class_new_instance/Lambda.java @@ -1,5 +1,6 @@ class Lambda {{ - XYZ xyz = () -> String.class.newInstance(); + XYZ xyz = () -> String.class.newInstance//end of line comment + (); }} interface XYZ { void m() throws InstantiationException, IllegalAccessException;