From 8463342b69bc4828c54cbbaef997ade70af828e3 Mon Sep 17 00:00:00 2001 From: peter Date: Tue, 8 Sep 2015 11:24:45 +0200 Subject: [PATCH] remove dfa ConditionCheckManager mortal remains --- .../dataFlow/ConditionCheckDialog.java | 296 -------- .../dataFlow/DataFlowInspection.java | 20 - .../dataFlow/MethodCheckerDetailsDialog.java | 709 ------------------ .../codeInsight/ConditionCheckManager.java | 183 ----- .../codeInsight/ConditionChecker.java | 431 ----------- resources/src/META-INF/IdeaPlugin.xml | 3 - 6 files changed, 1642 deletions(-) delete mode 100644 java/java-impl/src/com/intellij/codeInspection/dataFlow/ConditionCheckDialog.java delete mode 100644 java/java-impl/src/com/intellij/codeInspection/dataFlow/MethodCheckerDetailsDialog.java delete mode 100644 java/java-indexing-impl/src/com/intellij/codeInsight/ConditionCheckManager.java delete mode 100644 java/java-psi-impl/src/com/intellij/codeInsight/ConditionChecker.java diff --git a/java/java-impl/src/com/intellij/codeInspection/dataFlow/ConditionCheckDialog.java b/java/java-impl/src/com/intellij/codeInspection/dataFlow/ConditionCheckDialog.java deleted file mode 100644 index a1a9a1bd01bf..000000000000 --- a/java/java-impl/src/com/intellij/codeInspection/dataFlow/ConditionCheckDialog.java +++ /dev/null @@ -1,296 +0,0 @@ -/* - * Copyright 2000-2015 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.codeInspection.dataFlow; - -import com.intellij.codeInsight.ConditionCheckManager; -import com.intellij.codeInsight.ConditionChecker; -import com.intellij.codeInspection.InspectionsBundle; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.ui.DialogWrapper; -import com.intellij.openapi.ui.Splitter; -import com.intellij.ui.*; -import com.intellij.ui.components.JBList; -import com.intellij.util.ui.JBUI; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import javax.swing.*; -import java.awt.*; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * Dialog that appears when user clicks the "Configure IsNull/IsNotNull/True/False Check/Assertion Methods" - * on the Errors dialog for the Constant Conditions Inspection. It is divided into 6 parts - *
    - *
  1. Is Null Check MethodsPanel
  2. - *
  3. Is Not Null Check MethodsPanel
  4. - *
  5. Assert Is Null MethodsPanel
  6. - *
  7. Assert Is Not Null MethodsPanel
  8. - *
  9. Assert True MethodsPanel
  10. - *
  11. Assert False MethodsPanel
  12. - *
- * - * @author Johnny Clark - * Creation Date: 8/3/12 - */ -public class ConditionCheckDialog extends DialogWrapper { - private final Project myProject; - @NotNull private final Splitter mainSplitter; - @NotNull private final MethodsPanel myIsNullCheckMethodPanel; - @NotNull private final MethodsPanel myIsNotNullCheckMethodPanel; - @NotNull private final MethodsPanel myAssertIsNullMethodPanel; - @NotNull private final MethodsPanel myAssertIsNotNullMethodPanel; - @NotNull private final MethodsPanel myAssertTrueMethodPanel; - @NotNull private final MethodsPanel myAssertFalseMethodPanel; - - public ConditionCheckDialog(Project project, String mainDialogTitle) { - super(project, true); - myProject = project; - - final ConditionCheckManager manager = ConditionCheckManager.getInstance(myProject); - mainSplitter = new Splitter(true, 0.3f); - final Splitter topThirdSplitter = new Splitter(false); - final Splitter bottomTwoThirdsSplitter = new Splitter(true); - final Splitter isNullIsNotNullCheckMethodSplitter = new Splitter(false); - final Splitter assertTrueFalseMethodSplitter = new Splitter(false); - - List isNullCheckMethods = new ArrayList(manager.getIsNullCheckMethods()); - List isNotNullCheckMethods = new ArrayList(manager.getIsNotNullCheckMethods()); - List assertIsNullMethods = new ArrayList(manager.getAssertIsNullMethods()); - List assertIsNotNullMethods = new ArrayList(manager.getAssertIsNotNullMethods()); - List assertTrueMethods = new ArrayList(manager.getAssertTrueMethods()); - List assertFalseMethods = new ArrayList(manager.getAssertFalseMethods()); - - myAssertIsNullMethodPanel = new MethodsPanel(assertIsNullMethods, ConditionChecker.Type.ASSERT_IS_NULL_METHOD, myProject); - myAssertIsNotNullMethodPanel = new MethodsPanel(assertIsNotNullMethods, ConditionChecker.Type.ASSERT_IS_NOT_NULL_METHOD, myProject); - myIsNullCheckMethodPanel = new MethodsPanel(isNullCheckMethods, ConditionChecker.Type.IS_NULL_METHOD, myProject); - myIsNotNullCheckMethodPanel = new MethodsPanel(isNotNullCheckMethods, ConditionChecker.Type.IS_NOT_NULL_METHOD, myProject); - myAssertTrueMethodPanel = new MethodsPanel(assertTrueMethods, ConditionChecker.Type.ASSERT_TRUE_METHOD, myProject); - myAssertFalseMethodPanel = new MethodsPanel(assertFalseMethods, ConditionChecker.Type.ASSERT_FALSE_METHOD, myProject); - - isNullIsNotNullCheckMethodSplitter.setFirstComponent(myIsNullCheckMethodPanel.getComponent()); - isNullIsNotNullCheckMethodSplitter.setSecondComponent(myIsNotNullCheckMethodPanel.getComponent()); - assertTrueFalseMethodSplitter.setFirstComponent(myAssertTrueMethodPanel.getComponent()); - assertTrueFalseMethodSplitter.setSecondComponent(myAssertFalseMethodPanel.getComponent()); - - topThirdSplitter.setFirstComponent(myAssertIsNullMethodPanel.getComponent()); - topThirdSplitter.setSecondComponent(myAssertIsNotNullMethodPanel.getComponent()); - bottomTwoThirdsSplitter.setFirstComponent(isNullIsNotNullCheckMethodSplitter); - bottomTwoThirdsSplitter.setSecondComponent(assertTrueFalseMethodSplitter); - - mainSplitter.setFirstComponent(topThirdSplitter); - mainSplitter.setSecondComponent(bottomTwoThirdsSplitter); - - topThirdSplitter.setPreferredSize(JBUI.size(600, 150)); - bottomTwoThirdsSplitter.setPreferredSize(JBUI.size(600, 300)); - - myAssertIsNullMethodPanel - .setOtherMethodsPanels(myAssertIsNotNullMethodPanel, myIsNullCheckMethodPanel, myIsNotNullCheckMethodPanel, myAssertTrueMethodPanel, - myAssertFalseMethodPanel); - myAssertIsNotNullMethodPanel - .setOtherMethodsPanels(myAssertIsNullMethodPanel, myIsNullCheckMethodPanel, myIsNotNullCheckMethodPanel, myAssertTrueMethodPanel, - myAssertFalseMethodPanel); - myIsNullCheckMethodPanel - .setOtherMethodsPanels(myAssertIsNullMethodPanel, myAssertIsNotNullMethodPanel, myIsNotNullCheckMethodPanel, myAssertTrueMethodPanel, - myAssertFalseMethodPanel); - myIsNotNullCheckMethodPanel - .setOtherMethodsPanels(myAssertIsNullMethodPanel, myAssertIsNotNullMethodPanel, myIsNullCheckMethodPanel, myAssertTrueMethodPanel, - myAssertFalseMethodPanel); - myAssertTrueMethodPanel - .setOtherMethodsPanels(myAssertIsNullMethodPanel, myAssertIsNotNullMethodPanel, myIsNotNullCheckMethodPanel, myIsNullCheckMethodPanel, - myAssertFalseMethodPanel); - myAssertFalseMethodPanel - .setOtherMethodsPanels(myAssertIsNullMethodPanel, myAssertIsNotNullMethodPanel, myIsNotNullCheckMethodPanel, myIsNullCheckMethodPanel, - myAssertTrueMethodPanel); - - init(); - setTitle(mainDialogTitle); - } - - @Override - protected JComponent createCenterPanel() { - return mainSplitter; - } - - @Override - protected void doOKAction() { - final ConditionCheckManager manager = ConditionCheckManager.getInstance(myProject); - manager.setIsNotNullCheckMethods(myIsNotNullCheckMethodPanel.getConditionChecker()); - manager.setIsNullCheckMethods(myIsNullCheckMethodPanel.getConditionChecker()); - manager.setAssertIsNotNullMethods(myAssertIsNotNullMethodPanel.getConditionChecker()); - manager.setAssertIsNullMethods(myAssertIsNullMethodPanel.getConditionChecker()); - manager.setAssertTrueMethods(myAssertTrueMethodPanel.getConditionChecker()); - manager.setAssertFalseMethods(myAssertFalseMethodPanel.getConditionChecker()); - - super.doOKAction(); - } - - /** - * Is Null, Is Not Null, Assert True and Assert False Method Panel at the top of the main Dialog. - */ - class MethodsPanel { - @NotNull private final JBList myList; - @NotNull private final JPanel myPanel; - @NotNull private final Project myProject; - private Set otherPanels; - - public MethodsPanel(final List checkers, final ConditionChecker.Type type, @NotNull final Project myProject) { - this.myProject = myProject; - myList = new JBList(new CollectionListModel(checkers)); - myPanel = new JPanel(new BorderLayout()); - myPanel.setBorder(IdeBorderFactory.createTitledBorder(initTitle(type), false, new Insets(10, 0, 0, 0))); - myPanel.setPreferredSize(JBUI.size(400, 150)); - - myList.setCellRenderer(new ColoredListCellRenderer() { - @Override - protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus) { - String s = value.toString(); - if (s.contains("*")) { - int indexOfAsterix1 = s.indexOf("*"); - int indexOfAsterix2 = s.lastIndexOf("*"); - if (indexOfAsterix1 >= 0 && - indexOfAsterix1 < s.length() && - indexOfAsterix2 >= 0 && - indexOfAsterix2 < s.length() && - indexOfAsterix1 < indexOfAsterix2) { - append(s.substring(0, indexOfAsterix1), SimpleTextAttributes.REGULAR_ATTRIBUTES); - append(s.substring(indexOfAsterix1 + 1, indexOfAsterix2), SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES); - append(s.substring(indexOfAsterix2 + 1), SimpleTextAttributes.REGULAR_ATTRIBUTES); - } - else { - append(s, SimpleTextAttributes.REGULAR_ATTRIBUTES); - } - } - } - }); - - final ToolbarDecorator toolbarDecorator = - ToolbarDecorator.createDecorator(myList).disableUpDownActions().setAddAction(new AnActionButtonRunnable() { - @Override - public void run(AnActionButton anActionButton) { - chooseMethod(null, type, myList.getModel().getSize()); - } - }).setRemoveAction(new AnActionButtonRunnable() { - @Override - public void run(AnActionButton anActionButton) { - CollectionListModel model = getCollectionListModel(); - if (myList.getSelectedIndex() >= 0 && myList.getSelectedIndex() < model.getSize()) { - model.remove(myList.getSelectedIndex()); - } - } - }); - - myList.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent e) { - if (e.getClickCount() == 2) { - int index = myList.locationToIndex(e.getPoint()); - CollectionListModel model = getCollectionListModel(); - if (index >= 0 && model.getSize() > index) { - chooseMethod(model.getElementAt(index), type, index); - } - } - } - }); - final JPanel panel = toolbarDecorator.createPanel(); - myPanel.add(panel); - myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - } - - private String initTitle(@NotNull ConditionChecker.Type type) { - if (type.equals(ConditionChecker.Type.IS_NULL_METHOD)) { - return InspectionsBundle.message("configure.checker.option.isNull.method.panel.title"); - } - else if (type.equals(ConditionChecker.Type.IS_NOT_NULL_METHOD)) { - return InspectionsBundle.message("configure.checker.option.isNotNull.method.panel.title"); - } - else if (type.equals(ConditionChecker.Type.ASSERT_IS_NULL_METHOD)) { - return InspectionsBundle.message("configure.checker.option.assert.isNull.method.panel.title"); - } - else if (type.equals(ConditionChecker.Type.ASSERT_IS_NOT_NULL_METHOD)) { - return InspectionsBundle.message("configure.checker.option.assert.isNotNull.method.panel.title"); - } - else if (type.equals(ConditionChecker.Type.ASSERT_TRUE_METHOD)) { - return InspectionsBundle.message("configure.checker.option.assert.true.method.panel.title"); - } - else if (type.equals(ConditionChecker.Type.ASSERT_FALSE_METHOD)) { - return InspectionsBundle.message("configure.checker.option.assert.false.method.panel.title"); - } - else { - throw new IllegalArgumentException("MethodCheckerDetailsDialog does not support type " + type); - } - } - - private void chooseMethod(@Nullable ConditionChecker checker, ConditionChecker.Type type, int index) { - MethodCheckerDetailsDialog pickMethodPanel = - new MethodCheckerDetailsDialog(checker, type, myProject, myPanel, getConditionCheckers(), getOtherCheckers()); - pickMethodPanel.show(); - ConditionChecker chk = pickMethodPanel.getConditionChecker(); - if (chk != null) { - CollectionListModel model = getCollectionListModel(); - if (model.getSize() <= index) { - model.add(chk); - } - else { - model.setElementAt(chk, index); - } - } - } - - private CollectionListModel getCollectionListModel() { - //noinspection unchecked - return (CollectionListModel)myList.getModel(); - } - - @NotNull - public JPanel getComponent() { - return myPanel; - } - - public List getConditionChecker() { - CollectionListModel model = getCollectionListModel(); - return new ArrayList(model.getItems()); - } - - public Set getConditionCheckers() { - Set set = new HashSet(); - set.addAll(getConditionChecker()); - return set; - } - - public void setOtherMethodsPanels(MethodsPanel p1, MethodsPanel p2, MethodsPanel p3, MethodsPanel p4, MethodsPanel p5) { - otherPanels = new HashSet(); - otherPanels.add(p1); - otherPanels.add(p2); - otherPanels.add(p3); - otherPanels.add(p4); - otherPanels.add(p5); - } - - public Set getOtherCheckers() { - Set otherCheckers = new HashSet(); - for (MethodsPanel otherPanel : otherPanels) { - otherCheckers.addAll(otherPanel.getConditionCheckers()); - } - return otherCheckers; - } - } -} diff --git a/java/java-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspection.java b/java/java-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspection.java index de132b85be40..fd426f327a16 100644 --- a/java/java-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspection.java @@ -147,26 +147,6 @@ public class DataFlowInspection extends DataFlowInspectionBase { gc.insets.bottom = 15; add(configureAnnotations, gc); - if ("true".equals(System.getProperty("dfa.inspection.show.method.configuration", "false"))) { - final JButton configureCheckAnnotations = new JButton(InspectionsBundle.message("configure.checker.option.button")); - configureCheckAnnotations.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - Project project = CommonDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext(OptionsPanel.this)); - if (project == null) project = ProjectManager.getInstance().getDefaultProject(); - final ConditionCheckDialog dialog = new ConditionCheckDialog(project, - InspectionsBundle.message("configure.checker.option.main.dialog.title") - ); - dialog.show(); - } - }); - gc.gridy++; - gc.fill = GridBagConstraints.NONE; - gc.insets.left = 20; - gc.insets.bottom = 15; - add(configureCheckAnnotations, gc); - } - gc.fill = GridBagConstraints.HORIZONTAL; gc.weighty = 1; gc.insets.left = 0; diff --git a/java/java-impl/src/com/intellij/codeInspection/dataFlow/MethodCheckerDetailsDialog.java b/java/java-impl/src/com/intellij/codeInspection/dataFlow/MethodCheckerDetailsDialog.java deleted file mode 100644 index 008ae770153a..000000000000 --- a/java/java-impl/src/com/intellij/codeInspection/dataFlow/MethodCheckerDetailsDialog.java +++ /dev/null @@ -1,709 +0,0 @@ -/* - * Copyright 2000-2013 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.codeInspection.dataFlow; - -import com.intellij.codeInsight.*; -import com.intellij.codeInspection.*; -import com.intellij.ide.util.*; -import com.intellij.openapi.editor.event.*; -import com.intellij.openapi.project.*; -import com.intellij.openapi.ui.*; -import com.intellij.psi.*; -import com.intellij.psi.search.*; -import com.intellij.ui.*; -import org.jetbrains.annotations.*; - -import javax.swing.*; -import java.awt.*; -import java.awt.event.*; -import java.beans.*; -import java.util.*; -import java.util.List; - -import static com.intellij.codeInsight.ConditionChecker.Type.*; - -/** - * Dialog that appears when the user clicks the Add Button or double clicks a row item in a MethodsPanel. The MethodsPanel is accessed from the ConditionCheckDialog - */ -class MethodCheckerDetailsDialog extends DialogWrapper implements PropertyChangeListener, ItemListener { - @NotNull private final ConditionChecker.Type myType; - @NotNull private final Project myProject; - @NotNull private final ParameterDropDown parameterDropDown; - @NotNull private final MethodDropDown methodDropDown; - @NotNull private final ClassField classField; - @NotNull private final Set myOtherCheckers; - @Nullable private final ConditionChecker myPreviouslySelectedChecker; - /** - * Set by the OK and/or Cancel actions so that the caller can retrieve it via a call to getMethodIsNullIsNotNullChecker - */ - @Nullable private ConditionChecker mySelectedChecker; - - MethodCheckerDetailsDialog(@Nullable ConditionChecker previouslySelectedChecker, - @NotNull ConditionChecker.Type type, - @NotNull Project project, - @NotNull Component component, - @NotNull Set otherCheckersSameType, - @NotNull Set otherCheckers) { - super(component, true); - if (!isSupported(type)) throw new IllegalArgumentException("Type is invalid " + type); - - myProject = project; - myType = type; - myOtherCheckers = new HashSet(otherCheckersSameType); - myOtherCheckers.addAll(otherCheckers); - myPreviouslySelectedChecker = previouslySelectedChecker; - if (myPreviouslySelectedChecker != null) myOtherCheckers.remove(myPreviouslySelectedChecker); - - PsiClass psiClass = null; - PsiMethod psiMethod = null; - PsiParameter psiParameter = null; - if (previouslySelectedChecker != null) { - psiClass = - JavaPsiFacade.getInstance(myProject).findClass(previouslySelectedChecker.getClassName(), GlobalSearchScope.allScope(myProject)); - if (psiClass != null) { - for (PsiMethod method : psiClass.findMethodsByName(previouslySelectedChecker.getMethodName(), true)) { - if (previouslySelectedChecker.equals(buildParameterClassListFromPsiMethod(method))) { - psiMethod = method; - break; - } - } - } - - if (psiMethod != null) { - PsiParameter[] parameters = psiMethod.getParameterList().getParameters(); - if (parameters.length - 1 >= previouslySelectedChecker.getCheckedParameterIndex()) { - psiParameter = parameters[previouslySelectedChecker.getCheckedParameterIndex()]; - } - } - } - - if (psiClass == null || psiMethod == null || psiParameter == null) { - psiClass = null; - psiMethod = null; - psiParameter = null; - } - - classField = new ClassField(myProject, psiClass); - methodDropDown = new MethodDropDown(psiClass, psiMethod, myType, MethodDropDown.buildModel()); - parameterDropDown = new ParameterDropDown(psiMethod, psiParameter, ParameterDropDown.buildModel(), myType); - classField.addPropertyChangeListener(methodDropDown); - classField.addPropertyChangeListener(parameterDropDown); - classField.addPropertyChangeListener(this); - methodDropDown.addItemListener(parameterDropDown); - methodDropDown.addItemListener(this); - parameterDropDown.addItemListener(this); - init(); - checkOkActionEnable(); - setTitle(initTitle(type)); - } - - private static boolean isSupported(ConditionChecker.Type type) { - return type == IS_NULL_METHOD || type == IS_NOT_NULL_METHOD || - type == ASSERT_IS_NULL_METHOD || type == ASSERT_IS_NOT_NULL_METHOD || - type == ASSERT_TRUE_METHOD || type == ASSERT_FALSE_METHOD; - } - - private static List buildParameterClassListFromPsiMethod(PsiMethod psiMethod) { - List parameterClasses = new ArrayList(); - PsiParameter[] parameters = psiMethod.getParameterList().getParameters(); - for (PsiParameter param : parameters) { - PsiTypeElement typeElement = param.getTypeElement(); - if (typeElement == null) return new ArrayList(); - - PsiType psiType = typeElement.getType(); - - parameterClasses.add(psiType.getCanonicalText()); - } - return parameterClasses; - } - - private static String initTitle(@NotNull ConditionChecker.Type type) { - if (type.equals(IS_NULL_METHOD)) { - return InspectionsBundle.message("configure.checker.option.isNull.add.method.checker.dialog.title"); - } - else if (type.equals(IS_NOT_NULL_METHOD)) { - return InspectionsBundle.message("configure.checker.option.isNotNull.add.method.checker.dialog.title"); - } - else if (type.equals(ASSERT_IS_NULL_METHOD)) { - return InspectionsBundle.message("configure.checker.option.assert.isNull.add.method.checker.dialog.title"); - } - else if (type.equals(ASSERT_IS_NOT_NULL_METHOD)) { - return InspectionsBundle.message("configure.checker.option.assert.isNotNull.add.method.checker.dialog.title"); - } - else if (type.equals(ASSERT_TRUE_METHOD)) { - return InspectionsBundle.message("configure.checker.option.assert.true.add.method.checker.dialog.title"); - } - else if (type.equals(ASSERT_FALSE_METHOD)) { - return InspectionsBundle.message("configure.checker.option.assert.false.add.method.checker.dialog.title"); - } - else { - throw new IllegalArgumentException("MethodCheckerDetailsDialog does not support type " + type); - } - } - - @Override - protected JComponent createCenterPanel() { - final JPanel panel = new JPanel(); - panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); - - final LabeledComponent classComponent = new LabeledComponent(); - final LabeledComponent methodComponent = new LabeledComponent(); - final LabeledComponent parameterComponent = new LabeledComponent(); - - classComponent.setText("Class"); - methodComponent.setText("Method"); - parameterComponent.setText("Parameter"); - - classComponent.setComponent(classField); - methodComponent.setComponent(methodDropDown); - parameterComponent.setComponent(parameterDropDown); - - panel.add(classComponent); - panel.add(methodComponent); - panel.add(parameterComponent); - - return panel; - } - - @Nullable - ConditionChecker getConditionChecker() { - return mySelectedChecker; - } - - @Nullable - private ConditionChecker buildConditionChecker() { - PsiClass psiClass = classField.getPsiClass(); - PsiMethod psiMethod = methodDropDown.getSelectedPsiMethod(); - PsiParameter psiParameter = parameterDropDown.getSelectedPsiParameter(); - if (psiClass != null && psiMethod != null && psiParameter != null) { - return new ConditionChecker.FromPsiBuilder(psiMethod, psiParameter, myType).build(); - } - else { - return null; - } - } - - private boolean overlaps(ConditionChecker thisChecker) { - for (ConditionChecker overlappingChecker : myOtherCheckers) { - if (thisChecker.overlaps(overlappingChecker)) { - Messages.showMessageDialog(myProject, InspectionsBundle.message("configure.checker.option.overlap.error.msg") + - " " + - overlappingChecker.getConditionCheckType() + " " + overlappingChecker.toString(), - InspectionsBundle.message("configure.checker.option.overlap.error.title"), Messages.getErrorIcon()); - return true; - } - } - return false; - } - - @Override - public void propertyChange(PropertyChangeEvent evt) { - checkOkActionEnable(); - } - - @Override - public void itemStateChanged(ItemEvent e) { - checkOkActionEnable(); - } - - private void checkOkActionEnable() { - if (classField.getPsiClass() == null || - methodDropDown.getSelectedPsiMethod() == null || - parameterDropDown.getSelectedPsiParameter() == null) { - setOKActionEnabled(false); - } - else { - setOKActionEnabled(true); - } - } - - @Override - protected void doOKAction() { - ConditionChecker checker = buildConditionChecker(); - if (checker != null && !overlaps(checker)) { - if (checker.equals(myPreviouslySelectedChecker)) { - mySelectedChecker = myPreviouslySelectedChecker; - } - else { - mySelectedChecker = checker; - } - super.doOKAction(); - } - } - - @Override - public boolean isOKActionEnabled() { - if (!myOKAction.isEnabled()) return false; - PsiClass psiClass = classField.getPsiClass(); - PsiMethod psiMethod = methodDropDown.getSelectedPsiMethod(); - PsiParameter psiParameter = parameterDropDown.getSelectedPsiParameter(); - if (psiClass == null || psiMethod == null || psiParameter == null) { - return false; - } - else { - return true; - } - } - - /** - * Input Text Field for Class Name - */ - static class ClassField extends EditorTextFieldWithBrowseButton implements ActionListener, DocumentListener { - public static final String PROPERTY_PSICLASS = "ClassField.myPsiClass"; - @NotNull private final Project myProject; - @Nullable private PsiClass myPsiClass; - - public ClassField(@NotNull Project project, @Nullable PsiClass psiClass) { - super(project, true, buildVisibilityChecker()); - myProject = project; - myPsiClass = psiClass; - setPreferredSize(new Dimension(500, (int)getPreferredSize().getHeight())); - if (myPsiClass != null) { //noinspection ConstantConditions - setText(myPsiClass.getQualifiedName()); - } - addActionListener(this); - getChildComponent().addDocumentListener(this); - } - - private static JavaCodeFragment.VisibilityChecker buildVisibilityChecker() { - return new JavaCodeFragment.VisibilityChecker() { - @Override - public Visibility isDeclarationVisible(PsiElement declaration, PsiElement place) { - return Visibility.VISIBLE; - } - }; - } - - @Override - public void actionPerformed(ActionEvent e) { - final TreeClassChooser chooser = TreeClassChooserFactory.getInstance(myProject) - .createNoInnerClassesScopeChooser("Choose Class", GlobalSearchScope.allScope(myProject), new ClassFilter() { - @Override - public boolean isAccepted(PsiClass aClass) { - return !aClass.isAnnotationType(); - } - }, null); - chooser.showDialog(); - PsiClass psiClass = chooser.getSelected(); - if (psiClass != null) { //noinspection ConstantConditions - setText(chooser.getSelected().getQualifiedName()); - } - } - - @Nullable - public PsiClass getPsiClass() { - return myPsiClass; - } - - @Override - public void beforeDocumentChange(DocumentEvent event) { - } - - @Override - public void documentChanged(DocumentEvent event) { - String className = event.getDocument().getText(); - PsiClass psiClass = null; - if (className != null) { - psiClass = JavaPsiFacade.getInstance(myProject).findClass(className, GlobalSearchScope.allScope(myProject)); - } - - if (psiClass != null && myPsiClass != null) { - if (!psiClass.equals(myPsiClass)) { - firePropertyChange(PROPERTY_PSICLASS, myPsiClass, psiClass); - myPsiClass = psiClass; - } - } - else if (psiClass != null) { - firePropertyChange(PROPERTY_PSICLASS, myPsiClass, psiClass); - myPsiClass = psiClass; - } - else if (myPsiClass != null) { - firePropertyChange(PROPERTY_PSICLASS, myPsiClass, psiClass); - myPsiClass = null; - } - } - } - - /** - * Drop Down for picking Method Name - */ - static class MethodDropDown extends JComboBox implements PropertyChangeListener { - @NotNull private final ConditionChecker.Type myType; - @NotNull private final SortedComboBoxModel myModel; - @Nullable private PsiClass myPsiClass; - - MethodDropDown(@Nullable PsiClass psiClass, - @Nullable PsiMethod psiMethod, - @NotNull ConditionChecker.Type type, - @NotNull SortedComboBoxModel model) { - super(model); - - if (!isSupported(type)) throw new IllegalArgumentException("Type is invalid " + type); - - myPsiClass = psiClass; - myType = type; - myModel = model; - setEnabled(myPsiClass != null); - initValues(); - if (psiMethod != null) { - for (Iterator iterator = myModel.iterator(); iterator.hasNext(); ) { - MethodWrapper methodWrapper = iterator.next(); - if (methodWrapper.getPsiMethod().equals(psiMethod)) { - setSelectedItem(methodWrapper); - } - } - } - } - - private static boolean isMethodFromJavaLangObject(PsiMethod method) { - if (method == null) return false; - - PsiClass containingClass = method.getContainingClass(); - if (containingClass == null) return false; - String name = containingClass.getQualifiedName(); - if (name == null) return false; - - if (CommonClassNames.JAVA_LANG_OBJECT.equals(name)) return true; - - return false; - } - - @NotNull - public static SortedComboBoxModel buildModel() { - return new SortedComboBoxModel(new Comparator() { - @Override - public int compare(MethodWrapper o1, MethodWrapper o2) { - return o1.compareTo(o2); - } - }); - } - - private void initValues() { - if (myPsiClass != null) { - myModel.clear(); - myModel.setSelectedItem(null); - PsiMethod[] allMethods = myPsiClass.getAllMethods(); - for (PsiMethod method : allMethods) { - MethodWrapper methodWrapper = new MethodWrapper(method); - if (qualifies(method) && !myModel.getItems().contains(methodWrapper)) myModel.add(methodWrapper); - } - } - } - - public boolean qualifies(PsiMethod psiMethod) { - if (isMethodFromJavaLangObject(psiMethod)) { - return false; - } - - final PsiParameter[] parameters = psiMethod.getParameterList().getParameters(); - if (parameters.length < 1) { - return false; - } - - if (myType == IS_NULL_METHOD || myType == IS_NOT_NULL_METHOD) { - PsiType returnType = psiMethod.getReturnType(); - if (returnType != PsiType.BOOLEAN && (returnType == null || !returnType.getCanonicalText().equals(Boolean.class.toString()))) { - return false; - } - } - else if (myType == ASSERT_TRUE_METHOD || myType == ASSERT_FALSE_METHOD) { - boolean booleanParamExists = false; - for (PsiParameter psiParameter : parameters) { - PsiType type = psiParameter.getType(); - if (type.equals(PsiType.BOOLEAN) || type.getCanonicalText().equals(Boolean.class.toString())) { - booleanParamExists = true; - break; - } - } - - if (!booleanParamExists) { - return false; - } - } - // Else it's ASSERT_IS_NULL_METHOD or ASSERT_IS_NOT_NULL_METHOD. - // In that case there is no additional validation - - return true; - } - - /** - * Called when ClassField is set and when user selects entry in the MethodDropDown - */ - @Override - public void propertyChange(PropertyChangeEvent evt) { - if (evt.getPropertyName().equals(ClassField.PROPERTY_PSICLASS)) { - if (evt.getNewValue() == null) { - clear(); - } - else { - setEnabled(true); - if (myPsiClass == null || !myPsiClass.equals(evt.getNewValue())) { // ClassChanged so refresh list - myPsiClass = (PsiClass)evt.getNewValue(); - initValues(); - } - } - } - } - - public void clear() { - myModel.clear(); - myModel.setSelectedItem(null); - setEnabled(false); - myPsiClass = null; - } - - @Nullable - public PsiMethod getSelectedPsiMethod() { - MethodWrapper methodWrapper = myModel.getSelectedItem(); - if (methodWrapper == null) return null; - - return methodWrapper.getPsiMethod(); - } - } - - /** - * Drop Down for picking Parameter Name - */ - static class ParameterDropDown extends JComboBox implements PropertyChangeListener, ItemListener { - @NotNull private final SortedComboBoxModel myModel; - @NotNull private final ConditionChecker.Type myType; - @Nullable private PsiMethod myPsiMethod; - - public ParameterDropDown(@Nullable PsiMethod psiMethod, - @Nullable PsiParameter psiParameter, - @NotNull SortedComboBoxModel model, - @NotNull ConditionChecker.Type type) { - super(model); - - if (!isSupported(type)) throw new IllegalArgumentException("Type is invalid " + type); - - myPsiMethod = psiMethod; - myModel = model; - myType = type; - - if (myPsiMethod != null) { - setEnabled(true); - myModel.addAll(getParameterWrappers()); - if (psiParameter != null) { - for (Iterator iterator = myModel.iterator(); iterator.hasNext(); ) { - ParameterWrapper wrapper = (ParameterWrapper)iterator.next(); - if (wrapper.getPsiParameter().equals(psiParameter)) setSelectedItem(wrapper); - } - } - } - else { - setEnabled(false); - } - } - - public static SortedComboBoxModel buildModel() { - return new SortedComboBoxModel(new Comparator() { - @Override - public int compare(ParameterWrapper o1, ParameterWrapper o2) { - return o1.compareTo(o2); - } - }); - } - - List getParameterWrappers() { - List wrappers = new ArrayList(); - if (myPsiMethod != null) { - PsiParameterList parameterList = myPsiMethod.getParameterList(); - for (int i = 0; i < parameterList.getParameters().length; i++) { - PsiParameter psiParameter = parameterList.getParameters()[i]; - if (myType == ASSERT_TRUE_METHOD || myType == ASSERT_FALSE_METHOD) { - PsiType type = psiParameter.getType(); - if (type.equals(PsiType.BOOLEAN) || type.getCanonicalText().equals(Boolean.class.toString())) { - wrappers.add(new ParameterWrapper(psiParameter, i)); - } - } - else { - wrappers.add(new ParameterWrapper(psiParameter, i)); - } - - } - } - return wrappers; - } - - @Override - public void itemStateChanged(ItemEvent e) { - if (e.getSource() instanceof MethodDropDown) { // The MethodDropDown has changed. - MethodDropDown methodDropDown = (MethodDropDown)e.getSource(); - if (methodDropDown.getSelectedPsiMethod() != null) { - setEnabled(true); - if (myPsiMethod == null || !myPsiMethod.equals(methodDropDown.getSelectedPsiMethod())) { - myPsiMethod = methodDropDown.getSelectedPsiMethod(); - myModel.clear(); - myModel.addAll(getParameterWrappers()); - myModel.setSelectedItem(null); - } - } - else { - myPsiMethod = null; - myModel.clear(); - myModel.setSelectedItem(null); - setEnabled(false); - } - } - else { - throw new RuntimeException("Unexpected Configuration ParameterDropDown is only expected to receive events from MethodDropDown."); - } - } - - @Override - public void propertyChange(PropertyChangeEvent evt) { - if (evt.getPropertyName().equals(ClassField.PROPERTY_PSICLASS)) { - if (evt.getNewValue() == null) { - setEnabled(false); - } - } - } - - @Nullable - public PsiParameter getSelectedPsiParameter() { - ParameterWrapper parameterWrapper = myModel.getSelectedItem(); - if (parameterWrapper == null) return null; - - return parameterWrapper.getPsiParameter(); - } - - class ParameterWrapper implements Comparable { - @NotNull private final String id; - @NotNull private final PsiParameter psiParameter; - private final int index; - - ParameterWrapper(@NotNull PsiParameter psiParameter, int index) { - this.psiParameter = psiParameter; - this.index = index; - String typeName; - PsiTypeElement typeElement = psiParameter.getTypeElement(); - if (typeElement == null) { - typeName = ""; - } - else { - if (typeElement.getType() instanceof PsiPrimitiveType) { - typeName = ((PsiPrimitiveType)typeElement.getType()).getBoxedTypeName(); - } - else { - typeName = typeElement.getType().getCanonicalText(); - } - } - - id = typeName + " " + psiParameter.getName(); - } - - @Override - public int compareTo(ParameterWrapper o) { - return index - o.index; - } - - @Override - public String toString() { - return id; - } - - @NotNull - public PsiParameter getPsiParameter() { - return psiParameter; - } - } - } - - static class MethodWrapper implements Comparable { - @NotNull private final PsiMethod myPsiMethod; - @NotNull private final String myId; - - MethodWrapper(@NotNull PsiMethod psiMethod) { - this.myPsiMethod = psiMethod; - - List parameterClassNames = new ArrayList(); - PsiParameter[] parameters = psiMethod.getParameterList().getParameters(); - for (PsiParameter psiParameter : parameters) { - parameterClassNames.add(getParameterQualifiedName(psiParameter)); - } - - myId = initId(psiMethod.getName(), parameterClassNames); - } - - private static String getParameterQualifiedName(PsiParameter psiParameter) { - PsiTypeElement typeElement = psiParameter.getTypeElement(); - if (typeElement == null) { - return ""; - } - - if (typeElement.getType() instanceof PsiPrimitiveType) { - return ((PsiPrimitiveType)typeElement.getType()).getBoxedTypeName(); - } - - return typeElement.getType().getCanonicalText(); - } - - private static String initId(String methodName, List parameterNames) { - String shortName = methodName + "("; - for (String parameterName : parameterNames) { - if (parameterNames.lastIndexOf(".") > -1) { - shortName += parameterName.substring(parameterName.lastIndexOf(".") + 1) + ", "; - } - else { - shortName += parameterName + ", "; - } - } - - if (parameterNames.size() > 0) shortName = shortName.substring(0, shortName.lastIndexOf(", ")); - - shortName += ")"; - return shortName; - } - - @NotNull - public PsiMethod getPsiMethod() { - return myPsiMethod; - } - - @NotNull - public String getId() { - return myId; - } - - @Override - public String toString() { - return myId; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - MethodWrapper that = (MethodWrapper)o; - - if (!myId.equals(that.myId)) return false; - - return true; - } - - @Override - public int hashCode() { - return myId.hashCode(); - } - - @Override - public int compareTo(MethodWrapper o) { - return myId.compareTo(o.myId); - } - } -} diff --git a/java/java-indexing-impl/src/com/intellij/codeInsight/ConditionCheckManager.java b/java/java-indexing-impl/src/com/intellij/codeInsight/ConditionCheckManager.java deleted file mode 100644 index df9ca37c0d83..000000000000 --- a/java/java-indexing-impl/src/com/intellij/codeInsight/ConditionCheckManager.java +++ /dev/null @@ -1,183 +0,0 @@ -/* - * Copyright 2000-2013 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; - -import com.intellij.openapi.components.*; -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiMethod; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author Johnny Clark - * Creation Date: 8/3/12 - * todo remove in IDEA 15 - */ -@State( - name = "ConditionCheckManager", - storages = { - @Storage(id = "dir", file = StoragePathMacros.PROJECT_CONFIG_DIR + "/checker.xml", scheme = StorageScheme.DIRECTORY_BASED), - @Storage(file = StoragePathMacros.PROJECT_FILE) - } -) -public class ConditionCheckManager implements PersistentStateComponent { - @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) private State state; - private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.ConditionCheckManager"); - - private final List myIsNullCheckMethods = new ArrayList(); - private final List myIsNotNullCheckMethods = new ArrayList(); - - private final List myAssertIsNullMethods = new ArrayList(); - private final List myAssertIsNotNullMethods = new ArrayList(); - - private final List myAssertTrueMethods = new ArrayList(); - private final List myAssertFalseMethods = new ArrayList(); - - public static ConditionCheckManager getInstance(Project project) { - return ServiceManager.getService(project, ConditionCheckManager.class); - } - - public void setIsNullCheckMethods(List methodConditionChecks) { - myIsNullCheckMethods.clear(); - myIsNullCheckMethods.addAll(methodConditionChecks); - } - - public void setIsNotNullCheckMethods(List methodConditionChecks) { - myIsNotNullCheckMethods.clear(); - myIsNotNullCheckMethods.addAll(methodConditionChecks); - } - - public void setAssertIsNullMethods(List methodConditionChecks) { - myAssertIsNullMethods.clear(); - myAssertIsNullMethods.addAll(methodConditionChecks); - } - - public void setAssertIsNotNullMethods(List methodConditionChecks) { - myAssertIsNotNullMethods.clear(); - myAssertIsNotNullMethods.addAll(methodConditionChecks); - } - - public void setAssertTrueMethods(List psiMethodWrappers) { - myAssertTrueMethods.clear(); - myAssertTrueMethods.addAll(psiMethodWrappers); - } - - public void setAssertFalseMethods(List psiMethodWrappers) { - myAssertFalseMethods.clear(); - myAssertFalseMethods.addAll(psiMethodWrappers); - } - - public List getIsNullCheckMethods() { - return myIsNullCheckMethods; - } - - public List getIsNotNullCheckMethods() { - return myIsNotNullCheckMethods; - } - - public List getAssertIsNullMethods() { - return myAssertIsNullMethods; - } - - public List getAssertIsNotNullMethods() { - return myAssertIsNotNullMethods; - } - - public List getAssertFalseMethods() { - return myAssertFalseMethods; - } - - public List getAssertTrueMethods() { - return myAssertTrueMethods; - } - - public static class State { - public List myIsNullCheckMethods = new ArrayList(); - public List myIsNotNullCheckMethods = new ArrayList(); - public List myAssertIsNullMethods = new ArrayList(); - public List myAssertIsNotNullMethods = new ArrayList(); - public List myAssertTrueMethods = new ArrayList(); - public List myAssertFalseMethods = new ArrayList(); - } - - @Override - public State getState() { - State state = new State(); - - loadMethodChecksToState(state.myIsNullCheckMethods, myIsNullCheckMethods); - loadMethodChecksToState(state.myIsNotNullCheckMethods, myIsNotNullCheckMethods); - loadMethodChecksToState(state.myAssertIsNullMethods, myAssertIsNullMethods); - loadMethodChecksToState(state.myAssertIsNotNullMethods, myAssertIsNotNullMethods); - loadMethodChecksToState(state.myAssertTrueMethods, myAssertTrueMethods); - loadMethodChecksToState(state.myAssertFalseMethods, myAssertFalseMethods); - - return state; - } - - private static void loadMethodChecksToState(List listToLoadTo, List listToLoadFrom) { - for (ConditionChecker checker : listToLoadFrom) { - listToLoadTo.add(checker.toString()); - } - } - - @Override - public void loadState(State state) { - this.state = state; - loadMethods(myIsNullCheckMethods, state.myIsNullCheckMethods, ConditionChecker.Type.IS_NULL_METHOD); - loadMethods(myIsNotNullCheckMethods, state.myIsNotNullCheckMethods, ConditionChecker.Type.IS_NOT_NULL_METHOD); - loadMethods(myAssertIsNullMethods, state.myAssertIsNullMethods, ConditionChecker.Type.ASSERT_IS_NULL_METHOD); - loadMethods(myAssertIsNotNullMethods, state.myAssertIsNotNullMethods, ConditionChecker.Type.ASSERT_IS_NOT_NULL_METHOD); - loadMethods(myAssertTrueMethods, state.myAssertTrueMethods, ConditionChecker.Type.ASSERT_TRUE_METHOD); - loadMethods(myAssertFalseMethods, state.myAssertFalseMethods, ConditionChecker.Type.ASSERT_FALSE_METHOD); - } - - private static void loadMethods(List listToLoadTo, List listToLoadFrom, ConditionChecker.Type type){ - listToLoadTo.clear(); - for (String setting : listToLoadFrom) { - try { - listToLoadTo.add(new ConditionChecker.FromConfigBuilder(setting, type).build()); - } catch (Exception e) { - LOG.error("Problem occurred while attempting to load Condition Check from configuration file. " + e.getMessage()); - } - } - } - - @Nullable - public static ConditionChecker findConditionChecker(@NotNull PsiMethod method) { - ConditionCheckManager instance = getInstance(method.getProject()); - ConditionChecker checker = methodMatches(method, instance.getIsNullCheckMethods()); - if (checker == null) checker = methodMatches(method, instance.getIsNotNullCheckMethods()); - if (checker == null) checker = methodMatches(method, instance.getAssertIsNullMethods()); - if (checker == null) checker = methodMatches(method, instance.getAssertIsNotNullMethods()); - if (checker == null) checker = methodMatches(method, instance.getAssertTrueMethods()); - if (checker == null) checker = methodMatches(method, instance.getAssertFalseMethods()); - return checker; - } - - private static ConditionChecker methodMatches(PsiMethod psiMethod, List checkers) { - for (ConditionChecker checker : checkers) { - if (checker.matchesPsiMethod(psiMethod)) { - return checker; - } - } - return null; - } - -} diff --git a/java/java-psi-impl/src/com/intellij/codeInsight/ConditionChecker.java b/java/java-psi-impl/src/com/intellij/codeInsight/ConditionChecker.java deleted file mode 100644 index 22769dda4b2f..000000000000 --- a/java/java-psi-impl/src/com/intellij/codeInsight/ConditionChecker.java +++ /dev/null @@ -1,431 +0,0 @@ -/* - * Copyright 2000-2013 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; - -import com.intellij.openapi.diagnostic.*; -import com.intellij.psi.*; -import org.jetbrains.annotations.*; - -import java.io.*; -import java.util.*; - - -/** - * Used by Constant Condition Inspection to identify methods which perform some type of Validation on the parameters passed into them. - * For example given the following method - *
- * {@code
- *  class Foo {
- *     static boolean validateNotNull(Object o) {
- *       if (o == null) return false;
- *       else return true;
- *     }
- *   }
- * }
- *
- * The corresponding ConditionCheck would be 

- * myConditionCheckType=Type.IS_NOT_NULL_METHOD - * myClassName=Foo - * myMethodName=validateNotNull - * myPsiParameter=o - * - * The following block of code would produce a Inspection Warning that o is always true - * - *

- * {@code
- *   if (Value.isNotNull(o)) {
- *     if(o != null) {}
- *   }
- * }
- * 
- * - * @author Johnny Clark - * Creation Date: 8/14/12 - */ -public class ConditionChecker implements Serializable { - @NotNull private final Type myConditionCheckType; - - public enum Type { - IS_NULL_METHOD("IsNull Method"), - IS_NOT_NULL_METHOD("IsNotNull Method"), - ASSERT_IS_NULL_METHOD("Assert IsNull Method"), - ASSERT_IS_NOT_NULL_METHOD("Assert IsNotNull Method"), - ASSERT_TRUE_METHOD("Assert True Method"), - ASSERT_FALSE_METHOD("Assert False Method"); - private final String myStringRepresentation; - - Type(String stringRepresentation) { - myStringRepresentation = stringRepresentation; - } - - @Override - public String toString() { - return myStringRepresentation; - } - } - - @NotNull private final String myClassName; - @NotNull private final String myMethodName; - @NotNull private final List myParameterClassList; - private final int myCheckedParameterIndex; - private final String myFullName; - - private ConditionChecker(@NotNull String className, - @NotNull String methodName, - @NotNull List parameterClassList, - int checkedParameterIndex, - @NotNull Type type, - @NotNull String fullName) { - checkState(!className.isEmpty(), "Class Name is blank"); - checkState(!methodName.isEmpty(), "Method Name is blank"); - checkState(!parameterClassList.isEmpty(), "Parameter Class List is empty"); - checkState(checkedParameterIndex >= 0, "CheckedParameterIndex must be greater than or equal to zero"); - checkState(parameterClassList.size() >= checkedParameterIndex, "CheckedParameterIndex is greater than Parameter Class List's size"); - checkState(!fullName.isEmpty(), "Method Name is blank"); - - myConditionCheckType = type; - myClassName = className; - myMethodName = methodName; - myParameterClassList = parameterClassList; - myCheckedParameterIndex = checkedParameterIndex; - myFullName = fullName; - } - - private static void checkState(boolean condition, String errorMsg) { - if (!condition) throw new IllegalArgumentException(errorMsg); - } - - public static String getFullyQualifiedName(PsiParameter psiParameter) { - PsiTypeElement typeElement = psiParameter.getTypeElement(); - if (typeElement == null) throw new RuntimeException("Parameter has null typeElement " + psiParameter.getName()); - - PsiType psiType = typeElement.getType(); - - return psiType.getCanonicalText(); - } - - public boolean matchesPsiMethod(PsiMethod psiMethod) { - if (!myMethodName.equals(psiMethod.getName())) return false; - - PsiClass containingClass = psiMethod.getContainingClass(); - if (containingClass == null) return false; - - String qualifiedName = containingClass.getQualifiedName(); - if (qualifiedName == null) return false; - - if (!myClassName.equals(qualifiedName)) return false; - - PsiParameterList psiParameterList = psiMethod.getParameterList(); - if (myParameterClassList.size() != psiParameterList.getParameters().length) return false; - - for (int i = 0; i < psiParameterList.getParameters().length; i++) { - PsiParameter psiParameter = psiParameterList.getParameters()[i]; - PsiTypeElement psiTypeElement = psiParameter.getTypeElement(); - if (psiTypeElement == null) return false; - - PsiType psiType = psiTypeElement.getType(); - String parameterCanonicalText = psiType.getCanonicalText(); - String myParameterCanonicalText = myParameterClassList.get(i); - if (!myParameterCanonicalText.equals(parameterCanonicalText)) return false; - } - - return true; - } - - public boolean matchesPsiMethod(PsiMethod psiMethod, int paramIndex) { - if (matchesPsiMethod(psiMethod) && paramIndex == myCheckedParameterIndex) return true; - - return false; - } - - public boolean overlaps(ConditionChecker otherChecker) { - if (myClassName.equals(otherChecker.myClassName) && - myMethodName.equals(otherChecker.myMethodName) && - myParameterClassList.equals(otherChecker.myParameterClassList) && - myCheckedParameterIndex == otherChecker.myCheckedParameterIndex) { - return true; - } - - return false; - } - - @NotNull - public Type getConditionCheckType() { - return myConditionCheckType; - } - - @NotNull - public String getClassName() { - return myClassName; - } - - @NotNull - public String getMethodName() { - return myMethodName; - } - - public int getCheckedParameterIndex() { - return myCheckedParameterIndex; - } - - public String getFullName() { - return myFullName; - } - - /** - * In addition to normal duties, this controls the manner in which the ConditionCheck appears in the ConditionCheckDialog.MethodsPanel - */ - @Override - public String toString() { - return myFullName; - } - - private static class Builder { - - static String initFullName(String className, - String methodName, - List parameterClasses, - List parameterNames, - int checkedParameterIndex) { - String s = className + "." + methodName + "("; - int index = 0; - for (String parameterClass : parameterClasses) { - String parameterClassAndName = parameterClass + " " + parameterNames.get(index); - if (index == checkedParameterIndex) parameterClassAndName = "*" + parameterClassAndName + "*"; - - s += parameterClassAndName + ", "; - index++; - } - s = s.substring(0, s.length() - 2); - s += ")"; - return s; - } - } - - static class FromConfigBuilder extends Builder { - private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.ConditionCheck.FromConfigBuilder"); - @NotNull private final String serializedRepresentation; - @NotNull private final Type type; - - FromConfigBuilder(@NotNull String serializedRepresentation, @NotNull Type type) { - this.serializedRepresentation = serializedRepresentation; - this.type = type; - } - - private String parseClassAndMethodName() { - if (!serializedRepresentation.contains("(")) { - throw new IllegalArgumentException("Name should contain a opening parenthesis. " + serializedRepresentation); - } - else if (!serializedRepresentation.contains(")")) { - throw new IllegalArgumentException("Name should contain a closing parenthesis. " + serializedRepresentation); - } - else if (serializedRepresentation.indexOf("(", serializedRepresentation.indexOf("(") + 1) > -1) { - throw new IllegalArgumentException("Name should only contain one opening parenthesis. " + serializedRepresentation); - } - else if (serializedRepresentation.indexOf(")", serializedRepresentation.indexOf(")") + 1) > -1) { - throw new IllegalArgumentException("Name should only contain one closing parenthesis. " + serializedRepresentation); - } - else if (serializedRepresentation.indexOf(")") < serializedRepresentation.indexOf("(")) { - throw new IllegalArgumentException("Opening parenthesis should precede closing parenthesis. " + serializedRepresentation); - } - - String classAndMethodName = serializedRepresentation.substring(0, serializedRepresentation.indexOf("(")); - if (!classAndMethodName.contains(".")) { - throw new IllegalArgumentException( - "Name should contain a dot between the class name and method name (before the opening parenthesis). " + - serializedRepresentation); - } - return classAndMethodName; - } - - @Nullable - public ConditionChecker build() { - try { - String classAndMethodName = parseClassAndMethodName(); - - String className = classAndMethodName.substring(0, classAndMethodName.lastIndexOf(".")); - String methodName = classAndMethodName.substring(classAndMethodName.lastIndexOf(".") + 1); - - String allParametersSubString = - serializedRepresentation.substring(serializedRepresentation.indexOf("(") + 1, serializedRepresentation.lastIndexOf(")")).trim(); - if (allParametersSubString.isEmpty()) { - throw new IllegalArgumentException( - "Name should contain 1+ parameter (between opening and closing parenthesis). " + serializedRepresentation); - } - if (allParametersSubString.contains("*") && allParametersSubString.indexOf("*") == allParametersSubString.lastIndexOf("*")) { - throw new IllegalArgumentException("Selected Parameter should be surrounded by asterisks. " + serializedRepresentation); - } - - List parameterClasses = new ArrayList(); - List parameterNames = new ArrayList(); - int checkParameterIndex = -1; - int index = 0; - for (String parameterClassAndName : allParametersSubString.split(",")) { - parameterClassAndName = parameterClassAndName.trim(); - if (parameterClassAndName.startsWith("*") && parameterClassAndName.endsWith("*")) { - checkParameterIndex = index; - parameterClassAndName = parameterClassAndName.substring(1, parameterClassAndName.length() - 1); - } - - String[] parameterClassAndNameSplit = parameterClassAndName.split(" "); - String parameterClass = parameterClassAndNameSplit[0]; - String parameterName = parameterClassAndNameSplit[1]; - parameterClasses.add(parameterClass); - parameterNames.add(parameterName); - index++; - } - String fullName = initFullName(className, methodName, parameterClasses, parameterNames, checkParameterIndex); - return new ConditionChecker(className, methodName, parameterClasses, checkParameterIndex, type, fullName); - } - catch (Exception e) { - LOG.error("An Exception occurred while attempting to build ConditionCheck for Serialized String '" + - serializedRepresentation + - "' and Type '" + - type + - "'", e); - return null; - } - } - } - - public static class FromPsiBuilder extends Builder { - private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.ConditionCheck.FromPsiBuilder"); - @NotNull private final PsiMethod psiMethod; - @NotNull private final PsiParameter psiParameter; - @NotNull private final Type type; - - public FromPsiBuilder(@NotNull PsiMethod psiMethod, @NotNull PsiParameter psiParameter, @NotNull Type type) { - this.psiMethod = psiMethod; - this.psiParameter = psiParameter; - this.type = type; - } - - private static void validatePsiMethodHasContainingClass(PsiMethod psiMethod) { - PsiElement psiElement = psiMethod.getContainingClass(); - if (!(psiElement instanceof PsiClass)) { - throw new IllegalArgumentException("PsiMethod " + psiMethod + " can not have a null containing class."); - } - } - - private static void validatePsiMethodReturnTypeForNonAsserts(PsiMethod psiMethod, Type type) { - PsiType returnType = psiMethod.getReturnType(); - if (isAssert(type)) return; - - if (returnType == null) throw new IllegalArgumentException("PsiMethod " + psiMethod + " has a null return type PsiType."); - - if (returnType != PsiType.BOOLEAN && !returnType.getCanonicalText().equals(Boolean.class.toString())) { - throw new IllegalArgumentException("PsiMethod " + psiMethod + " must have a null return type PsiType of boolean or Boolean."); - } - } - - private static void validatePsiParameterExistsInPsiMethod(PsiMethod psiMethod, PsiParameter psiParameter) { - boolean parameterFound = false; - PsiParameter[] parameters = psiMethod.getParameterList().getParameters(); - for (PsiParameter parameter : parameters) { - if (psiParameter.equals(parameter)) { - parameterFound = true; - break; - } - } - - if (!parameterFound) { - throw new IllegalArgumentException("PsiMethod " + psiMethod + " must have parameter " + getFullyQualifiedName(psiParameter)); - } - } - - private static boolean isAssert(Type type) { - return type == Type.ASSERT_IS_NULL_METHOD || - type == Type.ASSERT_IS_NOT_NULL_METHOD || - type == Type.ASSERT_TRUE_METHOD || - type == Type.ASSERT_FALSE_METHOD; - } - - private static String initClassNameFromPsiMethod(PsiMethod psiMethod) { - PsiElement psiElement = psiMethod.getContainingClass(); - PsiClass psiClass = (PsiClass)psiElement; - if (psiClass == null) throw new IllegalStateException("PsiClass is null"); - - String qualifiedName = psiClass.getQualifiedName(); - if (qualifiedName == null || qualifiedName.isEmpty()) throw new IllegalStateException("Qualified Name is Blank"); - return qualifiedName; - } - - private static String initMethodNameFromPsiMethod(PsiMethod psiMethod) { - return psiMethod.getName(); - } - - private static List initParameterClassListFromPsiMethod(PsiMethod psiMethod) { - List parameterClasses = new ArrayList(); - PsiParameter[] parameters = psiMethod.getParameterList().getParameters(); - for (PsiParameter param : parameters) { - PsiTypeElement typeElement = param.getTypeElement(); - if (typeElement == null) throw new RuntimeException("Parameter has null typeElement " + param.getName()); - - PsiType psiType = typeElement.getType(); - - parameterClasses.add(psiType.getCanonicalText()); - } - return parameterClasses; - } - - private static List initParameterNameListFromPsiMethod(PsiMethod psiMethod) { - List parameterNames = new ArrayList(); - PsiParameter[] parameters = psiMethod.getParameterList().getParameters(); - for (PsiParameter param : parameters) { - parameterNames.add(param.getName()); - } - return parameterNames; - } - - private static int initCheckedParameterIndex(PsiMethod psiMethod, PsiParameter psiParameterToFind) { - PsiParameter[] parameters = psiMethod.getParameterList().getParameters(); - for (int i = 0; i < parameters.length; i++) { - PsiParameter param = parameters[i]; - if (param.equals(psiParameterToFind)) return i; - } - throw new IllegalStateException(); - } - - private void validateConstructorArgs(PsiMethod psiMethod, PsiParameter psiParameter) { - validatePsiMethodHasContainingClass(psiMethod); - validatePsiMethodReturnTypeForNonAsserts(psiMethod, type); - validatePsiParameterExistsInPsiMethod(psiMethod, psiParameter); - } - - @Nullable - public ConditionChecker build() { - try { - validateConstructorArgs(psiMethod, psiParameter); - - String className = initClassNameFromPsiMethod(psiMethod); - String methodName = initMethodNameFromPsiMethod(psiMethod); - List parameterClassList = initParameterClassListFromPsiMethod(psiMethod); - List parameterNameList = initParameterNameListFromPsiMethod(psiMethod); - int checkedParameterIndex = initCheckedParameterIndex(psiMethod, psiParameter); - String fullName = initFullName(className, methodName, parameterClassList, parameterNameList, checkedParameterIndex); - return new ConditionChecker(className, methodName, parameterClassList, checkedParameterIndex, type, fullName); - } - catch (Exception e) { - LOG.error("An Exception occurred while attempting to build ConditionCheck for PsiMethod '" + psiMethod + - "' PsiParameter='" + psiParameter + "' " + - "' and Type '" + - type + - "'", e); - return null; - } - } - } -} diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 84bd52a1a684..f56ab058f842 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -540,9 +540,6 @@ - -