mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
IDEA-31831 Intention aciton: Move initializer to setUp(). Implemented
This commit is contained in:
+205
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInsight.intention.impl;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil;
|
||||
import com.intellij.codeInsight.daemon.impl.quickfix.CreateFromUsageUtils;
|
||||
import com.intellij.codeInsight.highlighting.HighlightManager;
|
||||
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.colors.EditorColors;
|
||||
import com.intellij.openapi.editor.colors.EditorColorsManager;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.jsp.jspJava.JspClass;
|
||||
import com.intellij.psi.javadoc.PsiDocComment;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* refactored from {@link com.intellij.codeInsight.intention.impl.MoveInitializerToConstructorAction}
|
||||
*
|
||||
* @author Danila Ponomarenko
|
||||
*/
|
||||
public abstract class BaseMoveInitializerToMethodAction extends PsiElementBaseIntentionAction {
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
|
||||
if (element instanceof PsiCompiledElement) return false;
|
||||
final PsiField field = PsiTreeUtil.getParentOfType(element, PsiField.class, false, PsiMember.class, PsiCodeBlock.class, PsiDocComment.class);
|
||||
if (field == null || hasUnsuitableModifiers(field)) return false;
|
||||
if (!field.hasInitializer()) return false;
|
||||
PsiClass psiClass = field.getContainingClass();
|
||||
|
||||
return psiClass != null && !psiClass.isInterface() && !(psiClass instanceof PsiAnonymousClass) && !(psiClass instanceof JspClass);
|
||||
}
|
||||
|
||||
private boolean hasUnsuitableModifiers(@NotNull PsiField field) {
|
||||
for (@PsiModifier.ModifierConstant String modifier : getUnsuitableModifiers()) {
|
||||
if (field.hasModifierProperty(modifier)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected abstract Collection<String> getUnsuitableModifiers();
|
||||
|
||||
@Override
|
||||
public final void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
|
||||
|
||||
final PsiField field = getFieldAtCaret(editor, file);
|
||||
assert field != null;
|
||||
final PsiClass aClass = field.getContainingClass();
|
||||
if (aClass == null) return;
|
||||
|
||||
final Collection<PsiMethod> methodsToAddInitialization = getOrCreateMethods(project, editor, file, aClass);
|
||||
|
||||
|
||||
final List<PsiExpressionStatement> assignments = addFieldAssignments(field, methodsToAddInitialization);
|
||||
field.getInitializer().delete();
|
||||
|
||||
if (!assignments.isEmpty()) {
|
||||
highlightRExpression((PsiAssignmentExpression)assignments.get(0).getExpression(), project, editor);
|
||||
}
|
||||
}
|
||||
|
||||
private static void highlightRExpression(@NotNull PsiAssignmentExpression assignment, @NotNull Project project, Editor editor) {
|
||||
final EditorColorsManager manager = EditorColorsManager.getInstance();
|
||||
final TextAttributes attributes = manager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
|
||||
final PsiExpression expression = assignment.getRExpression();
|
||||
|
||||
HighlightManager.getInstance(project).addOccurrenceHighlights(editor, new PsiElement[]{expression}, attributes, false, null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<PsiExpressionStatement> addFieldAssignments(@NotNull PsiField field, @NotNull Collection<PsiMethod> methods) {
|
||||
final List<PsiExpressionStatement> assignments = new ArrayList<PsiExpressionStatement>();
|
||||
for (PsiMethod method : methods) {
|
||||
assignments.add(addAssignment(getOrCreateMethodBody(method), field));
|
||||
}
|
||||
return assignments;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static PsiCodeBlock getOrCreateMethodBody(@NotNull PsiMethod method) {
|
||||
PsiCodeBlock codeBlock = method.getBody();
|
||||
if (codeBlock == null) {
|
||||
CreateFromUsageUtils.setupMethodBody(method);
|
||||
codeBlock = method.getBody();
|
||||
}
|
||||
return codeBlock;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected abstract Collection<PsiMethod> getOrCreateMethods(@NotNull Project project, @NotNull Editor editor, PsiFile file, @NotNull PsiClass aClass);
|
||||
|
||||
@Nullable
|
||||
private static PsiField getFieldAtCaret(@NotNull Editor editor, @NotNull PsiFile file) {
|
||||
final int offset = editor.getCaretModel().getOffset();
|
||||
return PsiTreeUtil.getParentOfType(file.findElementAt(offset), PsiField.class);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static PsiExpressionStatement addAssignment(@NotNull PsiCodeBlock codeBlock, @NotNull PsiField field) throws IncorrectOperationException {
|
||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(codeBlock.getProject()).getElementFactory();
|
||||
|
||||
final PsiExpressionStatement statement = (PsiExpressionStatement)factory.createStatementFromText(field.getName() + " = y;", codeBlock);
|
||||
|
||||
PsiExpression initializer = field.getInitializer();
|
||||
if (initializer instanceof PsiArrayInitializerExpression) {
|
||||
initializer = arrayInitializerToNewExpression((PsiArrayInitializerExpression)initializer, factory, codeBlock);
|
||||
}
|
||||
|
||||
final PsiAssignmentExpression expression = (PsiAssignmentExpression)statement.getExpression();
|
||||
expression.getRExpression().replace(initializer);
|
||||
|
||||
final PsiElement newStatement = codeBlock.addBefore(statement, findFirstFieldUsage(codeBlock.getStatements(), field));
|
||||
replaceWithQualifiedReferences(newStatement, newStatement, factory);
|
||||
return (PsiExpressionStatement)newStatement;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiElement findFirstFieldUsage(@NotNull PsiStatement[] statements, @NotNull PsiField field) {
|
||||
for (PsiStatement blockStatement : statements) {
|
||||
if (!isSuperOrThisMethodCall(blockStatement) && containsReference(blockStatement, field)) {
|
||||
return blockStatement;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean isSuperOrThisMethodCall(@NotNull PsiStatement statement) {
|
||||
if (statement instanceof PsiExpressionStatement) {
|
||||
final PsiElement expression = ((PsiExpressionStatement)statement).getExpression();
|
||||
if (HighlightUtil.isSuperOrThisMethodCall(expression)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static PsiExpression arrayInitializerToNewExpression(@NotNull PsiArrayInitializerExpression initializer,
|
||||
@NotNull PsiElementFactory factory,
|
||||
@NotNull PsiElement context) {
|
||||
final PsiType type = initializer.getType();
|
||||
final PsiNewExpression newExpression = (PsiNewExpression)factory.createExpressionFromText("new " + type.getCanonicalText() + "{}", context);
|
||||
newExpression.getArrayInitializer().replace(initializer);
|
||||
return newExpression;
|
||||
}
|
||||
|
||||
private static boolean containsReference(final @NotNull PsiElement element,
|
||||
final @NotNull PsiField field) {
|
||||
final Ref<Boolean> result = new Ref<Boolean>(Boolean.FALSE);
|
||||
element.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitReferenceExpression(PsiReferenceExpression expression) {
|
||||
if (expression.resolve() == field) {
|
||||
result.set(Boolean.TRUE);
|
||||
}
|
||||
super.visitReferenceExpression(expression);
|
||||
}
|
||||
});
|
||||
return result.get().booleanValue();
|
||||
}
|
||||
|
||||
private static void replaceWithQualifiedReferences(@NotNull PsiElement expression, @NotNull PsiElement root, @NotNull PsiElementFactory factory) throws IncorrectOperationException {
|
||||
final PsiReference reference = expression.getReference();
|
||||
if (reference == null) {
|
||||
for (PsiElement child : expression.getChildren()) {
|
||||
replaceWithQualifiedReferences(child, root, factory);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
final PsiElement resolved = reference.resolve();
|
||||
if (resolved instanceof PsiVariable && !(resolved instanceof PsiField) && !PsiTreeUtil.isAncestor(root, resolved, false)) {
|
||||
final PsiVariable variable = (PsiVariable)resolved;
|
||||
PsiElement qualifiedExpr = factory.createExpressionFromText("this." + variable.getName(), expression);
|
||||
expression.replace(qualifiedExpr);
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
-123
@@ -16,25 +16,15 @@
|
||||
package com.intellij.codeInsight.intention.impl;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightBundle;
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil;
|
||||
import com.intellij.codeInsight.daemon.impl.quickfix.CreateFromUsageUtils;
|
||||
import com.intellij.codeInsight.highlighting.HighlightManager;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
|
||||
import com.intellij.codeInsight.intention.QuickFixFactory;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.colors.EditorColors;
|
||||
import com.intellij.openapi.editor.colors.EditorColorsManager;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.jsp.jspJava.JspClass;
|
||||
import com.intellij.psi.javadoc.PsiDocComment;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
@@ -42,7 +32,7 @@ import java.util.*;
|
||||
/**
|
||||
* @author cdr
|
||||
*/
|
||||
public class MoveInitializerToConstructorAction extends PsiElementBaseIntentionAction {
|
||||
public class MoveInitializerToConstructorAction extends BaseMoveInitializerToMethodAction {
|
||||
@Override
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
@@ -55,124 +45,48 @@ public class MoveInitializerToConstructorAction extends PsiElementBaseIntentionA
|
||||
return CodeInsightBundle.message("intention.move.initializer.to.constructor");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
|
||||
if (element instanceof PsiCompiledElement) return false;
|
||||
final PsiField field = PsiTreeUtil.getParentOfType(element, PsiField.class, false, PsiMember.class, PsiCodeBlock.class, PsiDocComment.class);
|
||||
if (field == null || field.hasModifierProperty(PsiModifier.STATIC)) return false;
|
||||
if (!field.hasInitializer()) return false;
|
||||
PsiClass psiClass = field.getContainingClass();
|
||||
|
||||
return psiClass != null && !psiClass.isInterface() && !(psiClass instanceof PsiAnonymousClass) && !(psiClass instanceof JspClass);
|
||||
protected Collection<String> getUnsuitableModifiers() {
|
||||
return Arrays.asList(PsiModifier.STATIC);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
|
||||
|
||||
int offset = editor.getCaretModel().getOffset();
|
||||
|
||||
PsiElement element = file.findElementAt(offset);
|
||||
final PsiField field = PsiTreeUtil.getParentOfType(element, PsiField.class);
|
||||
|
||||
assert field != null;
|
||||
PsiClass aClass = field.getContainingClass();
|
||||
PsiMethod[] constructors = aClass.getConstructors();
|
||||
Collection<PsiMethod> constructorsToAddInitialization;
|
||||
if (constructors.length == 0) {
|
||||
IntentionAction addDefaultConstructorFix = QuickFixFactory.getInstance().createAddDefaultConstructorFix(aClass);
|
||||
addDefaultConstructorFix.invoke(project, editor, file);
|
||||
editor.getCaretModel().moveToOffset(offset); //restore caret
|
||||
constructorsToAddInitialization = Arrays.asList(aClass.getConstructors());
|
||||
}
|
||||
else {
|
||||
constructorsToAddInitialization = new ArrayList<PsiMethod>(Arrays.asList(constructors));
|
||||
for (Iterator<PsiMethod> iterator = constructorsToAddInitialization.iterator(); iterator.hasNext();) {
|
||||
PsiMethod ctr = iterator.next();
|
||||
List<PsiMethod> chained = HighlightControlFlowUtil.getChainedConstructors(ctr);
|
||||
if (chained != null) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
protected Collection<PsiMethod> getOrCreateMethods(@NotNull Project project, @NotNull Editor editor, PsiFile file, @NotNull PsiClass aClass) {
|
||||
final Collection<PsiMethod> constructors = Arrays.asList(aClass.getConstructors());
|
||||
if (constructors.isEmpty()) {
|
||||
return createConstructor(project, editor, file, aClass);
|
||||
}
|
||||
|
||||
PsiExpressionStatement toMove = null;
|
||||
for (PsiMethod constructor : constructorsToAddInitialization) {
|
||||
PsiCodeBlock codeBlock = constructor.getBody();
|
||||
if (codeBlock == null) {
|
||||
CreateFromUsageUtils.setupMethodBody(constructor);
|
||||
codeBlock = constructor.getBody();
|
||||
}
|
||||
PsiExpressionStatement added = addAssignment(codeBlock, field);
|
||||
if (toMove == null) toMove = added;
|
||||
}
|
||||
field.getInitializer().delete();
|
||||
if (toMove != null) {
|
||||
PsiAssignmentExpression assignment = (PsiAssignmentExpression)toMove.getExpression();
|
||||
PsiExpression expression = assignment.getRExpression();
|
||||
EditorColorsManager manager = EditorColorsManager.getInstance();
|
||||
TextAttributes attributes = manager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
|
||||
HighlightManager.getInstance(project).addOccurrenceHighlights(editor, new PsiElement[] {expression}, attributes, false,null);
|
||||
}
|
||||
return removeChainedConstructors(constructors);
|
||||
}
|
||||
|
||||
private static PsiExpressionStatement addAssignment(@NotNull PsiCodeBlock codeBlock, @NotNull PsiField field) throws IncorrectOperationException {
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(codeBlock.getProject()).getElementFactory();
|
||||
PsiExpressionStatement statement = (PsiExpressionStatement)factory.createStatementFromText(field.getName()+" = y;", codeBlock);
|
||||
PsiAssignmentExpression expression = (PsiAssignmentExpression)statement.getExpression();
|
||||
PsiExpression initializer = field.getInitializer();
|
||||
if (initializer instanceof PsiArrayInitializerExpression) {
|
||||
PsiType type = initializer.getType();
|
||||
PsiNewExpression newExpression = (PsiNewExpression)factory.createExpressionFromText("new " + type.getCanonicalText() + "{}", codeBlock);
|
||||
newExpression.getArrayInitializer().replace(initializer);
|
||||
initializer = newExpression;
|
||||
}
|
||||
expression.getRExpression().replace(initializer);
|
||||
PsiStatement[] statements = codeBlock.getStatements();
|
||||
PsiElement anchor = null;
|
||||
for (PsiStatement blockStatement : statements) {
|
||||
if (blockStatement instanceof PsiExpressionStatement &&
|
||||
HighlightUtil.isSuperOrThisMethodCall(((PsiExpressionStatement)blockStatement).getExpression())) {
|
||||
continue;
|
||||
}
|
||||
if (containsReference(blockStatement, field)) {
|
||||
anchor = blockStatement;
|
||||
break;
|
||||
@NotNull
|
||||
private static Collection<PsiMethod> removeChainedConstructors(@NotNull Collection<PsiMethod> constructors) {
|
||||
final List<PsiMethod> result = new ArrayList<PsiMethod>(constructors);
|
||||
|
||||
final Iterator<PsiMethod> iterator = result.iterator();
|
||||
//noinspection ForLoopThatDoesntUseLoopVariable
|
||||
for (PsiMethod constructor = iterator.next(); iterator.hasNext(); constructor = iterator.next()) {
|
||||
final List<PsiMethod> chained = HighlightControlFlowUtil.getChainedConstructors(constructor);
|
||||
if (chained != null) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
PsiElement newStatement = codeBlock.addBefore(statement,anchor);
|
||||
replaceWithQualifiedReferences(newStatement, newStatement);
|
||||
return (PsiExpressionStatement)newStatement;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static boolean containsReference(final PsiElement element, final PsiField field) {
|
||||
final Ref<Boolean> result = new Ref<Boolean>(Boolean.FALSE);
|
||||
element.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override public void visitReferenceExpression(PsiReferenceExpression expression) {
|
||||
if (expression.resolve() == field) {
|
||||
result.set(Boolean.TRUE);
|
||||
}
|
||||
super.visitReferenceExpression(expression);
|
||||
}
|
||||
});
|
||||
return result.get().booleanValue();
|
||||
@NotNull
|
||||
private static Collection<PsiMethod> createConstructor(@NotNull Project project,
|
||||
@NotNull Editor editor,
|
||||
PsiFile file,
|
||||
@NotNull PsiClass aClass) {
|
||||
final IntentionAction addDefaultConstructorFix = QuickFixFactory.getInstance().createAddDefaultConstructorFix(aClass);
|
||||
final int offset = editor.getCaretModel().getOffset();
|
||||
addDefaultConstructorFix.invoke(project, editor, file);
|
||||
editor.getCaretModel().moveToOffset(offset); //restore caret
|
||||
return Arrays.asList(aClass.getConstructors());
|
||||
}
|
||||
|
||||
private static void replaceWithQualifiedReferences(final PsiElement expression, PsiElement root) throws IncorrectOperationException {
|
||||
PsiReference reference = expression.getReference();
|
||||
if (reference != null) {
|
||||
PsiElement resolved = reference.resolve();
|
||||
if (resolved instanceof PsiVariable && !(resolved instanceof PsiField) && !PsiTreeUtil.isAncestor(root, resolved, false)) {
|
||||
PsiVariable variable = (PsiVariable)resolved;
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(resolved.getProject()).getElementFactory();
|
||||
PsiElement qualifiedExpr = factory.createExpressionFromText("this." + variable.getName(), expression);
|
||||
expression.replace(qualifiedExpr);
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (PsiElement child : expression.getChildren()) {
|
||||
replaceWithQualifiedReferences(child, root);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-18
@@ -65,7 +65,7 @@ public class BaseGenerateTestSupportMethodAction extends BaseGenerateAction {
|
||||
|
||||
@Override
|
||||
protected boolean isValidForClass(PsiClass targetClass) {
|
||||
List<TestFramework> frameworks = findSuitableFrameworks(targetClass);
|
||||
List<TestFramework> frameworks = TestIntegrationUtils.findSuitableFrameworks(targetClass);
|
||||
if (frameworks.isEmpty()) return false;
|
||||
|
||||
for (TestFramework each : frameworks) {
|
||||
@@ -78,22 +78,6 @@ public class BaseGenerateTestSupportMethodAction extends BaseGenerateAction {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static List<TestFramework> findSuitableFrameworks(PsiClass targetClass) {
|
||||
TestFramework[] frameworks = Extensions.getExtensions(TestFramework.EXTENSION_NAME);
|
||||
for (TestFramework each : frameworks) {
|
||||
if (each.isTestClass(targetClass)) {
|
||||
return Collections.singletonList(each);
|
||||
}
|
||||
}
|
||||
|
||||
List<TestFramework> result = new SmartList<TestFramework>();
|
||||
for (TestFramework each : frameworks) {
|
||||
if (each.isPotentialTestClass(targetClass)) {
|
||||
result.add(each);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static class MyHandler implements CodeInsightActionHandler {
|
||||
private TestIntegrationUtils.MethodKind myMethodKind;
|
||||
@@ -104,7 +88,7 @@ public class BaseGenerateTestSupportMethodAction extends BaseGenerateAction {
|
||||
|
||||
public void invoke(@NotNull Project project, @NotNull final Editor editor, @NotNull final PsiFile file) {
|
||||
final PsiClass targetClass = findTargetClass(editor, file);
|
||||
final List<TestFramework> frameworks = findSuitableFrameworks(targetClass);
|
||||
final List<TestFramework> frameworks = TestIntegrationUtils.findSuitableFrameworks(targetClass);
|
||||
if (frameworks.isEmpty()) return;
|
||||
|
||||
if (frameworks.size() == 1) {
|
||||
|
||||
@@ -29,17 +29,20 @@ import com.intellij.ide.fileTemplates.FileTemplateManager;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.util.classMembers.MemberInfo;
|
||||
import com.intellij.util.SmartList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -234,4 +237,24 @@ public class TestIntegrationUtils {
|
||||
PsiElementFactory f = JavaPsiFacade.getInstance(project).getElementFactory();
|
||||
return f.createMethod("dummy", PsiType.VOID);
|
||||
}
|
||||
|
||||
public static List<TestFramework> findSuitableFrameworks(PsiClass targetClass) {
|
||||
TestFramework[] frameworks = Extensions.getExtensions(TestFramework.EXTENSION_NAME);
|
||||
for (TestFramework each : frameworks) {
|
||||
if (each.isTestClass(targetClass)) {
|
||||
return Collections.singletonList(each);
|
||||
}
|
||||
}
|
||||
|
||||
List<TestFramework> result = new SmartList<TestFramework>();
|
||||
for (TestFramework each : frameworks) {
|
||||
if (each.isPotentialTestClass(targetClass)) {
|
||||
result.add(each);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private TestIntegrationUtils() {
|
||||
}
|
||||
}
|
||||
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.testIntegration.intention;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightBundle;
|
||||
import com.intellij.codeInsight.TestFrameworks;
|
||||
import com.intellij.codeInsight.intention.impl.BaseMoveInitializerToMethodAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.testIntegration.TestIntegrationUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* @author cdr
|
||||
*/
|
||||
public class MoveInitializerToSetUpMethodAction extends BaseMoveInitializerToMethodAction {
|
||||
@Override
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
return getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getText() {
|
||||
return CodeInsightBundle.message("intention.move.initializer.to.set.up");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
|
||||
return super.isAvailable(project, editor, element) && TestIntegrationUtils.isTest(element);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Collection<String> getUnsuitableModifiers() {
|
||||
return Arrays.asList(PsiModifier.STATIC, PsiModifier.FINAL);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Collection<PsiMethod> getOrCreateMethods(@NotNull Project project, @NotNull Editor editor, PsiFile file, @NotNull PsiClass aClass) {
|
||||
final PsiMethod setUpMethod = TestFrameworks.getInstance().findOrCreateSetUpMethod(aClass);
|
||||
return setUpMethod == null ? Collections.<PsiMethod>emptyList() : Arrays.asList(setUpMethod);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Move initializer to setUp method" "true"
|
||||
package junit.framework;
|
||||
|
||||
public class X extends TestCase {
|
||||
int i;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
i = 7;
|
||||
}
|
||||
}
|
||||
|
||||
//HACK: making test possible without attaching jUnit
|
||||
public abstract class TestCase {
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Move initializer to setUp method" "true"
|
||||
public class X {
|
||||
<caret>int i;
|
||||
|
||||
@org.testng.annotations.BeforeMethod
|
||||
public void setUp() throws Exception {
|
||||
i = 7;
|
||||
}
|
||||
|
||||
@org.testng.annotations.Test
|
||||
public void test() {
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Move initializer to setUp method" "true"
|
||||
public class X {
|
||||
<caret>int i;
|
||||
|
||||
@org.junit.Before
|
||||
public void setUp() throws Exception {
|
||||
i = 7;
|
||||
}
|
||||
|
||||
@org.junit.Test
|
||||
public void test() {
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Move initializer to setUp method" "true"
|
||||
package junit.framework;
|
||||
|
||||
public class X extends TestCase {
|
||||
<caret>int i = 7;
|
||||
}
|
||||
|
||||
//HACK: making test possible without attaching jUnit
|
||||
public abstract class TestCase {
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// "Move initializer to setUp method" "false"
|
||||
public class X {
|
||||
<caret>int i = 7;
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Move initializer to setUp method" "true"
|
||||
public class X {
|
||||
<caret>int i = 7;
|
||||
|
||||
@org.testng.annotations.Test
|
||||
public void test() {
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Move initializer to setUp method" "true"
|
||||
public class X {
|
||||
<caret>int i = 7;
|
||||
|
||||
@org.junit.Test
|
||||
public void test() {
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInsight.intention;
|
||||
|
||||
import com.intellij.codeInsight.daemon.LightIntentionActionTestCase;
|
||||
|
||||
/**
|
||||
* @author ven
|
||||
*/
|
||||
public class MoveInitializerToSetUpMethodActionTest extends LightIntentionActionTestCase {
|
||||
public void test() throws Exception { doAllTests(); }
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/daemonCodeAnalyzer/quickFix/moveInitializerToSetUp";
|
||||
}
|
||||
}
|
||||
@@ -390,6 +390,7 @@ inspection.i18n.option.ignore.comment.title=Non-Nls comment pattern
|
||||
inspection.i18n.option.ignore.assigned.to.constants=Ignore literals assigned to constants
|
||||
inspection.i18n.option.ignore.tostring=Ignore contents of toString() method
|
||||
intention.move.initializer.to.constructor=Move initializer to constructor
|
||||
intention.move.initializer.to.set.up=Move initializer to setUp method
|
||||
intention.move.field.assignment.to.declaration=Move assignment to field declaration
|
||||
i18nize.jsp.error=Please select JSP text to I18nize.\nMake sure you have not selected any scriptlets, custom tags or other foreign languages elements.\nAlso, HTML tags inside selection must be balanced.
|
||||
i18nize.error.title=Cannot I18nize Selection
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
public class X extends TestCase{
|
||||
int field;
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
field = 0;
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
public class X extends TestCase{
|
||||
<spot>int field = 0;</spot>
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
|
||||
This intention moves field initialization into suitable setUp method.
|
||||
</body>
|
||||
</html>
|
||||
@@ -615,6 +615,10 @@
|
||||
<className>com.intellij.codeInsight.intention.impl.MoveInitializerToConstructorAction</className>
|
||||
<category>Declaration</category>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<className>com.intellij.testIntegration.intention.MoveInitializerToSetUpMethodAction</className>
|
||||
<category>Declaration</category>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<className>com.intellij.codeInsight.intention.impl.MoveFieldAssignmentToInitializerAction</className>
|
||||
<category>Declaration</category>
|
||||
|
||||
Reference in New Issue
Block a user