From b0fb01bf2586f43da5e5e87c0719f020bdbf9a46 Mon Sep 17 00:00:00 2001 From: manusha karunathilaka <36305124+manushakaru@users.noreply.github.com> Date: Sat, 7 Nov 2020 19:49:26 +0530 Subject: [PATCH] Code cleanup --- src/main/java/Analyse.java | 9 - src/main/java/AnalyseReport.java | 24 +-- src/main/java/GitVcs.java | 81 +++------ src/main/java/HighlightAction.java | 71 ++++---- src/main/java/ListItemPanel.java | 88 +++++---- src/main/java/MyToolWindow.java | 218 +++++++++-------------- src/main/java/MyToolWindowFactory.java | 2 +- src/main/java/ProjectManager.java | 112 ++++++------ src/main/java/Utils.java | 10 +- src/main/java/model/ClassEntity.java | 29 --- src/main/java/model/ImpactSet.java | 18 +- src/main/java/model/MethodEntity.java | 21 +-- src/main/java/model/ReferenceEntity.java | 32 ++-- src/test/java/model/ClassEntityTest.java | 36 ---- 14 files changed, 288 insertions(+), 463 deletions(-) delete mode 100644 src/main/java/model/ClassEntity.java delete mode 100644 src/test/java/model/ClassEntityTest.java diff --git a/src/main/java/Analyse.java b/src/main/java/Analyse.java index d085c24..b020cae 100644 --- a/src/main/java/Analyse.java +++ b/src/main/java/Analyse.java @@ -1,19 +1,10 @@ import com.intellij.openapi.actionSystem.AnActionEvent; -import com.intellij.openapi.ui.Messages; -import com.intellij.openapi.util.Computable; -import com.intellij.psi.PsiClass; -import com.intellij.psi.impl.search.MethodUsagesSearcher; import org.jetbrains.annotations.NotNull; public class Analyse extends com.intellij.openapi.actionSystem.AnAction { - AnalyseReport analyseReport; @Override public void actionPerformed(@NotNull AnActionEvent e) { - /*analyseReport=new AnalyseReport(true); - String depth=Messages.showInputDialog("Insert Depth","Analyse",Messages.getInformationIcon()); - analyseReport.showAndGet();*/ GitVcs.getAffectedFiles(e.getProject()); - } } diff --git a/src/main/java/AnalyseReport.java b/src/main/java/AnalyseReport.java index 8832123..8daf0fd 100644 --- a/src/main/java/AnalyseReport.java +++ b/src/main/java/AnalyseReport.java @@ -1,18 +1,12 @@ -import org.jetbrains.annotations.Nullable; - import com.intellij.openapi.ui.DialogWrapper; -import java.awt.Dimension; -import java.awt.GridBagLayout; -import javax.swing.JComponent; -import javax.swing.JPanel; -import javax.swing.JTextField; + +import javax.swing.*; +import java.awt.*; public class AnalyseReport extends DialogWrapper { - private JPanel mainPanel = new JPanel(new GridBagLayout()); - //private JScrollPane ChangesScrollPane; - //private GridBag gridBag; - private JTextField textField=new JTextField(); + private final JPanel mainPanel = new JPanel(new GridBagLayout()); + private final JTextField textField = new JTextField(); protected AnalyseReport(boolean canBeParent) { super(canBeParent); @@ -22,16 +16,12 @@ protected AnalyseReport(boolean canBeParent) { protected void init() { super.init(); setTitle("Analyse Report"); - setSize(600,400); + setSize(600, 400); } @Override protected JComponent createCenterPanel() { - mainPanel.setPreferredSize(new Dimension(600,400)); - /*gridBag = new GridBag(). - setDefaultInsets(new Insets(0,0, AbstractLayout.DEFAULT_VGAP,AbstractLayout.DEFAULT_HGAP)) - .setDefaultWeightX(1.0) - .setDefaultFill(GridBagConstraints.HORIZONTAL);*/ + mainPanel.setPreferredSize(new Dimension(600, 400)); mainPanel.add(textField); return mainPanel; } diff --git a/src/main/java/GitVcs.java b/src/main/java/GitVcs.java index a8ad585..e4f849c 100644 --- a/src/main/java/GitVcs.java +++ b/src/main/java/GitVcs.java @@ -1,38 +1,30 @@ import com.intellij.lang.java.JavaLanguage; import com.intellij.openapi.project.Project; -import com.intellij.openapi.vcs.AbstractVcs; -import com.intellij.openapi.vcs.ProjectLevelVcsManager; -import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vcs.changes.Change; import com.intellij.openapi.vcs.changes.ChangeListManager; import com.intellij.openapi.vcs.changes.ContentRevision; import com.intellij.openapi.vcs.changes.LocalChangeList; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; -import com.intellij.psi.search.FilenameIndex; import model.MethodEntity; -import org.jetbrains.annotations.NotNull; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import static com.intellij.psi.util.MethodSignatureUtil.areParametersErasureEqual; import static com.intellij.psi.util.MethodSignatureUtil.areSignaturesEqual; public class GitVcs { public static List getAffectedFiles(Project project) { + List methodList = new ArrayList<>(); final ChangeListManager changeListManager = ChangeListManager.getInstance(project); - final List modifiedFiles = ChangeListManager.getInstance(project).getAffectedFiles(); // ?? - //modifiedFiles.get(0); - //now it only gets first changeList--> change it to get all + final List modifiedFiles = ChangeListManager.getInstance(project).getAffectedFiles(); final LocalChangeList changeList = changeListManager.getChangeLists().get(0); if (changeList != null) { - System.out.println("ChangeListSize : "+changeList.getChanges().size()); + System.out.println("ChangeListSize : " + changeList.getChanges().size()); for (Change change : changeList.getChanges()) { final ContentRevision afterRevision = change.getAfterRevision(); final ContentRevision beforeRevision = change.getBeforeRevision(); @@ -43,13 +35,12 @@ public static List getAffectedFiles(Project project) { final PsiFileFactory factory = PsiFileFactory.getInstance(project); PsiFile before = factory.createFileFromText(JavaLanguage.INSTANCE, beforeRevision.getContent()); - //PsiFile after = factory.createFileFromText(JavaLanguage.INSTANCE, beforeRevision.getContent()); PsiFile after = PsiManager.getInstance(project).findFile(afterRevision.getFile().getVirtualFile()); if (before instanceof PsiJavaFile && after instanceof PsiJavaFile) { - PsiJavaFile jBefore=(PsiJavaFile) before; - PsiJavaFile jAfter=(PsiJavaFile) after; + PsiJavaFile jBefore = (PsiJavaFile) before; + PsiJavaFile jAfter = (PsiJavaFile) after; PsiClass[] psiClassesBefore = jBefore.getClasses(); PsiClass[] psiClassesAfter = jAfter.getClasses(); @@ -59,92 +50,71 @@ public static List getAffectedFiles(Project project) { afterMethods.add(method); } } - //System.out.println("Checking equal"); - for (PsiMethod psiMethodAfter:afterMethods) { + + for (PsiMethod psiMethodAfter : afterMethods) { beforeMethods.clear(); for (PsiClass psiClass : psiClassesBefore) { - if(psiClass.getName().equals(psiMethodAfter.getContainingClass().getName())) { + if (psiClass.getName().equals(psiMethodAfter.getContainingClass().getName())) { for (PsiMethod method : psiClass.getMethods()) { - if(psiMethodAfter.getName().equals(method.getName())) { + if (psiMethodAfter.getName().equals(method.getName())) { beforeMethods.add(method); } } } } - if(beforeMethods.size()==0){ + if (beforeMethods.size() == 0) { //method has changed methodList.add(new MethodEntity(psiMethodAfter)); continue; } - boolean hasSimilar=false; - for (PsiMethod psiMethodBefore:beforeMethods) { - if(psiMethodAfter.getName().equals(psiMethodBefore.getName()) && areSignaturesEqual(psiMethodAfter,psiMethodBefore)){ - //System.out.println(psiMethodAfter.getName()+" equal "+psiMethodBefore.getName()); - // System.out.println(psiMethodAfter.getModifierList().getText()); --> Modifier list equality is not checking - + boolean hasSimilar = false; + for (PsiMethod psiMethodBefore : beforeMethods) { + if (psiMethodAfter.getName().equals(psiMethodBefore.getName()) && areSignaturesEqual(psiMethodAfter, psiMethodBefore)) { for (int i = 0; i < psiMethodAfter.getModifierList().getChildren().length; i++) { - System.out.println(psiMethodAfter.getName()+" "+psiMethodAfter.getModifierList().getChildren()[i]+" CHILD "+compare(psiMethodAfter.getModifierList().getChildren()[i],psiMethodBefore.getModifierList().getChildren()[i])); + System.out.println(psiMethodAfter.getName() + " " + psiMethodAfter.getModifierList().getChildren()[i] + " CHILD " + compare(psiMethodAfter.getModifierList().getChildren()[i], psiMethodBefore.getModifierList().getChildren()[i])); } - String af=psiMethodAfter.getText().trim(); - String bef=psiMethodBefore.getText().trim(); - if(compare(psiMethodBefore.getBody(),psiMethodAfter.getBody())==1) { - hasSimilar=true; + String af = psiMethodAfter.getText().trim(); + String bef = psiMethodBefore.getText().trim(); + if (compare(psiMethodBefore.getBody(), psiMethodAfter.getBody()) == 1) { + hasSimilar = true; break; } - }else{ - //System.out.println(psiMethodBefore.getName()+" & "+psiMethodAfter.getName()+" "+0); } } - if(!hasSimilar){ + if (!hasSimilar) { methodList.add(new MethodEntity(psiMethodAfter)); } } } - //-->Important-->PsiFile psiFile = PsiManager.getInstance(project).findFile(change.getVirtualFile()); - }catch (Exception e){ + + } catch (Exception e) { System.out.println(e.getMessage()); } - - //PsiFile[] psiFiles= FilenameIndex.getFilesByName(project,afterRevision.getFile().getName(), GlobalSearchScope.projectScope(project)); - - /*for (PsiFile psiFile:psiFiles) { - System.out.println("Path-"+psiFile.getVirtualFile().getPath()); - if (psiFile instanceof PsiJavaFile) { - PsiClass[] psiClasses = ((PsiJavaFile) psiFile).getClasses(); - for (PsiClass psiClass : psiClasses) { - for (PsiMethod method : psiClass.getMethods()) { - //System.out.println(method.getName()); - System.out.println(method); - - } - } - } - }*/ } } return methodList; } + //return 0 if not equal public static int compare(PsiElement before, PsiElement after) { if (!before.toString().trim().equals(after.toString().trim())) { - System.out.println(before+"="+after); + System.out.println(before + "=" + after); return 0; } else { if (before.getChildren().length == 0 || after.getChildren().length == 0) { return 1; } else if (after.getChildren().length != before.getChildren().length) { - //System.out.println(before.getChildren().length+"="+after.getChildren().length); return 0; } else { int comp = 1; for (int i = 0; i < before.getChildren().length; i++) { - if(compare(before.getChildren()[i],after.getChildren()[i])==0){ - comp=0; + if (compare(before.getChildren()[i], after.getChildren()[i]) == 0) { + comp = 0; break; } } @@ -153,4 +123,3 @@ public static int compare(PsiElement before, PsiElement after) { } } } -//areSignaturesEqual(psiMethodAfter,psiMethodBefore) \ No newline at end of file diff --git a/src/main/java/HighlightAction.java b/src/main/java/HighlightAction.java index 1793817..3f06f08 100644 --- a/src/main/java/HighlightAction.java +++ b/src/main/java/HighlightAction.java @@ -1,13 +1,10 @@ import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; -import model.ClassEntity; -import model.MethodEntity; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.ListBranchCommand; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.DiffFormatter; -import org.eclipse.jgit.internal.storage.file.FileRepository; import org.eclipse.jgit.lib.ObjectReader; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; @@ -19,44 +16,57 @@ import org.eclipse.jgit.treewalk.CanonicalTreeParser; import org.eclipse.jgit.treewalk.FileTreeIterator; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; -import java.util.ArrayList; import java.util.List; public class HighlightAction extends AnAction { + private static AbstractTreeIterator prepareTreeParser(Repository repository, RevCommit commit) throws Exception { + + RevWalk walk = new RevWalk(repository); + RevTree tree = walk.parseTree(commit.getTree().getId()); + CanonicalTreeParser oldTreeParser = new CanonicalTreeParser(); + ObjectReader oldReader = repository.newObjectReader(); + + try { + oldTreeParser.reset(oldReader, tree.getId()); + } finally { + + } + return oldTreeParser; + } + @Override public void actionPerformed(AnActionEvent anActionEvent) { - System.out.println(anActionEvent.getProject().getBasePath()+"/.git"); + System.out.println(anActionEvent.getProject().getBasePath() + "/.git"); try { Repository existingRepo = new FileRepositoryBuilder() - .setGitDir(new File(anActionEvent.getProject().getBasePath()+"/.git")) + .setGitDir(new File(anActionEvent.getProject().getBasePath() + "/.git")) .build(); Git git = new Git(existingRepo); List branches = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call(); - RevCommit youngestCommit=null; - RevWalk walk = new RevWalk(git.getRepository()); - for(Ref branch : branches) { - RevCommit commit = walk.parseCommit(branch.getObjectId()); - if (youngestCommit == null || commit.getAuthorIdent().getWhen().compareTo( - youngestCommit.getAuthorIdent().getWhen()) > 0) - youngestCommit = commit; - } + RevCommit youngestCommit = null; + RevWalk walk = new RevWalk(git.getRepository()); + for (Ref branch : branches) { + RevCommit commit = walk.parseCommit(branch.getObjectId()); + if (youngestCommit == null || commit.getAuthorIdent().getWhen().compareTo( + youngestCommit.getAuthorIdent().getWhen()) > 0) + youngestCommit = commit; + } - DiffFormatter formatter = new DiffFormatter( System.out ); - formatter.setRepository( git.getRepository() ); - AbstractTreeIterator commitTreeIterator = prepareTreeParser( git.getRepository(), youngestCommit ); - FileTreeIterator workTreeIterator = new FileTreeIterator( git.getRepository() ); - List diffEntries = formatter.scan( commitTreeIterator, workTreeIterator ); + DiffFormatter formatter = new DiffFormatter(System.out); + formatter.setRepository(git.getRepository()); + AbstractTreeIterator commitTreeIterator = prepareTreeParser(git.getRepository(), youngestCommit); + FileTreeIterator workTreeIterator = new FileTreeIterator(git.getRepository()); + List diffEntries = formatter.scan(commitTreeIterator, workTreeIterator); - for( DiffEntry entry : diffEntries ) { - System.out.println( "Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId() ); - formatter.format( entry ); + for (DiffEntry entry : diffEntries) { + System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId()); + formatter.format(entry); } } catch (IOException | GitAPIException e) { e.printStackTrace(); @@ -65,19 +75,4 @@ public void actionPerformed(AnActionEvent anActionEvent) { } } - private static AbstractTreeIterator prepareTreeParser(Repository repository, RevCommit commit) throws Exception { - RevWalk walk = new RevWalk(repository); - RevTree tree = walk.parseTree(commit.getTree().getId()); - - CanonicalTreeParser oldTreeParser = new CanonicalTreeParser(); - ObjectReader oldReader = repository.newObjectReader(); - try { - oldTreeParser.reset(oldReader, tree.getId()); - } finally { - - } - return oldTreeParser; - } - - } diff --git a/src/main/java/ListItemPanel.java b/src/main/java/ListItemPanel.java index af39970..b0dff98 100644 --- a/src/main/java/ListItemPanel.java +++ b/src/main/java/ListItemPanel.java @@ -1,5 +1,4 @@ import com.intellij.openapi.editor.colors.EditorColorsManager; -import com.intellij.structuralsearch.plugin.ui.UIUtil; import model.ReferenceEntity; import javax.swing.*; @@ -7,6 +6,17 @@ public class ListItemPanel extends javax.swing.JPanel implements ListCellRenderer { + private javax.swing.JLabel classLabel; + private javax.swing.JLabel depthLabel; + private javax.swing.JLabel methodLabel; + private final Color textSelectionColor = Color.WHITE; + private final Color backgroundSelectionColor = new java.awt.Color(38, 118, 191); + private final Color textNonSelectionColor = EditorColorsManager.getInstance().getGlobalScheme().getDefaultForeground(); + private final Color backgroundNonSelectionColor = EditorColorsManager.getInstance().getGlobalScheme().getDefaultBackground(); + private Icon iconUp; + private Icon iconDown; + private Icon iconClass; + private Icon iconMethod; public ListItemPanel() { initComponents(); } @@ -14,22 +24,22 @@ public ListItemPanel() { @SuppressWarnings("unchecked") private void initComponents() { - ClassLabel = new javax.swing.JLabel(); - MethodLabel = new javax.swing.JLabel(); - DepthLabel = new javax.swing.JLabel(); + classLabel = new javax.swing.JLabel(); + methodLabel = new javax.swing.JLabel(); + depthLabel = new javax.swing.JLabel(); - iconUp=new ImageIcon(new ImageIcon(getClass().getClassLoader().getResource("drawables/up.png")).getImage()); - iconDown=new ImageIcon(new ImageIcon(getClass().getClassLoader().getResource("drawables/down.png")).getImage()); - iconClass=new ImageIcon(new ImageIcon(getClass().getClassLoader().getResource("drawables/class.png")).getImage()); - iconMethod=new ImageIcon(new ImageIcon(getClass().getClassLoader().getResource("drawables/method.png")).getImage()); + iconUp = new ImageIcon(new ImageIcon(getClass().getClassLoader().getResource("drawables/up.png")).getImage()); + iconDown = new ImageIcon(new ImageIcon(getClass().getClassLoader().getResource("drawables/down.png")).getImage()); + iconClass = new ImageIcon(new ImageIcon(getClass().getClassLoader().getResource("drawables/class.png")).getImage()); + iconMethod = new ImageIcon(new ImageIcon(getClass().getClassLoader().getResource("drawables/method.png")).getImage()); setBackground(backgroundNonSelectionColor); - ClassLabel.setText("Class"); + classLabel.setText("Class"); - MethodLabel.setText("Method"); + methodLabel.setText("Method"); - DepthLabel.setText("Depth"); + depthLabel.setText("Depth"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); @@ -37,11 +47,11 @@ private void initComponents() { layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(10, 10, 10) - .addComponent(ClassLabel) + .addComponent(classLabel) .addGap(10, 10, 10) - .addComponent(MethodLabel) + .addComponent(methodLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 280, Short.MAX_VALUE) - .addComponent(DepthLabel) + .addComponent(depthLabel) .addGap(10, 10, 10)) ); layout.setVerticalGroup( @@ -49,52 +59,40 @@ private void initComponents() { .addGroup(layout.createSequentialGroup() .addGap(2, 2, 2) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(ClassLabel) - .addComponent(MethodLabel) - .addComponent(DepthLabel)) + .addComponent(classLabel) + .addComponent(methodLabel) + .addComponent(depthLabel)) .addGap(2, 2, 2)) ); } @Override public Component getListCellRendererComponent(JList list, ReferenceEntity value, int index, boolean isSelected, boolean cellHasFocus) { - ClassLabel.setText(value.getPsiClass().getName()); - MethodLabel.setText(value.getPsiMethod().getName()); - DepthLabel.setText(String.valueOf(value.getDepth())); - - ClassLabel.setIcon(iconClass); - MethodLabel.setIcon(iconMethod); - if(value.isCaller()) { - DepthLabel.setIcon(iconUp); - }else { - DepthLabel.setIcon(iconDown); + classLabel.setText(value.getPsiClass().getName()); + methodLabel.setText(value.getPsiMethod().getName()); + depthLabel.setText(String.valueOf(value.getDepth())); + + classLabel.setIcon(iconClass); + methodLabel.setIcon(iconMethod); + if (value.isCaller()) { + depthLabel.setIcon(iconUp); + } else { + depthLabel.setIcon(iconDown); } if (cellHasFocus) { setBackground(backgroundSelectionColor); - ClassLabel.setForeground(textSelectionColor); - MethodLabel.setForeground(textSelectionColor); - DepthLabel.setForeground(textSelectionColor); + classLabel.setForeground(textSelectionColor); + methodLabel.setForeground(textSelectionColor); + depthLabel.setForeground(textSelectionColor); } else { setBackground(backgroundNonSelectionColor); - ClassLabel.setForeground(textNonSelectionColor); - MethodLabel.setForeground(textNonSelectionColor); - DepthLabel.setForeground(textNonSelectionColor); + classLabel.setForeground(textNonSelectionColor); + methodLabel.setForeground(textNonSelectionColor); + depthLabel.setForeground(textNonSelectionColor); } return this; } - private javax.swing.JLabel ClassLabel; - private javax.swing.JLabel DepthLabel; - private javax.swing.JLabel MethodLabel; - private Color textSelectionColor = Color.WHITE; - private Color backgroundSelectionColor = new java.awt.Color(38,118,191); - private Color textNonSelectionColor = EditorColorsManager.getInstance().getGlobalScheme().getDefaultForeground(); - private Color backgroundNonSelectionColor = EditorColorsManager.getInstance().getGlobalScheme().getDefaultBackground(); - private Icon iconUp; - private Icon iconDown; - private Icon iconClass; - private Icon iconMethod; - } diff --git a/src/main/java/MyToolWindow.java b/src/main/java/MyToolWindow.java index 03c281b..dc9a77b 100644 --- a/src/main/java/MyToolWindow.java +++ b/src/main/java/MyToolWindow.java @@ -1,82 +1,73 @@ -import com.intellij.openapi.actionSystem.AnActionEvent; -import com.intellij.openapi.progress.ProgressIndicator; -import com.intellij.openapi.progress.ProgressIndicatorProvider; -import com.intellij.openapi.progress.ProgressManager; -import com.intellij.openapi.progress.Task; import com.intellij.openapi.project.Project; import com.intellij.openapi.wm.ToolWindow; import model.MethodEntity; import model.ReferenceEntity; -import org.jetbrains.annotations.NotNull; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; import java.util.ArrayList; import java.util.List; public class MyToolWindow { - private javax.swing.JLabel ChangesLabel; - private javax.swing.JScrollPane ChangesScrollPane; - private javax.swing.JLabel ImpactLabel; - private javax.swing.JScrollPane ImpactScrollPane; - private javax.swing.JList jList1; - private javax.swing.JList jList2; + + private javax.swing.JLabel changesLabel; + private javax.swing.JScrollPane changesScrollPane; + private javax.swing.JLabel impactLabel; + private javax.swing.JScrollPane impactScrollPane; + private javax.swing.JList changesList; + private javax.swing.JList impactList; private javax.swing.JPanel mainPanel; - private javax.swing.JSpinner jSpinnerDepth; - private javax.swing.JButton jButtonRun; + private javax.swing.JSpinner depthSpinner; + private javax.swing.JButton runBtn; private SpinnerNumberModel spinnerNumberModel; - private javax.swing.JRadioButton jRBCurrent; - private javax.swing.JRadioButton jRBGit; - + private javax.swing.JRadioButton currentRB; + private javax.swing.JRadioButton gitRB; private DefaultListModel listModel; private List methodList; - private ProjectManager projectManager; - private Project project; + private final ProjectManager projectManager; + private final Project project; - public MyToolWindow(ToolWindow toolWindow,Project project) { + public MyToolWindow(ToolWindow toolWindow, Project project) { initComponents(); - projectManager=new ProjectManager(); - this.project=project; + projectManager = new ProjectManager(); + this.project = project; } private void initComponents() { mainPanel = new javax.swing.JPanel(); - ChangesScrollPane = new javax.swing.JScrollPane(); - - ImpactScrollPane = new javax.swing.JScrollPane(); - jList2 = new javax.swing.JList<>(); - ChangesLabel = new javax.swing.JLabel(); - ImpactLabel = new javax.swing.JLabel(); - jButtonRun = new javax.swing.JButton(); - spinnerNumberModel=new SpinnerNumberModel(1,1,20,1); - jSpinnerDepth = new javax.swing.JSpinner(spinnerNumberModel); - jRBGit = new javax.swing.JRadioButton(); - jRBCurrent = new javax.swing.JRadioButton(); + changesScrollPane = new javax.swing.JScrollPane(); + + impactScrollPane = new javax.swing.JScrollPane(); + impactList = new javax.swing.JList<>(); + changesLabel = new javax.swing.JLabel(); + impactLabel = new javax.swing.JLabel(); + runBtn = new javax.swing.JButton(); + spinnerNumberModel = new SpinnerNumberModel(1, 1, 20, 1); + depthSpinner = new javax.swing.JSpinner(spinnerNumberModel); + gitRB = new javax.swing.JRadioButton(); + currentRB = new javax.swing.JRadioButton(); String[] strings = new String[0]; - jList1 = new javax.swing.JList<>(strings); - ChangesScrollPane.setViewportView(jList1); + changesList = new javax.swing.JList<>(strings); + changesScrollPane.setViewportView(changesList); - jList2 = new javax.swing.JList<>(); - listModel=new DefaultListModel<>(); - jList2.setModel(listModel); - jList2.setCellRenderer(new ListItemPanel()); - ImpactScrollPane.setViewportView(jList2); + impactList = new javax.swing.JList<>(); + listModel = new DefaultListModel<>(); + impactList.setModel(listModel); + impactList.setCellRenderer(new ListItemPanel()); + impactScrollPane.setViewportView(impactList); - ChangesLabel.setText("Methods"); - ImpactLabel.setText("Impact"); - jRBGit.setText("Git"); - jRBCurrent.setText("Current"); - jButtonRun.setText("Run"); - jRBCurrent.setSelected(true); + changesLabel.setText("Methods"); + impactLabel.setText("Impact"); + gitRB.setText("Git"); + currentRB.setText("Current"); + runBtn.setText("Run"); + currentRB.setSelected(true); javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel); mainPanel.setLayout(mainPanelLayout); @@ -85,21 +76,21 @@ private void initComponents() { .addGroup(mainPanelLayout.createSequentialGroup() .addGap(11, 11, 11) .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(ChangesLabel) - .addComponent(ChangesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)) + .addComponent(changesLabel) + .addComponent(changesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(mainPanelLayout.createSequentialGroup() - .addComponent(ImpactLabel) + .addComponent(impactLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 130, Short.MAX_VALUE) - .addComponent(jRBCurrent) + .addComponent(currentRB) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jRBGit) + .addComponent(gitRB) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jSpinnerDepth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(depthSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButtonRun)) - .addComponent(ImpactScrollPane)) + .addComponent(runBtn)) + .addComponent(impactScrollPane)) .addGap(11, 11, 11)) ); mainPanelLayout.setVerticalGroup( @@ -107,98 +98,67 @@ private void initComponents() { .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup() .addGap(11, 11, 11) .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(ImpactLabel) - .addComponent(jButtonRun) - .addComponent(jSpinnerDepth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(ChangesLabel) - .addComponent(jRBGit) - .addComponent(jRBCurrent)) + .addComponent(impactLabel) + .addComponent(runBtn) + .addComponent(depthSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(changesLabel) + .addComponent(gitRB) + .addComponent(currentRB)) .addGap(4, 4, 4) .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(ChangesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE) - .addComponent(ImpactScrollPane)) + .addComponent(changesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE) + .addComponent(impactScrollPane)) .addGap(11, 11, 11)) ); - jButtonRun.addActionListener(new AbstractAction() { + runBtn.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { - jButtonRun.setEnabled(false); + runBtn.setEnabled(false); actionRun(); - jButtonRun.setEnabled(true); + runBtn.setEnabled(true); } }); - jSpinnerDepth.addChangeListener(new ChangeListener() { + depthSpinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { - Utils.depth=(Integer)jSpinnerDepth.getValue(); + Utils.depth = (Integer) depthSpinner.getValue(); } }); - jRBCurrent.addChangeListener(new ChangeListener() { + currentRB.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { - if(jRBCurrent.isSelected()){ - jRBGit.setSelected(false); - Utils.isCurrent=true; - }else{ - jRBGit.setSelected(true); - Utils.isCurrent=false; + if (currentRB.isSelected()) { + gitRB.setSelected(false); + Utils.isCurrent = true; + } else { + gitRB.setSelected(true); + Utils.isCurrent = false; } } }); - jRBGit.addChangeListener(new ChangeListener() { + gitRB.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { - if(jRBGit.isSelected()){ - jRBCurrent.setSelected(false); - }else{ - jRBCurrent.setSelected(true); - } + currentRB.setSelected(!gitRB.isSelected()); } }); } - public void setChanges(String[] strings){ - jList1 = new javax.swing.JList<>(strings); - ChangesScrollPane.setViewportView(jList1); + public void setChanges(String[] strings) { + changesList = new javax.swing.JList<>(strings); + changesScrollPane.setViewportView(changesList); - /*jList1.addListSelectionListener(new ListSelectionListener() { - @Override - public void valueChanged(ListSelectionEvent e) { - if(e.getValueIsAdjusting()) { - //jList2 = new javax.swing.JList();//methodList.get(jList1.getSelectedIndex()).getImpactSet().getReferences() - //DefaultListModel - listModel.clear(); - - for (ReferenceEntity reference:methodList.get(jList1.getSelectedIndex()).getImpactSet().getReferences()) { - listModel.addElement(reference); - } - - ImpactScrollPane.setViewportView(jList2); - jList2.addMouseListener(new ListListener() { - @Override - public void mouseClicked(MouseEvent e) { - JList list = (JList)e.getSource(); - if (e.getClickCount() == 2) { - methodList.get(jList1.getSelectedIndex()).getImpactSet().navigate(jList2.getSelectedIndex()); - } - } - }); - - ImpactLabel.setText("Impact("+methodList.get(jList1.getSelectedIndex()).getImpactSet().getReferencesString().size()+")"); - } - } - });*/ - jList1.addMouseListener(new ListListener() { + changesList.addMouseListener(new ListListener() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { - methodList.get(jList1.getSelectedIndex()).navigate(); - }else if(e.getClickCount() == 1) { + methodList.get(changesList.getSelectedIndex()).navigate(); + } else if (e.getClickCount() == 1) { createImpactList(); } } @@ -206,46 +166,40 @@ public void mouseClicked(MouseEvent e) { } public void actionRun() { - /*ProgressManager.getInstance().run(new Task.Backgroundable(project,"Searching") { - @Override - public void run(@NotNull ProgressIndicator indicator) { - - } - });*/ methodList = projectManager.getClassEntityList(project); + ArrayList strings = new ArrayList(); + int i = 0; - ArrayList strings=new ArrayList(); - int i=0; for (MethodEntity method : methodList) { strings.add(method.toString()); i++; } setChanges(Utils.GetStringArray(strings)); - jList1.setSelectedIndex(0); + changesList.setSelectedIndex(0); createImpactList(); } - public void createImpactList(){ + public void createImpactList() { listModel.clear(); - for (ReferenceEntity reference:methodList.get(jList1.getSelectedIndex()).getImpactSet().getReferences()) { + for (ReferenceEntity reference : methodList.get(changesList.getSelectedIndex()).getImpactSet().getReferences()) { listModel.addElement(reference); } - ImpactScrollPane.setViewportView(jList2); - jList2.addMouseListener(new ListListener() { + impactScrollPane.setViewportView(impactList); + impactList.addMouseListener(new ListListener() { @Override public void mouseClicked(MouseEvent e) { - JList list = (JList)e.getSource(); + JList list = (JList) e.getSource(); if (e.getClickCount() == 2) { - methodList.get(jList1.getSelectedIndex()).getImpactSet().navigate(jList2.getSelectedIndex()); + methodList.get(changesList.getSelectedIndex()).getImpactSet().navigate(impactList.getSelectedIndex()); } } }); - ImpactLabel.setText("Impact("+methodList.get(jList1.getSelectedIndex()).getImpactSet().getReferencesString().size()+")"); + impactLabel.setText("Impact(" + methodList.get(changesList.getSelectedIndex()).getImpactSet().getReferencesString().size() + ")"); } public JPanel getContent() { diff --git a/src/main/java/MyToolWindowFactory.java b/src/main/java/MyToolWindowFactory.java index 44d4f55..e0a65d7 100644 --- a/src/main/java/MyToolWindowFactory.java +++ b/src/main/java/MyToolWindowFactory.java @@ -8,7 +8,7 @@ public class MyToolWindowFactory implements ToolWindowFactory { public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) { - MyToolWindow myToolWindow = new MyToolWindow(toolWindow,project); + MyToolWindow myToolWindow = new MyToolWindow(toolWindow, project); ContentFactory contentFactory = ContentFactory.SERVICE.getInstance(); Content content = contentFactory.createContent(myToolWindow.getContent(), "", false); toolWindow.getContentManager().addContent(content); diff --git a/src/main/java/ProjectManager.java b/src/main/java/ProjectManager.java index 24af06c..4e20baf 100644 --- a/src/main/java/ProjectManager.java +++ b/src/main/java/ProjectManager.java @@ -1,31 +1,23 @@ -//import com.intellij.ide.highlighter.JavaFileType; + import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.fileEditor.FileEditorManager; -import com.intellij.openapi.fileEditor.OpenFileDescriptor; -import com.intellij.openapi.progress.ProgressIndicator; -import com.intellij.openapi.progress.ProgressManager; -import com.intellij.openapi.progress.Task; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.impl.PsiDocumentManagerImpl; -import com.intellij.psi.search.FilenameIndex; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.search.searches.MethodReferencesSearch; import com.intellij.psi.util.PsiTreeUtil; import model.ImpactSet; import model.MethodEntity; import model.ReferenceEntity; -import org.jetbrains.annotations.NotNull; -import org.jf.dexlib2.iface.reference.MethodReference; - import java.util.ArrayList; import java.util.List; public class ProjectManager { public List getClassEntityList(Project project) { - if(Utils.isCurrent) { + if (Utils.isCurrent) { List methodList = new ArrayList<>(); Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor(); if (editor == null) @@ -38,7 +30,7 @@ public List getClassEntityList(Project project) { } return methodList; - }else{ + } else { List methodList = GitVcs.getAffectedFiles(project); analyzeClasses(methodList, project); return methodList; @@ -49,58 +41,60 @@ private void analyzeClasses(List methodList, PsiClass[] psiClasses for (PsiClass psiClas : psiClasses) { for (PsiMethod method : psiClas.getMethods()) { - MethodEntity methodEntity=new MethodEntity(method); - methodEntity.setImpactSet(execute(method,Utils.depth,project)); + MethodEntity methodEntity = new MethodEntity(method); + methodEntity.setImpactSet(execute(method, Utils.depth, project)); - for (ReferenceEntity referenceEntity:executeCallee(method,Utils.depth).getReferences()) { - if(!methodEntity.getImpactSet().contains(referenceEntity)){ + for (ReferenceEntity referenceEntity : executeCallee(method, Utils.depth).getReferences()) { + if (!methodEntity.getImpactSet().contains(referenceEntity)) { methodEntity.getImpactSet().addReference(referenceEntity); } } - methodList.add(methodEntity); - //PsiFile[] psiFile=FilenameIndex.getFilesByName(project,"",GlobalSearchScope.projectScope(project)); } - //indicator.setFraction(indicator.getFraction()+fraction); + } } private void analyzeClasses(List methodList, Project project) { - for (MethodEntity methodEntity : methodList) { - methodEntity.setImpactSet(execute(methodEntity.getPsiMethod(),Utils.depth,project)); + for (MethodEntity methodEntity : methodList) { + methodEntity.setImpactSet(execute(methodEntity.getPsiMethod(), Utils.depth, project)); - for (ReferenceEntity referenceEntity:executeCallee(methodEntity.getPsiMethod(),Utils.depth).getReferences()) { - if(!methodEntity.getImpactSet().contains(referenceEntity)){ - methodEntity.getImpactSet().addReference(referenceEntity); - } + for (ReferenceEntity referenceEntity : executeCallee(methodEntity.getPsiMethod(), Utils.depth).getReferences()) { + if (!methodEntity.getImpactSet().contains(referenceEntity)) { + methodEntity.getImpactSet().addReference(referenceEntity); } } + } } - public ImpactSet execute(PsiMethod method,int depth,Project project){ - if(depth==0){return null;} + public ImpactSet execute(PsiMethod method, int depth, Project project) { + if (depth == 0) { + return null; + } - ImpactSet impactSet =new ImpactSet(); + ImpactSet impactSet = new ImpactSet(); - try{ + try { for (PsiReference psiReference : - MethodReferencesSearch.search(method, GlobalSearchScope.projectScope(project),false).findAll()) { - impactSet.addReference(new ReferenceEntity(psiReference,Utils.depth-depth+1)); + MethodReferencesSearch.search(method, GlobalSearchScope.projectScope(project), false).findAll()) { + impactSet.addReference(new ReferenceEntity(psiReference, Utils.depth - depth + 1)); } - }catch (Exception e){ - System.out.println("ProjectManager:75:"+e.getMessage()); + } catch (Exception e) { + System.out.println("ProjectManager:75:" + e.getMessage()); } - ImpactSet indirectImpactSet =new ImpactSet(); + ImpactSet indirectImpactSet = new ImpactSet(); - for (ReferenceEntity referenceEntity:impactSet.getReferences()) { - indirectImpactSet=execute(referenceEntity.getPsiMethod(),depth-1,project); + for (ReferenceEntity referenceEntity : impactSet.getReferences()) { + indirectImpactSet = execute(referenceEntity.getPsiMethod(), depth - 1, project); } - if(indirectImpactSet==null){return impactSet;} + if (indirectImpactSet == null) { + return impactSet; + } - for (ReferenceEntity referenceEntity:indirectImpactSet.getReferences()) { - if(!impactSet.contains(referenceEntity)){ + for (ReferenceEntity referenceEntity : indirectImpactSet.getReferences()) { + if (!impactSet.contains(referenceEntity)) { impactSet.addReference(referenceEntity); } } @@ -108,36 +102,40 @@ public ImpactSet execute(PsiMethod method,int depth,Project project){ return impactSet; } - public ImpactSet executeCallee(PsiMethod method,int depth){ - if(depth==0){return null;} + public ImpactSet executeCallee(PsiMethod method, int depth) { + if (depth == 0) { + return null; + } - ImpactSet impactSet =new ImpactSet(); + ImpactSet impactSet = new ImpactSet(); - try{ - PsiStatement[] psiStatements=method.getBody().getStatements(); + try { + PsiStatement[] psiStatements = method.getBody().getStatements(); for (int i = 0; i < psiStatements.length; i++) { - for(PsiElement child:psiStatements[i].getChildren()){ - if(child instanceof PsiMethodCallExpression){ - PsiMethodCallExpression psiMethodCallExpression= (PsiMethodCallExpression)child; - impactSet.addReference(new ReferenceEntity(psiMethodCallExpression.resolveMethod(),Utils.depth-depth+1)); + for (PsiElement child : psiStatements[i].getChildren()) { + if (child instanceof PsiMethodCallExpression) { + PsiMethodCallExpression psiMethodCallExpression = (PsiMethodCallExpression) child; + impactSet.addReference(new ReferenceEntity(psiMethodCallExpression.resolveMethod(), Utils.depth - depth + 1)); } } } - }catch (Exception e){ - System.out.println("ProjectManager:111:"+e.getMessage()+" > "+method.getName()); + } catch (Exception e) { + System.out.println("ProjectManager:111:" + e.getMessage() + " > " + method.getName()); e.printStackTrace(); } - ImpactSet indirectImpactSet =new ImpactSet(); + ImpactSet indirectImpactSet = new ImpactSet(); - for (ReferenceEntity referenceEntity:impactSet.getReferences()) { - indirectImpactSet=executeCallee(referenceEntity.getPsiMethod(),depth-1); + for (ReferenceEntity referenceEntity : impactSet.getReferences()) { + indirectImpactSet = executeCallee(referenceEntity.getPsiMethod(), depth - 1); } - if(indirectImpactSet==null){return impactSet;} + if (indirectImpactSet == null) { + return impactSet; + } - for (ReferenceEntity referenceEntity:indirectImpactSet.getReferences()) { - if(!impactSet.contains(referenceEntity)){ + for (ReferenceEntity referenceEntity : indirectImpactSet.getReferences()) { + if (!impactSet.contains(referenceEntity)) { impactSet.addReference(referenceEntity); } } @@ -145,11 +143,11 @@ public ImpactSet executeCallee(PsiMethod method,int depth){ return impactSet; } - public PsiElement getElement(int line,PsiFile psiFile,Project project){ + public PsiElement getElement(int line, PsiFile psiFile, Project project) { final Document document = PsiDocumentManagerImpl.getInstance(project).getDocument(psiFile); final int offset = document.getLineStartOffset(line - 1); PsiMethod psiMethod = PsiTreeUtil.getParentOfType(psiFile.findElementAt(offset), PsiMethod.class); - System.out.println("Text : "+psiFile.findElementAt(offset)+psiMethod.getName()); + System.out.println("Text : " + psiFile.findElementAt(offset) + psiMethod.getName()); return psiFile.findElementAt(offset); } diff --git a/src/main/java/Utils.java b/src/main/java/Utils.java index 6cf7dc4..21b06c2 100644 --- a/src/main/java/Utils.java +++ b/src/main/java/Utils.java @@ -1,12 +1,12 @@ import java.util.ArrayList; public class Utils { - public static int depth=1; - public static boolean isCurrent=true; + public static int depth = 1; + public static boolean isCurrent = true; - public static String[] GetStringArray(ArrayList arr) - {//check to get from directly Referance entity - String str[] = new String[arr.size()]; + public static String[] GetStringArray(ArrayList arr) { + //check to get from directly Referance entity + String[] str = new String[arr.size()]; for (int j = 0; j < arr.size(); j++) { str[j] = arr.get(j); } diff --git a/src/main/java/model/ClassEntity.java b/src/main/java/model/ClassEntity.java deleted file mode 100644 index 06af129..0000000 --- a/src/main/java/model/ClassEntity.java +++ /dev/null @@ -1,29 +0,0 @@ -package model; - -import java.util.ArrayList; -import java.util.List; - -public class ClassEntity { - private String mName; - private List mMethodList = new ArrayList(); - - public ClassEntity(String name) { - mName = name; - } - - public String getName() { - return mName; - } - - public void setName(String name) { - mName = name; - } - - public void addMethod(MethodEntity methodName) { - mMethodList.add(methodName); - } - - public List getMethodList() { - return mMethodList; - } -} diff --git a/src/main/java/model/ImpactSet.java b/src/main/java/model/ImpactSet.java index 366b7dd..38b77f3 100644 --- a/src/main/java/model/ImpactSet.java +++ b/src/main/java/model/ImpactSet.java @@ -1,18 +1,14 @@ package model; -import com.intellij.pom.Navigatable; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiReference; - import java.util.ArrayList; public class ImpactSet { - private ArrayList references; - private ArrayList referencesString; + private final ArrayList references; + private final ArrayList referencesString; public ImpactSet() { - references=new ArrayList(); - referencesString=new ArrayList(); + references = new ArrayList<>(); + referencesString = new ArrayList<>(); } public void addReference(ReferenceEntity reference) { @@ -32,9 +28,11 @@ public ArrayList getReferencesString() { return referencesString; } - public ArrayList getReferences() { return references; } + public ArrayList getReferences() { + return references; + } - public boolean contains(ReferenceEntity referenceEntity){ + public boolean contains(ReferenceEntity referenceEntity) { //to check signature return referencesString.contains(referenceEntity.getDisplayString()); } diff --git a/src/main/java/model/MethodEntity.java b/src/main/java/model/MethodEntity.java index a1f9fb1..54d9b00 100644 --- a/src/main/java/model/MethodEntity.java +++ b/src/main/java/model/MethodEntity.java @@ -3,18 +3,15 @@ import com.intellij.pom.Navigatable; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiMethod; -import com.intellij.psi.PsiReference; - -import java.util.ArrayList; public class MethodEntity { - private String name; + private final String name; private ImpactSet impactSet; - private PsiMethod psiMethod; + private final PsiMethod psiMethod; public MethodEntity(PsiMethod psiMethod) { this.name = psiMethod.getName(); - this.psiMethod=psiMethod; + this.psiMethod = psiMethod; } @Override @@ -22,25 +19,25 @@ public String toString() { return this.name; } - public void setImpactSet(ImpactSet impactSet) { - this.impactSet = impactSet; - } - public ImpactSet getImpactSet() { return impactSet; } + public void setImpactSet(ImpactSet impactSet) { + this.impactSet = impactSet; + } + public PsiMethod getPsiMethod() { return psiMethod; } public void navigate() { - try{ + try { PsiElement navigationElement = psiMethod.getNavigationElement(); if (navigationElement != null && navigationElement instanceof Navigatable && ((Navigatable) navigationElement).canNavigate()) { ((Navigatable) navigationElement).navigate(true); } - }catch (Exception e){ + } catch (Exception e) { } } diff --git a/src/main/java/model/ReferenceEntity.java b/src/main/java/model/ReferenceEntity.java index be78a79..0959cd6 100644 --- a/src/main/java/model/ReferenceEntity.java +++ b/src/main/java/model/ReferenceEntity.java @@ -6,29 +6,29 @@ public class ReferenceEntity { - private PsiReference psiReference; - private PsiMethod psiMethod; - private PsiClass psiClass; - private String displayString; - private int depth; + private final PsiReference psiReference; + private final PsiMethod psiMethod; + private final PsiClass psiClass; + private final String displayString; + private final int depth; private boolean isCaller = false; - public ReferenceEntity(PsiReference psiReference,int depth) { + public ReferenceEntity(PsiReference psiReference, int depth) { this.psiReference = psiReference; psiMethod = PsiTreeUtil.getParentOfType(psiReference.getElement(), PsiMethod.class); - psiClass=psiMethod.getContainingClass(); - this.depth=depth; - this.isCaller=true; - displayString=psiClass.getContainingFile().getContainingDirectory().toString()+"->"+psiClass.getName()+"->"+psiMethod.getSignature(PsiSubstitutor.EMPTY).toString(); + psiClass = psiMethod.getContainingClass(); + this.depth = depth; + this.isCaller = true; + displayString = psiClass.getContainingFile().getContainingDirectory().toString() + "->" + psiClass.getName() + "->" + psiMethod.getSignature(PsiSubstitutor.EMPTY).toString(); System.out.println(displayString); } - public ReferenceEntity(PsiMethod psiMethod,int depth) { + public ReferenceEntity(PsiMethod psiMethod, int depth) { this.psiReference = psiMethod.getReference(); this.psiMethod = psiMethod; - psiClass=psiMethod.getContainingClass(); - this.depth=depth; - displayString=psiClass.getContainingFile().getContainingDirectory().toString()+"->"+psiClass.getName()+"->"+psiMethod.getSignature(PsiSubstitutor.EMPTY).toString(); + psiClass = psiMethod.getContainingClass(); + this.depth = depth; + displayString = psiClass.getContainingFile().getContainingDirectory().toString() + "->" + psiClass.getName() + "->" + psiMethod.getSignature(PsiSubstitutor.EMPTY).toString(); } public PsiMethod getPsiMethod() { @@ -52,13 +52,13 @@ public boolean isCaller() { } public void navigate() { - try{ + try { PsiElement navigationElement = psiReference.getElement().getNavigationElement(); if (navigationElement != null && navigationElement instanceof Navigatable && ((Navigatable) navigationElement).canNavigate()) { ((Navigatable) navigationElement).navigate(true); } - }catch (Exception e){ + } catch (Exception e) { PsiElement navigationElement = psiMethod.getNavigationElement(); if (navigationElement != null && navigationElement instanceof Navigatable && ((Navigatable) navigationElement).canNavigate()) { ((Navigatable) navigationElement).navigate(true); diff --git a/src/test/java/model/ClassEntityTest.java b/src/test/java/model/ClassEntityTest.java deleted file mode 100644 index 47bdb58..0000000 --- a/src/test/java/model/ClassEntityTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package model; - -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; - -import static org.junit.Assert.*; - -public class ClassEntityTest { - private ClassEntity clsEnt = new ClassEntity("test"); - @Test - public void getName() { - assertEquals("test", clsEnt.getName()); - } - - @Test - public void setName() { - clsEnt.setName("test_"); - assertEquals("test_", clsEnt.getName()); - } - - @Test - public void addMethod() { - List testMethodList = new ArrayList<>(); - MethodEntity methodEnt = new MethodEntity("testMethod"); - testMethodList.add(methodEnt); - clsEnt.addMethod(methodEnt); - assertEquals(testMethodList.get(0),clsEnt.getMethodList().get(0)); - } - - @Test - public void getMethodList() { - assertEquals(0, clsEnt.getMethodList().size()); - } -} \ No newline at end of file