mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
preserve comments: class newInstance
This commit is contained in:
+21
-40
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -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;
|
||||
|
||||
+2
-1
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user