├── .gitignore ├── .idea ├── .gitignore ├── .name ├── codeStyleSettings.xml ├── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── compiler.xml ├── copyright │ ├── profiles_settings.xml │ └── stefku.xml ├── encodings.xml ├── inspectionProfiles │ └── Project_Default.xml ├── libraries │ ├── Maven__junit_junit_4_11.xml │ ├── Maven__org_hamcrest_hamcrest_core_1_3.xml │ └── jdepend_2_9_5.xml ├── misc.xml ├── modules.xml ├── scopes │ └── scope_settings.xml ├── uiDesigner.xml └── vcs.xml ├── LICENSE ├── META-INF └── plugin.xml ├── README.md ├── doc ├── ExampleDiagram_ch.docksnet.app.MainClass.png ├── Example_show_cluster_count.png ├── coupling_through_OtherReferences_tool_window.png ├── settings_default_categories.png └── show_path_between_nodes.gif ├── intellij-reference-diagram.iml ├── lib └── jdepend-2.9.5.jar ├── src └── ch │ └── docksnet │ ├── rgraph │ ├── LCOMConverter.java │ ├── ProjectService.java │ ├── PsiElementDispatcher.java │ ├── PsiUtils.java │ ├── ReferenceDiagramColorManager.java │ ├── ReferenceDiagramDataModel.java │ ├── ReferenceDiagramElementManager.java │ ├── ReferenceDiagramExtras.java │ ├── ReferenceDiagramProvider.java │ ├── ReferenceDiagramVfsResolver.java │ ├── ReferenceUmlCategoryManager.java │ ├── actions │ │ ├── ActionHelper.java │ │ ├── DeleteMarkedAction.java │ │ ├── IsolateMarkedAction.java │ │ ├── MarkAction.java │ │ ├── MarkCalleesAction.java │ │ ├── MarkCallersAction.java │ │ ├── ShowClusterCountAction.java │ │ ├── ShowOuterReferencesAction.java │ │ ├── UnmarkAction.java │ │ └── UnmarkAllAction.java │ ├── directory │ │ ├── PackageReferenceDiagramDataModel.java │ │ └── References.java │ ├── fqn │ │ ├── ClassFQN.java │ │ ├── FQN.java │ │ ├── FieldFQN.java │ │ ├── FileFQN.java │ │ ├── FileFQNReference.java │ │ ├── Hierarchically.java │ │ ├── MethodFQN.java │ │ └── PackageFQN.java │ ├── method │ │ ├── MethodReferenceDiagramDataModel.java │ │ ├── OuterReferences.java │ │ ├── ReferenceCount.java │ │ ├── ReferenceEdge.java │ │ ├── ReferenceNode.java │ │ └── SourceTargetPair.java │ └── toolwindow │ │ ├── ReferenceListToolWindow.java │ │ ├── ReferenceToolWindow.java │ │ ├── TestToolWindow.java │ │ └── VirtualFileCellRenderer.java │ └── utils │ ├── IncrementableSet.java │ ├── PreConditionUtil.java │ └── lcom │ ├── AbstractLCOMHSAnalyzer.java │ ├── CalleesSubgraphAnalyzer.java │ ├── CallersSubgraphAnalyzer.java │ ├── ClusterAnalyzer.java │ ├── LCOMAnalyzerData.java │ ├── LCOMHSAnalyzer.java │ └── LCOMNode.java └── test ├── Example_InnerClasses.png └── java └── ch └── docksnet ├── DependencyTest.java ├── app ├── AnonymousClass.java ├── InnerClasses.java ├── MainClass.java └── TestEnum.java ├── rgraph ├── MethodFQNTest.java └── fqn │ └── HierarchicallyTest.java └── utils └── IncrementableSetTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### JetBrains template 3 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion 4 | 5 | *.iml 6 | 7 | ## Directory-based project format: 8 | 9 | # User-specific stuff: 10 | # .idea/workspace.xml 11 | # .idea/tasks.xml 12 | # .idea/dictionaries 13 | 14 | # Sensitive or high-churn files: 15 | # .idea/dataSources.ids 16 | # .idea/dataSources.xml 17 | # .idea/sqlDataSources.xml 18 | # .idea/dynamic.xml 19 | # .idea/uiDesigner.xml 20 | 21 | ## Plugin-specific files: 22 | 23 | # IntelliJ 24 | /out/ 25 | 26 | .idea_modules/ 27 | 28 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | intellij-reference-diagram -------------------------------------------------------------------------------- /.idea/codeStyleSettings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 13 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/copyright/stefku.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__junit_junit_4_11.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/jdepend_2_9_5.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | ch.docksnet.rgraph 19 | Java Method Reference Diagram 20 | 3.1.1 21 | Stefan Zeller 22 | com.intellij.diagram 23 | com.intellij.modules.java 24 | 26 | This plugins gives an overview about coupling and cohesion of java files in a package, methods in a class. 27 | 41 | See https://github.com/Stefku/intellij-reference-diagram
42 | ]]>
43 | 44 | 46 |
  • 47 | Version 3.1.1: Fixes Toolbar in IntelliJ 2021.1+ 48 |

    49 | fixes: Toolbar is partially not working. See GitHub Issue #73. Thanks to @bash-spbu for contribution. 50 |

    51 |
  • 52 |
  • 53 | Version 3.1.0: IntelliJ 2021.1+ support 54 |

    55 | Known Issue: Toolbar is partially not working. See GitHub Issue #73. 56 |

    57 |
  • 58 |
  • 59 | Version 3.0.0 60 |
      61 |
    • Class reference diagram can be opened out of project view with a java file or class at focus. (Issue 61)
    • 62 |
    • New reference graph of java files of a whole package. Choose Show UML Diagram with a package at focus. (Issue 58)
    • 63 |
    • Overview of references not show in graph from outside with grouping by levels of coupling. See chapter Other References in README.md.
    • 64 |
    65 |
  • 66 |
  • 67 | Version 2.2.0: IntelliJ 2019.1+ support 68 |
  • 69 |
  • 70 | Version 2.1.0: Improve appearance of marked nodes. 71 |
  • 72 |
  • 73 | Version 2.0.0: Ability to mark nodes and delete or isolate them; Mark subgraph of callees / callers of a node and to show the path between two nodes. 74 |
  • 75 |
  • 76 | Version 1.6.0: Show cluster count in Toolbar; Bugfix with constructors of anonymous classes. 77 |
  • 78 |
  • 79 | Version 1.5.0: Support inner classes and enums. 80 |
  • 81 |
  • 82 | Version 1.4.0: Ability to hide node types like fields, static fields, methods, etc.; Show cluster count in pup up menu; Some 83 | bug fixes. 84 |
  • 85 |
  • 86 | Version 1.3.1: Bug with callees from inner classes. 87 |
  • 88 |
  • 89 | Version 1.3.0: Bug with callees from anonymous inner classes; Add ability to remove node from diagram; Diagram is not 90 | updated after refactoring. 91 |
  • 92 |
  • 93 | Version 1.2.0: Improve icons and lines; Small Bugfix. 94 |
  • 95 |
  • 96 | Version 1.1.1:Bug fixes. E.g. searching in diagram is now working CTRL+F12 / CTRL+F. 97 |
  • 98 |
  • 99 | Version 1.1.0: show modifiers and the count of reference from one element to another 100 |
  • 101 |
  • 102 | Version 1.0.1: Goto source (F4) is possible 103 |
  • 104 | 105 | ]]> 106 |
    107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 139 | 140 | 141 | 142 | 143 |
    144 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java Reference Diagram Plugin for IntelliJ IDEA Ultimate 2 | 3 | This plugin for IntelliJ IDEA Ultimate helps to get an overview of coupling and cohesion in java projects in two different contexts: 4 | - References of methods and fields in classes 5 | - References between files in packages 6 | 7 | This plugin is available in IntelliJ Plugin Registry: [https://plugins.jetbrains.com/plugin/7996](https://plugins.jetbrains.com/plugin/7996). 8 | 9 | # Usage 10 | With this plugin a new diagram is available embedded in the action "Show UML Diagram" ⌥+⇧+⌘+U / CTRL+SHIFT+ALT+U. When focus 11 | is on a file or inside a file then the method reference diagram will be opened. If fucus is on a package, then file reference diagram will be 12 | opened. 13 | 14 | # Features 15 | - Show or hide categories of elements, like fields, methods, constructors, class initializers and their static companions. 16 | - If the return value of a method is assigned to a field, then this is also threaded like a dependency and is shown in a different line style. 17 | - As other diagrams, features available like Goto Source (F4), File Structure (Strg+F12) and Search (Ctrl+F), refactorings, Find Usages etc. 18 | - Nodes can be removed from graph to help analyze cohesive structure of the cluss. 19 | - Ability to isolate a subgraph based on a node: A subgraph of all it's callers or callees. 20 | - Show the connection between two nodes. 21 | - The cluster count is shown in the toolbar. 22 | - Overview of other references not shown in graph (see chapter Outer References below). 23 | 24 | ## Cohesive clusters 25 | If you have a suspect class and you want to analyze the cohesivnes of it this diagram helps you to visualize the [lack of cohesion of methods] 26 | (http://sonar-jenkins.blogspot.ch/2012/12/what-is-lcom4.html). The clusters are shown visual in the diagram. Also, the cluster count is shown 27 | in the toolbar. This is useful if you have big classes in legacy projects and you are not able to see the cluster count directly in the 28 | diagram. 29 | 30 | Sometimes, cohesive groups are not separeted because they are connected through common methods or fields. For example for logging purposes. 31 | There's a great chance to see this as single nodes that are highly connected. With removing these nodes from the diagram you can see if there 32 | were hidden disconnected clusters. Comparing the cluster count before and after the removal helps with this task. 33 | 34 | ![](https://github.com/stefku/intellij-reference-diagram/raw/master/doc/Example_show_cluster_count.png) 35 | 36 | Does static methods and fields play a role in cohesion of a class? Just show or hide them via the toolbar. You can set the default behavior 37 | in the usual settings dialog of the others diagrams. 38 | 39 | ![](https://github.com/stefku/intellij-reference-diagram/raw/master/doc/settings_default_categories.png) 40 | 41 | ## Isolate subgraphs 42 | If you are interested in all methods and fields that can be reached by a certain method, then you can select that method and choose actions 43 | subsequently _Mark Callees_ and _Isolate Marked_. Or you want to see which methods can reach a given diagram element, then you choose actions 44 | subsequently _Mark Callers_ and _Isolate Marked_. 45 | 46 | ## Show Connection between two Nodes 47 | If you want to see the path between two methods you 48 | 1. Select the source of the desired path and _Mark Callees_ then _Isolate Marked_. 49 | 2. Select the destination of the desired path and _Mark Callers_ then _Isolate Marked_. 50 | 51 | ![](https://github.com/stefku/intellij-reference-diagram/raw/master/doc/show_path_between_nodes.gif) 52 | 53 | ## Other References 54 | For overview of coupling there is the information of _other references_ on the top right of the diagram. 55 | There are three numbers show: 56 | 1. Number of references from same package. (= package private) 57 | 2. Number of references from packages in the same hierarchy. (= public api) 58 | 3. Number of references from packages in a other hierarchy. (= spaghetti?) 59 | These are different kind of references. 60 | Where references from same package are kind of local of the package, the references from same hierarchy are part of the public api of the package. 61 | The references from other hierarchy (i.e. from sibling packages) could be a sign of spaghetti. 62 | 63 | **Example** 64 | ![](https://github.com/stefku/intellij-reference-diagram/raw/master/doc/coupling_through_OtherReferences_tool_window.png) 65 | 1. `ReferenceDiagramDataModel` references `OuterReferences`. In terms of package hierarchy `ch.docksnet.rgraph.method` is referenced from `ch.docksnet.rgraph`, which is a kind of wanted dependency direction. 66 | 2. `TestToolWindow` references `OuterReferences`. Here a sibling package ch.docksnet.rgraph.toolwindow references `ch.docksnet.rgraph.method`. 67 | 68 | ## Example 69 | 70 | This example shows five cohesive clusters in MainClass (source is showed below). 71 | A blue line indicates a method call, where a green line means that a field is coupled to a method in terms of cohesion. 72 | The numbers on the edges indicating the number of references. 73 | 74 | ![](https://github.com/stefku/intellij-reference-diagram/raw/master/doc/ExampleDiagram_ch.docksnet.app.MainClass.png) 75 | 76 | ```java 77 | public abstract class MainClass { 78 | 79 | { 80 | // class initializer 81 | method1(); 82 | } 83 | 84 | static { 85 | // static class initializer 86 | staticMethod(); 87 | staticField2 = StaticInnerClass.TEXT; 88 | } 89 | 90 | // field is coupled to a method, in terms of cohesion 91 | public static String staticField1 = staticMethod2c(); 92 | 93 | private static String staticField2; 94 | 95 | int field1; 96 | 97 | private int field2 = createInt(); 98 | 99 | public ENUM field3 = ENUM.A; 100 | 101 | public MainClass() { 102 | InnerClass innerClass = new InnerClass(); 103 | innerClass.getName(); 104 | } 105 | 106 | public MainClass(int field1, int field2, ENUM field3) { 107 | this.field1 = field1; 108 | this.field2 = field2; 109 | this.field3 = field3; 110 | } 111 | 112 | private int createInt() { 113 | return 0; 114 | } 115 | 116 | public static void staticMethod() { 117 | staticMethod2c(); 118 | } 119 | 120 | private static String staticMethod2c() { 121 | return null; 122 | } 123 | 124 | public final void method1() { 125 | method2(); 126 | } 127 | 128 | protected void method1(int a) { 129 | method2(); 130 | method2(); 131 | method2(); 132 | } 133 | 134 | void method2() { 135 | field1 = 4; 136 | field1 = 1; 137 | field3.getString(); 138 | } 139 | 140 | abstract void abstractMethod(); 141 | 142 | private void recursiveMethod() { 143 | recursiveMethod(); 144 | } 145 | 146 | class InnerClass { 147 | // TODO what about inner classes? 148 | String innerField = staticMethod2c(); 149 | public String getName() { 150 | return "Name"; 151 | } 152 | } 153 | 154 | static class StaticInnerClass { 155 | public int i =3; 156 | public static final String TEXT = "text"; 157 | static { 158 | System.out.println(TEXT); 159 | System.out.println(staticField1); 160 | } 161 | } 162 | 163 | enum ENUM { 164 | A, B, C; 165 | 166 | public String getString() { 167 | return "String"; 168 | } 169 | } 170 | 171 | } 172 | ``` 173 | # Known Issues 174 | - Diagram does not work for library files [#38](https://github.com/Stefku/intellij-reference-diagram/issues/38) 175 | 176 | # Support 177 | [Buy Me a Coffee](https://ko-fi.com/H2H3DOZZ) 178 | -------------------------------------------------------------------------------- /doc/ExampleDiagram_ch.docksnet.app.MainClass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Stefku/intellij-reference-diagram/5e21362f3da52dc814c49c8b689d85fc0da44e72/doc/ExampleDiagram_ch.docksnet.app.MainClass.png -------------------------------------------------------------------------------- /doc/Example_show_cluster_count.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Stefku/intellij-reference-diagram/5e21362f3da52dc814c49c8b689d85fc0da44e72/doc/Example_show_cluster_count.png -------------------------------------------------------------------------------- /doc/coupling_through_OtherReferences_tool_window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Stefku/intellij-reference-diagram/5e21362f3da52dc814c49c8b689d85fc0da44e72/doc/coupling_through_OtherReferences_tool_window.png -------------------------------------------------------------------------------- /doc/settings_default_categories.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Stefku/intellij-reference-diagram/5e21362f3da52dc814c49c8b689d85fc0da44e72/doc/settings_default_categories.png -------------------------------------------------------------------------------- /doc/show_path_between_nodes.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Stefku/intellij-reference-diagram/5e21362f3da52dc814c49c8b689d85fc0da44e72/doc/show_path_between_nodes.gif -------------------------------------------------------------------------------- /intellij-reference-diagram.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /lib/jdepend-2.9.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Stefku/intellij-reference-diagram/5e21362f3da52dc814c49c8b689d85fc0da44e72/lib/jdepend-2.9.5.jar -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/LCOMConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph; 18 | 19 | import ch.docksnet.rgraph.method.ReferenceNode; 20 | import ch.docksnet.utils.lcom.LCOMNode; 21 | import com.intellij.diagram.DiagramEdge; 22 | import com.intellij.diagram.DiagramNode; 23 | import com.intellij.psi.PsiClass; 24 | import com.intellij.psi.PsiClassInitializer; 25 | import com.intellij.psi.PsiElement; 26 | import com.intellij.psi.PsiField; 27 | import com.intellij.psi.PsiJavaFile; 28 | import com.intellij.psi.PsiMethod; 29 | import com.intellij.psi.impl.file.PsiJavaDirectoryImpl; 30 | 31 | import java.util.Collection; 32 | import java.util.HashMap; 33 | import java.util.Map; 34 | 35 | /** 36 | * @author Stefan Zeller 37 | */ 38 | class LCOMConverter { 39 | 40 | private final ReferenceDiagramVfsResolver vfsResolver = new ReferenceDiagramVfsResolver(); 41 | private final Map lcomNodeRegistry = new HashMap<>(); 42 | 43 | /** 44 | * Returns nodes representating a directed graph regarding to given {@code nodes} and {@code edges}. 45 | */ 46 | Collection convert(Collection> nodes, Collection> edges) { 48 | for (DiagramNode node : nodes) { 49 | String fqn = this.vfsResolver.getQualifiedName(node.getIdentifyingElement()); 50 | LCOMNode.Type type = resolveType(node); 51 | this.lcomNodeRegistry.put(fqn, new LCOMNode(fqn, type, (ReferenceNode) node)); 52 | } 53 | 54 | for (DiagramEdge edge : edges) { 55 | ReferenceNode source = (ReferenceNode) edge.getSource(); 56 | ReferenceNode target = (ReferenceNode) edge.getTarget(); 57 | String sourceFqn = this.vfsResolver.getQualifiedName(source.getIdentifyingElement()); 58 | String targetFqn = this.vfsResolver.getQualifiedName(target.getIdentifyingElement()); 59 | 60 | if (isSourceOrTargetNotRegistered(sourceFqn, targetFqn)) { 61 | continue; 62 | } 63 | 64 | this.lcomNodeRegistry.get(sourceFqn).addCallee(this.lcomNodeRegistry.get(targetFqn)); 65 | } 66 | 67 | return this.lcomNodeRegistry.values(); 68 | } 69 | 70 | private boolean isSourceOrTargetNotRegistered(String sourceFqn, String targetFqn) { 71 | return this.lcomNodeRegistry.get(sourceFqn) == null || this.lcomNodeRegistry.get(targetFqn) == null; 72 | } 73 | 74 | private LCOMNode.Type resolveType(DiagramNode referenceNode) { 75 | PsiElementDispatcher elementDispatcher = new PsiElementDispatcher() { 76 | 77 | @Override 78 | public LCOMNode.Type processClass(PsiClass psiClass) { 79 | return LCOMNode.Type.Class; 80 | } 81 | 82 | @Override 83 | public LCOMNode.Type processMethod(PsiMethod psiMethod) { 84 | if (psiMethod.isConstructor()) { 85 | return LCOMNode.Type.Constructur; 86 | } else { 87 | return LCOMNode.Type.Method; 88 | } 89 | } 90 | 91 | @Override 92 | public LCOMNode.Type processField(PsiField psiField) { 93 | if (psiField.hasModifierProperty("static")) { 94 | return LCOMNode.Type.Constant; 95 | } else { 96 | return LCOMNode.Type.Field; 97 | } 98 | } 99 | 100 | @Override 101 | public LCOMNode.Type processClassInitializer(PsiClassInitializer psiClassInitializer) { 102 | return LCOMNode.Type.ClassInitializer; 103 | } 104 | 105 | @Override 106 | public LCOMNode.Type processInnerClass(PsiClass innerClass) { 107 | return LCOMNode.Type.InnerClass; 108 | } 109 | 110 | @Override 111 | public LCOMNode.Type processStaticInnerClass(PsiClass staticInnerClass) { 112 | return LCOMNode.Type.StaticInnerClass; 113 | } 114 | 115 | @Override 116 | public LCOMNode.Type processEnum(PsiClass anEnum) { 117 | return LCOMNode.Type.Enum; 118 | } 119 | 120 | @Override 121 | public LCOMNode.Type processPackage(PsiJavaDirectoryImpl aPackage) { 122 | return LCOMNode.Type.Package; 123 | } 124 | 125 | @Override 126 | public LCOMNode.Type processFile(PsiJavaFile psiElement) { 127 | return LCOMNode.Type.File; 128 | } 129 | }; 130 | return elementDispatcher.dispatch(referenceNode.getIdentifyingElement()); 131 | } 132 | 133 | } 134 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/ProjectService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph; 18 | 19 | import ch.docksnet.rgraph.toolwindow.ReferenceListToolWindow; 20 | import com.intellij.openapi.project.Project; 21 | 22 | public class ProjectService { 23 | private final ReferenceListToolWindow otherHierarchieReferences; 24 | private final ReferenceListToolWindow sameHierarchieReferences; 25 | private final ReferenceListToolWindow samePackageReferences; 26 | 27 | public ProjectService(Project project) { 28 | this.otherHierarchieReferences = new ReferenceListToolWindow("Other Hierarchy", project); 29 | this.sameHierarchieReferences = new ReferenceListToolWindow("Same Hierarchy", project); 30 | this.samePackageReferences = new ReferenceListToolWindow("Same Package", project); 31 | } 32 | 33 | public ReferenceListToolWindow getOtherHierarchieReferences() { 34 | return this.otherHierarchieReferences; 35 | } 36 | 37 | public ReferenceListToolWindow getSameHierarchieReferences() { 38 | return this.sameHierarchieReferences; 39 | } 40 | 41 | public ReferenceListToolWindow getSamePackageReferences() { 42 | return this.samePackageReferences; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/PsiElementDispatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph; 18 | 19 | import com.intellij.psi.PsiClass; 20 | import com.intellij.psi.PsiClassInitializer; 21 | import com.intellij.psi.PsiElement; 22 | import com.intellij.psi.PsiField; 23 | import com.intellij.psi.PsiJavaFile; 24 | import com.intellij.psi.PsiMethod; 25 | import com.intellij.psi.impl.file.PsiJavaDirectoryImpl; 26 | 27 | /** 28 | * @author Stefan Zeller 29 | */ 30 | public abstract class PsiElementDispatcher { 31 | 32 | T dispatch(PsiElement psiElement) { 33 | if (psiElement instanceof PsiClass) { 34 | if (((PsiClass) psiElement).getContainingClass() == null) { 35 | return processClass((PsiClass) psiElement); 36 | } else { 37 | if (((PsiClass) psiElement).isEnum()) { 38 | return processEnum((PsiClass) psiElement); 39 | } else { 40 | if (((PsiClass) psiElement).hasModifierProperty("static")) { 41 | return processStaticInnerClass((PsiClass) psiElement); 42 | } else { 43 | return processInnerClass((PsiClass) psiElement); 44 | } 45 | } 46 | } 47 | } 48 | if (psiElement instanceof PsiMethod) { 49 | return processMethod((PsiMethod) psiElement); 50 | } 51 | if (psiElement instanceof PsiField) { 52 | return processField((PsiField) psiElement); 53 | } 54 | if (psiElement instanceof PsiClassInitializer) { 55 | return processClassInitializer((PsiClassInitializer) psiElement); 56 | } 57 | if (psiElement instanceof PsiJavaDirectoryImpl) { 58 | return processPackage((PsiJavaDirectoryImpl) psiElement); 59 | } 60 | if (psiElement instanceof PsiJavaFile) { 61 | return processFile((PsiJavaFile) psiElement); 62 | } 63 | throw new IllegalArgumentException("Type of PsiElement not supported: " + psiElement); 64 | } 65 | 66 | 67 | public abstract T processClass(PsiClass psiClass); 68 | 69 | public abstract T processMethod(PsiMethod psiMethod); 70 | 71 | public abstract T processField(PsiField psiField); 72 | 73 | public abstract T processClassInitializer(PsiClassInitializer psiClassInitializer); 74 | 75 | public abstract T processInnerClass(PsiClass innerClass); 76 | 77 | public abstract T processStaticInnerClass(PsiClass staticInnerClass); 78 | 79 | public abstract T processEnum(PsiClass anEnum); 80 | 81 | public abstract T processPackage(PsiJavaDirectoryImpl aPackage); 82 | 83 | public abstract T processFile(PsiJavaFile psiElement); 84 | } 85 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/PsiUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph; 18 | 19 | import ch.docksnet.rgraph.fqn.ClassFQN; 20 | import ch.docksnet.rgraph.fqn.FQN; 21 | import ch.docksnet.rgraph.fqn.FieldFQN; 22 | import ch.docksnet.rgraph.fqn.FileFQN; 23 | import ch.docksnet.rgraph.fqn.MethodFQN; 24 | import ch.docksnet.rgraph.fqn.PackageFQN; 25 | import com.intellij.openapi.application.ApplicationManager; 26 | import com.intellij.openapi.fileEditor.OpenFileDescriptor; 27 | import com.intellij.openapi.project.Project; 28 | import com.intellij.openapi.vfs.VirtualFile; 29 | import com.intellij.pom.Navigatable; 30 | import com.intellij.psi.JavaPsiFacade; 31 | import com.intellij.psi.PsiAnonymousClass; 32 | import com.intellij.psi.PsiClass; 33 | import com.intellij.psi.PsiClassInitializer; 34 | import com.intellij.psi.PsiDirectory; 35 | import com.intellij.psi.PsiElement; 36 | import com.intellij.psi.PsiField; 37 | import com.intellij.psi.PsiFile; 38 | import com.intellij.psi.PsiJavaFile; 39 | import com.intellij.psi.PsiMethod; 40 | import com.intellij.psi.impl.file.PsiJavaDirectoryImpl; 41 | import com.intellij.psi.impl.source.tree.CompositePsiElement; 42 | import com.intellij.psi.search.GlobalSearchScope; 43 | import org.jetbrains.annotations.Nullable; 44 | 45 | import java.util.LinkedList; 46 | import java.util.List; 47 | 48 | /** 49 | * @author Stefan Zeller 50 | */ 51 | public class PsiUtils { 52 | 53 | @Nullable 54 | public static PsiElement getRootPsiElement(PsiClass psiClass, CompositePsiElement psiElement) { 55 | return getRootPsiElementWithStack(psiClass, psiElement, new LinkedList()); 56 | } 57 | 58 | private static PsiElement getRootPsiElementWithStack(PsiClass psiClass, PsiElement psiElement, List stack) { 59 | stack.add(psiElement); 60 | PsiElement parent = psiElement.getParent(); 61 | if (parent == null) { 62 | return null; 63 | } 64 | try { 65 | if (parent instanceof PsiMethod) { 66 | if (PsiUtils.classHasMethod(psiClass, (PsiMethod) parent)) { 67 | return parent; 68 | } 69 | } else if (parent instanceof PsiClassInitializer) { 70 | if (PsiUtils.classHasClassInitializer(psiClass, (PsiClassInitializer) parent)) { 71 | return parent; 72 | } 73 | } else if (parent instanceof PsiField) { 74 | if (PsiUtils.classHasField(psiClass, (PsiField) parent)) { 75 | return parent; 76 | } 77 | } else if (parent instanceof PsiClass) { 78 | if (psiClass.equals(((PsiClass) parent).getContainingClass())) { 79 | return parent; 80 | } 81 | } else if (parent instanceof PsiAnonymousClass) { 82 | if (((PsiAnonymousClass) parent).getContainingClass().equals(psiClass)) { 83 | return parent; 84 | } 85 | } 86 | } catch (Exception ex) { 87 | stack.add(parent); 88 | String preparedStack = prepareStack(stack); 89 | throw new IllegalStateException("Cannot get root element. Stack: " + preparedStack); 90 | } 91 | 92 | return getRootPsiElementWithStack(psiClass, parent, stack); 93 | } 94 | 95 | private static String prepareStack(List stack) { 96 | StringBuilder sb = new StringBuilder(); 97 | 98 | for (PsiElement element : stack) { 99 | sb.append(element.toString()); 100 | sb.append(", "); 101 | } 102 | 103 | return sb.toString(); 104 | } 105 | 106 | private static boolean classHasMethod(PsiClass psiClass, PsiMethod other) { 107 | for (PsiMethod psiMethod : psiClass.getMethods()) { 108 | if (psiMethod.equals(other)) { 109 | return true; 110 | } 111 | } 112 | return false; 113 | } 114 | 115 | private static boolean classHasField(PsiClass psiClass, PsiField other) { 116 | for (PsiField psiField : psiClass.getFields()) { 117 | if (psiField.equals(other)) { 118 | return true; 119 | } 120 | } 121 | return false; 122 | } 123 | 124 | private static boolean classHasClassInitializer(PsiClass psiClass, PsiClassInitializer other) { 125 | for (PsiClassInitializer classInitializer : psiClass.getInitializers()) { 126 | if (classInitializer.equals(other)) { 127 | return true; 128 | } 129 | } 130 | return false; 131 | } 132 | 133 | static PsiClass getPsiClass(String classFQN, Project project) { 134 | return JavaPsiFacade.getInstance(project).findClass(classFQN, GlobalSearchScope 135 | .projectScope(project)); 136 | } 137 | 138 | public static PsiDirectory getPsiJavaDirectory(String packageFQN, Project project) { 139 | return JavaPsiFacade.getInstance(project).findPackage(packageFQN).getDirectories()[0]; 140 | } 141 | 142 | private static String getName(PsiClassInitializer psiClassInitializer) { 143 | if (psiClassInitializer.getModifierList().hasModifierProperty("static")) { 144 | return "[static init]"; 145 | } else { 146 | return "[init]"; 147 | } 148 | } 149 | 150 | static String getPresentableName(PsiElement psiElement) { 151 | PsiElementDispatcher psiElementDispatcher = new PsiElementDispatcher() { 152 | 153 | @Override 154 | public String processClass(PsiClass psiClass) { 155 | return psiClass.getName(); 156 | } 157 | 158 | @Override 159 | public String processMethod(PsiMethod psiMethod) { 160 | List parameterArray = MethodFQN.getParameterArray(psiMethod); 161 | String parameterRepresentation = MethodFQN.createParameterRepresentation(parameterArray); 162 | return psiMethod.getName() + "(" + parameterRepresentation + ")"; 163 | } 164 | 165 | @Override 166 | public String processField(PsiField psiField) { 167 | return psiField.getName(); 168 | } 169 | 170 | @Override 171 | public String processClassInitializer(PsiClassInitializer psiClassInitializer) { 172 | return getName(psiClassInitializer); 173 | } 174 | 175 | @Override 176 | public String processInnerClass(PsiClass innerClass) { 177 | return innerClass.getName(); 178 | } 179 | 180 | @Override 181 | public String processStaticInnerClass(PsiClass staticInnerClass) { 182 | return staticInnerClass.getName(); 183 | } 184 | 185 | @Override 186 | public String processEnum(PsiClass anEnum) { 187 | return anEnum.getName(); 188 | } 189 | 190 | @Override 191 | public String processPackage(PsiJavaDirectoryImpl aPackage) { 192 | return aPackage.getName(); 193 | } 194 | 195 | @Override 196 | public String processFile(PsiJavaFile aFile) { 197 | return aFile.getName(); 198 | } 199 | }; 200 | 201 | return psiElementDispatcher.dispatch(psiElement); 202 | } 203 | 204 | public static FQN getFqn(PsiElement psiElement) { 205 | PsiElementDispatcher psiElementDispatcher = new PsiElementDispatcher() { 206 | 207 | @Override 208 | public FQN processClass(PsiClass psiClass) { 209 | return ClassFQN.create(psiClass); 210 | } 211 | 212 | @Override 213 | public FQN processMethod(PsiMethod psiMethod) { 214 | return MethodFQN.create(psiMethod); 215 | } 216 | 217 | @Override 218 | public FQN processField(PsiField psiField) { 219 | return FieldFQN.create(psiField); 220 | } 221 | 222 | @Override 223 | public FQN processClassInitializer(PsiClassInitializer psiClassInitializer) { 224 | return new FQN() { 225 | @Override 226 | public String getFQN() { 227 | return getName(psiClassInitializer); 228 | } 229 | }; 230 | } 231 | 232 | @Override 233 | public FQN processInnerClass(PsiClass innerClass) { 234 | return ClassFQN.create(innerClass); 235 | } 236 | 237 | @Override 238 | public FQN processStaticInnerClass(PsiClass staticInnerClass) { 239 | return ClassFQN.create(staticInnerClass); 240 | } 241 | 242 | @Override 243 | public FQN processEnum(PsiClass anEnum) { 244 | return ClassFQN.create(anEnum); 245 | } 246 | 247 | @Override 248 | public FQN processPackage(PsiJavaDirectoryImpl aPackage) { 249 | return PackageFQN.create(aPackage); 250 | } 251 | 252 | @Override 253 | public FQN processFile(PsiJavaFile psiElement) { 254 | return FileFQN.create(psiElement); 255 | } 256 | }; 257 | 258 | return psiElementDispatcher.dispatch(psiElement); 259 | } 260 | 261 | public static void navigate(PsiElement psiElement, Project project) { 262 | ApplicationManager.getApplication().invokeLater( 263 | () -> { 264 | ApplicationManager.getApplication().assertIsDispatchThread(); 265 | Navigatable n = (Navigatable) psiElement; 266 | //this is for better cursor position 267 | if (psiElement instanceof PsiFile) { 268 | VirtualFile file = ((PsiFile) psiElement).getVirtualFile(); 269 | if (file == null) return; 270 | OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, 0, 0); 271 | n = descriptor.setUseCurrentWindow(true); 272 | } 273 | 274 | if (n.canNavigate()) { 275 | n.navigate(true); 276 | } 277 | } 278 | ); 279 | } 280 | 281 | } 282 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/ReferenceDiagramColorManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph; 18 | 19 | import ch.docksnet.rgraph.method.ReferenceEdge; 20 | import com.intellij.diagram.DiagramColorManagerBase; 21 | import com.intellij.diagram.DiagramEdge; 22 | import com.intellij.ui.JBColor; 23 | 24 | import java.awt.*; 25 | 26 | /** 27 | * @author Stefan Zeller 28 | */ 29 | public class ReferenceDiagramColorManager extends DiagramColorManagerBase { 30 | 31 | @Override 32 | public Color getEdgeColor(DiagramEdge edge) { 33 | final String edgeType = edge.getRelationship().toString(); 34 | if (ReferenceEdge.Type.FIELD_TO_METHOD.name().equals(edgeType)) { 35 | return new JBColor(new Color(9, 128, 0), new Color(83, 128, 103)); 36 | } 37 | if (ReferenceEdge.Type.REFERENCE.name().equals(edgeType)) { 38 | return new JBColor(new Color(0, 26, 128), new Color(140, 177, 197)); 39 | } 40 | return super.getEdgeColor(edge); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/ReferenceDiagramElementManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph; 18 | 19 | import com.intellij.diagram.AbstractDiagramElementManager; 20 | import com.intellij.diagram.presentation.DiagramState; 21 | import com.intellij.openapi.actionSystem.CommonDataKeys; 22 | import com.intellij.openapi.actionSystem.DataContext; 23 | import com.intellij.psi.PsiClass; 24 | import com.intellij.psi.PsiElement; 25 | import com.intellij.psi.impl.file.PsiJavaDirectoryImpl; 26 | import com.intellij.psi.util.PsiTreeUtil; 27 | import com.intellij.ui.SimpleColoredText; 28 | import org.jetbrains.annotations.Nullable; 29 | 30 | /** 31 | * @author Stefan Zeller 32 | */ 33 | @SuppressWarnings("ALL") 34 | public class ReferenceDiagramElementManager extends AbstractDiagramElementManager { 35 | 36 | /** 37 | * Entry point to decide if a diagram can be show for the given DataContext. 38 | * 39 | * @return null if no diagram can be shown. 40 | */ 41 | @Nullable 42 | @Override 43 | public PsiElement findInDataContext(DataContext context) { 44 | 45 | if (CommonDataKeys.PSI_ELEMENT.getData(context) instanceof PsiClass) { 46 | return CommonDataKeys.PSI_ELEMENT.getData(context); 47 | } 48 | 49 | if (CommonDataKeys.PSI_ELEMENT.getData(context) instanceof PsiJavaDirectoryImpl) { 50 | return CommonDataKeys.PSI_ELEMENT.getData(context); 51 | } 52 | 53 | if (CommonDataKeys.PSI_FILE.getData(context) == null) { 54 | return null; 55 | } 56 | 57 | if (CommonDataKeys.EDITOR.getData(context) == null) { 58 | return null; 59 | } 60 | 61 | PsiElement psiElement = CommonDataKeys.PSI_FILE.getData(context).findElementAt(CommonDataKeys.EDITOR 62 | .getData(context).getCaretModel().getOffset()); 63 | 64 | if (psiElement == null) { 65 | return null; 66 | } 67 | 68 | PsiClass psiClass = PsiTreeUtil.getParentOfType(psiElement, PsiClass.class, true); 69 | 70 | if (psiClass == null) { 71 | return null; 72 | } 73 | 74 | return psiClass; 75 | } 76 | 77 | @Override 78 | public boolean isAcceptableAsNode(Object o) { 79 | return o != null; 80 | } 81 | 82 | @Nullable 83 | @Override 84 | public String getElementTitle(PsiElement psiElement) { 85 | return PsiUtils.getPresentableName(psiElement); 86 | } 87 | 88 | @Nullable 89 | @Override 90 | public SimpleColoredText getItemName(Object o, DiagramState state) { 91 | if (o instanceof PsiElement) { 92 | return new SimpleColoredText(getElementTitle((PsiElement) o), DEFAULT_TEXT_ATTR); 93 | } else { 94 | return null; 95 | } 96 | } 97 | 98 | @Override 99 | public String getNodeTooltip(PsiElement PsiElement) { 100 | return null; 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/ReferenceDiagramExtras.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph; 18 | 19 | import ch.docksnet.rgraph.actions.DeleteMarkedAction; 20 | import ch.docksnet.rgraph.actions.IsolateMarkedAction; 21 | import ch.docksnet.rgraph.actions.MarkAction; 22 | import ch.docksnet.rgraph.actions.MarkCalleesAction; 23 | import ch.docksnet.rgraph.actions.MarkCallersAction; 24 | import ch.docksnet.rgraph.actions.ShowClusterCountAction; 25 | import ch.docksnet.rgraph.actions.ShowOuterReferencesAction; 26 | import ch.docksnet.rgraph.actions.UnmarkAction; 27 | import ch.docksnet.rgraph.actions.UnmarkAllAction; 28 | import ch.docksnet.rgraph.method.ReferenceNode; 29 | import com.intellij.diagram.DiagramBuilder; 30 | import com.intellij.diagram.DiagramNode; 31 | import com.intellij.diagram.extras.DiagramExtras; 32 | import com.intellij.openapi.actionSystem.AnAction; 33 | import com.intellij.openapi.actionSystem.CommonDataKeys; 34 | import com.intellij.psi.PsiElement; 35 | import org.jetbrains.annotations.NotNull; 36 | import org.jetbrains.annotations.Nullable; 37 | 38 | import javax.swing.*; 39 | import javax.swing.border.LineBorder; 40 | import java.awt.*; 41 | import java.util.ArrayList; 42 | import java.util.List; 43 | 44 | /** 45 | * @author Stefan Zeller 46 | */ 47 | @SuppressWarnings("MethodDoesntCallSuperMethod") 48 | public class ReferenceDiagramExtras extends DiagramExtras { 49 | 50 | @Nullable 51 | @Override 52 | public Object getData(String dataId, List> nodes, DiagramBuilder builder) { 53 | if (nodes.size() == 1) { 54 | if (CommonDataKeys.PSI_ELEMENT.is(dataId)) { 55 | PsiElement psiElement = nodes.get(0).getIdentifyingElement(); 56 | assert psiElement != null : "psiElement has no identifying element: " + psiElement; 57 | return psiElement; 58 | } 59 | } 60 | return super.getData(dataId, nodes, builder); 61 | } 62 | 63 | @Override 64 | public List getExtraActions() { 65 | final List result = new ArrayList<>(); 66 | result.add(new ShowOuterReferencesAction()); 67 | result.add(new ShowClusterCountAction()); 68 | result.add(new MarkAction()); 69 | result.add(new UnmarkAction()); 70 | result.add(new UnmarkAllAction()); 71 | result.add(new MarkCalleesAction()); 72 | result.add(new MarkCallersAction()); 73 | result.add(new DeleteMarkedAction()); 74 | result.add(new IsolateMarkedAction()); 75 | return result; 76 | } 77 | 78 | @NotNull 79 | @Override 80 | public JComponent createNodeComponent(DiagramNode node, DiagramBuilder builder, Point basePoint, JPanel wrapper) { 81 | JComponent nodeComponent = super.createNodeComponent(node, builder, basePoint, wrapper); 82 | 83 | if (node instanceof ReferenceNode) { 84 | if (((ReferenceNode) node).isMarked()) { 85 | nodeComponent.setBorder(new LineBorder(Color.BLUE, 2, true)); 86 | } 87 | } 88 | 89 | return nodeComponent; 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/ReferenceDiagramProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph; 18 | 19 | import ch.docksnet.rgraph.directory.PackageReferenceDiagramDataModel; 20 | import ch.docksnet.rgraph.method.MethodReferenceDiagramDataModel; 21 | import com.intellij.diagram.BaseDiagramProvider; 22 | import com.intellij.diagram.DiagramCategory; 23 | import com.intellij.diagram.DiagramColorManager; 24 | import com.intellij.diagram.DiagramDataModel; 25 | import com.intellij.diagram.DiagramElementManager; 26 | import com.intellij.diagram.DiagramNodeContentManager; 27 | import com.intellij.diagram.DiagramPresentationModel; 28 | import com.intellij.diagram.DiagramProvider; 29 | import com.intellij.diagram.DiagramVfsResolver; 30 | import com.intellij.diagram.extras.DiagramExtras; 31 | import com.intellij.openapi.project.Project; 32 | import com.intellij.openapi.vfs.VirtualFile; 33 | import com.intellij.psi.PsiClass; 34 | import com.intellij.psi.PsiElement; 35 | import com.intellij.psi.impl.file.PsiJavaDirectoryImpl; 36 | import org.intellij.lang.annotations.Pattern; 37 | import org.jetbrains.annotations.NotNull; 38 | import org.jetbrains.annotations.Nullable; 39 | 40 | /** 41 | * @author Stefan Zeller 42 | */ 43 | @SuppressWarnings("MethodDoesntCallSuperMethod") 44 | public class ReferenceDiagramProvider extends BaseDiagramProvider { 45 | 46 | private static final String ID = "ReferenceDiagramProvider"; 47 | private final DiagramElementManager myElementManager = new ReferenceDiagramElementManager(); 48 | private final DiagramVfsResolver myVfsResolver = new ReferenceDiagramVfsResolver(); 49 | private final ReferenceDiagramExtras myExtras = new ReferenceDiagramExtras(); 50 | private final DiagramColorManager myColorManager = new ReferenceDiagramColorManager(); 51 | private final ReferenceUmlCategoryManager myUmlCategoryManager = new ReferenceUmlCategoryManager(); 52 | 53 | @Pattern("[a-zA-Z0-9_-]*") 54 | @Override 55 | public String getID() { 56 | return ID; 57 | } 58 | 59 | @Override 60 | public DiagramElementManager getElementManager() { 61 | return this.myElementManager; 62 | } 63 | 64 | @Override 65 | public DiagramVfsResolver getVfsResolver() { 66 | return this.myVfsResolver; 67 | } 68 | 69 | @Override 70 | public String getPresentableName() { 71 | return "Java Reference Diagram"; 72 | } 73 | 74 | @NotNull 75 | @Override 76 | public DiagramExtras getExtras() { 77 | return this.myExtras; 78 | } 79 | 80 | @Override 81 | public DiagramDataModel createDataModel(@NotNull Project project, @Nullable PsiElement 82 | psiElement, @Nullable VirtualFile virtualFile, DiagramPresentationModel model) { 83 | if (psiElement instanceof PsiClass) { 84 | return new MethodReferenceDiagramDataModel(project, (PsiClass) psiElement); 85 | } 86 | if (psiElement instanceof PsiJavaDirectoryImpl) { 87 | return new PackageReferenceDiagramDataModel(project, (PsiJavaDirectoryImpl) psiElement); 88 | } 89 | return null; 90 | } 91 | 92 | @Override 93 | public DiagramColorManager getColorManager() { 94 | return this.myColorManager; 95 | } 96 | 97 | public static ReferenceDiagramProvider getInstance() { 98 | return (ReferenceDiagramProvider) DiagramProvider.findByID(ID); 99 | } 100 | 101 | @Override 102 | public DiagramNodeContentManager getNodeContentManager() { 103 | return this.myUmlCategoryManager; 104 | } 105 | 106 | @Override 107 | public @NotNull DiagramNodeContentManager createNodeContentManager() { 108 | return new ReferenceUmlCategoryManager(); 109 | } 110 | 111 | @Override 112 | public @NotNull DiagramCategory[] getAllContentCategories() { 113 | return ReferenceUmlCategoryManager.CATEGORIES; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/ReferenceDiagramVfsResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph; 18 | 19 | import ch.docksnet.rgraph.fqn.ClassFQN; 20 | import ch.docksnet.rgraph.fqn.FieldFQN; 21 | import ch.docksnet.rgraph.fqn.MethodFQN; 22 | import ch.docksnet.rgraph.fqn.PackageFQN; 23 | import com.intellij.diagram.DiagramVfsResolver; 24 | import com.intellij.openapi.project.Project; 25 | import com.intellij.psi.PsiClass; 26 | import com.intellij.psi.PsiElement; 27 | import com.intellij.psi.PsiField; 28 | import com.intellij.psi.PsiMethod; 29 | import org.jetbrains.annotations.Nullable; 30 | 31 | import java.util.List; 32 | 33 | /** 34 | * @author Stefan Zeller 35 | */ 36 | public class ReferenceDiagramVfsResolver implements DiagramVfsResolver { 37 | 38 | @Override 39 | public String getQualifiedName(PsiElement psiElement) { 40 | return PsiUtils.getFqn(psiElement).getFQN(); 41 | } 42 | 43 | @Nullable 44 | @Override 45 | public PsiElement resolveElementByFQN(String fqn, Project project) { 46 | if (MethodFQN.isMethodFQN(fqn)) { 47 | MethodFQN methodFQN = MethodFQN.create(fqn); 48 | PsiClass psiClass = PsiUtils.getPsiClass(methodFQN.getClassName(), project); 49 | 50 | PsiMethod[] methodsByName = psiClass.findMethodsByName(methodFQN.getMethodName(), true); 51 | for (PsiMethod psiMethod : methodsByName) { 52 | List parameterArray = MethodFQN.getParameterArray(psiMethod); 53 | String parameterRepresentation = MethodFQN.createParameterRepresentation(parameterArray); 54 | if (MethodFQN.createParameterRepresentation(methodFQN.getParameters()).equals 55 | (parameterRepresentation)) { 56 | return psiMethod; 57 | } 58 | } 59 | throw new IllegalArgumentException("Method not found: " + fqn); 60 | } else if (FieldFQN.isFieldFQN(fqn)) { 61 | FieldFQN fieldFQN = FieldFQN.create(fqn); 62 | PsiClass psiClass = PsiUtils.getPsiClass(fieldFQN.getClassName(), project); 63 | 64 | for (PsiField psiField : psiClass.getFields()) { 65 | if (psiField.getName().equals(fieldFQN.getFieldName())) { 66 | return psiField; 67 | } 68 | } 69 | throw new IllegalArgumentException("Field not found: " + fqn); 70 | 71 | } else if (ClassFQN.isClassFQN(fqn)) { 72 | ClassFQN classFQN = ClassFQN.create(fqn); 73 | return PsiUtils.getPsiClass(classFQN.getFQN(), project); 74 | } else if (PackageFQN.isPackage(fqn)) { 75 | PackageFQN packageFQN = PackageFQN.create(fqn); 76 | return PsiUtils.getPsiJavaDirectory(packageFQN.getFQN(), project); 77 | } 78 | throw new IllegalStateException("Cannot processs fqn: " + fqn); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/ReferenceUmlCategoryManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph; 18 | 19 | import com.intellij.diagram.AbstractDiagramNodeContentManager; 20 | import com.intellij.diagram.DiagramCategory; 21 | import com.intellij.diagram.presentation.DiagramState; 22 | import com.intellij.icons.AllIcons; 23 | import com.intellij.psi.PsiClass; 24 | import com.intellij.psi.PsiClassInitializer; 25 | import com.intellij.psi.PsiField; 26 | import com.intellij.psi.PsiMethod; 27 | import com.intellij.ui.LayeredIcon; 28 | import com.intellij.uml.UmlIcons; 29 | 30 | /** 31 | * @author Stefan Zeller 32 | */ 33 | @SuppressWarnings("MethodDoesntCallSuperMethod") 34 | public class ReferenceUmlCategoryManager extends AbstractDiagramNodeContentManager { 35 | private static final DiagramCategory STATIC_FIELDS; 36 | private static final DiagramCategory FIELDS; 37 | private static final DiagramCategory CONSTRUCTORS; 38 | private static final DiagramCategory STATIC_METHODS; 39 | private static final DiagramCategory METHODS; 40 | private static final DiagramCategory STATIC_CLASS_INITIALIZER; 41 | private static final DiagramCategory CLASS_INITIALIZER; 42 | private static final DiagramCategory INNER_CLASS; 43 | private static final DiagramCategory STATIC_INNER_CLASS; 44 | private static final DiagramCategory ENUM; 45 | static final DiagramCategory[] CATEGORIES; 46 | 47 | ReferenceUmlCategoryManager() { 48 | } 49 | 50 | public DiagramCategory[] getContentCategories() { 51 | return CATEGORIES; 52 | } 53 | 54 | public boolean isInCategory(Object element, DiagramCategory category, DiagramState presentation) { 55 | if (ENUM.equals(category)) { 56 | if (element instanceof PsiClass) { 57 | if (((PsiClass) element).getContainingClass() != null) { 58 | if (((PsiClass) element).isEnum()) { 59 | return true; 60 | } 61 | } 62 | return false; 63 | } 64 | } 65 | 66 | if (STATIC_INNER_CLASS.equals(category)) { 67 | if (element instanceof PsiClass) { 68 | if (((PsiClass) element).getContainingClass() != null) { 69 | if (!((PsiClass) element).isEnum() && ((PsiClass) element).hasModifierProperty("static")) { 70 | return true; 71 | } 72 | } 73 | return false; 74 | } 75 | } 76 | 77 | if (INNER_CLASS.equals(category)) { 78 | if (element instanceof PsiClass) { 79 | if (((PsiClass) element).getContainingClass() != null) { 80 | if (!((PsiClass) element).isEnum() && !((PsiClass) element).hasModifierProperty("static")) { 81 | return true; 82 | } 83 | } 84 | return false; 85 | } 86 | } 87 | 88 | if (STATIC_FIELDS.equals(category)) { 89 | if (element instanceof PsiField) { 90 | if (((PsiField) element).hasModifierProperty("static")) { 91 | return true; 92 | } else { 93 | return false; 94 | } 95 | } 96 | } 97 | if (FIELDS.equals(category)) { 98 | if (element instanceof PsiField) { 99 | if (!((PsiField) element).hasModifierProperty("static")) { 100 | return true; 101 | } else { 102 | return false; 103 | } 104 | } 105 | } 106 | if (CONSTRUCTORS.equals(category)) { 107 | if (element instanceof PsiMethod) { 108 | if (((PsiMethod) element).isConstructor()) { 109 | return true; 110 | } else { 111 | return false; 112 | } 113 | } 114 | } 115 | if (METHODS.equals(category)) { 116 | if (element instanceof PsiMethod) { 117 | if (!((PsiMethod) element).isConstructor()) { 118 | if (!((PsiMethod) element).hasModifierProperty("static")) { 119 | return true; 120 | } else { 121 | return false; 122 | } 123 | } else { 124 | return false; 125 | } 126 | } 127 | } 128 | if (STATIC_METHODS.equals(category)) { 129 | if (element instanceof PsiMethod) { 130 | if (!((PsiMethod) element).isConstructor()) { 131 | if (((PsiMethod) element).hasModifierProperty("static")) { 132 | return true; 133 | } else { 134 | return false; 135 | } 136 | } else { 137 | return false; 138 | } 139 | } 140 | } 141 | if (CLASS_INITIALIZER.equals(category)) { 142 | if (element instanceof PsiClassInitializer) { 143 | if (!((PsiClassInitializer) element).hasModifierProperty("static")) { 144 | return true; 145 | } else { 146 | return false; 147 | } 148 | } 149 | } 150 | if (STATIC_CLASS_INITIALIZER.equals(category)) { 151 | if (element instanceof PsiClassInitializer) { 152 | if (((PsiClassInitializer) element).hasModifierProperty("static")) { 153 | return true; 154 | } else { 155 | return false; 156 | } 157 | } 158 | } 159 | 160 | return false; 161 | } 162 | 163 | static { 164 | FIELDS = new DiagramCategory("Fields", AllIcons.Nodes.Field, true, true); 165 | 166 | METHODS = new DiagramCategory("Methods", AllIcons.Nodes.Method, true, true); 167 | 168 | CONSTRUCTORS = new DiagramCategory("Constructors", UmlIcons.Constructor, true, true); 169 | 170 | LayeredIcon staticField = new LayeredIcon(AllIcons.Nodes.Field, AllIcons.Nodes.StaticMark); 171 | STATIC_FIELDS = new DiagramCategory("Static Fields", staticField, true, true); 172 | 173 | LayeredIcon staticMethod = new LayeredIcon(AllIcons.Nodes.Method, AllIcons.Nodes.StaticMark); 174 | STATIC_METHODS = new DiagramCategory("Static Methods", staticMethod, true, true); 175 | 176 | CLASS_INITIALIZER = new DiagramCategory("Class Initializer", AllIcons.Nodes.ClassInitializer, true, true); 177 | 178 | LayeredIcon staticClassInitializer = new LayeredIcon(AllIcons.Nodes.ClassInitializer, AllIcons.Nodes.StaticMark); 179 | STATIC_CLASS_INITIALIZER = new DiagramCategory("Static Class Initializer", staticClassInitializer, true, true); 180 | 181 | INNER_CLASS = new DiagramCategory("Inner Class", UmlIcons.Innerclass, true, true); 182 | 183 | LayeredIcon staticInnerClass = new LayeredIcon(UmlIcons.Innerclass, AllIcons.Nodes.StaticMark); 184 | STATIC_INNER_CLASS = new DiagramCategory("Static Inner Class", staticInnerClass, true, true); 185 | 186 | ENUM = new DiagramCategory("Enum", AllIcons.Nodes.Enum, true, true); 187 | 188 | 189 | CATEGORIES = new DiagramCategory[]{FIELDS, METHODS, CONSTRUCTORS, CLASS_INITIALIZER, STATIC_FIELDS, STATIC_METHODS, 190 | STATIC_CLASS_INITIALIZER, INNER_CLASS, STATIC_INNER_CLASS, ENUM}; 191 | } 192 | 193 | } 194 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/actions/ActionHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.actions; 18 | 19 | import com.intellij.diagram.DiagramAction; 20 | import com.intellij.openapi.actionSystem.AnActionEvent; 21 | 22 | /** 23 | * @author Stefan Zeller 24 | */ 25 | public class ActionHelper { 26 | 27 | public static void enableIfNodesSelected(AnActionEvent e) { 28 | if (!DiagramAction.getSelectedNodes(e).isEmpty()) { 29 | e.getPresentation().setEnabled(true); 30 | } else { 31 | e.getPresentation().setEnabled(false); 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/actions/DeleteMarkedAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.actions; 18 | 19 | import ch.docksnet.rgraph.ReferenceDiagramDataModel; 20 | import com.intellij.diagram.DiagramAction; 21 | import com.intellij.diagram.DiagramDataModel; 22 | import com.intellij.openapi.actionSystem.AnActionEvent; 23 | 24 | /** 25 | * @author Stefan Zeller 26 | */ 27 | public class DeleteMarkedAction extends DiagramAction { 28 | 29 | @Override 30 | public void perform(AnActionEvent e) { 31 | DiagramDataModel dataModel = getDataModel(e); 32 | if (dataModel instanceof ReferenceDiagramDataModel) { 33 | ((ReferenceDiagramDataModel) dataModel).removeMarkedNodes(); 34 | } 35 | getBuilder(e).getPresentationModel().update(); 36 | } 37 | 38 | @Override 39 | public String getActionName() { 40 | return "Delete Marked"; 41 | } 42 | 43 | @Override 44 | public void update(AnActionEvent e) { 45 | e.getPresentation().setVisible(true); 46 | e.getPresentation().setEnabled(true); 47 | e.getPresentation().setText(getActionName()); 48 | super.update(e); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/actions/IsolateMarkedAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.actions; 18 | 19 | import ch.docksnet.rgraph.ReferenceDiagramDataModel; 20 | import com.intellij.diagram.DiagramAction; 21 | import com.intellij.diagram.DiagramDataModel; 22 | import com.intellij.openapi.actionSystem.AnActionEvent; 23 | 24 | /** 25 | * @author Stefan Zeller 26 | */ 27 | public class IsolateMarkedAction extends DiagramAction { 28 | 29 | @Override 30 | public void perform(AnActionEvent e) { 31 | DiagramDataModel dataModel = getDataModel(e); 32 | if (dataModel instanceof ReferenceDiagramDataModel) { 33 | ((ReferenceDiagramDataModel) dataModel).isolateMarkedNodes(); 34 | } 35 | getBuilder(e).getPresentationModel().update(); 36 | } 37 | 38 | @Override 39 | public String getActionName() { 40 | return "Isolate Marked"; 41 | } 42 | 43 | @Override 44 | public void update(AnActionEvent e) { 45 | e.getPresentation().setVisible(true); 46 | e.getPresentation().setEnabled(true); 47 | e.getPresentation().setText(getActionName()); 48 | super.update(e); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/actions/MarkAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.actions; 18 | 19 | import ch.docksnet.rgraph.method.ReferenceNode; 20 | import com.intellij.diagram.DiagramAction; 21 | import com.intellij.diagram.DiagramNode; 22 | import com.intellij.openapi.actionSystem.AnActionEvent; 23 | 24 | /** 25 | * @author Stefan Zeller 26 | */ 27 | public class MarkAction extends DiagramAction { 28 | 29 | @Override 30 | public void perform(AnActionEvent e) { 31 | for (DiagramNode diagramNode : getSelectedNodes(e)) { 32 | if (diagramNode instanceof ReferenceNode) { 33 | ((ReferenceNode) diagramNode).setMarked(); 34 | } 35 | } 36 | getBuilder(e).getPresentationModel().update(); 37 | } 38 | 39 | @Override 40 | public String getActionName() { 41 | return "Mark Selected"; 42 | } 43 | 44 | @Override 45 | public void update(AnActionEvent e) { 46 | e.getPresentation().setVisible(true); 47 | ActionHelper.enableIfNodesSelected(e); 48 | e.getPresentation().setText(getActionName()); 49 | super.update(e); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/actions/MarkCalleesAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.actions; 18 | 19 | import ch.docksnet.rgraph.ReferenceDiagramDataModel; 20 | import com.intellij.diagram.DiagramAction; 21 | import com.intellij.diagram.DiagramDataModel; 22 | import com.intellij.diagram.DiagramNode; 23 | import com.intellij.openapi.actionSystem.AnActionEvent; 24 | 25 | import java.util.List; 26 | 27 | /** 28 | * @author Stefan Zeller 29 | */ 30 | public class MarkCalleesAction extends DiagramAction { 31 | 32 | @Override 33 | public void perform(AnActionEvent e) { 34 | //noinspection rawtypes 35 | List selectedNodes = getSelectedNodes(e); 36 | 37 | DiagramDataModel dataModel = getDataModel(e); 38 | if (dataModel instanceof ReferenceDiagramDataModel) { 39 | ((ReferenceDiagramDataModel) dataModel).markCallees(selectedNodes); 40 | } 41 | getBuilder(e).getPresentationModel().update(); 42 | } 43 | 44 | @Override 45 | public String getActionName() { 46 | return "Mark Callees"; 47 | } 48 | 49 | @Override 50 | public void update(AnActionEvent e) { 51 | e.getPresentation().setVisible(true); 52 | ActionHelper.enableIfNodesSelected(e); 53 | e.getPresentation().setText(getActionName()); 54 | super.update(e); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/actions/MarkCallersAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.actions; 18 | 19 | import ch.docksnet.rgraph.ReferenceDiagramDataModel; 20 | import com.intellij.diagram.DiagramAction; 21 | import com.intellij.diagram.DiagramDataModel; 22 | import com.intellij.diagram.DiagramNode; 23 | import com.intellij.openapi.actionSystem.AnActionEvent; 24 | 25 | import java.util.List; 26 | 27 | /** 28 | * @author Stefan Zeller 29 | */ 30 | @SuppressWarnings("rawtypes") 31 | public class MarkCallersAction extends DiagramAction { 32 | 33 | @Override 34 | public void perform(AnActionEvent e) { 35 | List selectedNodes = getSelectedNodes(e); 36 | 37 | DiagramDataModel dataModel = getDataModel(e); 38 | if (dataModel instanceof ReferenceDiagramDataModel) { 39 | ((ReferenceDiagramDataModel) dataModel).markCallers(selectedNodes); 40 | } 41 | getBuilder(e).getPresentationModel().update(); 42 | } 43 | 44 | @Override 45 | public String getActionName() { 46 | return "Mark Callers"; 47 | } 48 | 49 | @Override 50 | public void update(AnActionEvent e) { 51 | e.getPresentation().setVisible(true); 52 | ActionHelper.enableIfNodesSelected(e); 53 | e.getPresentation().setText(getActionName()); 54 | super.update(e); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/actions/ShowClusterCountAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.actions; 18 | 19 | import ch.docksnet.rgraph.ReferenceDiagramDataModel; 20 | import com.intellij.diagram.DiagramAction; 21 | import com.intellij.diagram.DiagramBuilder; 22 | import com.intellij.openapi.actionSystem.AnActionEvent; 23 | import org.jetbrains.annotations.NotNull; 24 | 25 | /** 26 | * @author Stefan Zeller 27 | */ 28 | public class ShowClusterCountAction extends DiagramAction { 29 | 30 | @Override 31 | public void perform(AnActionEvent e) { 32 | } 33 | 34 | @Override 35 | public boolean displayTextInToolbar() { 36 | return true; 37 | } 38 | 39 | @Override 40 | public String getActionName() { 41 | return "Show Cluster Count"; 42 | } 43 | 44 | @Override 45 | public void update(AnActionEvent e) { 46 | if (getDataModel(e) instanceof ReferenceDiagramDataModel) { 47 | e.getPresentation().setVisible(true); 48 | e.getPresentation().setEnabled(false); 49 | long currentClusterCount = ((ReferenceDiagramDataModel) getDataModel(e)).getCurrentClusterCount(); 50 | e.getPresentation().setText("Cluster Count: " + currentClusterCount); 51 | } else { 52 | e.getPresentation().setVisible(false); 53 | } 54 | super.update(e); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/actions/ShowOuterReferencesAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.actions; 18 | 19 | import ch.docksnet.rgraph.ReferenceDiagramDataModel; 20 | import ch.docksnet.rgraph.toolwindow.TestToolWindow; 21 | import ch.docksnet.rgraph.method.OuterReferences; 22 | import com.intellij.diagram.DiagramAction; 23 | import com.intellij.openapi.actionSystem.AnActionEvent; 24 | import com.intellij.openapi.application.ApplicationManager; 25 | import com.intellij.openapi.project.Project; 26 | 27 | /** 28 | * @author Stefan Zeller 29 | */ 30 | public class ShowOuterReferencesAction extends DiagramAction { 31 | 32 | @Override 33 | public void perform(AnActionEvent e) { 34 | OuterReferences outerReferences = ((ReferenceDiagramDataModel) getDataModel(e)).getOuterReferences(); 35 | Project project = e.getProject(); 36 | ApplicationManager.getApplication().invokeLater( 37 | () -> TestToolWindow.show(project, outerReferences) 38 | ); 39 | } 40 | 41 | @Override 42 | public boolean displayTextInToolbar() { 43 | return true; 44 | } 45 | 46 | @Override 47 | public String getActionName() { 48 | return "Other References"; 49 | } 50 | 51 | @Override 52 | public void update(AnActionEvent e) { 53 | if (getDataModel(e) instanceof ReferenceDiagramDataModel) { 54 | e.getPresentation().setVisible(true); 55 | e.getPresentation().setEnabled(false); 56 | OuterReferences outerReferences = ((ReferenceDiagramDataModel) getDataModel(e)).getOuterReferences(); 57 | e.getPresentation().setText("Other References: " + outerReferences.toToolbarString()); 58 | } else { 59 | e.getPresentation().setVisible(false); 60 | } 61 | super.update(e); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/actions/UnmarkAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.actions; 18 | 19 | import ch.docksnet.rgraph.method.ReferenceNode; 20 | import com.intellij.diagram.DiagramAction; 21 | import com.intellij.diagram.DiagramNode; 22 | import com.intellij.openapi.actionSystem.AnActionEvent; 23 | 24 | /** 25 | * @author Stefan Zeller 26 | */ 27 | public class UnmarkAction extends DiagramAction { 28 | 29 | @Override 30 | public void perform(AnActionEvent e) { 31 | for (DiagramNode diagramNode : getSelectedNodes(e)) { 32 | if (diagramNode instanceof ReferenceNode) { 33 | ((ReferenceNode) diagramNode).unsetMarked(); 34 | } 35 | } 36 | getBuilder(e).getPresentationModel().update(); 37 | } 38 | 39 | @Override 40 | public String getActionName() { 41 | return "Unmark Selected"; 42 | } 43 | 44 | @Override 45 | public void update(AnActionEvent e) { 46 | e.getPresentation().setVisible(true); 47 | ActionHelper.enableIfNodesSelected(e); 48 | e.getPresentation().setText(getActionName()); 49 | super.update(e); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/actions/UnmarkAllAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.actions; 18 | 19 | import ch.docksnet.rgraph.ReferenceDiagramDataModel; 20 | import com.intellij.diagram.DiagramAction; 21 | import com.intellij.diagram.DiagramDataModel; 22 | import com.intellij.openapi.actionSystem.AnActionEvent; 23 | 24 | /** 25 | * @author Stefan Zeller 26 | */ 27 | public class UnmarkAllAction extends DiagramAction { 28 | 29 | @Override 30 | public void perform(AnActionEvent e) { 31 | DiagramDataModel dataModel = getDataModel(e); 32 | if (dataModel instanceof ReferenceDiagramDataModel) { 33 | ((ReferenceDiagramDataModel) dataModel).unmarkAllNodes(); 34 | } 35 | getBuilder(e).getPresentationModel().update(); 36 | } 37 | 38 | @Override 39 | public String getActionName() { 40 | return "Unmark All"; 41 | } 42 | 43 | @Override 44 | public void update(AnActionEvent e) { 45 | e.getPresentation().setVisible(true); 46 | e.getPresentation().setEnabled(true); 47 | e.getPresentation().setText(getActionName()); 48 | super.update(e); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/directory/PackageReferenceDiagramDataModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.directory; 18 | 19 | import ch.docksnet.rgraph.PsiUtils; 20 | import ch.docksnet.rgraph.ReferenceDiagramDataModel; 21 | import ch.docksnet.rgraph.ReferenceDiagramProvider; 22 | import ch.docksnet.rgraph.fqn.FQN; 23 | import ch.docksnet.rgraph.method.ReferenceEdge; 24 | import ch.docksnet.rgraph.method.SourceTargetPair; 25 | import ch.docksnet.utils.IncrementableSet; 26 | import com.intellij.diagram.DiagramEdge; 27 | import com.intellij.diagram.DiagramNode; 28 | import com.intellij.diagram.DiagramRelationshipInfo; 29 | import com.intellij.diagram.DiagramRelationshipInfoAdapter; 30 | import com.intellij.openapi.project.Project; 31 | import com.intellij.psi.PsiClass; 32 | import com.intellij.psi.PsiElement; 33 | import com.intellij.psi.PsiJavaFile; 34 | import com.intellij.psi.PsiReference; 35 | import com.intellij.psi.impl.file.PsiJavaDirectoryImpl; 36 | import com.intellij.psi.search.GlobalSearchScopes; 37 | import com.intellij.psi.search.searches.ReferencesSearch; 38 | import org.jetbrains.annotations.NotNull; 39 | import org.jetbrains.annotations.Nullable; 40 | 41 | import java.awt.*; 42 | import java.util.ArrayList; 43 | import java.util.Collection; 44 | 45 | public class PackageReferenceDiagramDataModel extends ReferenceDiagramDataModel { 46 | 47 | private final PsiElement baseElement; 48 | private final References references = new References(); 49 | 50 | public PackageReferenceDiagramDataModel(Project project, PsiJavaDirectoryImpl directory) { 51 | super(project, ReferenceDiagramProvider.getInstance()); 52 | this.baseElement = directory; 53 | init(directory); 54 | } 55 | 56 | private void init(PsiJavaDirectoryImpl directory) { 57 | this.references.createNodes(directory).forEach(it -> addUserElement(it)); 58 | } 59 | 60 | @Override 61 | public void rebuild(PsiElement element) { 62 | super.rebuild(element); 63 | clearAll(); 64 | init((PsiJavaDirectoryImpl) element); 65 | refreshDataModel(); 66 | } 67 | 68 | @Override 69 | protected PsiElement getBaseElement() { 70 | return this.baseElement; 71 | } 72 | 73 | @Override 74 | protected FQN getBaseForOuterReferences(PsiElement psiElement) { 75 | return PsiUtils.getFqn(psiElement); 76 | } 77 | 78 | @NotNull 79 | @Override 80 | protected Collection resolveOuterReferences(PsiElement psiElement) { 81 | Collection result = new ArrayList<>(); 82 | if (!(psiElement instanceof PsiJavaFile)) { 83 | return result; 84 | } 85 | PsiClass[] classes = ((PsiJavaFile) psiElement).getClasses(); 86 | for (PsiClass psiClass : classes) { 87 | result.addAll(ReferencesSearch.search(psiClass, GlobalSearchScopes.projectProductionScope(getProject())).findAll()); 88 | } 89 | return result; 90 | } 91 | 92 | @Nullable 93 | @Override 94 | protected DiagramEdge toEdge(@NotNull DiagramNode from, 95 | @NotNull DiagramNode to, 96 | Long value) { 97 | final DiagramRelationshipInfo relationship = createEdgeFromNonField(value == null ? 0 : value); 98 | return new ReferenceEdge(from, to, relationship); 99 | } 100 | 101 | @NotNull 102 | private DiagramRelationshipInfo createEdgeFromNonField(final long count) { 103 | DiagramRelationshipInfo r; 104 | //noinspection MethodDoesntCallSuperMethod 105 | r = new DiagramRelationshipInfoAdapter(ReferenceEdge.Type.REFERENCE.name()) { 106 | @Override 107 | public Shape getStartArrow() { 108 | return STANDARD; 109 | } 110 | 111 | @Override 112 | public String getToLabel() { 113 | if (count == 1) { 114 | return ""; 115 | } else { 116 | return Long.toString(count); 117 | } 118 | } 119 | 120 | @Override 121 | public boolean equals(Object obj) { 122 | if (!(obj instanceof DiagramRelationshipInfoAdapter)) { 123 | return false; 124 | } 125 | return toString().equals(obj.toString()) && getToLabel().equals(((DiagramRelationshipInfoAdapter) obj).getToLabel()); 126 | } 127 | }; 128 | return r; 129 | } 130 | 131 | @Override 132 | protected boolean isAllowedToShow(PsiElement element) { 133 | return true; 134 | } 135 | 136 | @NotNull 137 | @Override 138 | protected IncrementableSet resolveRelationships() { 139 | return this.references.createRelationships(getNodes(), getProject()); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/directory/References.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.directory; 18 | 19 | import ch.docksnet.rgraph.PsiUtils; 20 | import ch.docksnet.rgraph.fqn.FQN; 21 | import ch.docksnet.rgraph.fqn.Hierarchically; 22 | import ch.docksnet.rgraph.method.SourceTargetPair; 23 | import ch.docksnet.utils.IncrementableSet; 24 | import com.intellij.diagram.DiagramNode; 25 | import com.intellij.openapi.project.Project; 26 | import com.intellij.psi.*; 27 | import com.intellij.psi.impl.file.PsiJavaDirectoryImpl; 28 | import com.intellij.psi.impl.source.tree.CompositePsiElement; 29 | import com.intellij.psi.search.GlobalSearchScope; 30 | import com.intellij.psi.search.searches.ReferencesSearch; 31 | 32 | import java.util.ArrayList; 33 | import java.util.Collection; 34 | import java.util.List; 35 | 36 | public class References { 37 | public List createNodes(PsiJavaDirectoryImpl directory) { 38 | List result = new ArrayList<>(); 39 | for (PsiElement child : directory.getChildren()) { 40 | if (child instanceof PsiJavaFile) { 41 | result.add(child); 42 | } else if (child instanceof PsiJavaDirectoryImpl) { 43 | result.add(child); 44 | } 45 | } 46 | return result; 47 | } 48 | 49 | 50 | public IncrementableSet createRelationships(Collection> nodes, Project project) { 51 | IncrementableSet incrementableSet = new IncrementableSet<>(); 52 | 53 | for (DiagramNode node : nodes) { 54 | PsiElement callee = node.getIdentifyingElement(); 55 | 56 | 57 | if (callee instanceof PsiJavaFile) { 58 | PsiClass[] classes = ((PsiJavaFile) callee).getClasses(); 59 | for (PsiClass psiClass : classes) { 60 | Collection references = ReferencesSearch.search(psiClass, GlobalSearchScope.projectScope(project)).findAll(); 61 | 62 | for (PsiReference psiReference : references) { 63 | if (!(psiReference instanceof CompositePsiElement)) { 64 | continue; 65 | } 66 | if (((CompositePsiElement) psiReference).getParent() instanceof PsiImportStatement) { 67 | // don't count import statements 68 | continue; 69 | } 70 | PsiElement caller = ((CompositePsiElement) psiReference).getContainingFile(); 71 | 72 | if (caller == null) { 73 | continue; 74 | } 75 | 76 | FQN callerFqn = PsiUtils.getFqn(caller); 77 | 78 | if (callerFqn instanceof Hierarchically) { 79 | Hierarchically calleeH = (Hierarchically) PsiUtils.getFqn(callee); 80 | Hierarchically callerH = (Hierarchically) callerFqn; 81 | if (calleeH.samePackage(callerH)) { 82 | incrementableSet.increment(new SourceTargetPair(caller, callee)); 83 | } else if (callerH.sameHierarchy(calleeH)) { 84 | String accumulationPackage = calleeH.getNextHierarchyTowards(callerH); 85 | PsiElement accumulator = PsiUtils.getPsiJavaDirectory(accumulationPackage, project); 86 | incrementableSet.increment(new SourceTargetPair(accumulator, callee)); 87 | } 88 | } 89 | } 90 | 91 | } 92 | } 93 | } 94 | return incrementableSet; 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/fqn/ClassFQN.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.fqn; 18 | 19 | import com.intellij.psi.PsiClass; 20 | 21 | /** 22 | * @author Stefan Zeller 23 | */ 24 | public class ClassFQN extends FQN { 25 | private final String fqn; 26 | 27 | private ClassFQN(String fqn) { 28 | this.fqn = fqn; 29 | } 30 | 31 | public static ClassFQN create(String string) { 32 | return new ClassFQN(string); 33 | } 34 | 35 | public static ClassFQN create(PsiClass psiClass) { 36 | String className = psiClass.getQualifiedName(); 37 | return new ClassFQN(className); 38 | } 39 | 40 | public static boolean isClassFQN(String string) { 41 | String[] split = string.split("\\."); 42 | if (Character.isUpperCase(split[split.length - 1].charAt(0))) { 43 | return true; 44 | } 45 | return false; 46 | } 47 | 48 | public String getFQN() { 49 | return this.fqn; 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/fqn/FQN.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.fqn; 18 | 19 | import java.util.Objects; 20 | 21 | public abstract class FQN { 22 | abstract public String getFQN(); 23 | 24 | @Override 25 | public boolean equals(Object other) { 26 | if (!(other instanceof FQN)) { 27 | return false; 28 | } 29 | FQN otherFqn = (FQN) other; 30 | return Objects.equals(this.getFQN(), otherFqn.getFQN()); 31 | } 32 | 33 | @Override 34 | public int hashCode() { 35 | return Objects.hashCode(getFQN()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/fqn/FieldFQN.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.fqn; 18 | 19 | import com.intellij.psi.PsiField; 20 | 21 | import java.util.regex.Matcher; 22 | import java.util.regex.Pattern; 23 | 24 | /** 25 | * @author Stefan Zeller 26 | */ 27 | public class FieldFQN extends FQN { 28 | private final String className; 29 | private final String fieldName; 30 | private final String fqn; 31 | 32 | private FieldFQN(String className, String fieldName) { 33 | this.className = className; 34 | this.fieldName = fieldName; 35 | this.fqn = createFqn(); 36 | } 37 | 38 | private String createFqn() { 39 | return this.className + "#" + this.fieldName; 40 | } 41 | 42 | public static FieldFQN create(String string) { 43 | Pattern pattern = Pattern.compile("(.*)#(.*)"); 44 | Matcher matcher = pattern.matcher(string); 45 | 46 | matcher.find(); 47 | 48 | String className = matcher.group(1); 49 | String fieldName = matcher.group(2); 50 | 51 | return new FieldFQN(className, fieldName); 52 | } 53 | 54 | public String getClassName() { 55 | return this.className; 56 | } 57 | 58 | public String getFieldName() { 59 | return this.fieldName; 60 | } 61 | 62 | @Override 63 | public String getFQN() { 64 | return this.fqn; 65 | } 66 | 67 | public static FieldFQN create(PsiField psiField) { 68 | String qualifiedClassName = psiField.getContainingClass().getQualifiedName(); 69 | return new FieldFQN(qualifiedClassName, psiField.getName()); 70 | } 71 | 72 | public static boolean isFieldFQN(String fqn) { 73 | if (fqn.contains("#") && !fqn.contains("(")) { 74 | return true; 75 | } 76 | return false; 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/fqn/FileFQN.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.fqn; 18 | 19 | import com.intellij.psi.PsiElement; 20 | import com.intellij.psi.PsiJavaFile; 21 | import com.intellij.psi.util.PsiTreeUtil; 22 | 23 | import java.util.Objects; 24 | 25 | public class FileFQN extends FQN implements Hierarchically { 26 | private final String packageName; 27 | private final String fileName; 28 | private final PsiJavaFile psiJavaFile; 29 | 30 | private FileFQN(String packageName, String fileName, PsiJavaFile psiJavaFile) { 31 | this.psiJavaFile = psiJavaFile; 32 | if (packageName == null) { 33 | throw new IllegalArgumentException("packageName: null not allowed"); 34 | } 35 | if (fileName == null) { 36 | throw new IllegalArgumentException("fileName: null not allowed"); 37 | } 38 | this.packageName = packageName; 39 | this.fileName = fileName; 40 | } 41 | 42 | public static FileFQN from(PsiJavaFile psiJavaFile) { 43 | return new FileFQN(psiJavaFile.getPackageName(), psiJavaFile.getName(), psiJavaFile); 44 | } 45 | 46 | public static FileFQN resolveHierarchically(PsiElement psiElement) { 47 | PsiJavaFile psiJavaFile = PsiTreeUtil.getParentOfType(psiElement, PsiJavaFile.class, true); 48 | if (psiJavaFile == null) { 49 | return null; 50 | } 51 | return from((PsiJavaFile) psiJavaFile); 52 | } 53 | 54 | public static FileFQN create(PsiJavaFile psiJavaFile) { 55 | return from(psiJavaFile); 56 | } 57 | 58 | @Override 59 | public String getHierarchie() { 60 | return this.packageName; 61 | } 62 | 63 | @Override 64 | public boolean equals(Object o) { 65 | if (this == o) return true; 66 | if (o == null || getClass() != o.getClass()) return false; 67 | FileFQN fileFQN = (FileFQN) o; 68 | return this.packageName.equals(fileFQN.packageName) && 69 | this.fileName.equals(fileFQN.fileName); 70 | } 71 | 72 | @Override 73 | public int hashCode() { 74 | return Objects.hash(this.packageName, this.fileName); 75 | } 76 | 77 | @Override 78 | public String toString() { 79 | return this.packageName + "." + this.fileName; 80 | } 81 | 82 | public PsiJavaFile getPsiJavaFile() { 83 | return this.psiJavaFile; 84 | } 85 | 86 | public String getFQN() { 87 | return this.toString(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/fqn/FileFQNReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.fqn; 18 | 19 | import com.intellij.psi.PsiElement; 20 | 21 | public class FileFQNReference { 22 | private final FileFQN fileFQN; 23 | private final int referencesCount; 24 | 25 | public FileFQNReference(FileFQN fileFQN, int referencesCount) { 26 | this.fileFQN = fileFQN; 27 | this.referencesCount = referencesCount; 28 | } 29 | 30 | public PsiElement getPsiElement() { 31 | return this.fileFQN.getPsiJavaFile(); 32 | } 33 | 34 | public String toUsageString() { 35 | StringBuilder sb = new StringBuilder(); 36 | sb.append(this.referencesCount); 37 | sb.append(" "); 38 | if (this.referencesCount == 1) { 39 | sb.append("usage"); 40 | } else { 41 | sb.append("usages"); 42 | } 43 | return sb.toString(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/fqn/Hierarchically.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.fqn; 18 | 19 | import java.util.ArrayList; 20 | import java.util.Arrays; 21 | import java.util.List; 22 | 23 | public interface Hierarchically { 24 | default boolean samePackage(Hierarchically otherFile) { 25 | return getHierarchie().equals(otherFile.getHierarchie()); 26 | } 27 | 28 | default boolean sameHierarchy(Hierarchically otherFile) { 29 | List packagePathsOtherFile = Hierarchically.getPackagePaths(this); 30 | return packagePathsOtherFile.stream().anyMatch(it -> otherFile.getHierarchie().equals(it)); 31 | } 32 | 33 | static List getPackages(Hierarchically base) { 34 | String[] packages = base.getHierarchie().split("\\."); 35 | return new ArrayList<>(Arrays.asList(packages)); 36 | } 37 | 38 | static List getPackagePaths(Hierarchically base) { 39 | List result = new ArrayList<>(); 40 | List packages = getPackages(base); 41 | for (int i = 0; i < packages.size() + 1; i++) { 42 | List subList = packages.subList(0, i); 43 | if (subList.isEmpty()) { 44 | continue; 45 | } 46 | result.add(String.join(".", subList)); 47 | } 48 | return result; 49 | } 50 | 51 | String getHierarchie(); 52 | 53 | default String getNextHierarchyTowards(Hierarchically target) { 54 | if (!target.sameHierarchy(this)) { 55 | throw new IllegalArgumentException("target is not in same hierarchy"); 56 | } 57 | String targetHierarchie = target.getHierarchie(); 58 | String substring = targetHierarchie.substring(getHierarchie().length() + 1); 59 | int indexOfDot = substring.indexOf("."); 60 | if (indexOfDot >= 0) { 61 | return targetHierarchie.substring(0, getHierarchie().length() + indexOfDot +1); 62 | } else { 63 | return targetHierarchie; 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/fqn/MethodFQN.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.fqn; 18 | 19 | import com.intellij.openapi.util.text.StringUtil; 20 | import com.intellij.psi.PsiMethod; 21 | import com.intellij.psi.PsiParameter; 22 | import org.jetbrains.annotations.NotNull; 23 | 24 | import java.util.ArrayList; 25 | import java.util.Arrays; 26 | import java.util.Collections; 27 | import java.util.List; 28 | import java.util.regex.Matcher; 29 | import java.util.regex.Pattern; 30 | 31 | /** 32 | * @author Stefan Zeller 33 | */ 34 | public class MethodFQN extends FQN { 35 | public static final Pattern METHOD_PATTERN = Pattern.compile("(.*)#(.*)\\((.*)\\)"); 36 | private final String className; 37 | private final String methodName; 38 | private final List parameters; 39 | private final String fqn; 40 | 41 | private MethodFQN(String className, String methodName, List parameters) { 42 | this.className = className; 43 | this.methodName = methodName; 44 | this.parameters = parameters; 45 | this.fqn = createFqn(); 46 | } 47 | 48 | @NotNull 49 | private String createFqn() { 50 | final String parameterString = createParameterRepresentation(this.parameters); 51 | return this.className + "#" + this.methodName + "(" + parameterString + ")"; 52 | } 53 | 54 | @NotNull 55 | public static String createParameterRepresentation(List parameters) { 56 | return StringUtil.join(parameters, ","); 57 | } 58 | 59 | @NotNull 60 | public static MethodFQN create(String string) { 61 | Matcher matcher = METHOD_PATTERN.matcher(string); 62 | 63 | if (!matcher.find()) { 64 | throw new IllegalArgumentException("String does not match the pattern: " + string); 65 | } 66 | 67 | String className = matcher.group(1); 68 | String methodName = matcher.group(2); 69 | String parameterString = matcher.group(3); 70 | 71 | String[] parameters = parameterString.split(","); 72 | 73 | return new MethodFQN(className, methodName, Arrays.asList(parameters)); 74 | } 75 | 76 | public static MethodFQN create(PsiMethod psiMethod) { 77 | String classFqn = ClassFQN.create(psiMethod.getContainingClass()).getFQN(); 78 | String methodName = psiMethod.getName(); 79 | Builder builder = new Builder(classFqn, methodName); 80 | 81 | List parameters = getParameterArray(psiMethod); 82 | 83 | for (String parameter : parameters) { 84 | builder.addParameter(parameter); 85 | } 86 | 87 | return builder.create(); 88 | } 89 | 90 | @NotNull 91 | public static List getParameterArray(PsiMethod psiMethod) { 92 | List parameters = new ArrayList<>(); 93 | for (PsiParameter psiParameter : psiMethod.getParameterList().getParameters()) { 94 | String parameter = psiParameter.getType().getPresentableText(); 95 | parameters.add(parameter); 96 | } 97 | return parameters; 98 | } 99 | 100 | public static boolean isMethodFQN(String string) { 101 | Matcher matcher = METHOD_PATTERN.matcher(string); 102 | if (matcher.find()) { 103 | return true; 104 | } else { 105 | return false; 106 | } 107 | } 108 | 109 | public String getClassName() { 110 | return this.className; 111 | } 112 | 113 | public String getMethodName() { 114 | return this.methodName; 115 | } 116 | 117 | public List getParameters() { 118 | return this.parameters; 119 | } 120 | 121 | public String getFQN() { 122 | return this.fqn; 123 | } 124 | 125 | public static class Builder { 126 | private final String className; 127 | private final String methodName; 128 | private List parameters = new ArrayList<>(); 129 | 130 | public Builder(String className, String methodName) { 131 | this.className = className; 132 | this.methodName = methodName; 133 | } 134 | 135 | public Builder addParameter(String parameter) { 136 | this.parameters.add(parameter); 137 | return this; 138 | } 139 | 140 | public MethodFQN create() { 141 | return new MethodFQN(this.className, this.methodName, Collections.unmodifiableList(this.parameters)); 142 | } 143 | 144 | } 145 | 146 | } 147 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/fqn/PackageFQN.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.fqn; 18 | 19 | import com.intellij.psi.impl.file.PsiJavaDirectoryImpl; 20 | 21 | /** 22 | * @author Stefan Zeller 23 | */ 24 | public class PackageFQN extends FQN implements Hierarchically { 25 | private final String fqn; 26 | 27 | private PackageFQN(String fqn) { 28 | this.fqn = fqn; 29 | } 30 | 31 | public static PackageFQN create(String string) { 32 | return new PackageFQN(string); 33 | } 34 | 35 | public static PackageFQN create(PsiJavaDirectoryImpl psiJavaDirectory) { 36 | String className = psiJavaDirectory.getPresentation().getLocationString(); 37 | return new PackageFQN(className); 38 | } 39 | 40 | public static boolean isPackage(String string) { 41 | String[] split = string.split("\\."); 42 | if (Character.isLowerCase(split[split.length - 1].charAt(0))) { 43 | return true; 44 | } 45 | return false; 46 | } 47 | 48 | public String getFQN() { 49 | return this.fqn; 50 | } 51 | 52 | @Override 53 | public String getHierarchie() { 54 | return this.fqn; 55 | } 56 | } -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/method/MethodReferenceDiagramDataModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.method; 18 | 19 | import ch.docksnet.rgraph.PsiUtils; 20 | import ch.docksnet.rgraph.ReferenceDiagramDataModel; 21 | import ch.docksnet.rgraph.ReferenceDiagramProvider; 22 | import ch.docksnet.rgraph.fqn.FQN; 23 | import ch.docksnet.rgraph.fqn.FileFQN; 24 | import ch.docksnet.utils.IncrementableSet; 25 | import com.intellij.diagram.DiagramCategory; 26 | import com.intellij.diagram.DiagramEdge; 27 | import com.intellij.diagram.DiagramNode; 28 | import com.intellij.diagram.DiagramNodeContentManager; 29 | import com.intellij.diagram.DiagramRelationshipInfo; 30 | import com.intellij.diagram.DiagramRelationshipInfoAdapter; 31 | import com.intellij.diagram.presentation.DiagramLineType; 32 | import com.intellij.openapi.project.Project; 33 | import com.intellij.psi.PsiClass; 34 | import com.intellij.psi.PsiClassInitializer; 35 | import com.intellij.psi.PsiElement; 36 | import com.intellij.psi.PsiField; 37 | import com.intellij.psi.PsiJavaFile; 38 | import com.intellij.psi.PsiMethod; 39 | import com.intellij.psi.PsiReference; 40 | import com.intellij.psi.SmartPsiElementPointer; 41 | import com.intellij.psi.impl.source.tree.CompositePsiElement; 42 | import com.intellij.psi.search.GlobalSearchScopes; 43 | import com.intellij.psi.search.LocalSearchScope; 44 | import com.intellij.psi.search.searches.ReferencesSearch; 45 | import org.jetbrains.annotations.NotNull; 46 | import org.jetbrains.annotations.Nullable; 47 | 48 | import java.awt.*; 49 | import java.util.Collection; 50 | 51 | /** 52 | * @author Stefan Zeller 53 | */ 54 | public class MethodReferenceDiagramDataModel extends ReferenceDiagramDataModel { 55 | 56 | private SmartPsiElementPointer baseElement; 57 | 58 | public MethodReferenceDiagramDataModel(Project project, PsiClass psiClass) { 59 | super(project, ReferenceDiagramProvider.getInstance()); 60 | init(psiClass); 61 | } 62 | 63 | private void init(PsiClass psiClass) { 64 | this.baseElement = psiClass == null ? null : createSmartPsiElementPointer(psiClass); 65 | collectNodes(psiClass); 66 | } 67 | 68 | private void collectNodes(PsiClass psiClass) { 69 | for (PsiMethod psiMethod : psiClass.getMethods()) { 70 | addUserElement(psiMethod); 71 | } 72 | 73 | for (PsiField psiField : psiClass.getFields()) { 74 | addUserElement(psiField); 75 | } 76 | 77 | for (PsiClassInitializer psiClassInitializer : psiClass.getInitializers()) { 78 | addUserElement(psiClassInitializer); 79 | } 80 | 81 | for (PsiClass innerClass : psiClass.getInnerClasses()) { 82 | addUserElement(innerClass); 83 | } 84 | } 85 | 86 | @Override 87 | public void rebuild(PsiElement element) { 88 | super.rebuild(element); 89 | clearAll(); 90 | init((PsiClass) element); 91 | refreshDataModel(); 92 | } 93 | 94 | @Nullable 95 | @Override 96 | protected DiagramEdge toEdge(@NotNull DiagramNode from, 97 | @NotNull DiagramNode to, 98 | Long value) { 99 | final DiagramRelationshipInfo relationship; 100 | if (from.getIdentifyingElement() instanceof PsiField) { 101 | relationship = createEdgeFromField(); 102 | } else { 103 | relationship = createEdgeFromNonField(value == null ? 0 : value); 104 | } 105 | 106 | return new ReferenceEdge(from, to, relationship); 107 | } 108 | 109 | @NotNull 110 | @Override 111 | protected IncrementableSet resolveRelationships() { 112 | PsiElement psiElement = getBaseElement(); 113 | IncrementableSet incrementableSet = new IncrementableSet<>(); 114 | 115 | for (DiagramNode node : getNodes()) { 116 | PsiElement callee = node.getIdentifyingElement(); 117 | 118 | Collection all = ReferencesSearch.search(callee, new LocalSearchScope 119 | (psiElement)).findAll(); 120 | 121 | for (PsiReference psiReference : all) { 122 | if (!(psiReference instanceof CompositePsiElement)) { 123 | continue; 124 | } 125 | PsiElement caller = PsiUtils.getRootPsiElement((PsiClass) psiElement, (CompositePsiElement) psiReference); 126 | 127 | if (caller == null) { 128 | continue; 129 | } 130 | 131 | incrementableSet.increment(new SourceTargetPair(caller, callee)); 132 | } 133 | } 134 | return incrementableSet; 135 | } 136 | 137 | @Nullable 138 | @Override 139 | protected PsiElement getBaseElement() { 140 | if (this.baseElement == null) { 141 | return null; 142 | } else { 143 | PsiElement element = this.baseElement.getElement(); 144 | if (element != null && element.isValid()) { 145 | return element; 146 | } else { 147 | return null; 148 | } 149 | } 150 | } 151 | 152 | @Override 153 | protected FQN getBaseForOuterReferences(PsiElement psiElement) { 154 | return FileFQN.from((PsiJavaFile) psiElement.getContainingFile()); 155 | } 156 | 157 | @NotNull 158 | @Override 159 | protected Collection resolveOuterReferences(PsiElement callee) { 160 | return ReferencesSearch.search(callee, GlobalSearchScopes.projectProductionScope(getProject())).findAll(); 161 | } 162 | 163 | @Override 164 | protected boolean isAllowedToShow(PsiElement psiElement) { 165 | if (psiElement != null && psiElement.isValid()) { 166 | DiagramNodeContentManager nodeContentManager = getNodeContentManager(); 167 | for (DiagramCategory enabledCategory : nodeContentManager.getEnabledCategories()) { 168 | if (nodeContentManager.isInCategory(psiElement, enabledCategory)) { 169 | return true; 170 | } 171 | } 172 | } 173 | return false; 174 | } 175 | 176 | @NotNull 177 | private DiagramRelationshipInfo createEdgeFromNonField(final long count) { 178 | DiagramRelationshipInfo r; 179 | r = new DiagramRelationshipInfoAdapter(ReferenceEdge.Type.REFERENCE.name()) { 180 | @Override 181 | public Shape getStartArrow() { 182 | return STANDARD; 183 | } 184 | 185 | @Override 186 | public String getToLabel() { 187 | if (count == 1) { 188 | return ""; 189 | } else { 190 | return Long.toString(count); 191 | } 192 | } 193 | 194 | @Override 195 | public boolean equals(Object obj) { 196 | if (!(obj instanceof DiagramRelationshipInfoAdapter)) { 197 | return false; 198 | } 199 | return toString().equals(obj.toString()) && getToLabel().equals(((DiagramRelationshipInfoAdapter) obj).getToLabel()); 200 | } 201 | }; 202 | return r; 203 | } 204 | 205 | @NotNull 206 | private DiagramRelationshipInfo createEdgeFromField() { 207 | DiagramRelationshipInfo r; 208 | r = new DiagramRelationshipInfoAdapter(ReferenceEdge.Type.FIELD_TO_METHOD.name()) { 209 | @Override 210 | public Shape getStartArrow() { 211 | return DELTA_SMALL; 212 | } 213 | 214 | @Override 215 | public DiagramLineType getLineType() { 216 | return DiagramLineType.DASHED; 217 | } 218 | 219 | @Override 220 | public boolean equals(Object obj) { 221 | if (!(obj instanceof DiagramRelationshipInfoAdapter)) { 222 | return false; 223 | } 224 | return toString().equals(obj.toString()); 225 | } 226 | }; 227 | return r; 228 | } 229 | 230 | } 231 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/method/OuterReferences.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.method; 18 | 19 | import ch.docksnet.rgraph.fqn.FileFQN; 20 | import ch.docksnet.rgraph.fqn.FileFQNReference; 21 | import ch.docksnet.rgraph.fqn.Hierarchically; 22 | import com.intellij.psi.PsiElement; 23 | import org.jetbrains.annotations.NotNull; 24 | 25 | import javax.swing.tree.DefaultMutableTreeNode; 26 | import java.util.List; 27 | 28 | /** 29 | * @author Stefan Zeller 30 | */ 31 | public class OuterReferences { 32 | 33 | private ReferenceCount samePackage = new ReferenceCount(); 34 | private ReferenceCount inHierarchy = new ReferenceCount(); 35 | private ReferenceCount otherHierarchy = new ReferenceCount(); 36 | private final PsiElement baseElement; 37 | 38 | public OuterReferences(PsiElement baseElement) { 39 | this.baseElement = baseElement; 40 | } 41 | 42 | public static OuterReferences empty() { 43 | return new OuterReferences(null); 44 | } 45 | 46 | public void update(Hierarchically ownHierarchy, FileFQN otherFile) { 47 | if (ownHierarchy.equals(otherFile)) { 48 | return; 49 | } 50 | if (ownHierarchy.samePackage(otherFile)) { 51 | this.samePackage.increment(otherFile); 52 | } else if (ownHierarchy.sameHierarchy(otherFile)) { 53 | this.inHierarchy.increment(otherFile); 54 | } else { 55 | this.otherHierarchy.increment(otherFile); 56 | } 57 | } 58 | 59 | public List getReferencesSamePackage() { 60 | return this.samePackage.referenceList(); 61 | } 62 | 63 | public List getReferencesSameHierarchy() { 64 | return this.inHierarchy.referenceList(); 65 | } 66 | 67 | public List getReferencesOtherHierarchy() { 68 | return this.otherHierarchy.referenceList(); 69 | } 70 | 71 | public String toToolbarString() { 72 | return "" + this.samePackage.fileCount() + "/" + this.inHierarchy.fileCount() + "/" + this.otherHierarchy.fileCount(); 73 | } 74 | 75 | public DefaultMutableTreeNode asTree() { 76 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(); 77 | root.add(createSub("Same Package", this.getReferencesSamePackage())); 78 | root.add(createSub("Same Hierarchy", this.getReferencesSameHierarchy())); 79 | root.add(createSub("Other Hierarchy", this.getReferencesOtherHierarchy())); 80 | return root; 81 | } 82 | 83 | @NotNull 84 | private DefaultMutableTreeNode createSub(String s, List content) { 85 | DefaultMutableTreeNode node = new DefaultMutableTreeNode(s); 86 | content.forEach(it -> node.add(new DefaultMutableTreeNode(it))); 87 | return node; 88 | } 89 | 90 | public PsiElement getBaseElement() { 91 | return this.baseElement; 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/method/ReferenceCount.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.method; 18 | 19 | import ch.docksnet.rgraph.fqn.FileFQN; 20 | import ch.docksnet.rgraph.fqn.FileFQNReference; 21 | 22 | import java.util.ArrayList; 23 | import java.util.HashMap; 24 | import java.util.List; 25 | import java.util.Map; 26 | 27 | public class ReferenceCount { 28 | private final Map references = new HashMap<>(); 29 | 30 | public void increment(FileFQN fileFQN) { 31 | if (!this.references.containsKey(fileFQN)) { 32 | this.references.put(fileFQN, 0); 33 | } 34 | this.references.put(fileFQN, this.references.get(fileFQN) + 1); 35 | } 36 | 37 | public List referenceList() { 38 | List list = new ArrayList<>(); 39 | for (FileFQN it : this.references.keySet()) { 40 | int count = this.references.get(it); 41 | list.add(new FileFQNReference(it, count)); 42 | } 43 | return list; 44 | } 45 | 46 | public int fileCount() { 47 | return this.references.size(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/method/ReferenceEdge.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.method; 18 | 19 | import com.intellij.diagram.DiagramEdgeBase; 20 | import com.intellij.diagram.DiagramNode; 21 | import com.intellij.diagram.DiagramRelationshipInfo; 22 | import com.intellij.psi.PsiElement; 23 | 24 | /** 25 | * @author Stefan Zeller 26 | */ 27 | public class ReferenceEdge extends DiagramEdgeBase { 28 | 29 | 30 | public ReferenceEdge(DiagramNode source, DiagramNode target, 31 | DiagramRelationshipInfo relationship) { 32 | super(source, target, relationship); 33 | } 34 | 35 | public enum Type { 36 | FIELD_TO_METHOD, REFERENCE 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/method/ReferenceNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.method; 18 | 19 | import com.intellij.diagram.DiagramProvider; 20 | import com.intellij.diagram.PsiDiagramNode; 21 | import com.intellij.openapi.util.Iconable; 22 | import com.intellij.psi.PsiElement; 23 | import com.intellij.psi.PsiMethod; 24 | import com.intellij.uml.UmlIcons; 25 | import org.jetbrains.annotations.NotNull; 26 | import org.jetbrains.annotations.Nullable; 27 | 28 | import javax.swing.*; 29 | 30 | /** 31 | * @author Stefan Zeller 32 | */ 33 | public class ReferenceNode extends PsiDiagramNode { 34 | 35 | private boolean marked; 36 | 37 | public ReferenceNode(PsiElement psiElement, DiagramProvider provider) { 38 | super(psiElement, provider); 39 | } 40 | 41 | @Nullable 42 | @Override 43 | public String getTooltip() { 44 | return null; 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return "ReferenceNode{} " + getIdentifyingElement(); 50 | } 51 | 52 | @Override 53 | public Icon getIcon() { 54 | if (getIdentifyingElement() instanceof PsiMethod && ((PsiMethod) getIdentifyingElement()).isConstructor()) { 55 | return UmlIcons.Constructor; 56 | } 57 | return getElement().getIcon(Iconable.ICON_FLAG_VISIBILITY); 58 | } 59 | 60 | public boolean isMarked() { 61 | return this.marked; 62 | } 63 | 64 | public void setMarked() { 65 | this.marked = true; 66 | } 67 | 68 | public void unsetMarked() { 69 | this.marked = false; 70 | } 71 | 72 | public void switchMarked() { 73 | if (isMarked()) { 74 | unsetMarked(); 75 | } else { 76 | setMarked(); 77 | } 78 | } 79 | 80 | @NotNull 81 | @Override 82 | public PsiElement getIdentifyingElement() { 83 | return super.getIdentifyingElement(); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/method/SourceTargetPair.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.method; 18 | 19 | import com.intellij.psi.PsiElement; 20 | import org.jetbrains.annotations.NotNull; 21 | 22 | /** 23 | * @author Stefan Zeller 24 | */ 25 | public class SourceTargetPair { 26 | private final PsiElement source; 27 | private final PsiElement target; 28 | 29 | public SourceTargetPair(@NotNull PsiElement source, @NotNull PsiElement target) { 30 | this.source = source; 31 | this.target = target; 32 | } 33 | 34 | public 35 | @NotNull 36 | PsiElement getSource() { 37 | return this.source; 38 | } 39 | 40 | public 41 | @NotNull 42 | PsiElement getTarget() { 43 | return this.target; 44 | } 45 | 46 | @Override 47 | public boolean equals(Object o) { 48 | if (this == o) { 49 | return true; 50 | } 51 | if (o == null || getClass() != o.getClass()) { 52 | return false; 53 | } 54 | 55 | final SourceTargetPair that = (SourceTargetPair) o; 56 | 57 | if (!this.source.equals(that.source)) { 58 | return false; 59 | } 60 | if (!this.target.equals(that.target)) { 61 | return false; 62 | } 63 | 64 | return true; 65 | } 66 | 67 | @Override 68 | public int hashCode() { 69 | int result = this.source.hashCode(); 70 | result = 31 * result + this.target.hashCode(); 71 | return result; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/toolwindow/ReferenceListToolWindow.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.toolwindow; 18 | 19 | import ch.docksnet.rgraph.PsiUtils; 20 | import ch.docksnet.rgraph.fqn.FileFQNReference; 21 | import com.intellij.ide.util.gotoByName.GotoFileCellRenderer; 22 | import com.intellij.openapi.project.Project; 23 | import com.intellij.psi.PsiElement; 24 | import com.intellij.ui.components.JBList; 25 | import com.intellij.util.AstLoadingFilter; 26 | 27 | import javax.swing.*; 28 | import java.awt.*; 29 | import java.awt.event.KeyAdapter; 30 | import java.awt.event.KeyEvent; 31 | import java.awt.event.KeyListener; 32 | import java.awt.event.MouseAdapter; 33 | import java.awt.event.MouseEvent; 34 | import java.awt.event.MouseListener; 35 | import java.util.function.Consumer; 36 | 37 | 38 | public class ReferenceListToolWindow { 39 | private JPanel myToolWindowContent; 40 | private DefaultListModel listModel; 41 | private final String name; 42 | private Consumer updateTabNameCallback = noop -> { 43 | }; 44 | 45 | public ReferenceListToolWindow(String name, Project project) { 46 | this.name = name; 47 | this.myToolWindowContent = new JPanel(new BorderLayout()); 48 | 49 | this.listModel = new DefaultListModel(); 50 | JList myList = new JBList<>(this.listModel); 51 | myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 52 | myList.setLayoutOrientation(JList.VERTICAL); 53 | myList.setVisibleRowCount(5); 54 | JScrollPane listScrollPane = new JScrollPane(myList); 55 | this.myToolWindowContent.add(listScrollPane, BorderLayout.CENTER); 56 | 57 | // kind of stolen from com.intellij.ide.util.gotoByName.ChooseByNameBase 58 | myList.setCellRenderer((list, value, index, isSelected, cellHasFocus) -> AstLoadingFilter.disallowTreeLoading( 59 | () -> new GotoFileCellRenderer(500).getListCellRendererComponent(list, value, index, isSelected, cellHasFocus) 60 | )); 61 | 62 | MouseListener mouseListener = new MouseAdapter() { 63 | public void mouseClicked(MouseEvent event) { 64 | JList theList = (JList) event.getSource(); 65 | if (event.getClickCount() == 2) { 66 | int index = theList.locationToIndex(event.getPoint()); 67 | if (index >= 0) { 68 | Object o = theList.getModel().getElementAt(index); 69 | if (o instanceof PsiElement) { 70 | PsiUtils.navigate((PsiElement) o, project); 71 | } 72 | } 73 | } 74 | } 75 | }; 76 | myList.addMouseListener(mouseListener); 77 | 78 | KeyListener keyListener = new KeyAdapter() { 79 | @Override 80 | public void keyPressed(KeyEvent event) { 81 | JList theList = (JList) event.getSource(); 82 | if (event.getExtendedKeyCode() == KeyEvent.VK_ENTER) { 83 | Object o = theList.getSelectedValue(); 84 | if (o instanceof PsiElement) { 85 | PsiUtils.navigate((PsiElement) o, project); 86 | } 87 | } 88 | } 89 | }; 90 | myList.addKeyListener(keyListener); 91 | } 92 | 93 | 94 | JPanel getContent() { 95 | return this.myToolWindowContent; 96 | } 97 | 98 | public void replaceContent(java.util.List entries) { 99 | this.listModel.clear(); 100 | for (FileFQNReference entry : entries) { 101 | this.listModel.addElement(entry.getPsiElement()); 102 | } 103 | this.updateTabNameCallback.accept(this.getName()); 104 | } 105 | 106 | String getName() { 107 | return this.name + "(" + this.listModel.size() + ")"; 108 | } 109 | 110 | public void setUpdateTabNameCallback(Consumer updateTabNameCallback) { 111 | this.updateTabNameCallback = updateTabNameCallback; 112 | } 113 | 114 | } -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/toolwindow/ReferenceToolWindow.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.toolwindow; 18 | 19 | import ch.docksnet.rgraph.ProjectService; 20 | import com.intellij.openapi.application.ApplicationManager; 21 | import com.intellij.openapi.components.ServiceManager; 22 | import com.intellij.openapi.project.Project; 23 | import com.intellij.openapi.wm.ToolWindow; 24 | import com.intellij.openapi.wm.ToolWindowFactory; 25 | import com.intellij.ui.content.Content; 26 | import com.intellij.ui.content.ContentFactory; 27 | import org.jetbrains.annotations.NotNull; 28 | 29 | public class ReferenceToolWindow implements ToolWindowFactory { 30 | 31 | public static final String ID = "Package References"; 32 | 33 | @Override 34 | public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) { 35 | ContentFactory contentFactory = ContentFactory.SERVICE.getInstance(); 36 | 37 | create(toolWindow, contentFactory, ServiceManager.getService(project, ProjectService.class).getSamePackageReferences()); 38 | create(toolWindow, contentFactory, ServiceManager.getService(project, ProjectService.class).getSameHierarchieReferences()); 39 | create(toolWindow, contentFactory, ServiceManager.getService(project, ProjectService.class).getOtherHierarchieReferences()); 40 | } 41 | 42 | private void create(@NotNull ToolWindow toolWindow, ContentFactory contentFactory, ReferenceListToolWindow window) { 43 | Content content = contentFactory.createContent(window.getContent(), window.getName(), false); 44 | window.setUpdateTabNameCallback(newName -> { 45 | ApplicationManager.getApplication().invokeLater( 46 | () -> { 47 | content.setDisplayName(newName); 48 | }); 49 | }); 50 | toolWindow.getContentManager().addContent(content); 51 | } 52 | 53 | @Override 54 | public void init(ToolWindow window) { 55 | 56 | } 57 | 58 | @Override 59 | public boolean shouldBeAvailable(@NotNull Project project) { 60 | return true; 61 | } 62 | 63 | @Override 64 | public boolean isDoNotActivateOnStart() { 65 | return true; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/toolwindow/TestToolWindow.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.toolwindow; 18 | 19 | import ch.docksnet.rgraph.PsiUtils; 20 | import ch.docksnet.rgraph.fqn.FileFQNReference; 21 | import ch.docksnet.rgraph.method.OuterReferences; 22 | import com.intellij.openapi.project.Project; 23 | import com.intellij.openapi.wm.ToolWindow; 24 | import com.intellij.openapi.wm.ToolWindowAnchor; 25 | import com.intellij.openapi.wm.ToolWindowManager; 26 | import com.intellij.psi.PsiElement; 27 | import com.intellij.psi.presentation.java.SymbolPresentationUtil; 28 | import com.intellij.ui.ColoredTreeCellRenderer; 29 | import com.intellij.ui.SimpleTextAttributes; 30 | import com.intellij.ui.components.JBScrollPane; 31 | import com.intellij.ui.content.impl.ContentImpl; 32 | import com.intellij.ui.treeStructure.Tree; 33 | import org.jetbrains.annotations.NotNull; 34 | 35 | import javax.swing.*; 36 | import javax.swing.tree.DefaultMutableTreeNode; 37 | import javax.swing.tree.DefaultTreeModel; 38 | import javax.swing.tree.TreePath; 39 | import java.awt.*; 40 | import java.awt.event.*; 41 | 42 | public class TestToolWindow extends JPanel { 43 | private static final String TOOL_WINDOW_ID = "References"; 44 | private final Tree myTree; 45 | 46 | private TestToolWindow(Project project) { 47 | this.myTree = new Tree(new DefaultTreeModel(new DefaultMutableTreeNode())); 48 | this.myTree.setRootVisible(false); 49 | this.myTree.setCellRenderer(new ColoredTreeCellRenderer() { 50 | @Override 51 | public void customizeCellRenderer(@NotNull JTree tree, 52 | Object value, 53 | boolean selected, 54 | boolean expanded, 55 | boolean leaf, 56 | int row, 57 | boolean hasFocus) { 58 | final DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; 59 | final Object userObject = node.getUserObject(); 60 | if (userObject == null) { 61 | return; 62 | } 63 | if (userObject instanceof FileFQNReference) { 64 | FileFQNReference ref = (FileFQNReference) userObject; 65 | VirtualFileCellRenderer.render(this, ref, project); 66 | } else if (userObject instanceof String) { 67 | append((String) userObject, SimpleTextAttributes.GRAY_ATTRIBUTES); 68 | } else { 69 | append(userObject.toString()); 70 | } 71 | } 72 | }); 73 | MouseListener mouseListener = new MouseAdapter() { 74 | public void mouseClicked(MouseEvent event) { 75 | Tree theTree = (Tree) event.getSource(); 76 | if (event.getClickCount() == 2) { 77 | TreePath pathForLocation = theTree.getPathForLocation(event.getPoint().x, event.getPoint().y); 78 | if (pathForLocation == null) { 79 | return; 80 | } 81 | Object o = pathForLocation.getLastPathComponent(); 82 | if (!(o instanceof DefaultMutableTreeNode)) { 83 | return; 84 | } 85 | navigate((DefaultMutableTreeNode) o, project); 86 | } 87 | } 88 | }; 89 | this.myTree.addMouseListener(mouseListener); 90 | 91 | KeyListener keyListener = new KeyAdapter() { 92 | @Override 93 | public void keyPressed(KeyEvent event) { 94 | Tree theTree = (Tree) event.getSource(); 95 | if (event.getExtendedKeyCode() == KeyEvent.VK_ENTER) { 96 | Object o = theTree.getSelectionPath().getLastPathComponent(); 97 | if (!(o instanceof DefaultMutableTreeNode)) { 98 | return; 99 | } 100 | navigate((DefaultMutableTreeNode) o, project); 101 | } 102 | } 103 | }; 104 | this.myTree.addKeyListener(keyListener); 105 | setLayout(new BorderLayout()); 106 | add(new JBScrollPane(this.myTree)); 107 | } 108 | 109 | private void navigate(DefaultMutableTreeNode node, Project project) { 110 | if (node.getUserObject() instanceof PsiElement) { 111 | PsiUtils.navigate((PsiElement) node.getUserObject(), project); 112 | } 113 | } 114 | 115 | public static void show(Project project, OuterReferences outerReferences) { 116 | final TestToolWindow view = createViewTab(project, outerReferences.getBaseElement()); 117 | final DefaultMutableTreeNode node = outerReferences.asTree(); 118 | ((DefaultTreeModel) view.myTree.getModel()).setRoot(node); 119 | } 120 | 121 | private static TestToolWindow createViewTab(Project project, PsiElement baseElement) { 122 | final ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project); 123 | ToolWindow toolWindow = toolWindowManager.getToolWindow(TOOL_WINDOW_ID); 124 | if (toolWindow == null) { 125 | toolWindow = toolWindowManager.registerToolWindow(TOOL_WINDOW_ID, 126 | true, 127 | ToolWindowAnchor.BOTTOM); 128 | } 129 | final TestToolWindow view = new TestToolWindow(project); 130 | ToolWindow finalToolWindow = toolWindow; 131 | toolWindow.activate(() -> { 132 | final String text = SymbolPresentationUtil.getSymbolPresentableText(baseElement); 133 | final ContentImpl content = new ContentImpl(view, "to " + text, true); 134 | finalToolWindow.getContentManager().addContent(content); 135 | finalToolWindow.getContentManager().setSelectedContent(content, true); 136 | }); 137 | return view; 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/ch/docksnet/rgraph/toolwindow/VirtualFileCellRenderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.toolwindow; 18 | 19 | import ch.docksnet.rgraph.fqn.FileFQNReference; 20 | import com.intellij.openapi.project.Project; 21 | import com.intellij.openapi.util.Iconable; 22 | import com.intellij.openapi.vfs.VFileProperty; 23 | import com.intellij.openapi.vfs.VirtualFile; 24 | import com.intellij.psi.PsiJavaFile; 25 | import com.intellij.psi.PsiManager; 26 | import com.intellij.ui.LayeredIcon; 27 | import com.intellij.ui.SimpleColoredComponent; 28 | import com.intellij.ui.SimpleTextAttributes; 29 | import com.intellij.util.IconUtil; 30 | import com.intellij.util.PlatformIcons; 31 | 32 | import javax.swing.*; 33 | import java.awt.*; 34 | 35 | import static com.intellij.openapi.fileChooser.FileElement.isFileHidden; 36 | import static com.intellij.openapi.util.IconLoader.getTransparentIcon; 37 | 38 | public class VirtualFileCellRenderer { 39 | private static final Color HIDDEN = SimpleTextAttributes.DARK_TEXT.getFgColor(); 40 | 41 | public static void render(SimpleColoredComponent renderer, FileFQNReference ref, Project project) { 42 | VirtualFile virtualFile = ref.getPsiElement().getContainingFile().getVirtualFile(); 43 | PsiJavaFile psiFile = (PsiJavaFile) PsiManager.getInstance(project).findFile(virtualFile); 44 | int style = SimpleTextAttributes.STYLE_PLAIN; 45 | Color color = SimpleTextAttributes.LINK_BOLD_ATTRIBUTES.getFgColor(); 46 | Icon icon = getIcon(virtualFile); 47 | String comment = null; 48 | if (!virtualFile.isValid()) style |= SimpleTextAttributes.STYLE_STRIKEOUT; 49 | boolean fileHidden = isFileHidden(virtualFile); 50 | if (fileHidden) { 51 | color = HIDDEN; 52 | } ; 53 | renderer.setIcon(!fileHidden || icon == null ? icon : getTransparentIcon(icon)); 54 | SimpleTextAttributes attributes = new SimpleTextAttributes(style, color); 55 | renderer.append(psiFile.getPackageName() + "." + psiFile.getName(), attributes); 56 | renderer.append(" " + ref.toUsageString(), new SimpleTextAttributes(SimpleTextAttributes.STYLE_PLAIN, SimpleTextAttributes.GRAY_ATTRIBUTES.getFgColor())); 57 | if (comment != null) renderer.append(comment, attributes); 58 | } 59 | 60 | private static Icon getIcon(final VirtualFile file) { 61 | return dressIcon(file, IconUtil.getIcon(file, Iconable.ICON_FLAG_READ_STATUS, null)); 62 | } 63 | 64 | private static Icon dressIcon(final VirtualFile file, final Icon baseIcon) { 65 | return file.isValid() && file.is(VFileProperty.SYMLINK) ? new LayeredIcon(baseIcon, PlatformIcons.SYMLINK_ICON) : baseIcon; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/ch/docksnet/utils/IncrementableSet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.utils; 18 | 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | import java.util.Set; 22 | 23 | /** 24 | * @author Stefan Zeller 25 | */ 26 | public class IncrementableSet { 27 | 28 | private final HashMap map = new HashMap<>(); 29 | 30 | public long get(T element) { 31 | if (this.map.containsKey(element)) { 32 | return this.map.get(element); 33 | } else { 34 | return 0; 35 | } 36 | } 37 | 38 | public void increment(T element) { 39 | if (this.map.containsKey(element)) { 40 | long count = this.map.get(element); 41 | count += 1; 42 | this.map.put(element, count); 43 | } else { 44 | this.map.put(element, 1L); 45 | } 46 | } 47 | 48 | public Set> elements() { 49 | return this.map.entrySet(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/ch/docksnet/utils/PreConditionUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.utils; 18 | 19 | /** 20 | * @author Stefan Zeller 21 | */ 22 | public class PreConditionUtil { 23 | 24 | public static void assertTrue(boolean bool, String message) { 25 | if (!bool) { 26 | throw new IllegalStateException(message); 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/ch/docksnet/utils/lcom/AbstractLCOMHSAnalyzer.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Stefku/intellij-reference-diagram/5e21362f3da52dc814c49c8b689d85fc0da44e72/src/ch/docksnet/utils/lcom/AbstractLCOMHSAnalyzer.java -------------------------------------------------------------------------------- /src/ch/docksnet/utils/lcom/CalleesSubgraphAnalyzer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.utils.lcom; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | /** 23 | * @author Stefan Zeller 24 | */ 25 | public class CalleesSubgraphAnalyzer { 26 | 27 | private final LCOMAnalyzerData data; 28 | 29 | public CalleesSubgraphAnalyzer(LCOMAnalyzerData data) { 30 | this.data = data; 31 | } 32 | 33 | public List getCallees(LCOMNode root) { 34 | final List result = new ArrayList<>(); 35 | visitCallees(root, result); 36 | return result; 37 | } 38 | 39 | private void visitCallees(LCOMNode node, List visited) { 40 | if (isNotVisited(node)) { 41 | markVisited(node); 42 | visited.add(node); 43 | for (LCOMNode callee : node.getCallees()) { 44 | visitCallees(callee, visited); 45 | } 46 | } 47 | } 48 | 49 | private boolean isNotVisited(LCOMNode node) { 50 | return !isVisited(node); 51 | } 52 | 53 | private Boolean isVisited(LCOMNode node) { 54 | return data.getNodeMarker().get(node.getFqn()); 55 | } 56 | 57 | private void markVisited(LCOMNode node) { 58 | data.getNodeMarker().put(node.getFqn(), true); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/ch/docksnet/utils/lcom/CallersSubgraphAnalyzer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.utils.lcom; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | /** 23 | * @author Stefan Zeller 24 | */ 25 | public class CallersSubgraphAnalyzer { 26 | 27 | private final LCOMAnalyzerData data; 28 | 29 | public CallersSubgraphAnalyzer(LCOMAnalyzerData data) { 30 | this.data = data; 31 | } 32 | 33 | public List getCallees(LCOMNode root) { 34 | final List result = new ArrayList<>(); 35 | visitCallers(root, result); 36 | return result; 37 | } 38 | 39 | private void visitCallers(LCOMNode node, List visited) { 40 | if (isNotVisited(node)) { 41 | markVisited(node); 42 | visited.add(node); 43 | for (LCOMNode callee : node.getCallers()) { 44 | visitCallers(callee, visited); 45 | } 46 | } 47 | } 48 | 49 | private boolean isNotVisited(LCOMNode node) { 50 | return !isVisited(node); 51 | } 52 | 53 | private Boolean isVisited(LCOMNode node) { 54 | return data.getNodeMarker().get(node.getFqn()); 55 | } 56 | 57 | private void markVisited(LCOMNode node) { 58 | data.getNodeMarker().put(node.getFqn(), true); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/ch/docksnet/utils/lcom/ClusterAnalyzer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.utils.lcom; 18 | 19 | /** 20 | * Count independend clusters of a graph. Two clusters are independend of each other, if there is no connection 21 | * between them. I.e. Two clusters of methods, fields that have no dependency between each other. 22 | * 23 | * @author Stefan Zeller 24 | */ 25 | public class ClusterAnalyzer { 26 | 27 | private final LCOMAnalyzerData data; 28 | 29 | public ClusterAnalyzer(LCOMAnalyzerData data) { 30 | this.data = data; 31 | } 32 | 33 | public long countCluster() { 34 | long result = 0; 35 | for (LCOMNode node : data.getNodeRegistry().values()) { 36 | if (isNotVisited(node)) { 37 | result += 1; 38 | visitCluster(node); 39 | } 40 | } 41 | return result; 42 | } 43 | 44 | private void visitCluster(LCOMNode node) { 45 | if (isNotVisited(node)) { 46 | markVisited(node); 47 | for (LCOMNode caller : node.getCallers()) { 48 | visitCluster(caller); 49 | } 50 | for (LCOMNode callee : node.getCallees()) { 51 | visitCluster(callee); 52 | } 53 | } 54 | } 55 | 56 | private boolean isNotVisited(LCOMNode node) { 57 | return !isVisited(node); 58 | } 59 | 60 | private Boolean isVisited(LCOMNode node) { 61 | return data.getNodeMarker().get(node.getFqn()); 62 | } 63 | 64 | private void markVisited(LCOMNode node) { 65 | data.getNodeMarker().put(node.getFqn(), true); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/ch/docksnet/utils/lcom/LCOMAnalyzerData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.utils.lcom; 18 | 19 | import java.util.Collection; 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | /** 24 | * @author Stefan Zeller 25 | */ 26 | public class LCOMAnalyzerData { 27 | 28 | private final Map nodeRegistry = new HashMap<>(); 29 | private final Map nodeMarker = new HashMap<>(); 30 | 31 | public LCOMAnalyzerData(Collection nodes) { 32 | init(nodes); 33 | } 34 | 35 | private void init(Collection nodes) { 36 | for (LCOMNode node : nodes) { 37 | nodeRegistry.put(node.getFqn(), node); 38 | nodeMarker.put(node.getFqn(), false); 39 | } 40 | } 41 | 42 | public Map getNodeRegistry() { 43 | return nodeRegistry; 44 | } 45 | 46 | public Map getNodeMarker() { 47 | return nodeMarker; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/ch/docksnet/utils/lcom/LCOMHSAnalyzer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.utils.lcom; 18 | 19 | /** 20 | * LCOM-HS Analyzer where 21 | *
      22 | *
    • constructors are also consideres as methods and
    • 23 | *
    • constants are not considered as fields.
    • 24 | *
    25 | * 26 | * @author Stefan Zeller 27 | */ 28 | public class LCOMHSAnalyzer extends AbstractLCOMHSAnalyzer { 29 | 30 | public LCOMHSAnalyzer(LCOMAnalyzerData data) { 31 | super(data); 32 | } 33 | 34 | @Override 35 | protected boolean isVariable(LCOMNode node) { 36 | switch (node.getType()) { 37 | case Field: 38 | return true; 39 | default: 40 | return false; 41 | } 42 | } 43 | 44 | @Override 45 | protected boolean isMethod(LCOMNode node) { 46 | switch (node.getType()) { 47 | case Method: 48 | case Constructur: 49 | return true; 50 | default: 51 | return false; 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/ch/docksnet/utils/lcom/LCOMNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.utils.lcom; 18 | 19 | import ch.docksnet.rgraph.method.ReferenceNode; 20 | import ch.docksnet.utils.PreConditionUtil; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | /** 26 | * @author Stefan Zeller 27 | */ 28 | public class LCOMNode { 29 | 30 | private List callees = new ArrayList<>(); 31 | private List callers = new ArrayList<>(); 32 | private final String fqn; 33 | private final Type type; 34 | private final ReferenceNode identifyingElement; 35 | 36 | public LCOMNode(String fqn, Type type, ReferenceNode identifyingElement) { 37 | this.identifyingElement = identifyingElement; 38 | PreConditionUtil.assertTrue(fqn != null, "Full qualified name must be set"); 39 | PreConditionUtil.assertTrue(type != null, "Type must be set"); 40 | this.type = type; 41 | this.fqn = fqn; 42 | } 43 | 44 | public void addCallee(LCOMNode callee) { 45 | callees.add(callee); 46 | callee.addCaller(this); 47 | } 48 | 49 | private void addCaller(LCOMNode caller) { 50 | callers.add(caller); 51 | } 52 | 53 | public String getFqn() { 54 | return fqn; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "LCOMNode{" + 60 | "fqn='" + fqn + '\'' + 61 | ", type=" + type + 62 | '}'; 63 | } 64 | 65 | @Override 66 | public boolean equals(Object o) { 67 | if (this == o) { 68 | return true; 69 | } 70 | if (o == null || getClass() != o.getClass()) { 71 | return false; 72 | } 73 | 74 | final LCOMNode lcomNode = (LCOMNode) o; 75 | 76 | if (!fqn.equals(lcomNode.fqn)) { 77 | return false; 78 | } 79 | 80 | return true; 81 | } 82 | 83 | @Override 84 | public int hashCode() { 85 | return fqn.hashCode(); 86 | } 87 | 88 | public List getCallers() { 89 | return callers; 90 | } 91 | 92 | public List getCallees() { 93 | return callees; 94 | } 95 | 96 | public ReferenceNode getIdentifyingElement() { 97 | return identifyingElement; 98 | } 99 | 100 | public Type getType() { 101 | return type; 102 | } 103 | 104 | public enum Type { 105 | Method, Field, Constant, Constructur, ClassInitializer, Class, InnerClass, StaticInnerClass, Enum, File, Package 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /test/Example_InnerClasses.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Stefku/intellij-reference-diagram/5e21362f3da52dc814c49c8b689d85fc0da44e72/test/Example_InnerClasses.png -------------------------------------------------------------------------------- /test/java/ch/docksnet/DependencyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet; 18 | 19 | import java.io.IOException; 20 | 21 | import jdepend.framework.DependencyDefiner; 22 | import jdepend.framework.JDepend; 23 | import jdepend.framework.JavaPackage; 24 | import jdepend.framework.PackageFilter; 25 | import org.junit.BeforeClass; 26 | import org.junit.Test; 27 | 28 | import static jdepend.framework.DependencyMatchers.hasNoCycles; 29 | import static jdepend.framework.DependencyMatchers.matchesPackages; 30 | import static org.junit.Assert.assertThat; 31 | 32 | /** 33 | * @author Stefan Zeller 34 | */ 35 | public class DependencyTest { 36 | private static JDepend depend; 37 | 38 | @BeforeClass 39 | public static void init() throws IOException { 40 | depend = new JDepend(PackageFilter.all().including("ch.docksnet").excludingRest()); 41 | depend.addDirectory("out/production/intellij-reference-diagram"); 42 | depend.analyze(); 43 | } 44 | 45 | @Test 46 | public void dependencies() { 47 | class ChDocksnet implements DependencyDefiner { 48 | JavaPackage rgraph, utils; 49 | 50 | public void dependUpon() { 51 | rgraph.dependsUpon(utils); 52 | } 53 | } 54 | assertThat(depend, matchesPackages(new ChDocksnet())); 55 | } 56 | 57 | @Test 58 | public void noCircularDependencies() { 59 | assertThat(depend, hasNoCycles()); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /test/java/ch/docksnet/app/AnonymousClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.app; 18 | 19 | /** 20 | * @author Stefan Zeller 21 | */ 22 | public class AnonymousClass { 23 | 24 | int i = 0; 25 | 26 | public void test1() { 27 | new Yo(i){ 28 | @Override 29 | public String toString() { 30 | return "$classname{}" + i; 31 | } 32 | }; 33 | } 34 | 35 | static class Yo { 36 | public Yo(int x) { 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /test/java/ch/docksnet/app/InnerClasses.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.app; 18 | 19 | /** 20 | * @author Stefan Zeller 21 | */ 22 | public class InnerClasses { 23 | 24 | Enum e = Enum.a; 25 | 26 | class InnerClass { 27 | 28 | { 29 | String test = method2(); 30 | } 31 | 32 | String innerField = method1(); 33 | 34 | void innerMethod() { 35 | method3(); 36 | } 37 | 38 | class InnerInnerClass { 39 | 40 | String innerInnerField = method4(); 41 | Enum e = Enum.a; 42 | 43 | } 44 | 45 | } 46 | 47 | private String method4() { 48 | return null; 49 | } 50 | 51 | private void method3() { 52 | 53 | } 54 | 55 | private String method2() { 56 | return null; 57 | } 58 | 59 | private String method1() { 60 | return null; 61 | } 62 | 63 | 64 | private static String sstaticMethod4() { 65 | return null; 66 | } 67 | 68 | private static void staticMethod3() { 69 | 70 | } 71 | 72 | private static String staticMethod2() { 73 | return null; 74 | } 75 | 76 | private static String staticMethod1() { 77 | return null; 78 | } 79 | 80 | enum Enum { 81 | a, b, c 82 | } 83 | 84 | static class StaticInnerClass { 85 | { 86 | String test = staticMethod2(); 87 | } 88 | 89 | String innerField = staticMethod1(); 90 | 91 | void innerMethod() { 92 | staticMethod3(); 93 | } 94 | 95 | class InnerInnerClass { 96 | 97 | String innerInnerField = sstaticMethod4(); 98 | Enum e = Enum.a; 99 | 100 | } 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /test/java/ch/docksnet/app/MainClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.app; 18 | 19 | public abstract class MainClass { 20 | 21 | { 22 | // class initializer 23 | method1(); 24 | } 25 | 26 | static { 27 | // static class initializer 28 | staticMethod(); 29 | staticField2 = StaticInnerClass.TEXT; 30 | } 31 | 32 | // field is coupled to a method, in terms of cohesion 33 | public static String staticField1 = staticMethod2c(); 34 | 35 | private static String staticField2; 36 | 37 | int field1; 38 | 39 | private int field2 = createInt(); 40 | 41 | public ENUM field3 = ENUM.A; 42 | 43 | public MainClass() { 44 | InnerClass innerClass = new InnerClass(); 45 | innerClass.getName(); 46 | } 47 | 48 | public MainClass(int field1, int field2, ENUM field3) { 49 | this.field1 = field1; 50 | this.field2 = field2; 51 | this.field3 = field3; 52 | } 53 | 54 | private int createInt() { 55 | return 0; 56 | } 57 | 58 | public static void staticMethod() { 59 | staticMethod2c(); 60 | } 61 | 62 | private static String staticMethod2c() { 63 | return null; 64 | } 65 | 66 | public final void method1() { 67 | method2(); 68 | } 69 | 70 | protected void method1(int a) { 71 | method2(); 72 | method2(); 73 | method2(); 74 | } 75 | 76 | void method2() { 77 | field1 = 4; 78 | field1 = 1; 79 | field3.getString(); 80 | } 81 | 82 | abstract void abstractMethod(); 83 | 84 | private void recursiveMethod() { 85 | recursiveMethod(); 86 | } 87 | 88 | class InnerClass { 89 | // TODO what about inner classes? 90 | String innerField = staticMethod2c(); 91 | public String getName() { 92 | return "Name"; 93 | } 94 | } 95 | 96 | static class StaticInnerClass { 97 | public int i =3; 98 | public static final String TEXT = "text"; 99 | static { 100 | System.out.println(TEXT); 101 | System.out.println(staticField1); 102 | } 103 | } 104 | 105 | enum ENUM { 106 | A, B, C; 107 | 108 | public String getString() { 109 | return "String"; 110 | } 111 | } 112 | 113 | } 114 | 115 | /* 116 | * This class must be ignored in the diagram 117 | */ 118 | class SiblingClass { 119 | 120 | public static String siblingStaticField = MainClass.staticField1; 121 | 122 | } 123 | -------------------------------------------------------------------------------- /test/java/ch/docksnet/app/TestEnum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.app; 18 | 19 | public enum TestEnum { 20 | A("a"), 21 | B("b"), 22 | C("a and b"); 23 | 24 | private String key; 25 | 26 | TestEnum() { 27 | } 28 | 29 | TestEnum(String key) { 30 | this.key = key; 31 | } 32 | 33 | public String getKey() { 34 | return this.toString(); 35 | } 36 | 37 | public String getDisplayKey() { 38 | return "my.proeprties." + key; 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /test/java/ch/docksnet/rgraph/MethodFQNTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph; 18 | 19 | import java.util.Arrays; 20 | 21 | import ch.docksnet.rgraph.fqn.MethodFQN; 22 | import org.junit.Test; 23 | 24 | import static org.junit.Assert.assertEquals; 25 | 26 | /** 27 | * @author Stefan Zeller 28 | */ 29 | public class MethodFQNTest { 30 | 31 | @Test 32 | public void test_create_from_string() throws Exception { 33 | MethodFQN result = MethodFQN.create("ch.docksnet.app.MainClass#method1(int,String,int)"); 34 | 35 | assertEquals("ch.docksnet.app.MainClass", result.getClassName()); 36 | assertEquals("method1", result.getMethodName()); 37 | assertEquals(Arrays.asList("int", "String", "int"), result.getParameters()); 38 | } 39 | 40 | @Test 41 | public void create_and_get_fqn() throws Exception { 42 | MethodFQN sut = new MethodFQN.Builder("ch.docksnet.app.MainClass", "method1") 43 | .addParameter("int") 44 | .addParameter("String") 45 | .addParameter("int") 46 | .create(); 47 | 48 | String result = sut.getFQN(); 49 | 50 | assertEquals("ch.docksnet.app.MainClass#method1(int,String,int)", result); 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /test/java/ch/docksnet/rgraph/fqn/HierarchicallyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.rgraph.fqn; 18 | 19 | import org.junit.Test; 20 | 21 | import java.util.Arrays; 22 | import java.util.List; 23 | 24 | import static org.junit.Assert.*; 25 | 26 | public class HierarchicallyTest { 27 | @Test 28 | public void getNextHierarchyTowards() { 29 | PackageFQN a = PackageFQN.create("root.sub"); 30 | PackageFQN b = PackageFQN.create("root.sub.another"); 31 | PackageFQN c = PackageFQN.create("root.sub.more.evenmore"); 32 | 33 | assertEquals("root.sub.another", a.getNextHierarchyTowards(b)); 34 | assertEquals("root.sub.more", a.getNextHierarchyTowards(c)); 35 | } 36 | 37 | @Test(expected = IllegalArgumentException.class) 38 | public void getNextHierarchyTowardsError1() { 39 | PackageFQN a = PackageFQN.create("root.sub"); 40 | PackageFQN b = PackageFQN.create("root.sibling"); 41 | a.getNextHierarchyTowards(b); 42 | } 43 | 44 | @Test(expected = IllegalArgumentException.class) 45 | public void getNextHierarchyTowardsError2() { 46 | PackageFQN a = PackageFQN.create("root.sub"); 47 | PackageFQN b = PackageFQN.create("root.subling"); 48 | a.getNextHierarchyTowards(b); 49 | } 50 | 51 | @Test 52 | public void sameHierarchy() { 53 | assertTrue(PackageFQN.create("root.sub.another").sameHierarchy(PackageFQN.create("root.sub.another"))); 54 | assertTrue(PackageFQN.create("root.sub.another").sameHierarchy(PackageFQN.create("root.sub"))); 55 | assertTrue(PackageFQN.create("root.sub.another").sameHierarchy(PackageFQN.create("root"))); 56 | 57 | assertFalse(PackageFQN.create("root.sub.another").sameHierarchy(PackageFQN.create("root.another"))); 58 | assertFalse(PackageFQN.create("root.sub.another").sameHierarchy(PackageFQN.create("root.sub.anotherrrr"))); 59 | } 60 | 61 | @Test 62 | public void getPackagePaths() { 63 | List packages = Hierarchically.getPackagePaths(PackageFQN.create("root.sub.another")); 64 | assertEquals(Arrays.asList("root", "root.sub", "root.sub.another"), packages); 65 | } 66 | 67 | @Test 68 | public void getPackages() { 69 | List packages = Hierarchically.getPackages(PackageFQN.create("root.sub.another")); 70 | assertEquals(Arrays.asList("root", "sub", "another"), packages); 71 | } 72 | } -------------------------------------------------------------------------------- /test/java/ch/docksnet/utils/IncrementableSetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Stefan Zeller 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ch.docksnet.utils; 18 | 19 | import org.junit.Before; 20 | import org.junit.Test; 21 | 22 | import static org.junit.Assert.assertEquals; 23 | 24 | /** 25 | * @author Stefan Zeller 26 | */ 27 | public class IncrementableSetTest { 28 | 29 | private IncrementableSet sut; 30 | 31 | @Before 32 | public void setUp() throws Exception { 33 | sut = new IncrementableSet<>(); 34 | } 35 | 36 | @Test 37 | public void can_create() throws Exception { 38 | } 39 | 40 | @Test 41 | public void get_zero_when_element_has_not_been_added() throws Exception { 42 | long result = sut.get("test"); 43 | 44 | assertEquals(0, result); 45 | } 46 | 47 | @Test 48 | public void get_one_when_added_once() throws Exception { 49 | sut.increment("test"); 50 | long result = sut.get("test"); 51 | 52 | assertEquals(1, result); 53 | } 54 | 55 | } --------------------------------------------------------------------------------