preserve comments: class newInstance

This commit is contained in:
Anna.Kozlova
2017-11-27 16:44:16 +01:00
parent 38374b1dd4
commit 39959d679b
3 changed files with 25 additions and 42 deletions
@@ -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<String> 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<String> 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);
}
}
@@ -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;
@@ -1,5 +1,6 @@
class Lambda {{
XYZ xyz = () -> String.class.<caret>newInstance();
XYZ xyz = () -> String.class.<caret>newInstance//end of line comment
();
}}
interface XYZ {
void m() throws InstantiationException, IllegalAccessException;