summaryrefslogtreecommitdiff
path: root/java/java-impl/src/com
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2013-09-05 16:07:26 -0700
committerTor Norbye <tnorbye@google.com>2013-09-05 16:07:52 -0700
commitc7f983b5dcd0499fc68aacaba043874648932b6b (patch)
treec3a4323111a1af487dadb054a7f8974d76427b6a /java/java-impl/src/com
parent932259520ebaedeb2ccf4b7594bad50c700963d7 (diff)
downloadidea-c7f983b5dcd0499fc68aacaba043874648932b6b.tar.gz
Snapshot 2cb2395a861df68b665447030b8eba2323968de0 from idea/132.106 of git://git.jetbrains.org/idea/community.git
2cb2395: 2013-09-04 Sergey Evdokimov - IDEA generate maven libraries with incorrect classes URL. Url points to pom.xml instead of jar. 52aca33: 2013-09-04 Vladimir Krivosheev - cleanup: remove NodeJSProcessHandler 2e683c3: 2013-09-04 peter - @VisibleForTesting: when no real visibility is specified, consider it is one less the one specified in the source (IDEA-107730) d3d2c37: 2013-09-04 Vladimir Krivosheev - CR-IC-2185 remove deprecated getStreamProviders() 6abec2b: 2013-09-04 Sergey Evdokimov - IDEA-90438 tooltip for constructor/method call should display package 7d20ed1: 2013-09-04 Vladislav.Soroka - IDEA-112870 Changing a library's scope from compile to testCompile in build.gradle does not change IDEA module settings d40c1f8: 2013-09-04 Konstantin Bulenkov - IDEA-113175 Expanding nodes in Variables(Debug tools window) while debug application throws exception 9733cb8: 2013-09-04 peter - dfa: use correct code block for closure dfa calculation 1bcdb8e: 2013-09-04 Vassiliy - http://ea.jetbrains.com/browser/ea_reports/533457 NPE after dispose (as I see) 03698e8: 2013-09-04 peter - uppercase prefix should middle-match lowercase string b2dce0d: 2013-09-04 Vladislav.Soroka - IDEA-78536 Gradle: New Project wizard: notifications on "Gradle home" field disappear too quickly 310cdb9: 2013-09-04 nik - simplified bdfbce8: 2013-09-04 nik - cleanup e2856e1: 2013-09-04 Anna Kozlova - EA-49556 - CCE: ExpectedTypesProvider$MyParentVisitor.visitAnnotationArrayInitializer a56c7b1: 2013-09-04 Alexey Kudravtsev - editor api modules refactoring: - editor-ui-api module containing public API editor interfaces - editor-ui-impl module containing editor implementation interfaces - analysis* modules made dependant on editor-ui* modules - highlighting classes moved to *analysis* modules - majority of highlighting quickfixes moved to QuickFixFactory - injections in GeneralHighlightingPass splitted away to InjectedGeneralHighlightingPass 06c2cde: 2013-09-04 Alexey Kudravtsev - removing of smart pointers fixed 2a8f1fb: 2013-09-04 Anna Kozlova - Merge remote-tracking branch 'origin/master' 9494207: 2013-09-04 Anna Kozlova - Revert: Fix all known "Field may be final" problems (IDEA-80439, IDEA-84561, IDEA-84614, IDEA-87373,IDEA-89711, IDEA-91969, IDEA-111396) (a3afcb0) 2c72731: 2013-09-04 Mikhail Golubev - Merge remote-tracking branch 'origin/master' 321e11e: 2013-09-04 Anna Kozlova - testdata updated 0622404: 2013-09-03 Anna Kozlova - method refs: ambiguity (IDEA-113078) a3afcb0: 2013-09-04 Bas Leijdekkers - Fix all known "Field may be final" problems (IDEA-80439, IDEA-84561, IDEA-84614, IDEA-87373,IDEA-89711, IDEA-91969, IDEA-111396) d1f8908: 2013-09-04 Vladislav.Soroka - IDEA-50450 Gradle code insight 071903a: 2013-09-04 nik - compilation fixed cc76e57: 2013-09-04 nik - xdebugger api: method intended for internal use only removed from interface dc939ea: 2013-09-04 nik - xdebugger api: constant moved to proper class 3b928f9: 2013-09-04 nik - deprecated XNamedValue class from inappropriate package, usages migrated to use proper class e34bd1c: 2013-09-03 Max Medvedev - Groovy: inplace introduce parameter. initial a8f70f8: 2013-08-29 Max Medvedev - Groovy: initial in-place introduce constant 0ad9f02: 2013-09-04 nik - xdebugger api streamlined: unnecessary intermediate classes removed, XNamedValue moved to proper package adf5a40: 2013-09-04 Vladimir Krivosheev - deprecated getStreamProviders d273f7d: 2013-09-03 Bas Leijdekkers - "Move assignment to field declaration" intention CCE fix 5ce8e21: 2013-09-03 Konstantin Bulenkov - NPE fix 318ea89: 2013-09-03 Konstantin Bulenkov - add retina to statistics 6274f22: 2013-09-03 Mikhail Golubev - Remove annoying warning about unparsable date from RedmineRepository d1ccda6: 2013-09-03 Andrey Vlasovskikh - Merge remote-tracking branch 'origin/master' e410ff2: 2013-09-03 Mikhail Golubev - Add attempt to move issue to "Reopened" state if some error occur d062130: 2013-09-03 Vladislav.Soroka - IDEA-50450 Gradle code insight 73cf33f: 2013-09-03 peter - dfa: analyze inner class field initializers b87a393: 2013-09-03 peter - don't pass null dependencies to fold region cached values f770424: 2013-09-03 Mikhail Golubev - Add TaskBuilder helper class. Update repository integration tests accordingly 48ef9f7: 2013-09-03 Andrey Vlasovskikh - Merge branch 'python-fixes' bed7303: 2013-09-03 peter - don't pass null dependencies to fold region cached values b85f89b: 2013-09-03 peter - clear meta data at least when its target element changes 3bb5907: 2013-09-03 peter - dfa: analyze only top-level methods together with their nested classes 1d180f0: 2013-09-03 peter - copy constructor for MultiMap e1cdae4: 2013-09-03 peter - render lookup elements in a read action (EA-49032, some accessibility stuff may access it from non-EDT) 48ba699: 2013-09-03 Vladislav.Soroka - IDEA-50450 Gradle code insight 45008fd: 2013-09-03 nik - added assertions to check that module root type id can be properly stored 5aa3fe4: 2013-09-03 nik - support for resource directories: enable java-specific actions only for directories under source roots 21b9422: 2013-09-03 Konstantin Bulenkov - IDEA-113150 Balloon shadow looks bad under Retina 35755e9: 2013-09-03 Alexey Kudravtsev - cleanup 2c3ff99: 2013-09-03 Alexey Kudravtsev - tighten expectations 8cde32f: 2013-09-03 Alexey Kudravtsev - cleanup 5e53dd4: 2013-08-30 Alexey Kudravtsev - assertions cadf902: 2013-08-30 Alexey Kudravtsev - notnull, cleanup 50638b6: 2013-08-30 Alexey Kudravtsev - notnull d58ae6e: 2013-08-30 Alexey Kudravtsev - cleanup 60a92d1: 2013-08-30 Alexey Kudravtsev - annotations 52eefc3: 2013-08-30 Alexey Kudravtsev - cleanup 67791f7: 2013-08-29 Alexey Kudravtsev - notnull 76d2c5e: 2013-08-29 Alexey Kudravtsev - removed @Nullable from getVirtualFile() since it is almost never null 0b9b6e5: 2013-08-29 Alexey Kudravtsev - removed @Nullable from getVirtualFile() since it is almost never null a2e1b80: 2013-09-03 Mikhail Golubev - IDEA-112857 Cannot open task for JIRA server. Testing connection works. JiraRepository's GSON instance uses TaskUtil#parseDate to deserialize dates 8583a9b: 2013-09-03 Konstantin Kolosovsky - Merge branch 'svn1_8_new' e976ec1: 2013-09-03 Mikhail Golubev - Merge remote-tracking branch 'origin/master' 3077f6c: 2013-09-03 Konstantin Kolosovsky - IDEA-94942 Fixed info command for nonexistent file 8861d44: 2013-09-03 Mikhail Golubev - IDEA-112857 Cannot open task for JIRA server. Testing connection works. Update date parsing to accept single date as well 132a601: 2013-09-03 Anna Kozlova - Merge remote-tracking branch 'origin/master' a419785: 2013-09-03 Anna Kozlova - NPE 4b9b570: 2013-09-03 Andrey Vlasovskikh - Added ignore shadowed built-in quick-fix (PY-8672) 7a47af4: 2013-09-02 nik - simplification c0ffb7c: 2013-09-02 nik - base class extracted c17c545: 2013-09-02 nik - refined types of FileIndex to make easier searching for ModuleFileIndex specific methods 52a0af9: 2013-09-02 nik - custom source root types supported in DirectoryIndex f6ed90c: 2013-09-03 Dmitry Avdeev - getting rid of PsiSoftReferenceDecorator 0fe9c4d: 2013-09-03 Anna Kozlova - optimization: do not obtain text 391e125: 2013-09-03 peter - avoid passing null dependencies to cached value 372bdda: 2013-09-03 Dmitry Avdeev - TextRange going to be fair: javadocs d091163: 2013-09-03 Dmitry Avdeev - TextRange going to be fair: javadocs 897f497: 2013-09-03 peter - PsiPackageImpl.findClassByShortName should check containsClassNamed before linearly traversing all the package files f045a48: 2013-09-03 Anna Kozlova - optimization: avoid searching for all classes when non-strict mode is on 6423cde: 2013-09-03 Anna Kozlova - cleanup 662b874: 2013-09-03 Dmitry Avdeev - TextRange going to be fair 7bd0a96: 2013-09-03 Konstantin Bulenkov - IDEA-113130 Project View on Mac: text is invisible under selection 89c8508: 2013-09-03 Sergey Evdokimov - Reference inside <relativePath>../pom.xml</relativePath> refers to <xs:element name="relativePath" minOccurs="0" type="xs:string" default="../pom.xml"> instead of pom.xml Fix tests. 445d3c1: 2013-09-03 Konstantin Kolosovsky - IDEA-112803 90c4159: 2013-09-03 peter - avoid passing null dependencies to cached value d0a3be5: 2013-09-03 peter - IDEA-112526 com.intellij.openapi.util.text.StringUtil.join() variants have inconsistent behavior 4b4209b: 2013-09-03 Sascha Weinreuter - Merge remote-tracking branch 'origin/master' 670eb2e: 2013-09-03 Sergey Evdokimov - Reference inside <relativePath>../pom.xml</relativePath> refers to <xs:element name="relativePath" minOccurs="0" type="xs:string" default="../pom.xml"> instead of pom.xml +review CR-IC @Dmitriy.Avdeev c300c0d: 2013-09-03 Sascha Weinreuter - IDEA-113046: XSL autocompletion insert unnecessary elements 725c90b: 2013-09-03 Dmitry Avdeev - TextRange going to be fair 0e01c80: 2013-09-03 Konstantin Kolosovsky - IDEA-94942 Revert of missing directories fixed 1328456: 2013-09-03 Roman Shevchenko - EA-40505 (don't fail on malformed .zip files) 208d804: 2013-09-03 Dmitry Avdeev - TextRange going to be fair 8a528da: 2013-09-02 Vladimir Krivosheev - mark StreamProvider.saveContent content param as NotNull 920594a: 2013-09-02 Konstantin Kolosovsky - IDEA-112805 "Merge from" -> "Manual select", "Select with pre-filter" base implementation de6b1c3: 2013-09-02 peter - IDEA-112779 ISE at com.intellij.ide.util.gotoByName.ChooseByNameBase.getNames 510ccb2: 2013-09-02 Vladimir Krivosheev - extract guessProjectForContentFile 41a0645: 2013-09-02 Konstantin Bulenkov - IDEA-113071 File Colors feature doesn't work in ProjectView under Darcula 8dbc50c: 2013-09-02 peter - use not-null CachedValue dependencies b4c2616: 2013-09-02 Aleksey Pivovarov - Github: run in EDT 83ebb64: 2013-09-02 Aleksey Pivovarov - IDEA-112867 Github: show diff for remote branch b716a1e: 2013-09-02 Aleksey Pivovarov - Github: add util function 0064502: 2013-09-02 Aleksey Pivovarov - Github: add computeValueInModal without exceptions 8f54a42: 2013-09-01 Aleksey Pivovarov - Remove code duplication 1d75872: 2013-09-01 Aleksey Pivovarov - Revert GitHub WebBrowserUrlProvider extension 40fa73f: 2013-09-02 Anna Kozlova - Merge remote-tracking branch 'origin/master' 9d1857b: 2013-09-02 Anna Kozlova - cache scope in ref c6e41b5: 2013-09-02 Anna Kozlova - parameter info generics: check resulted html 83ec3ed: 2013-09-02 Bas Leijdekkers - Fix "Loop variable not updated inside loop" inspection the the presence of polyadic expressions and add test 7d52e17: 2013-09-02 Konstantin Bulenkov - fix Ctrl+Click 6414f18: 2013-09-02 Konstantin Bulenkov - get back (JTree)e.getSource 33c79a3: 2013-09-02 Konstantin Bulenkov - multi selection dnd f135b4e: 2013-09-02 peter - provide missing dependencies for various CachedValue-s 9564cda: 2013-09-02 peter - [^kb] collect os type and os version stats in separate groups 01d5b0e: 2013-09-02 Yann Cébron - do not return invalid DOM element b14a923: 2013-09-02 Roman Shevchenko - IDEA-112412 ("Internal Error" dialog usability) 721701a: 2013-09-02 Roman Shevchenko - Bootstrap dependencies lessened 0282729: 2013-09-02 Konstantin Bulenkov - UI info statistics 015c3dd: 2013-09-02 Konstantin Kolosovsky - IDEA-94942 Subversion change lists implementation bfb3a8b: 2013-09-02 Anna Kozlova - remove testdata copyright 32fa8b7: 2013-09-02 Anna Kozlova - support downloadUrl for custom plugin repositories (IDEA-112778) e06741a: 2013-09-02 Roman Shevchenko - IDEA-112276 (turn startup exceptions in AS to Google) dd37027: 2013-09-02 Anna Kozlova - EA-49390 - assert: WriteCommandAction.execute d29cb10: 2013-09-02 Anna Kozlova - EA-49373 - assert: PsiFileImpl.loadTreeElement f2dace3: 2013-09-02 Anna Kozlova - optimization (CR-IC-2032) 38c6a24: 2013-09-02 Anna Kozlova - method refs: mismatched collection query (IDEA-112981) 465edc7: 2013-09-02 Anna Kozlova - cleanup after review (++IDEA-112553) 751525b: 2013-09-02 Vladislav.Soroka - Merge remote-tracking branch 'origin/master' c28ebf2: 2013-09-02 Dmitry Avdeev - IDEA-112914 Insert closing tag after typing </ : fixing tests 601f184: 2013-09-02 Roman Shevchenko - Test fixed 4358f3f: 2013-08-30 Alex Ruiz - Updated to Gradle version that supports single-pass project import. c056544: 2013-09-02 Konstantin Kolosovsky - IDEA-112805 "Merge from" -> "Merge all" implementation for reintegrate merge 1d626b5: 2013-09-02 Konstantin Kolosovsky - IDEA-94942 Ability to handle subversion properties recursively 127bc3f: 2013-09-02 peter - don't warn when calling @VisibleForTesting method with no original visibility specified (IDEA-107730) 44011ff: 2013-09-02 peter - first letter case sensitivity should match pattern start with name start even in middle matches ec36ca8: 2013-09-01 Konstantin Bulenkov - TreePath bound shouldn't be changed. Roll back last two changes abe39e9: 2013-08-31 peter - use the new stub index key processing API for class name completion 8b100aa: 2013-08-31 Konstantin Bulenkov - avoid extra gradient on Mac (NavBar + Run Bar) c4ba846: 2013-08-31 Konstantin Bulenkov - support fully transparent combo (no border and bg) 15809e5: 2013-08-31 peter - suggest better-matching non-imported classes by default 3d0bb87: 2013-08-31 peter - don't duplicate expected type argument variant when showing non-imported classes 4135d12: 2013-08-31 peter - make tests more independent from all/imported classes behavior 0b3f5d3: 2013-08-31 peter - editors should only handle bulk changes of their own documents (otherwise main editor constantly scrolls during test run) 445acfb: 2013-08-31 peter - make tests more independent from all/imported classes behavior fa68185: 2013-08-30 peter - IDEA-112173 Smart completion after 'new' should honor upper bound of generic parameters aff844a: 2013-08-30 Eugene Kudelevsky - IDEA-112979 spellchecker inspection should be suppressed for symbols user cannot edit 584c732: 2013-08-30 Anna Kozlova - Merge branch 'master' of git.labs.intellij.net:idea/community f1df9e8: 2013-08-30 Dmitry Jemerov - packaging UI moved to lang-impl 48205f5: 2013-08-30 Anna Kozlova - IDEA-112562 Pull up should allow to pull method body into interface as default method ca6489e: 2013-08-30 Yann Cébron - + "tuple" b5ea526: 2013-08-30 Roman Shevchenko - NPE fixed d562eba: 2013-08-30 Roman Shevchenko - vfs: async refresh for .jar files (IDEA-112873) 68e340a: 2013-08-30 Roman Shevchenko - vfs: .jar refresh test 76fa4e7: 2013-08-30 Dmitry Trofimov - Merge remote-tracking branch 'origin/master' fd1a3b6: 2013-08-30 Dmitry Trofimov - Fixed short-cup processing (VIM-552). 5426bcf: 2013-08-30 Anna Kozlova - NPE 0510fad: 2013-08-30 Bas Leijdekkers - java.util.Calendar methods magically annotated (IDEA-112317) b59b96d: 2013-08-30 Anna Kozlova - hide ignored tests: do not treat test case with ignored and non-ignored tests as ignored (IDEA-112757) 4eeaa0a: 2013-08-30 Anna Kozlova - EA-49189 - assert: GotoDeclarationAction.chooseAmbiguousTarget 41a598b: 2013-08-30 Anna Kozlova - accept @docRoot in parameter documentation 46fb5e8: 2013-08-30 Denis Fokin - IDEA-35480 Per project/frame modality f146f92: 2013-08-30 Vladimir Krivosheev - overrides 344c895: 2013-08-30 Kirill Likhodedov - IDEA-112868 "Compare middle and left panel contents" swaps the files b7e8a2a: 2013-08-30 Konstantin Kolosovsky - IDEA-94942 Info client refactored Create branch/tag action implemented ef8db2c: 2013-08-30 Vladimir Krivosheev - don't allow set root as root 1bb97bb: 2013-08-30 Vladimir Krivosheev - show NaN or Infinity as KEYWORD instead of NUMBER ae52dd2: 2013-08-30 Bas Leijdekkers - fix testdata 403a77b: 2013-08-30 Vassiliy Kudryashov - IDEA-112669 Project View: "Wide selection" broken 97c8241: 2013-08-30 Vladimir Krivosheev - AddToWatches action should be available in the Inspect pop-up too a8fdca8: 2013-08-30 Vladimir Krivosheev - XInspectDialog — use XValueHintTreeComponent, now UI of this component closer to inspect functionality from the editor 61ad075: 2013-08-30 Vladimir Krivosheev - cleanup 67d92f5: 2013-08-30 Roman Shevchenko - vfs: null-tolerant property change event (OC-7947) ffe6785: 2013-08-30 Roman Shevchenko - Test cleanup dedb189: 2013-08-30 Sergey Simonchik - refresh if not valid f287c7c: 2013-08-30 Anna Kozlova - inspection settings: do not override stored settings when try to init from source profile (IDEA-112668) 1e303c6: 2013-08-30 Anna Kozlova - IDEA-112684 0ea95a6: 2013-08-30 Mikhail Golubev - Remove old version of Assembla connector 7959ff2: 2013-08-30 Mikhail Golubev - Update repositories integration tests efa295e: 2013-08-30 Mikhail Golubev - Merge remote-tracking branch 'origin/master' 4466d42: 2013-08-30 Dmitry Avdeev - TypeOrElementOrAttributeReference extracted e310a22: 2013-08-30 nik - more usages of getSourceContentRoots corrected to fetch only java roots where needed 0d7f837: 2013-08-30 Alexander Zolotov - RUBY-14163 "p" Ruby live template expands to "params" in ERB files 5f62d94: 2013-08-30 Dmitry Avdeev - cleanup c9d4712: 2013-08-30 Dmitry Avdeev - cleanup 368636d: 2013-08-30 Vladislav.Soroka - IDEA-50450 Gradle code insight. 'configurations' resolving added 6ec1856: 2013-08-30 Vladimir Krivosheev - add "Collecting" message only if computation is not yet done cafb4a9: 2013-08-30 Vladimir Krivosheev - remove deprecated method c7c8f30: 2013-08-30 Vladimir Krivosheev - cleanup ca42bab: 2013-08-30 Dmitry Avdeev - @NotNull b100cb3: 2013-08-30 Dmitry Avdeev - IDEA-112914 Insert closing tag after typing </ 6dc07e7: 2013-08-30 Dmitry Avdeev - diagnostics for EA-48275 - SIOOBE: TextRange.substring 8091683: 2013-08-30 Dmitry Avdeev - cleanup c7ddea2: 2013-08-30 Bas Leijdekkers - enable "Ignore named constants in determining pointless expressions" option by default (IDEA-112864) 84f8dd7: 2013-08-30 Vladimir Krivosheev - correct XGroupingValue package 256e07b: 2013-08-30 Vladimir Krivosheev - move ObsolescentAsyncResults to xdebugger 7c56a37: 2013-08-30 Anna Kozlova - create var from instanceOf check: do not insert before previous line end-line comment 7d71ded: 2013-08-30 Bas Leijdekkers - EA-49286 (CCE: SimplifiableIfStatementInspection.buildNegatedExpressionText) 7bbb306: 2013-08-30 Sergey Evdokimov - Exclude target/work/* expect target/work/plugins 47a0044: 2013-08-30 Mikhail Golubev - Update JIRA integration tests: add new one to test status changing capability bdf2e58: 2013-08-30 Sergey Evdokimov - IDEA-112910 Grails advanced mode 48df8f8: 2013-08-30 nik - unused class deleted b56c3aa: 2013-08-30 nik - unused code removed 4f49ed7: 2013-08-30 nik - more usages of getSourceRoots corrected to fetch only java or only resource roots where needed 0b234d4: 2013-08-30 Anna Kozlova - method refs: perform highlighting checks on valid refs only 7584749: 2013-08-30 Anna Kozlova - method refs: accept 2 phase acceptance (static and receiver based), raise 'staticAccess' errors at highlighting level 8cb40bf: 2013-08-30 nik - notnullified 713a474: 2013-08-30 nik - added methods to get module source roots of different types; exclude resource roots from processing where needed ec7ee74: 2013-08-29 nik - don't treat deep inner classes names in run configurations as path variables 673a512: 2013-08-29 nik - module source roots: show correct icon for custom root types 19d4ebf: 2013-08-29 nik - NPE fixed 72c4726: 2013-08-28 nik - 'equals' for source folders fixed 4da8e26: 2013-08-28 nik - added 'Mark Directory as Root' actions for resource roots d0d47a5: 2013-08-28 nik - cleanup 345eb19: 2013-08-28 nik - mark as root action: obtain action presentation from root type fff4ee8: 2013-08-28 nik - resource roots supported for java modules (IDEA-71324) 14fa2eb: 2013-08-30 Dmitry Avdeev - IDEA-112885 Task management: Open Task makes no effect f2e34ac: 2013-08-30 Bas Leijdekkers - IDEA-112813 (Chain of instanceof checks: Support if/if/if/... chains) 52a714a: 2013-08-30 Aleksey Pivovarov - IDEA-112823 Github: use inherited URL TextField 6efccd8: 2013-08-30 Aleksey Pivovarov - Github: fix initialization order in copy constructor 91f93c0: 2013-08-29 Aleksey Pivovarov - Github: split function 9116ba2: 2013-08-29 Aleksey Pivovarov - Github: Simplify ecc4302: 2013-08-29 Aleksey Pivovarov - Github: strict isConfigured() 270c7b2: 2013-08-29 Bas Leijdekkers - IDEA-112755 ("JUnit test method contains no assertions" for hamcrest-based assertions) 44de4db: 2013-08-29 Bas Leijdekkers - IDEA-112753 ("JUnit test method contains no assertions" detailed description mentions missing table) e0480b7: 2013-08-29 Bas Leijdekkers - try to fix hard to reproduce NoSuchFieldException (IDEA-112678) d4f214a: 2013-08-29 Yann Cébron - IDEA-112700 Devkit: highlight usage of deprecated EPs 484fb86: 2013-08-29 Yann Cébron - DevKit: move test data to proper place e936fb2: 2013-08-29 Konstantin Kolosovsky - IDEA-94942 Updated SvnKit vs CommandLine resolution strategy c735071: 2013-08-29 Nadya Zabrodina - parsing tests added for log command and moved to separate test class 493eb87: 2013-08-29 Nadya Zabrodina - Template for old hg version added (without join function) 8cbe8e7: 2013-08-29 Nadya Zabrodina - HgVersion changed: use only 1 digit as minor version component de9bccd: 2013-08-29 Konstantin Kolosovsky - IDEA-112869 9f41ec2: 2013-08-29 Sergey Evdokimov - Add registry key 'grails_griffon.view' 6606b34: 2013-08-29 Alexey Kudravtsev - Merge remote-tracking branch 'origin/master' 3438be5: 2013-08-29 peter - IDEA-112329 IDE Fatal Errors dialog: Details tab can't be scrolled e130d1d: 2013-08-29 peter - IDE errors dialog: use better-looking JTextArea (IDEA-109834) 4b0b915: 2013-08-29 Alexey Kudravtsev - compilation dcb5208: 2013-08-29 Alexey Kudravtsev - notnull 48d2b9c: 2013-08-29 Alexey Kudravtsev - notnull 808f5ae: 2013-08-29 Alexey Kudravtsev - notnull b096e0c: 2013-08-29 Alexey Kudravtsev - notnull f988efe: 2013-08-29 Alexey Kudravtsev - notnull 029295f: 2013-08-29 Alexey Kudravtsev - moved to java-psi fed5eeb: 2013-08-29 Alexey Kudravtsev - notnull 6eff0fb: 2013-08-29 Alexey Kudravtsev - notnull a2a52b6: 2013-08-29 Konstantin Kolosovsky - IDEA-94942 Viewing subversion properties for item IDEA-112719 ecb474d: 2013-08-29 Mikhail Golubev - IDEA-112828 NPE when trying to close JIRA issue 0c8b65d: 2013-08-29 Roman Shevchenko - vfs: symlink target update w/o file recreation (OC-7947) 34c43fe: 2013-08-29 Roman Shevchenko - Cleanup 46e06f7: 2013-08-29 Vladimir Krivosheev - prefer doWhenDone(@NotNull final Consumer<T> consumer) e409a49: 2013-08-29 Vladimir Krivosheev - deprecate doWhenRejected(@NotNull final Handler<T> handler) 6e84f22: 2013-08-29 Vladislav.Soroka - IDEA-112865 Gradle: completion and quick-docs for standard gradle plugins 6a10552: 2013-08-29 Vladimir Krivosheev - WEB-8064 Debugger: Pause Program button is not working actionCallback — pass error message to child b8c9cdd: 2013-08-29 peter - test paste single tabbed string into block selection 2411065: 2013-08-29 Yann Cébron - Dom EPs: revert <with> for "dom.converter" (breaks for CustomReferenceConverter) c376d34: 2013-08-29 Dmitry Avdeev - EA-48655 - IAE: XmlMover.checkAvailable 7cb078e: 2013-08-29 Dmitry Avdeev - EA-48877 - NPE: GenerateByPatternDialog.update 3046258: 2013-08-29 Fedor Korotkov - Reverted a8992f523de8898: Fixed performance issue. 28a10e0: 2013-08-29 Vladimir Krivosheev - EditorTabPopupMenuEx — add separator between RunContextPopupGroup and VersionControlsGroup (the same as in the EditorPopupMenu) add OpenInBrowser to editor tab popup menu, move it to RunContextPopupGroup disable GithubWebBrowserUrlProvider until purpose of this implementation is not clear b7bc081: 2013-08-29 Max Medvedev - introduce constant to a script 2a3ccbb: 2013-08-29 Max Medvedev - choose target class for introduce constant 5750c71: 2013-08-29 Sergey Evdokimov - EA-49076 - assert: ComponentManagerImpl.getPicoContainer 5f56145: 2013-08-28 Mikhail Golubev - Slightly update JQL lexer rules ef0f8ed: 2013-08-28 Konstantin Kolosovsky - IDEA-94942 Parsing "svn status" output with remote changes IDEA-112729 Change-Id: Icbc9b8c16a0c7766bd64993999b5905e8cd24257
Diffstat (limited to 'java/java-impl/src/com')
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/ExpectedTypesProvider.java5
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/TargetElementUtil.java28
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/completion/JavaNoVariantsDelegator.java18
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/completion/JavaSmartCompletionContributor.java24
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/completion/LambdaCompletionProvider.java1
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/completion/TypeArgumentCompletionProvider.java26
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/completion/methodChains/completion/context/ContextUtil.java9
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaHighlightInfoTypes.java35
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/PostHighlightingPass.java46
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/RefCountHolder.java335
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java747
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java1295
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightClassUtil.java1030
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightControlFlowUtil.java750
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java1405
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightNamesUtil.java305
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java2755
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java1356
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/IncreaseLanguageLevelFix.java112
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/QualifyWithThisFix.java66
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddMethodBodyFix.java11
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddReturnFix.java11
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddTypeArgumentsConditionalFix.java153
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddTypeArgumentsFix.java113
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddTypeCastFix.java127
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddVariableInitializerFix.java11
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ArgumentFixerActionFactory.java116
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CastMethodArgumentFix.java74
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeExtendsToImplementsFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageFix.java38
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageReverseOrderFix.java12
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeNewOperatorTypeFix.java203
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeParameterClassFix.java51
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeStringLiteralToCharInMethodCallFix.java176
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeTypeArgumentsFix.java169
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConstructorParametersFixer.java47
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertDoubleToFloatFix.java115
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAbstractMethodFromUsageFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAnnotationMethodFromUsageFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromCallFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromSuperFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromThisFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorMatchingSuperFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateGetterSetterPropertyFromUsageFix.java3
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateLocalVarFromInstanceofAction.java8
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateMethodFromUsageFix.java22
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreatePropertyFromUsageFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DefaultQuickFixProvider.java4
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeferFinalAssignmentFix.java22
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteMethodBodyFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ExtendsListFix.java177
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImplementAbstractClassMethodsFix.java113
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImportClassFixBase.java7
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/InsertNewFix.java6
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/InsertSuperFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MakeVarargParameterLastFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MethodArgumentFix.java84
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveBoundClassToFrontFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveClassToModuleFix.java8
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveClassToSeparateFileFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/PermuteArgumentsFix.java193
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/PullAsAbstractUpFix.java12
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/QualifyThisArgumentFix.java121
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveNewQualifierFix.java7
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveRedundantArgumentsFix.java132
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveTypeArgumentsFix.java2
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceAddAllArrayToCollectionFix.java4
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceInaccessibleFieldWithGetterSetterFix.java32
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplacePrimitiveWithBoxedTypeAction.java (renamed from java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/ReplacePrimitiveWithBoxedTypeAction.java)6
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SuperMethodReturnFix.java8
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithArrayFix.java12
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableAccessFromInnerClassFix.java16
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableParameterizedTypeFix.java60
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableTypeFromCallFix.java52
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/WrapExpressionFix.java175
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/daemon/quickFix/CreateClassOrPackageFix.java4
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/hint/api/impls/MethodParameterInfoHandler.java11
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/intention/impl/CreateClassDialog.java3
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/intention/impl/MoveFieldAssignmentToInitializerAction.java7
-rw-r--r--java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java403
-rw-r--r--java/java-impl/src/com/intellij/codeInspection/SuppressManagerImpl.java14
-rw-r--r--java/java-impl/src/com/intellij/ide/actions/JavaCreateTemplateInPackageAction.java3
-rw-r--r--java/java-impl/src/com/intellij/ide/highlighter/JavaHighlightingColors.java49
-rw-r--r--java/java-impl/src/com/intellij/ide/navigationToolbar/JavaNavBarExtension.java3
-rw-r--r--java/java-impl/src/com/intellij/ide/projectView/impl/nodes/PackageViewProjectNode.java6
-rw-r--r--java/java-impl/src/com/intellij/ide/util/PackageChooserDialog.java3
-rw-r--r--java/java-impl/src/com/intellij/ide/util/PackageUtil.java13
-rw-r--r--java/java-impl/src/com/intellij/ide/util/gotoByName/DefaultClassNavigationContributor.java2
-rw-r--r--java/java-impl/src/com/intellij/javadoc/JavadocConfiguration.java7
-rw-r--r--java/java-impl/src/com/intellij/lang/java/JavaDocumentationProvider.java19
-rw-r--r--java/java-impl/src/com/intellij/openapi/module/JavaModuleType.java3
-rw-r--r--java/java-impl/src/com/intellij/psi/impl/JavaDirectoryIconProvider.java10
-rw-r--r--java/java-impl/src/com/intellij/psi/impl/PackagePrefixIndex.java3
-rw-r--r--java/java-impl/src/com/intellij/psi/impl/file/PsiPackageImplementationHelperImpl.java12
-rw-r--r--java/java-impl/src/com/intellij/psi/scope/processor/VariablesNotProcessor.java41
-rw-r--r--java/java-impl/src/com/intellij/psi/scope/processor/VariablesProcessor.java90
-rw-r--r--java/java-impl/src/com/intellij/psi/util/CreateClassUtil.java60
-rw-r--r--java/java-impl/src/com/intellij/refactoring/copy/CopyClassDialog.java3
-rw-r--r--java/java-impl/src/com/intellij/refactoring/extractSuperclass/JavaExtractSuperBaseDialog.java5
-rw-r--r--java/java-impl/src/com/intellij/refactoring/extractclass/ExtractClassDialog.java4
-rw-r--r--java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpDialog.java15
-rw-r--r--java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpHelper.java14
-rw-r--r--java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/DestinationFolderComboBox.java10
-rw-r--r--java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/JavaMoveClassesOrPackagesHandler.java3
-rw-r--r--java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesDialog.java14
-rw-r--r--java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesImpl.java3
-rw-r--r--java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesUtil.java16
-rw-r--r--java/java-impl/src/com/intellij/refactoring/move/moveInner/MoveInnerDialog.java8
-rw-r--r--java/java-impl/src/com/intellij/refactoring/typeMigration/ui/MigrationPanel.java50
-rw-r--r--java/java-impl/src/com/intellij/refactoring/util/RefactoringUtil.java19
-rw-r--r--java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestAction.java16
-rw-r--r--java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestDialog.java34
112 files changed, 933 insertions, 13134 deletions
diff --git a/java/java-impl/src/com/intellij/codeInsight/ExpectedTypesProvider.java b/java/java-impl/src/com/intellij/codeInsight/ExpectedTypesProvider.java
index 89b606e85324..7c8e4a0e5ae5 100644
--- a/java/java-impl/src/com/intellij/codeInsight/ExpectedTypesProvider.java
+++ b/java/java-impl/src/com/intellij/codeInsight/ExpectedTypesProvider.java
@@ -282,7 +282,10 @@ public class ExpectedTypesProvider {
}
@Override public void visitAnnotationArrayInitializer(@NotNull PsiArrayInitializerMemberValue initializer) {
- final PsiElement parent = initializer.getParent();
+ PsiElement parent = initializer.getParent();
+ while (parent instanceof PsiArrayInitializerMemberValue) {
+ parent = parent.getParent();
+ }
final PsiType type;
if (parent instanceof PsiNameValuePair) {
type = getAnnotationMethodType((PsiNameValuePair)parent);
diff --git a/java/java-impl/src/com/intellij/codeInsight/TargetElementUtil.java b/java/java-impl/src/com/intellij/codeInsight/TargetElementUtil.java
index a95d4985b12e..8e9b0b2ae484 100644
--- a/java/java-impl/src/com/intellij/codeInsight/TargetElementUtil.java
+++ b/java/java-impl/src/com/intellij/codeInsight/TargetElementUtil.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2009 JetBrains s.r.o.
+ * Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,13 +15,11 @@
*/
package com.intellij.codeInsight;
-import com.intellij.ide.IdeBundle;
import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.Computable;
import com.intellij.psi.*;
-import com.intellij.psi.presentation.java.ClassPresentationUtil;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.ClassInheritorsSearch;
@@ -309,22 +307,22 @@ public class TargetElementUtil extends TargetElementUtilBase {
if (referenceExpression != null && element instanceof PsiMethod) {
final PsiClass[] memberClass = getMemberClass(referenceExpression, element);
if (memberClass != null && memberClass.length == 1) {
- final List<PsiClass> classesToSearch = new ArrayList<PsiClass>();
- classesToSearch.addAll(ClassInheritorsSearch.search(memberClass[0], true).findAll());
-
- final Set<PsiClass> supers = new HashSet<PsiClass>();
- for (PsiClass psiClass : classesToSearch) {
- supers.addAll(InheritanceUtil.getSuperClasses(psiClass));
- }
- classesToSearch.addAll(supers);
-
- return CachedValuesManager.getManager(element.getProject()).createCachedValue(new CachedValueProvider<SearchScope>() {
+ return CachedValuesManager.getManager(element.getProject()).getCachedValue(referenceExpression, new CachedValueProvider<SearchScope>() {
@Nullable
@Override
public Result<SearchScope> compute() {
- return new Result<SearchScope>(new LocalSearchScope(PsiUtilCore.toPsiElementArray(classesToSearch)));
+ final List<PsiClass> classesToSearch = new ArrayList<PsiClass>();
+ classesToSearch.addAll(ClassInheritorsSearch.search(memberClass[0], true).findAll());
+
+ final Set<PsiClass> supers = new HashSet<PsiClass>();
+ for (PsiClass psiClass : classesToSearch) {
+ supers.addAll(InheritanceUtil.getSuperClasses(psiClass));
+ }
+ classesToSearch.addAll(supers);
+
+ return new Result<SearchScope>(new LocalSearchScope(PsiUtilCore.toPsiElementArray(classesToSearch)), PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT);
}
- }, false).getValue();
+ });
}
}
return super.getSearchScope(editor, element);
diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaNoVariantsDelegator.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaNoVariantsDelegator.java
index c28c80767326..ad2989f1dac4 100644
--- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaNoVariantsDelegator.java
+++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaNoVariantsDelegator.java
@@ -27,6 +27,7 @@ import com.intellij.psi.filters.ElementFilter;
import com.intellij.psi.search.PsiShortNamesCache;
import com.intellij.util.CollectConsumer;
import com.intellij.util.Consumer;
+import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.LinkedHashSet;
@@ -58,7 +59,14 @@ public class JavaNoVariantsDelegator extends CompletionContributor {
JavaCompletionContributor.isClassNamePossible(parameters) &&
!JavaSmartCompletionContributor.AFTER_NEW.accepts(parameters.getPosition())) {
result = result.withPrefixMatcher(new BetterPrefixMatcher(result.getPrefixMatcher(), BetterPrefixMatcher.getBestMatchingDegree(plainResults)));
- suggestNonImportedClasses(parameters, result);
+ InheritorsHolder holder = new InheritorsHolder(parameters.getPosition(), result);
+ for (CompletionResult plainResult : plainResults) {
+ LookupElement element = plainResult.getLookupElement();
+ if (element instanceof TypeArgumentCompletionProvider.TypeArgsLookupElement) {
+ ((TypeArgumentCompletionProvider.TypeArgsLookupElement)element).registerSingleClass(holder);
+ }
+ }
+ suggestNonImportedClasses(parameters, JavaCompletionSorting.addJavaSorting(parameters, result), holder);
}
}
}
@@ -86,7 +94,7 @@ public class JavaNoVariantsDelegator extends CompletionContributor {
if (parameters.getInvocationCount() <= 1 &&
JavaCompletionContributor.mayStartClassName(result) &&
JavaCompletionContributor.isClassNamePossible(parameters)) {
- suggestNonImportedClasses(parameters, result);
+ suggestNonImportedClasses(parameters, result, null);
return;
}
@@ -169,12 +177,14 @@ public class JavaNoVariantsDelegator extends CompletionContributor {
return allClasses;
}
- private static void suggestNonImportedClasses(CompletionParameters parameters, final CompletionResultSet _result) {
- final CompletionResultSet result = JavaCompletionSorting.addJavaSorting(parameters, _result);
+ private static void suggestNonImportedClasses(CompletionParameters parameters, final CompletionResultSet result, @Nullable final InheritorsHolder inheritorsHolder) {
JavaClassNameCompletionContributor.addAllClasses(parameters,
true, result.getPrefixMatcher(), new Consumer<LookupElement>() {
@Override
public void consume(LookupElement element) {
+ if (inheritorsHolder != null && inheritorsHolder.alreadyProcessed(element)) {
+ return;
+ }
JavaPsiClassReferenceElement classElement = element.as(JavaPsiClassReferenceElement.CLASS_CONDITION_KEY);
if (classElement != null) {
classElement.setAutoCompletionPolicy(AutoCompletionPolicy.NEVER_AUTOCOMPLETE);
diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaSmartCompletionContributor.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaSmartCompletionContributor.java
index ac752ed54ac1..cf58f11b81da 100644
--- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaSmartCompletionContributor.java
+++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaSmartCompletionContributor.java
@@ -19,10 +19,7 @@ import com.intellij.codeInsight.*;
import com.intellij.codeInsight.completion.scope.JavaCompletionProcessor;
import com.intellij.codeInsight.lookup.*;
import com.intellij.openapi.util.Key;
-import com.intellij.patterns.ElementPattern;
-import com.intellij.patterns.ElementPatternCondition;
-import com.intellij.patterns.PsiElementPattern;
-import com.intellij.patterns.PsiJavaPatterns;
+import com.intellij.patterns.*;
import com.intellij.psi.*;
import com.intellij.psi.filters.ElementExtractorFilter;
import com.intellij.psi.filters.ElementFilter;
@@ -94,21 +91,12 @@ public class JavaSmartCompletionContributor extends CompletionContributor {
psiElement().withText(")").withParent(PsiTypeCastExpression.class)));
static final PsiElementPattern.Capture<PsiElement> IN_TYPE_ARGS =
psiElement().inside(psiElement(PsiReferenceParameterList.class));
- static final PsiElementPattern.Capture<PsiElement> LAMBDA = psiElement().and(new FilterPattern(new ElementFilter() {
+ static final PsiElementPattern.Capture<PsiElement> LAMBDA = psiElement().with(new PatternCondition<PsiElement>("LAMBDA_CONTEXT") {
@Override
- public boolean isAcceptable(Object element, @Nullable PsiElement context) {
- if (context == null) return false;
- final PsiElement originalElement = context.getOriginalElement();
- if (originalElement == null) return false;
- final PsiElement rulezzRef = originalElement.getParent();
- return LambdaUtil.isValidLambdaContext(rulezzRef.getParent());
- }
-
- @Override
- public boolean isClassAcceptable(Class hintClass) {
- return true;
- }
- }));
+ public boolean accepts(@NotNull PsiElement element, ProcessingContext context) {
+ final PsiElement rulezzRef = element.getParent();
+ return rulezzRef != null && LambdaUtil.isValidLambdaContext(rulezzRef.getParent());
+ }});
@Nullable
private static ElementFilter getReferenceFilter(PsiElement element) {
diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/LambdaCompletionProvider.java b/java/java-impl/src/com/intellij/codeInsight/completion/LambdaCompletionProvider.java
index eca5453c20f8..8073d1b6a270 100644
--- a/java/java-impl/src/com/intellij/codeInsight/completion/LambdaCompletionProvider.java
+++ b/java/java-impl/src/com/intellij/codeInsight/completion/LambdaCompletionProvider.java
@@ -56,7 +56,6 @@ public class LambdaCompletionProvider extends CompletionProvider<CompletionParam
public void handleInsert(InsertionContext context, LookupElement item) {
final Editor editor = context.getEditor();
EditorModificationUtil.insertStringAtCaret(editor, " -> ");
- PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument());
}
});
result.addElement(builder.withAutoCompletionPolicy(AutoCompletionPolicy.NEVER_AUTOCOMPLETE));
diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/TypeArgumentCompletionProvider.java b/java/java-impl/src/com/intellij/codeInsight/completion/TypeArgumentCompletionProvider.java
index 5411a24b4cd9..0fbcb1eee360 100644
--- a/java/java-impl/src/com/intellij/codeInsight/completion/TypeArgumentCompletionProvider.java
+++ b/java/java-impl/src/com/intellij/codeInsight/completion/TypeArgumentCompletionProvider.java
@@ -108,15 +108,10 @@ class TypeArgumentCompletionProvider extends CompletionProvider<CompletionParame
typeItems.add(PsiTypeLookupItem.createLookupItem(arg, context));
}
- if (typeItems.size() == 1 && myInheritors != null) {
- PsiClass aClass = PsiUtil.resolveClassInClassTypeOnly(typeItems.get(0).getPsiType());
- if (aClass != null) {
- JavaCompletionUtil.setShowFQN(typeItems.get(0));
- myInheritors.registerClass(aClass);
- }
- }
-
- resultSet.addElement(new TypeArgsLookupElement(typeItems, globalTail, ConstructorInsertHandler.hasConstructorParameters(actualClass, context)));
+ boolean hasParameters = ConstructorInsertHandler.hasConstructorParameters(actualClass, context);
+ TypeArgsLookupElement element = new TypeArgsLookupElement(typeItems, globalTail, hasParameters);
+ element.registerSingleClass(myInheritors);
+ resultSet.addElement(element);
}
@Nullable
@@ -193,7 +188,7 @@ class TypeArgumentCompletionProvider extends CompletionProvider<CompletionParame
return Pair.create(referencedClass, parameterIndex);
}
- private static class TypeArgsLookupElement extends LookupElement {
+ public static class TypeArgsLookupElement extends LookupElement {
private String myLookupString;
private final List<PsiTypeLookupItem> myTypeItems;
private final TailType myGlobalTail;
@@ -217,6 +212,17 @@ class TypeArgumentCompletionProvider extends CompletionProvider<CompletionParame
return myTypeItems.get(0).getObject();
}
+ public void registerSingleClass(@Nullable InheritorsHolder inheritors) {
+ if (inheritors != null && myTypeItems.size() == 1) {
+ PsiType type = myTypeItems.get(0).getPsiType();
+ PsiClass aClass = PsiUtil.resolveClassInClassTypeOnly(type);
+ if (aClass != null && !aClass.hasTypeParameters()) {
+ JavaCompletionUtil.setShowFQN(myTypeItems.get(0));
+ inheritors.registerClass(aClass);
+ }
+ }
+ }
+
@NotNull
@Override
public String getLookupString() {
diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/methodChains/completion/context/ContextUtil.java b/java/java-impl/src/com/intellij/codeInsight/completion/methodChains/completion/context/ContextUtil.java
index 481c5f87ac63..da17e4d659e2 100644
--- a/java/java-impl/src/com/intellij/codeInsight/completion/methodChains/completion/context/ContextUtil.java
+++ b/java/java-impl/src/com/intellij/codeInsight/completion/methodChains/completion/context/ContextUtil.java
@@ -175,9 +175,12 @@ public class ContextUtil {
final Map<String, PsiVariable> stringVars = new HashMap<String, PsiVariable>();
for (final PsiMethod method : contextMethods) {
- final String returnTypeQName = method.getReturnType().getCanonicalText();
- if (returnTypeQName != null) {
- containingClassGetters.putValue(returnTypeQName, method);
+ PsiType returnType = method.getReturnType();
+ if (returnType != null) {
+ final String returnTypeQName = returnType.getCanonicalText();
+ if (returnTypeQName != null) {
+ containingClassGetters.putValue(returnTypeQName, method);
+ }
}
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaHighlightInfoTypes.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaHighlightInfoTypes.java
deleted file mode 100644
index 5aa830993cb2..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaHighlightInfoTypes.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl;
-
-import com.intellij.codeInsight.daemon.HighlightDisplayKey;
-import com.intellij.codeInspection.unusedImport.UnusedImportLocalInspection;
-import com.intellij.ide.highlighter.JavaHighlightingColors;
-import com.intellij.lang.annotation.HighlightSeverity;
-import com.intellij.openapi.editor.SyntaxHighlighterColors;
-import com.intellij.openapi.editor.colors.CodeInsightColors;
-
-/**
- * @author anna
- * Date: 01-Feb-2008
- */
-public interface JavaHighlightInfoTypes extends HighlightInfoType {
- HighlightInfoType UNUSED_IMPORT = new HighlightInfoType.HighlightInfoTypeSeverityByKey(
- HighlightDisplayKey.findOrRegister(UnusedImportLocalInspection.SHORT_NAME, UnusedImportLocalInspection.DISPLAY_NAME),
- CodeInsightColors.NOT_USED_ELEMENT_ATTRIBUTES);
-
- HighlightInfoType JAVA_KEYWORD = new HighlightInfoType.HighlightInfoTypeImpl(HighlightSeverity.INFORMATION, JavaHighlightingColors.KEYWORD);
-} \ No newline at end of file
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/PostHighlightingPass.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/PostHighlightingPass.java
index 8af7f8bd9c53..97629fbaff71 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/PostHighlightingPass.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/PostHighlightingPass.java
@@ -18,10 +18,7 @@ package com.intellij.codeInsight.daemon.impl;
import com.intellij.codeHighlighting.Pass;
import com.intellij.codeHighlighting.TextEditorHighlightingPass;
import com.intellij.codeInsight.CodeInsightSettings;
-import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
-import com.intellij.codeInsight.daemon.HighlightDisplayKey;
-import com.intellij.codeInsight.daemon.ImplicitUsageProvider;
-import com.intellij.codeInsight.daemon.JavaErrorMessages;
+import com.intellij.codeInsight.daemon.*;
import com.intellij.codeInsight.daemon.impl.analysis.*;
import com.intellij.codeInsight.daemon.impl.quickfix.*;
import com.intellij.codeInsight.intention.EmptyIntentionAction;
@@ -29,7 +26,6 @@ import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.IntentionManager;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection;
-import com.intellij.codeInspection.ex.InspectionManagerEx;
import com.intellij.codeInspection.reference.UnusedDeclarationFixProvider;
import com.intellij.codeInspection.unusedImport.UnusedImportLocalInspection;
import com.intellij.codeInspection.unusedParameters.UnusedParametersInspection;
@@ -57,6 +53,7 @@ import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.pom.PomNamedTarget;
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
@@ -123,15 +120,15 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
@Override
public void doCollectInformation(@NotNull final ProgressIndicator progress) {
- DaemonCodeAnalyzer daemonCodeAnalyzer = DaemonCodeAnalyzer.getInstance(myProject);
- final FileStatusMap fileStatusMap = ((DaemonCodeAnalyzerImpl)daemonCodeAnalyzer).getFileStatusMap();
+ DaemonCodeAnalyzerEx daemonCodeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(myProject);
+ final FileStatusMap fileStatusMap = daemonCodeAnalyzer.getFileStatusMap();
final List<HighlightInfo> highlights = new ArrayList<HighlightInfo>();
final FileViewProvider viewProvider = myFile.getViewProvider();
final Set<Language> relevantLanguages = viewProvider.getLanguages();
final Set<PsiElement> elementSet = new THashSet<PsiElement>();
for (Language language : relevantLanguages) {
PsiElement psiRoot = viewProvider.getPsi(language);
- if (!HighlightLevelUtil.shouldHighlight(psiRoot)) continue;
+ if (!HighlightingLevelManager.getInstance(myProject).shouldHighlight(psiRoot)) continue;
List<PsiElement> elements = CollectHighlightsUtil.getElementsInRange(psiRoot, myStartOffset, myEndOffset);
elementSet.addAll(elements);
}
@@ -282,7 +279,7 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
}
}
}
- if (unusedImportEnabled && myFile instanceof PsiJavaFile && HighlightLevelUtil.shouldHighlight(myFile)) {
+ if (unusedImportEnabled && myFile instanceof PsiJavaFile && HighlightingLevelManager.getInstance(myProject).shouldHighlight(myFile)) {
PsiImportList importList = ((PsiJavaFile)myFile).getImportList();
if (importList != null) {
final PsiImportStatementBase[] imports = importList.getAllImportStatements();
@@ -302,7 +299,7 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
@Nullable
private HighlightInfo processIdentifier(PsiIdentifier identifier, ProgressIndicator progress, GlobalUsageHelper helper) {
- if (InspectionManagerEx.inspectionResultSuppressed(identifier, myUnusedSymbolInspection)) return null;
+ if (SuppressionUtil.inspectionResultSuppressed(identifier, myUnusedSymbolInspection)) return null;
PsiElement parent = identifier.getParent();
if (PsiUtilCore.hasErrorElementChild(parent)) return null;
@@ -504,7 +501,7 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
options.addAll(IntentionManager.getInstance().getStandardIntentionOptions(myUnusedSymbolKey, myFile));
if (myUnusedParametersInspection != null) {
SuppressQuickFix[] batchSuppressActions = myUnusedParametersInspection.getBatchSuppressActions(parameter);
- Collections.addAll(options, SuppressManagerImpl.convertBatchToSuppressIntentionActions(batchSuppressActions));
+ Collections.addAll(options, SuppressIntentionActionFromFix.convertBatchToSuppressIntentionActions(batchSuppressActions));
}
//need suppress from Unused Parameters but settings from Unused Symbol
QuickFixAction.registerQuickFixAction(highlightInfo, new RemoveUnusedParameterFix(parameter),
@@ -764,12 +761,12 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
PsiJavaCodeReferenceElement reference = importStatement.getImportReference();
PsiElement resolved = reference == null ? null : reference.resolve();
if (resolved instanceof PsiPackage) {
- isRedundant = packageName.equals(((PsiPackage)resolved).getQualifiedName());
+ isRedundant = packageName.equals(((PsiQualifiedNamedElement)resolved).getQualifiedName());
}
else if (resolved instanceof PsiClass && !importStatement.isOnDemand()) {
String qName = ((PsiClass)resolved).getQualifiedName();
if (qName != null) {
- String name = ((PsiClass)resolved).getName();
+ String name = ((PomNamedTarget)resolved).getName();
isRedundant = qName.equals(packageName + '.' + name);
}
}
@@ -803,7 +800,7 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
private boolean timeToOptimizeImports() {
if (!CodeInsightSettings.getInstance().OPTIMIZE_IMPORTS_ON_THE_FLY) return false;
- DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(myProject);
+ DaemonCodeAnalyzerEx codeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(myProject);
PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(myDocument);
// dont optimize out imports in JSP since it can be included in other JSP
if (file == null || !codeAnalyzer.isHighlightingAvailable(file) || !(file instanceof PsiJavaFile) || file instanceof ServerPageFile) return false;
@@ -811,22 +808,23 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
if (!codeAnalyzer.isErrorAnalyzingFinished(file)) return false;
boolean errors = containsErrorsPreventingOptimize(file);
- return !errors && codeAnalyzer.canChangeFileSilently(myFile);
+ return !errors && DaemonListeners.canChangeFileSilently(myFile);
}
private boolean containsErrorsPreventingOptimize(@NotNull PsiFile file) {
// ignore unresolved imports errors
PsiImportList importList = ((PsiJavaFile)file).getImportList();
final TextRange importsRange = importList == null ? TextRange.EMPTY_RANGE : importList.getTextRange();
- boolean hasErrorsExceptUnresolvedImports = !DaemonCodeAnalyzerImpl.processHighlights(myDocument, myProject, HighlightSeverity.ERROR, 0, myDocument.getTextLength(), new Processor<HighlightInfo>() {
- @Override
- public boolean process(HighlightInfo error) {
- int infoStart = error.getActualStartOffset();
- int infoEnd = error.getActualEndOffset();
-
- return importsRange.containsRange(infoStart,infoEnd) && error.type.equals(HighlightInfoType.WRONG_REF);
- }
- });
+ boolean hasErrorsExceptUnresolvedImports = !DaemonCodeAnalyzerEx
+ .processHighlights(myDocument, myProject, HighlightSeverity.ERROR, 0, myDocument.getTextLength(), new Processor<HighlightInfo>() {
+ @Override
+ public boolean process(HighlightInfo error) {
+ int infoStart = error.getActualStartOffset();
+ int infoEnd = error.getActualEndOffset();
+
+ return importsRange.containsRange(infoStart, infoEnd) && error.type.equals(HighlightInfoType.WRONG_REF);
+ }
+ });
return hasErrorsExceptUnresolvedImports;
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/RefCountHolder.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/RefCountHolder.java
deleted file mode 100644
index 175c1332db46..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/RefCountHolder.java
+++ /dev/null
@@ -1,335 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl;
-
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.progress.ProgressIndicator;
-import com.intellij.openapi.util.Key;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.openapi.util.UserDataHolderEx;
-import com.intellij.psi.*;
-import com.intellij.psi.util.PsiMatcherImpl;
-import com.intellij.psi.util.PsiMatchers;
-import com.intellij.psi.util.PsiTreeUtil;
-import com.intellij.psi.util.PsiUtil;
-import com.intellij.util.ArrayUtilRt;
-import com.intellij.util.containers.BidirectionalMap;
-import com.intellij.util.containers.ConcurrentHashMap;
-import org.jetbrains.annotations.NonNls;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.lang.ref.SoftReference;
-import java.util.*;
-import java.util.concurrent.atomic.AtomicReference;
-
-public class RefCountHolder {
- private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.RefCountHolder");
-
- private final PsiFile myFile;
- private final BidirectionalMap<PsiReference,PsiElement> myLocalRefsMap = new BidirectionalMap<PsiReference, PsiElement>();
-
- private final Map<PsiNamedElement, Boolean> myDclsUsedMap = new ConcurrentHashMap<PsiNamedElement, Boolean>();
- private final Map<PsiReference, PsiImportStatementBase> myImportStatements = new ConcurrentHashMap<PsiReference, PsiImportStatementBase>();
- private final AtomicReference<ProgressIndicator> myState = new AtomicReference<ProgressIndicator>(VIRGIN);
- private static final ProgressIndicator VIRGIN = new DaemonProgressIndicator(); // just created or cleared
- private static final ProgressIndicator READY = new DaemonProgressIndicator();
- private volatile ProgressIndicator analyzedUnder;
-
- private static class HolderReference extends SoftReference<RefCountHolder> {
- // Map holding hard references to RefCountHolder for each highlighting pass (identified by its progress indicator)
- // there can be multiple passes running simultaneously (one actual and several passes just canceled and winding down but still alive)
- // so there is a chance they overlap the usage of RCH
- // As soon as everybody finished using RCH, map become empty and the RefCountHolder is eligible for gc
- private final Map<ProgressIndicator, RefCountHolder> map = new ConcurrentHashMap<ProgressIndicator, RefCountHolder>();
-
- public HolderReference(@NotNull RefCountHolder holder) {
- super(holder);
- }
-
- private void acquire(@NotNull ProgressIndicator indicator) {
- RefCountHolder holder = get();
- assert holder != null: "no way";
- map.put(indicator, holder);
- holder = get();
- assert holder != null: "can't be!";
- }
-
- private RefCountHolder release(@NotNull ProgressIndicator indicator) {
- return map.remove(indicator);
- }
- }
-
- private static final Key<HolderReference> REF_COUNT_HOLDER_IN_FILE_KEY = Key.create("REF_COUNT_HOLDER_IN_FILE_KEY");
-
- private static RefCountHolder getInstance(@NotNull PsiFile file, @NotNull ProgressIndicator indicator, boolean acquire) {
- HolderReference ref = file.getUserData(REF_COUNT_HOLDER_IN_FILE_KEY);
- RefCountHolder holder = ref == null ? null : ref.get();
- if (holder == null && acquire) {
- holder = new RefCountHolder(file);
- HolderReference newRef = new HolderReference(holder);
- while (true) {
- boolean replaced = ((UserDataHolderEx)file).replace(REF_COUNT_HOLDER_IN_FILE_KEY, ref, newRef);
- if (replaced) {
- ref = newRef;
- break;
- }
- ref = file.getUserData(REF_COUNT_HOLDER_IN_FILE_KEY);
- RefCountHolder newHolder = ref == null ? null : ref.get();
- if (newHolder != null) {
- holder = newHolder;
- break;
- }
- }
- }
- if (ref != null) {
- if (acquire) {
- ref.acquire(indicator);
- }
- else {
- ref.release(indicator);
- }
- }
- return holder;
- }
-
- @NotNull
- public static RefCountHolder startUsing(@NotNull PsiFile file, @NotNull ProgressIndicator indicator) {
- return getInstance(file, indicator, true);
- }
-
- @Nullable("might be gced")
- public static RefCountHolder endUsing(@NotNull PsiFile file, @NotNull ProgressIndicator indicator) {
- return getInstance(file, indicator, false);
- }
-
- private RefCountHolder(@NotNull PsiFile file) {
- myFile = file;
- log("c: created: ", myState.get(), " for ", file);
- }
-
- private void clear() {
- synchronized (myLocalRefsMap) {
- myLocalRefsMap.clear();
- }
- myImportStatements.clear();
- myDclsUsedMap.clear();
- }
-
- public void registerLocallyReferenced(@NotNull PsiNamedElement result) {
- myDclsUsedMap.put(result,Boolean.TRUE);
- }
-
- public void registerReference(@NotNull PsiJavaReference ref, @NotNull JavaResolveResult resolveResult) {
- PsiElement refElement = resolveResult.getElement();
- PsiFile psiFile = refElement == null ? null : refElement.getContainingFile();
- if (psiFile != null) psiFile = (PsiFile)psiFile.getNavigationElement(); // look at navigation elements because all references resolve into Cls elements when highlighting library source
- if (refElement != null && psiFile != null && myFile.getViewProvider().equals(psiFile.getViewProvider())) {
- registerLocalRef(ref, refElement.getNavigationElement());
- }
-
- PsiElement resolveScope = resolveResult.getCurrentFileResolveScope();
- if (resolveScope instanceof PsiImportStatementBase) {
- registerImportStatement(ref, (PsiImportStatementBase)resolveScope);
- }
- }
-
- private void registerImportStatement(@NotNull PsiReference ref, @NotNull PsiImportStatementBase importStatement) {
- myImportStatements.put(ref, importStatement);
- }
-
- public boolean isRedundant(@NotNull PsiImportStatementBase importStatement) {
- return !myImportStatements.containsValue(importStatement);
- }
-
- private void registerLocalRef(@NotNull PsiReference ref, PsiElement refElement) {
- if (refElement instanceof PsiMethod && PsiTreeUtil.isAncestor(refElement, ref.getElement(), true)) return; // filter self-recursive calls
- if (refElement instanceof PsiClass && PsiTreeUtil.isAncestor(refElement, ref.getElement(), true)) return; // filter inner use of itself
- synchronized (myLocalRefsMap) {
- myLocalRefsMap.put(ref, refElement);
- }
- }
-
- private void removeInvalidRefs() {
- synchronized (myLocalRefsMap) {
- for(Iterator<PsiReference> iterator = myLocalRefsMap.keySet().iterator(); iterator.hasNext();){
- PsiReference ref = iterator.next();
- if (!ref.getElement().isValid()){
- PsiElement value = myLocalRefsMap.get(ref);
- iterator.remove();
- List<PsiReference> array = myLocalRefsMap.getKeysByValue(value);
- LOG.assertTrue(array != null);
- array.remove(ref);
- }
- }
- }
- for (Iterator<PsiReference> iterator = myImportStatements.keySet().iterator(); iterator.hasNext();) {
- PsiReference ref = iterator.next();
- if (!ref.getElement().isValid()) {
- iterator.remove();
- }
- }
- removeInvalidFrom(myDclsUsedMap.keySet());
- }
-
- private static void removeInvalidFrom(@NotNull Collection<? extends PsiElement> collection) {
- for (Iterator<? extends PsiElement> it = collection.iterator(); it.hasNext();) {
- PsiElement element = it.next();
- if (!element.isValid()) it.remove();
- }
- }
-
- public boolean isReferenced(@NotNull PsiNamedElement element) {
- List<PsiReference> array;
- synchronized (myLocalRefsMap) {
- array = myLocalRefsMap.getKeysByValue(element);
- }
- if (array != null && !array.isEmpty() && !isParameterUsedRecursively(element, array)) return true;
-
- Boolean usedStatus = myDclsUsedMap.get(element);
- return usedStatus == Boolean.TRUE;
- }
-
- private static boolean isParameterUsedRecursively(@NotNull PsiElement element, @NotNull List<PsiReference> array) {
- if (!(element instanceof PsiParameter)) return false;
- PsiParameter parameter = (PsiParameter)element;
- PsiElement scope = parameter.getDeclarationScope();
- if (!(scope instanceof PsiMethod)) return false;
- PsiMethod method = (PsiMethod)scope;
- int paramIndex = ArrayUtilRt.find(method.getParameterList().getParameters(), parameter);
-
- for (PsiReference reference : array) {
- if (!(reference instanceof PsiElement)) return false;
- PsiElement argument = (PsiElement)reference;
-
- PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)new PsiMatcherImpl(argument)
- .dot(PsiMatchers.hasClass(PsiReferenceExpression.class))
- .parent(PsiMatchers.hasClass(PsiExpressionList.class))
- .parent(PsiMatchers.hasClass(PsiMethodCallExpression.class))
- .getElement();
- if (methodCallExpression == null) return false;
- PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
- if (method != methodExpression.resolve()) return false;
- PsiExpressionList argumentList = methodCallExpression.getArgumentList();
- PsiExpression[] arguments = argumentList.getExpressions();
- int argumentIndex = ArrayUtilRt.find(arguments, argument);
- if (paramIndex != argumentIndex) return false;
- }
-
- return true;
- }
-
- public boolean isReferencedForRead(@NotNull PsiElement element) {
- LOG.assertTrue(element instanceof PsiVariable);
- List<PsiReference> array;
- synchronized (myLocalRefsMap) {
- array = myLocalRefsMap.getKeysByValue(element);
- }
- if (array == null) return false;
- for (PsiReference ref : array) {
- PsiElement refElement = ref.getElement();
- if (!(refElement instanceof PsiExpression)) { // possible with incomplete code
- return true;
- }
- if (PsiUtil.isAccessedForReading((PsiExpression)refElement)) {
- if (refElement.getParent() instanceof PsiExpression &&
- refElement.getParent().getParent() instanceof PsiExpressionStatement &&
- PsiUtil.isAccessedForWriting((PsiExpression)refElement)) {
- continue; // "var++;"
- }
- return true;
- }
- }
- return false;
- }
-
- public boolean isReferencedForWrite(@NotNull PsiElement element) {
- LOG.assertTrue(element instanceof PsiVariable);
- List<PsiReference> array;
- synchronized (myLocalRefsMap) {
- array = myLocalRefsMap.getKeysByValue(element);
- }
- if (array == null) return false;
- for (PsiReference ref : array) {
- final PsiElement refElement = ref.getElement();
- if (!(refElement instanceof PsiExpression)) { // possible with incomplete code
- return true;
- }
- if (PsiUtil.isAccessedForWriting((PsiExpression)refElement)) {
- return true;
- }
- }
- return false;
- }
-
- public boolean analyze(@NotNull PsiFile file, TextRange dirtyScope, @NotNull Runnable analyze, @NotNull ProgressIndicator indicator) {
- ProgressIndicator old = myState.get();
- if (old != VIRGIN && old != READY) return false;
- if (!myState.compareAndSet(old, indicator)) {
- log("a: failed to change ", old, "->", indicator);
- return false;
- }
- log("a: changed ", old, "->", indicator);
- analyzedUnder = null;
- boolean completed = false;
- try {
- if (dirtyScope != null) {
- if (dirtyScope.equals(file.getTextRange())) {
- clear();
- }
- else {
- removeInvalidRefs();
- }
- }
-
- analyze.run();
- analyzedUnder = indicator;
- completed = true;
- }
- finally {
- ProgressIndicator resultState = completed ? READY : VIRGIN;
- boolean set = myState.compareAndSet(indicator, resultState);
- assert set : myState.get();
- log("a: changed after analyze", indicator, "->", resultState);
- }
- return true;
- }
-
- private static void log(@NonNls Object... s) {
- //System.err.println("RFC: "+ Arrays.asList(s));
- }
-
- public boolean retrieveUnusedReferencesInfo(@NotNull ProgressIndicator indicator, @NotNull Runnable analyze) {
- ProgressIndicator old = myState.get();
- if (!myState.compareAndSet(READY, indicator)) {
- log("r: failed to change ", old, "->", indicator);
- return false;
- }
- log("r: changed ", old, "->", indicator);
- try {
- if (analyzedUnder != indicator) {
- return false;
- }
- analyze.run();
- }
- finally {
- boolean set = myState.compareAndSet(indicator, READY);
- assert set : myState.get();
- log("r: changed back ", indicator, "->", READY);
- }
- return true;
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java
deleted file mode 100644
index 6c9723698a8d..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java
+++ /dev/null
@@ -1,747 +0,0 @@
-/*
- * Copyright 2000-2013 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.analysis;
-
-import com.intellij.codeInsight.FileModificationService;
-import com.intellij.codeInsight.daemon.JavaErrorMessages;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
-import com.intellij.codeInsight.daemon.impl.quickfix.CreateAnnotationMethodFromUsageFix;
-import com.intellij.codeInsight.daemon.impl.quickfix.QuickFixAction;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.Comparing;
-import com.intellij.patterns.ElementPattern;
-import com.intellij.pom.java.LanguageLevel;
-import com.intellij.psi.*;
-import com.intellij.psi.impl.PsiImplUtil;
-import com.intellij.psi.impl.source.PsiClassReferenceType;
-import com.intellij.psi.impl.source.PsiImmediateClassType;
-import com.intellij.psi.util.PsiTreeUtil;
-import com.intellij.psi.util.PsiUtil;
-import com.intellij.psi.util.TypeConversionUtil;
-import com.intellij.util.IncorrectOperationException;
-import com.intellij.util.containers.HashSet;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.lang.annotation.RetentionPolicy;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
-
-import static com.intellij.patterns.PsiJavaPatterns.psiElement;
-
-/**
- * @author ven
- */
-public class AnnotationsHighlightUtil {
- private static final Logger LOG = Logger.getInstance("com.intellij.codeInsight.daemon.impl.analysis.AnnotationsHighlightUtil");
-
- @Nullable
- public static HighlightInfo checkNameValuePair(PsiNameValuePair pair) {
- PsiReference ref = pair.getReference();
- if (ref == null) return null;
- PsiMethod method = (PsiMethod)ref.resolve();
- if (method == null) {
- if (pair.getName() != null) {
- final String description = JavaErrorMessages.message("annotation.unknown.method", ref.getCanonicalText());
- PsiElement element = ref.getElement();
- final HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(element).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(highlightInfo, new CreateAnnotationMethodFromUsageFix(pair));
- return highlightInfo;
- }
- else {
- String description = JavaErrorMessages.message("annotation.missing.method", ref.getCanonicalText());
- PsiElement element = ref.getElement();
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
- }
- }
- else {
- PsiType returnType = method.getReturnType();
- assert returnType != null : method;
- PsiAnnotationMemberValue value = pair.getValue();
- HighlightInfo info = checkMemberValueType(value, returnType);
- if (info != null) return info;
-
- return checkDuplicateAttribute(pair);
- }
- }
-
- @Nullable
- private static HighlightInfo checkDuplicateAttribute(PsiNameValuePair pair) {
- PsiAnnotationParameterList annotation = (PsiAnnotationParameterList)pair.getParent();
- PsiNameValuePair[] attributes = annotation.getAttributes();
- for (PsiNameValuePair attribute : attributes) {
- if (attribute == pair) break;
- String name = pair.getName();
- if (Comparing.equal(attribute.getName(), name)) {
- String description = JavaErrorMessages.message("annotation.duplicate.attribute",
- name == null ? PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME : name);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(pair).descriptionAndTooltip(description).create();
- }
- }
-
- return null;
- }
-
- private static String formatReference(PsiJavaCodeReferenceElement ref) {
- return ref.getCanonicalText();
- }
-
- @Nullable
- public static HighlightInfo checkMemberValueType(@Nullable PsiAnnotationMemberValue value, PsiType expectedType) {
- if (value == null) return null;
-
- if (expectedType instanceof PsiClassType && expectedType.equalsToText(CommonClassNames.JAVA_LANG_CLASS)) {
- if (!(value instanceof PsiClassObjectAccessExpression)) {
- String description = JavaErrorMessages.message("annotation.non.class.literal.attribute.value");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(value).descriptionAndTooltip(description).create();
- }
- }
-
- if (value instanceof PsiAnnotation) {
- PsiJavaCodeReferenceElement nameRef = ((PsiAnnotation)value).getNameReferenceElement();
- if (nameRef == null) return null;
-
- if (expectedType instanceof PsiClassType) {
- PsiClass aClass = ((PsiClassType)expectedType).resolve();
- if (aClass != null && nameRef.isReferenceTo(aClass)) return null;
- }
-
- if (expectedType instanceof PsiArrayType) {
- PsiType componentType = ((PsiArrayType)expectedType).getComponentType();
- if (componentType instanceof PsiClassType) {
- PsiClass aClass = ((PsiClassType)componentType).resolve();
- if (aClass != null && nameRef.isReferenceTo(aClass)) return null;
- }
- }
-
- String description = JavaErrorMessages.message("annotation.incompatible.types",
- formatReference(nameRef), JavaHighlightUtil.formatType(expectedType));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(value).descriptionAndTooltip(description).create();
- }
- if (value instanceof PsiArrayInitializerMemberValue) {
- if (expectedType instanceof PsiArrayType) return null;
- String description = JavaErrorMessages.message("annotation.illegal.array.initializer", JavaHighlightUtil.formatType(expectedType));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(value).descriptionAndTooltip(description).create();
- }
- if (value instanceof PsiExpression) {
- PsiExpression expr = (PsiExpression)value;
- PsiType type = expr.getType();
- if (type != null && TypeConversionUtil.areTypesAssignmentCompatible(expectedType, expr) ||
- expectedType instanceof PsiArrayType &&
- TypeConversionUtil.areTypesAssignmentCompatible(((PsiArrayType)expectedType).getComponentType(), expr)) {
- return null;
- }
-
- String description = JavaErrorMessages.message("annotation.incompatible.types",
- JavaHighlightUtil.formatType(type), JavaHighlightUtil.formatType(expectedType));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(value).descriptionAndTooltip(description).create();
- }
-
- LOG.error("Unknown annotation member value: " + value);
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkDuplicateAnnotations(PsiAnnotation annotationToCheck) {
- PsiAnnotationOwner owner = annotationToCheck.getOwner();
- if (owner == null) return null;
-
- PsiJavaCodeReferenceElement element = annotationToCheck.getNameReferenceElement();
- if (element == null) return null;
- PsiElement resolved = element.resolve();
- if (!(resolved instanceof PsiClass)) return null;
-
- for (PsiAnnotation annotation : owner.getAnnotations()) {
- if (annotation == annotationToCheck) continue;
- PsiJavaCodeReferenceElement nameRef = annotation.getNameReferenceElement();
- if (nameRef == null) continue;
- PsiElement aClass = nameRef.resolve();
- if (!resolved.equals(aClass)) continue;
-
- if (!PsiUtil.isLanguageLevel8OrHigher(annotationToCheck)) {
- String description = JavaErrorMessages.message("annotation.duplicate.annotation");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
- }
-
- PsiClass annotationType = (PsiClass)resolved;
- PsiAnnotation metaAnno = PsiImplUtil.findAnnotation(annotationType.getModifierList(), CommonClassNames.JAVA_LANG_ANNOTATION_REPEATABLE);
- if (metaAnno == null) {
- String explanation = JavaErrorMessages.message("annotation.non.repeatable", annotationType.getQualifiedName());
- String description = JavaErrorMessages.message("annotation.duplicate.explained", explanation);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
- }
-
- String explanation = doCheckRepeatableAnnotation(metaAnno);
- if (explanation != null) {
- String description = JavaErrorMessages.message("annotation.duplicate.explained", explanation);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
- }
-
- PsiClass container = getRepeatableContainer(metaAnno);
- if (container != null) {
- String containerName = container.getQualifiedName();
- if (containerName != null) {
- PsiAnnotation containerAnno = owner.findAnnotation(containerName);
- if (containerAnno != null) {
- String description = JavaErrorMessages.message("annotation.container.wrong.place", containerName);
- return annotationError(containerAnno, description);
- }
- }
-
- PsiAnnotation.TargetType[] targets = PsiImplUtil.getTargetsForLocation(owner);
- PsiAnnotation.TargetType applicable = PsiImplUtil.findApplicableTarget(container, targets);
- if (applicable == null) {
- String target = JavaErrorMessages.message("annotation.target." + targets[0]);
- String message = JavaErrorMessages.message("annotation.container.not.applicable", containerName, target);
- return annotationError(annotation, message);
- }
- }
- }
-
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkMissingAttributes(PsiAnnotation annotation) {
- PsiJavaCodeReferenceElement nameRef = annotation.getNameReferenceElement();
- if (nameRef == null) return null;
- PsiClass aClass = (PsiClass)nameRef.resolve();
- if (aClass != null && aClass.isAnnotationType()) {
- Set<String> names = new HashSet<String>();
- PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes();
- for (PsiNameValuePair attribute : attributes) {
- final String name = attribute.getName();
- if (name != null) {
- names.add(name);
- }
- else {
- names.add(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME);
- }
- }
-
- PsiMethod[] annotationMethods = aClass.getMethods();
- List<String> missed = new ArrayList<String>();
- for (PsiMethod method : annotationMethods) {
- if (PsiUtil.isAnnotationMethod(method)) {
- PsiAnnotationMethod annotationMethod = (PsiAnnotationMethod)method;
- if (annotationMethod.getDefaultValue() == null) {
- if (!names.contains(annotationMethod.getName())) {
- missed.add(annotationMethod.getName());
- }
- }
- }
- }
-
- if (!missed.isEmpty()) {
- StringBuffer buff = new StringBuffer("'" + missed.get(0) + "'");
- for (int i = 1; i < missed.size(); i++) {
- buff.append(", ");
- buff.append("'").append(missed.get(i)).append("'");
- }
-
- String description = JavaErrorMessages.message("annotation.missing.attribute", buff);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(nameRef).descriptionAndTooltip(description).create();
- }
- }
-
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkConstantExpression(PsiExpression expression) {
- final PsiElement parent = expression.getParent();
- if (PsiUtil.isAnnotationMethod(parent) || parent instanceof PsiNameValuePair || parent instanceof PsiArrayInitializerMemberValue) {
- if (!PsiUtil.isConstantExpression(expression)) {
- String description = JavaErrorMessages.message("annotation.non.constant.attribute.value");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
- }
- }
-
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkValidAnnotationType(final PsiTypeElement typeElement) {
- PsiType type = typeElement.getType();
- if (type.accept(AnnotationReturnTypeVisitor.INSTANCE).booleanValue()) {
- return null;
- }
- String description = JavaErrorMessages.message("annotation.invalid.annotation.member.type");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement).descriptionAndTooltip(description).create();
- }
-
- private static final ElementPattern<PsiElement> ANY_ANNOTATION_ALLOWED = psiElement().andOr(
- psiElement().withParent(PsiNameValuePair.class),
- psiElement().withParents(PsiArrayInitializerMemberValue.class, PsiNameValuePair.class),
- psiElement().withParent(PsiAnnotationMethod.class).afterLeaf(PsiKeyword.DEFAULT)
- );
-
- @Nullable
- public static HighlightInfo checkApplicability(@NotNull PsiAnnotation annotation, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- if (ANY_ANNOTATION_ALLOWED.accepts(annotation)) {
- return null;
- }
-
- PsiJavaCodeReferenceElement nameRef = annotation.getNameReferenceElement();
- if (nameRef == null) return null;
-
- PsiAnnotationOwner owner = annotation.getOwner();
- PsiAnnotation.TargetType[] targets = PsiImplUtil.getTargetsForLocation(owner);
- if (owner == null || targets.length == 0) {
- String message = JavaErrorMessages.message("annotation.not.allowed.here");
- return annotationError(annotation, message);
- }
-
- if (!(owner instanceof PsiModifierList)) {
- HighlightInfo info = HighlightUtil.checkTypeAnnotationFeature(annotation, languageLevel,containingFile);
- if (info != null) return info;
- }
-
- PsiAnnotation.TargetType applicable = PsiImplUtil.findApplicableTarget(annotation, targets);
- if (applicable == PsiAnnotation.TargetType.UNKNOWN) return null;
-
- if (applicable == null) {
- String target = JavaErrorMessages.message("annotation.target." + targets[0]);
- String message = JavaErrorMessages.message("annotation.not.applicable", nameRef.getText(), target);
- return annotationError(annotation, message);
- }
-
- if (applicable == PsiAnnotation.TargetType.TYPE_USE) {
- if (owner instanceof PsiClassReferenceType) {
- PsiJavaCodeReferenceElement ref = ((PsiClassReferenceType)owner).getReference();
- HighlightInfo info = checkReferenceTarget(annotation, ref);
- if (info != null) return info;
- }
- else if (owner instanceof PsiModifierList) {
- PsiElement nextElement = PsiTreeUtil.skipSiblingsForward((PsiModifierList)owner,
- PsiComment.class, PsiWhiteSpace.class, PsiTypeParameterList.class);
- if (nextElement instanceof PsiTypeElement) {
- PsiTypeElement typeElement = (PsiTypeElement)nextElement;
- PsiType type = typeElement.getType();
- if (PsiType.VOID.equals(type)) {
- String message = JavaErrorMessages.message("annotation.not.allowed.void");
- return annotationError(annotation, message);
- }
- if (!(type instanceof PsiPrimitiveType)) {
- PsiJavaCodeReferenceElement ref = getOutermostReferenceElement(typeElement.getInnermostComponentReferenceElement());
- HighlightInfo info = checkReferenceTarget(annotation, ref);
- if (info != null) return info;
- }
- }
- }
- else if (owner instanceof PsiTypeElement) {
- PsiElement context = PsiTreeUtil.skipParentsOfType((PsiTypeElement)owner, PsiTypeElement.class);
- if (context instanceof PsiClassObjectAccessExpression) {
- String message = JavaErrorMessages.message("annotation.not.allowed.class");
- return annotationError(annotation, message);
- }
- }
- }
-
- return null;
- }
-
- private static HighlightInfo annotationError(PsiAnnotation annotation, String message) {
- HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(annotation).descriptionAndTooltip(message).create();
- QuickFixAction.registerQuickFixAction(info, new DeleteAnnotationAction(annotation));
- return info;
- }
-
- @Nullable
- private static HighlightInfo checkReferenceTarget(PsiAnnotation annotation, @Nullable PsiJavaCodeReferenceElement ref) {
- if (ref == null) return null;
- PsiElement refTarget = ref.resolve();
- if (refTarget == null) return null;
-
- String message = null;
- if (!(refTarget instanceof PsiClass)) {
- message = JavaErrorMessages.message("annotation.not.allowed.ref");
- }
- else {
- PsiElement parent = ref.getParent();
- if (parent instanceof PsiJavaCodeReferenceElement) {
- PsiElement qualified = ((PsiJavaCodeReferenceElement)parent).resolve();
- if (qualified instanceof PsiMember && ((PsiMember)qualified).hasModifierProperty(PsiModifier.STATIC)) {
- message = JavaErrorMessages.message("annotation.not.allowed.static");
- }
- }
- }
-
- return message != null ? annotationError(annotation, message) : null;
- }
-
- @Nullable
- private static PsiJavaCodeReferenceElement getOutermostReferenceElement(@Nullable PsiJavaCodeReferenceElement ref) {
- if (ref == null) return null;
-
- PsiElement qualifier;
- while ((qualifier = ref.getQualifier()) instanceof PsiJavaCodeReferenceElement) {
- ref = (PsiJavaCodeReferenceElement)qualifier;
- }
- return ref;
- }
-
- public static HighlightInfo checkForeignInnerClassesUsed(final PsiAnnotation annotation) {
- final HighlightInfo[] infos = new HighlightInfo[1];
- final PsiAnnotationOwner owner = annotation.getOwner();
- if (owner instanceof PsiModifierList) {
- final PsiElement parent = ((PsiModifierList)owner).getParent();
- if (parent instanceof PsiClass) {
- annotation.accept(new JavaRecursiveElementWalkingVisitor() {
- @Override
- public void visitElement(PsiElement element) {
- if (infos[0] != null) return;
- super.visitElement(element);
- }
-
- @Override
- public void visitClassObjectAccessExpression(PsiClassObjectAccessExpression expression) {
- super.visitClassObjectAccessExpression(expression);
- final PsiTypeElement operand = expression.getOperand();
- final PsiClass classType = PsiUtil.resolveClassInType(operand.getType());
- if (classType != null) {
- checkAccessibility(expression, classType, HighlightUtil.formatClass(classType));
- }
- }
-
- @Override
- public void visitReferenceExpression(PsiReferenceExpression expression) {
- super.visitReferenceExpression(expression);
- final PsiElement resolve = expression.resolve();
- if (resolve instanceof PsiField) {
- checkAccessibility(expression, (PsiMember)resolve, HighlightUtil.formatField((PsiField)resolve));
- }
- }
-
- private void checkAccessibility(PsiExpression expression, PsiMember resolve, String memberString) {
- if (resolve.hasModifierProperty(PsiModifier.PRIVATE) &&
- PsiTreeUtil.isAncestor(parent, resolve, true)) {
- String description = JavaErrorMessages.message("private.symbol",
- memberString,
- HighlightUtil.formatClass((PsiClass)parent));
- infos[0] =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
- }
- }
- });
- }
- }
- return infos[0];
- }
-
- @Nullable
- public static HighlightInfo checkAnnotationType(PsiAnnotation annotation) {
- PsiJavaCodeReferenceElement nameReferenceElement = annotation.getNameReferenceElement();
- if (nameReferenceElement != null) {
- PsiElement resolved = nameReferenceElement.resolve();
- if (!(resolved instanceof PsiClass) || !((PsiClass)resolved).isAnnotationType()) {
- String description = JavaErrorMessages.message("annotation.annotation.type.expected");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(nameReferenceElement).descriptionAndTooltip(description).create();
- }
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkCyclicMemberType(PsiTypeElement typeElement, PsiClass aClass) {
- LOG.assertTrue(aClass.isAnnotationType());
- PsiType type = typeElement.getType();
- final Set<PsiClass> checked = new HashSet<PsiClass>();
- if (cyclicDependencies(aClass, type, checked, aClass.getManager())) {
- String description = JavaErrorMessages.message("annotation.cyclic.element.type");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
- private static boolean cyclicDependencies(PsiClass aClass, PsiType type, @NotNull Set<PsiClass> checked,@NotNull PsiManager manager) {
- final PsiClass resolvedClass = PsiUtil.resolveClassInType(type);
- if (resolvedClass != null && resolvedClass.isAnnotationType()) {
- if (aClass == resolvedClass) {
- return true;
- }
- if (!checked.add(resolvedClass) || !manager.isInProject(resolvedClass)) return false;
- final PsiMethod[] methods = resolvedClass.getMethods();
- for (PsiMethod method : methods) {
- if (cyclicDependencies(aClass, method.getReturnType(), checked,manager)) return true;
- }
- }
- return false;
- }
-
- public static HighlightInfo checkClashesWithSuperMethods(@NotNull PsiAnnotationMethod psiMethod) {
- final PsiIdentifier nameIdentifier = psiMethod.getNameIdentifier();
- if (nameIdentifier != null) {
- final PsiMethod[] methods = psiMethod.findDeepestSuperMethods();
- for (PsiMethod method : methods) {
- final PsiClass containingClass = method.getContainingClass();
- if (containingClass != null) {
- final String qualifiedName = containingClass.getQualifiedName();
- if (CommonClassNames.JAVA_LANG_OBJECT.equals(qualifiedName) || CommonClassNames.JAVA_LANG_ANNOTATION_ANNOTATION.equals(qualifiedName)) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(nameIdentifier).descriptionAndTooltip(
- "@interface member clashes with '" + JavaHighlightUtil.formatMethod(method) + "' in " + HighlightUtil.formatClass(containingClass)).create();
- }
- }
- }
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkAnnotationDeclaration(final PsiElement parent, final PsiReferenceList list) {
- if (PsiUtil.isAnnotationMethod(parent)) {
- PsiAnnotationMethod method = (PsiAnnotationMethod)parent;
- if (list == method.getThrowsList()) {
- String description = JavaErrorMessages.message("annotation.members.may.not.have.throws.list");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).descriptionAndTooltip(description).create();
- }
- }
- else if (parent instanceof PsiClass && ((PsiClass)parent).isAnnotationType()) {
- if (PsiKeyword.EXTENDS.equals(list.getFirstChild().getText())) {
- String description = JavaErrorMessages.message("annotation.may.not.have.extends.list");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).descriptionAndTooltip(description).create();
- }
- }
- return null;
- }
-
-
- @Nullable
- public static HighlightInfo checkPackageAnnotationContainingFile(final PsiPackageStatement statement) {
- if (statement.getAnnotationList() == null) {
- return null;
- }
- PsiFile file = statement.getContainingFile();
- if (file != null && !PsiPackage.PACKAGE_INFO_FILE.equals(file.getName())) {
- String description = JavaErrorMessages.message("invalid.package.annotation.containing.file");
- HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR);
- builder.range(statement.getAnnotationList().getTextRange());
- builder.descriptionAndTooltip(description);
- return builder.create();
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkTargetAnnotationDuplicates(PsiAnnotation annotation) {
- PsiJavaCodeReferenceElement nameRef = annotation.getNameReferenceElement();
- if (nameRef == null) return null;
-
- PsiElement resolved = nameRef.resolve();
- if (!(resolved instanceof PsiClass) || !CommonClassNames.JAVA_LANG_ANNOTATION_TARGET.equals(((PsiClass)resolved).getQualifiedName())) {
- return null;
- }
-
- PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes();
- if (attributes.length < 1) return null;
- PsiAnnotationMemberValue value = attributes[0].getValue();
- if (!(value instanceof PsiArrayInitializerMemberValue)) return null;
- PsiAnnotationMemberValue[] arrayInitializers = ((PsiArrayInitializerMemberValue) value).getInitializers();
- Set<PsiElement> targets = new HashSet<PsiElement>();
- for (PsiAnnotationMemberValue initializer : arrayInitializers) {
- if (initializer instanceof PsiReferenceExpression) {
- PsiElement target = ((PsiReferenceExpression) initializer).resolve();
- if (target != null) {
- if (targets.contains(target)) {
- String description = JavaErrorMessages.message("repeated.annotation.target");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(initializer).descriptionAndTooltip(description).create();
- }
- targets.add(target);
- }
- }
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkFunctionalInterface(@NotNull PsiAnnotation annotation, @NotNull LanguageLevel languageLevel) {
- final String errorMessage = LambdaUtil.checkFunctionalInterface(annotation, languageLevel);
- if (errorMessage != null) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.WARNING).range(annotation).descriptionAndTooltip(errorMessage).create();
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkRepeatableAnnotation(PsiAnnotation annotation) {
- String qualifiedName = annotation.getQualifiedName();
- if (!CommonClassNames.JAVA_LANG_ANNOTATION_REPEATABLE.equals(qualifiedName)) return null;
-
- String description = doCheckRepeatableAnnotation(annotation);
- if (description != null) {
- PsiAnnotationMemberValue containerRef = PsiImplUtil.findAttributeValue(annotation, null);
- if (containerRef != null) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(containerRef).descriptionAndTooltip(description).create();
- }
- }
-
- return null;
- }
-
- @Nullable
- private static String doCheckRepeatableAnnotation(PsiAnnotation annotation) {
- PsiAnnotationOwner owner = annotation.getOwner();
- if (!(owner instanceof PsiModifierList)) return null;
- PsiElement target = ((PsiModifierList)owner).getParent();
- if (!(target instanceof PsiClass) || !((PsiClass)target).isAnnotationType()) return null;
- PsiClass container = getRepeatableContainer(annotation);
- if (container == null) return null;
-
- PsiMethod[] methods = container.findMethodsByName("value", false);
- if (methods.length == 0) {
- return JavaErrorMessages.message("annotation.container.no.value", container.getQualifiedName());
- }
-
- if (methods.length == 1) {
- PsiType expected = new PsiImmediateClassType((PsiClass)target, PsiSubstitutor.EMPTY).createArrayType();
- if (!expected.equals(methods[0].getReturnType())) {
- return JavaErrorMessages.message("annotation.container.bad.type", container.getQualifiedName(), JavaHighlightUtil
- .formatType(expected));
- }
- }
-
- RetentionPolicy targetPolicy = getRetentionPolicy((PsiClass)target);
- if (targetPolicy != null) {
- RetentionPolicy containerPolicy = getRetentionPolicy(container);
- if (containerPolicy != null && targetPolicy.compareTo(containerPolicy) > 0) {
- return JavaErrorMessages.message("annotation.container.low.retention", container.getQualifiedName(), containerPolicy);
- }
- }
-
- Set<PsiAnnotation.TargetType> repeatableTargets = PsiImplUtil.getAnnotationTargets((PsiClass)target);
- if (repeatableTargets != null) {
- Set<PsiAnnotation.TargetType> containerTargets = PsiImplUtil.getAnnotationTargets(container);
- if (containerTargets != null && !repeatableTargets.containsAll(containerTargets)) {
- return JavaErrorMessages.message("annotation.container.wide.target", container.getQualifiedName());
- }
- }
-
- return null;
- }
-
- @Nullable
- private static PsiClass getRepeatableContainer(PsiAnnotation annotation) {
- PsiAnnotationMemberValue containerRef = PsiImplUtil.findAttributeValue(annotation, null);
- if (!(containerRef instanceof PsiClassObjectAccessExpression)) return null;
- PsiType containerType = ((PsiClassObjectAccessExpression)containerRef).getOperand().getType();
- if (!(containerType instanceof PsiClassType)) return null;
- PsiClass container = ((PsiClassType)containerType).resolve();
- if (container == null || !container.isAnnotationType()) return null;
- return container;
- }
-
- @Nullable
- private static RetentionPolicy getRetentionPolicy(PsiClass annotation) {
- PsiModifierList modifierList = annotation.getModifierList();
- if (modifierList != null) {
- PsiAnnotation retentionAnno = modifierList.findAnnotation(CommonClassNames.JAVA_LANG_ANNOTATION_RETENTION);
- if (retentionAnno == null) return RetentionPolicy.CLASS;
-
- PsiAnnotationMemberValue policyRef = PsiImplUtil.findAttributeValue(retentionAnno, null);
- if (policyRef instanceof PsiReference) {
- PsiElement field = ((PsiReference)policyRef).resolve();
- if (field instanceof PsiEnumConstant) {
- String name = ((PsiEnumConstant)field).getName();
- try {
- return RetentionPolicy.valueOf(name);
- }
- catch (Exception e) {
- LOG.warn("Unknown policy: " + name);
- }
- }
- }
- }
-
- return null;
- }
-
- public static class AnnotationReturnTypeVisitor extends PsiTypeVisitor<Boolean> {
- public static final AnnotationReturnTypeVisitor INSTANCE = new AnnotationReturnTypeVisitor();
- @Override
- public Boolean visitType(PsiType type) {
- return Boolean.FALSE;
- }
-
- @Override
- public Boolean visitPrimitiveType(PsiPrimitiveType primitiveType) {
- return PsiType.VOID.equals(primitiveType) || PsiType.NULL.equals(primitiveType) ? Boolean.FALSE : Boolean.TRUE;
- }
-
- @Override
- public Boolean visitArrayType(PsiArrayType arrayType) {
- if (arrayType.getArrayDimensions() != 1) return Boolean.FALSE;
- PsiType componentType = arrayType.getComponentType();
- return componentType.accept(this);
- }
-
- @Override
- public Boolean visitClassType(PsiClassType classType) {
- if (classType.getParameters().length > 0) {
- PsiClassType rawType = classType.rawType();
- return rawType.equalsToText(CommonClassNames.JAVA_LANG_CLASS);
- }
-
- PsiClass aClass = classType.resolve();
- if (aClass != null && (aClass.isAnnotationType() || aClass.isEnum())) {
- return Boolean.TRUE;
- }
-
- return classType.equalsToText(CommonClassNames.JAVA_LANG_CLASS) || classType.equalsToText(CommonClassNames.JAVA_LANG_STRING);
- }
- }
-
- private static class DeleteAnnotationAction implements IntentionAction {
- private final PsiAnnotation myAnnotation;
-
- public DeleteAnnotationAction(PsiAnnotation annotation) {
- myAnnotation = annotation;
- }
-
- @NotNull
- @Override
- public String getText() {
- return "Remove";
- }
-
- @NotNull
- @Override
- public String getFamilyName() {
- return getText();
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- return true;
- }
-
- @Override
- public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
- if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
- myAnnotation.delete();
- }
-
- @Override
- public boolean startInWriteAction() {
- return true;
- }
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java
deleted file mode 100644
index ac90dce8ec82..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java
+++ /dev/null
@@ -1,1295 +0,0 @@
-/*
- * Copyright 2000-2012 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.analysis;
-
-import com.intellij.codeInsight.daemon.JavaErrorMessages;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
-import com.intellij.codeInsight.daemon.impl.quickfix.*;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.codeInsight.intention.QuickFixFactory;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.project.IndexNotReadyException;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.projectRoots.JavaSdkVersion;
-import com.intellij.openapi.projectRoots.JavaVersionService;
-import com.intellij.openapi.util.Comparing;
-import com.intellij.openapi.util.Pair;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.pom.java.LanguageLevel;
-import com.intellij.psi.*;
-import com.intellij.psi.search.GlobalSearchScope;
-import com.intellij.psi.search.searches.ReferencesSearch;
-import com.intellij.psi.search.searches.SuperMethodsSearch;
-import com.intellij.psi.util.*;
-import com.intellij.util.ArrayUtilRt;
-import com.intellij.util.containers.ContainerUtil;
-import com.intellij.util.containers.HashMap;
-import com.intellij.util.containers.HashSet;
-import gnu.trove.THashMap;
-import org.jetbrains.annotations.NonNls;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.*;
-
-/**
- * @author cdr
- */
-public class GenericsHighlightUtil {
- private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.analysis.GenericsHighlightUtil");
-
- private static final QuickFixFactory QUICK_FIX_FACTORY = QuickFixFactory.getInstance();
-
- private GenericsHighlightUtil() { }
-
- @Nullable
- public static HighlightInfo checkInferredTypeArguments(PsiMethod genericMethod,
- PsiMethodCallExpression call,
- PsiSubstitutor substitutor) {
- final Pair<PsiTypeParameter, PsiType> inferredTypeArgument =
- GenericsUtil.findTypeParameterWithBoundError(genericMethod.getTypeParameters(), substitutor, call, false);
- if (inferredTypeArgument != null) {
- final PsiType extendsType = inferredTypeArgument.second;
- final PsiTypeParameter typeParameter = inferredTypeArgument.first;
- PsiClass boundClass = extendsType instanceof PsiClassType ? ((PsiClassType)extendsType).resolve() : null;
-
- @NonNls String messageKey = boundClass == null || typeParameter.isInterface() == boundClass.isInterface()
- ? "generics.inferred.type.for.type.parameter.is.not.within.its.bound.extend"
- : "generics.inferred.type.for.type.parameter.is.not.within.its.bound.implement";
-
- String description = JavaErrorMessages.message(
- messageKey,
- HighlightUtil.formatClass(typeParameter),
- JavaHighlightUtil.formatType(extendsType),
- JavaHighlightUtil.formatType(substitutor.substitute(typeParameter))
- );
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(call).descriptionAndTooltip(description).create();
- }
-
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkParameterizedReferenceTypeArguments(final PsiElement resolved,
- final PsiJavaCodeReferenceElement referenceElement,
- final PsiSubstitutor substitutor,
- @NotNull JavaSdkVersion javaSdkVersion) {
- if (!(resolved instanceof PsiTypeParameterListOwner)) return null;
- final PsiTypeParameterListOwner typeParameterListOwner = (PsiTypeParameterListOwner)resolved;
- return checkReferenceTypeArgumentList(typeParameterListOwner, referenceElement.getParameterList(), substitutor, true, javaSdkVersion);
- }
-
- @Nullable
- public static HighlightInfo checkReferenceTypeArgumentList(final PsiTypeParameterListOwner typeParameterListOwner,
- final PsiReferenceParameterList referenceParameterList,
- final PsiSubstitutor substitutor,
- boolean registerIntentions,
- @NotNull JavaSdkVersion javaSdkVersion) {
- PsiDiamondType.DiamondInferenceResult inferenceResult = null;
- PsiTypeElement[] referenceElements = null;
- if (referenceParameterList != null) {
- referenceElements = referenceParameterList.getTypeParameterElements();
- if (referenceElements.length == 1 && referenceElements[0].getType() instanceof PsiDiamondType) {
- if (!typeParameterListOwner.hasTypeParameters()) {
- final String description = JavaErrorMessages.message("generics.diamond.not.applicable");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(referenceElements[0]).descriptionAndTooltip(description).create();
- }
- inferenceResult = ((PsiDiamondType)referenceElements[0].getType()).resolveInferredTypes();
- final String errorMessage = inferenceResult.getErrorMessage();
- if (errorMessage != null) {
- final PsiType expectedType = detectExpectedType(referenceParameterList);
- if (!(inferenceResult.failedToInfer() && expectedType instanceof PsiClassType && ((PsiClassType)expectedType).isRaw())) {
- return HighlightInfo
- .newHighlightInfo(HighlightInfoType.ERROR).range(referenceElements[0]).descriptionAndTooltip(errorMessage).create();
- }
- }
- }
- }
-
- final PsiTypeParameter[] typeParameters = typeParameterListOwner.getTypeParameters();
- final int targetParametersNum = typeParameters.length;
- final int refParametersNum = referenceParameterList == null ? 0 : referenceParameterList.getTypeArguments().length;
- if (targetParametersNum != refParametersNum && refParametersNum != 0) {
- final String description;
- if (targetParametersNum == 0) {
- if (PsiTreeUtil.getParentOfType(referenceParameterList, PsiCall.class) != null &&
- typeParameterListOwner instanceof PsiMethod &&
- javaSdkVersion.isAtLeast(JavaSdkVersion.JDK_1_7)) {
- description = null;
- }
- else {
- description = JavaErrorMessages.message(
- "generics.type.or.method.does.not.have.type.parameters",
- typeParameterListOwnerCategoryDescription(typeParameterListOwner),
- typeParameterListOwnerDescription(typeParameterListOwner)
- );
- }
- }
- else {
- description = JavaErrorMessages.message("generics.wrong.number.of.type.arguments", refParametersNum, targetParametersNum);
- }
-
- if (description != null) {
- final HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(referenceParameterList).descriptionAndTooltip(description).create();
- if (registerIntentions) {
- PsiElement grandParent = referenceParameterList.getParent().getParent();
- if (grandParent instanceof PsiTypeElement) {
- PsiElement variable = grandParent.getParent();
- if (variable instanceof PsiVariable) {
- if (targetParametersNum == 0) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new RemoveTypeArgumentsFix(variable));
- }
- if (typeParameterListOwner instanceof PsiClass) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new ChangeClassSignatureFromUsageFix((PsiClass)typeParameterListOwner, referenceParameterList));
- }
- VariableParameterizedTypeFix.registerIntentions(highlightInfo, (PsiVariable)variable, referenceParameterList);
- }
- }
- }
- return highlightInfo;
- }
- }
-
- // bounds check
- if (targetParametersNum > 0 && refParametersNum != 0) {
- if (inferenceResult != null) {
- final PsiType[] types = inferenceResult.getTypes();
- for (int i = 0; i < typeParameters.length; i++) {
- final PsiType type = types[i];
- final HighlightInfo highlightInfo = checkTypeParameterWithinItsBound(typeParameters[i], substitutor, type, referenceElements[0], referenceParameterList);
- if (highlightInfo != null) return highlightInfo;
- }
- }
- else {
- for (int i = 0; i < typeParameters.length; i++) {
- final PsiTypeElement typeElement = referenceElements[i];
- final HighlightInfo highlightInfo = checkTypeParameterWithinItsBound(typeParameters[i], substitutor, typeElement.getType(), typeElement, referenceParameterList);
- if (highlightInfo != null) return highlightInfo;
- }
- }
- }
-
- return null;
- }
-
- private static PsiType detectExpectedType(PsiReferenceParameterList referenceParameterList) {
- final PsiNewExpression newExpression = PsiTreeUtil.getParentOfType(referenceParameterList, PsiNewExpression.class);
- LOG.assertTrue(newExpression != null);
- final PsiElement parent = newExpression.getParent();
- PsiType expectedType = null;
- if (parent instanceof PsiVariable && newExpression.equals(((PsiVariable)parent).getInitializer())) {
- expectedType = ((PsiVariable)parent).getType();
- }
- else if (parent instanceof PsiAssignmentExpression && newExpression.equals(((PsiAssignmentExpression)parent).getRExpression())) {
- expectedType = ((PsiAssignmentExpression)parent).getLExpression().getType();
- }
- else if (parent instanceof PsiReturnStatement) {
- PsiMethod method = PsiTreeUtil.getParentOfType(parent, PsiMethod.class);
- if (method != null) {
- expectedType = method.getReturnType();
- }
- }
- else if (parent instanceof PsiExpressionList) {
- final PsiElement pParent = parent.getParent();
- if (pParent instanceof PsiCallExpression && parent.equals(((PsiCallExpression)pParent).getArgumentList())) {
- final PsiMethod method = ((PsiCallExpression)pParent).resolveMethod();
- if (method != null) {
- final PsiExpression[] expressions = ((PsiCallExpression)pParent).getArgumentList().getExpressions();
- final int idx = ArrayUtilRt.find(expressions, newExpression);
- if (idx > -1) {
- final PsiParameterList parameterList = method.getParameterList();
- if (idx < parameterList.getParametersCount()) {
- expectedType = parameterList.getParameters()[idx].getType();
- }
- }
- }
- }
- }
- return expectedType;
- }
-
- @Nullable
- private static HighlightInfo checkTypeParameterWithinItsBound(PsiTypeParameter classParameter,
- final PsiSubstitutor substitutor,
- final PsiType type,
- final PsiElement typeElement2Highlight,
- PsiReferenceParameterList referenceParameterList) {
- final PsiClass referenceClass = type instanceof PsiClassType ? ((PsiClassType)type).resolve() : null;
- final PsiType psiType = substitutor.substitute(classParameter);
- if (psiType instanceof PsiClassType && !(PsiUtil.resolveClassInType(psiType) instanceof PsiTypeParameter)) {
- if (checkNotInBounds(type, psiType, referenceParameterList)) {
- final String description = "Actual type argument and inferred type contradict each other";
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement2Highlight).descriptionAndTooltip(description).create();
- }
- }
-
- final PsiClassType[] bounds = classParameter.getSuperTypes();
- for (PsiClassType type1 : bounds) {
- PsiType bound = substitutor.substitute(type1);
- if (!bound.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) && checkNotInBounds(type, bound, referenceParameterList)) {
- PsiClass boundClass = bound instanceof PsiClassType ? ((PsiClassType)bound).resolve() : null;
-
- @NonNls final String messageKey = boundClass == null || referenceClass == null || referenceClass.isInterface() == boundClass.isInterface()
- ? "generics.type.parameter.is.not.within.its.bound.extend"
- : "generics.type.parameter.is.not.within.its.bound.implement";
-
- String description = JavaErrorMessages.message(messageKey,
- referenceClass != null ? HighlightUtil.formatClass(referenceClass) : type.getPresentableText(),
- JavaHighlightUtil.formatType(bound));
-
- final HighlightInfo info =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement2Highlight).descriptionAndTooltip(description).create();
- if (bound instanceof PsiClassType && referenceClass != null && info != null) {
- QuickFixAction
- .registerQuickFixAction(info, QUICK_FIX_FACTORY.createExtendsListFix(referenceClass, (PsiClassType)bound, true),
- null);
- }
- return info;
- }
- }
- return null;
- }
-
- private static boolean checkNotInBounds(PsiType type, PsiType bound, PsiReferenceParameterList referenceParameterList) {
- if (type instanceof PsiClassType) {
- return checkNotAssignable(bound, type, allowUncheckedConversions((PsiClassType)type, referenceParameterList));
- }
- if (type instanceof PsiWildcardType) {
- if (((PsiWildcardType)type).isExtends()) {
- return checkExtendsWildcardCaptureFailure((PsiWildcardType)type, bound);
- }
- else if (((PsiWildcardType)type).isSuper()) {
- return checkNotAssignable(bound, ((PsiWildcardType)type).getSuperBound(), false);
- }
- }
- else if (type instanceof PsiArrayType) {
- return checkNotAssignable(bound, type, true);
- }
- return false;
- }
-
- //JLS 5.1.10
- private static boolean checkExtendsWildcardCaptureFailure(PsiWildcardType type, PsiType bound) {
- LOG.assertTrue(type.isExtends());
- final PsiType extendsBound = type.getExtendsBound();
- PsiType boundBound = bound;
- if (bound instanceof PsiWildcardType) {
- if (((PsiWildcardType)bound).isBounded()) {
- boundBound = ((PsiWildcardType)bound).isSuper()
- ? ((PsiWildcardType)bound).getSuperBound()
- : ((PsiWildcardType)bound).getExtendsBound();
- }
- else {
- return false;
- }
- }
- return !TypeConversionUtil.areTypesConvertible(boundBound, extendsBound) &&
- !TypeConversionUtil.areTypesConvertible(extendsBound, boundBound);
- }
-
- private static boolean checkNotAssignable(final PsiType bound,
- final PsiType type,
- final boolean allowUncheckedConversion) {
- if (bound instanceof PsiWildcardType) {
- if (((PsiWildcardType)bound).isBounded()) {
- final PsiType boundBound = ((PsiWildcardType)bound).isExtends()
- ? ((PsiWildcardType)bound).getExtendsBound()
- : ((PsiWildcardType)bound).getSuperBound();
- return !TypeConversionUtil.isAssignable(boundBound, type, allowUncheckedConversion);
- }
- else {
- return true;
- }
- }
- else {
- return !TypeConversionUtil.isAssignable(bound, type, allowUncheckedConversion);
- }
- }
-
- private static boolean allowUncheckedConversions(PsiClassType type, PsiReferenceParameterList referenceParameterList) {
- final PsiClass psiClass = type.resolve();
- if (psiClass != null) {
- for (PsiTypeParameter parameter : PsiUtil.typeParametersIterable(psiClass)) {
- if (parameter.getExtendsListTypes().length != 0) {
- return false;
- }
- }
- if (psiClass instanceof PsiTypeParameter && psiClass.getExtendsListTypes().length != 0) return false;
- }
- if (!type.isRaw()) return true;
- //allow unchecked conversions in method calls but not in type declaration
- return referenceParameterList.getParent() instanceof PsiReferenceExpression;
- }
-
- private static String typeParameterListOwnerDescription(final PsiTypeParameterListOwner typeParameterListOwner) {
- if (typeParameterListOwner instanceof PsiClass) {
- return HighlightUtil.formatClass((PsiClass)typeParameterListOwner);
- }
- else if (typeParameterListOwner instanceof PsiMethod) {
- return JavaHighlightUtil.formatMethod((PsiMethod)typeParameterListOwner);
- }
- else {
- LOG.error("Unknown " + typeParameterListOwner);
- return "?";
- }
- }
-
- private static String typeParameterListOwnerCategoryDescription(final PsiTypeParameterListOwner typeParameterListOwner) {
- if (typeParameterListOwner instanceof PsiClass) {
- return JavaErrorMessages.message("generics.holder.type");
- }
- else if (typeParameterListOwner instanceof PsiMethod) {
- return JavaErrorMessages.message("generics.holder.method");
- }
- else {
- LOG.error("Unknown " + typeParameterListOwner);
- return "?";
- }
- }
-
- @Nullable
- public static HighlightInfo checkElementInTypeParameterExtendsList(@NotNull PsiReferenceList referenceList,
- @NotNull PsiClass aClass,
- @NotNull JavaResolveResult resolveResult,
- @NotNull PsiElement element,
- @NotNull LanguageLevel languageLevel) {
- final PsiJavaCodeReferenceElement[] referenceElements = referenceList.getReferenceElements();
- PsiClass extendFrom = (PsiClass)resolveResult.getElement();
- if (extendFrom == null) return null;
- HighlightInfo errorResult = null;
- if (!extendFrom.isInterface() && referenceElements.length != 0 && element != referenceElements[0]) {
- String description = JavaErrorMessages.message("interface.expected");
- errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
- PsiClassType type =
- JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createType(extendFrom, resolveResult.getSubstitutor());
- QuickFixAction.registerQuickFixAction(errorResult, new MoveBoundClassToFrontFix(aClass, type), null);
- }
- else if (referenceElements.length != 0 && element != referenceElements[0] && referenceElements[0].resolve() instanceof PsiTypeParameter) {
- final String description = JavaErrorMessages.message("type.parameter.cannot.be.followed.by.other.bounds");
- errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
- PsiClassType type =
- JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createType(extendFrom, resolveResult.getSubstitutor());
- IntentionAction fix = QUICK_FIX_FACTORY.createExtendsListFix(aClass, type, false);
- QuickFixAction.registerQuickFixAction(errorResult, fix, null);
- }
- if (errorResult == null && languageLevel.isAtLeast(LanguageLevel.JDK_1_7) &&
- referenceElements.length > 1) {
- //todo suppress erased methods which come from the same class
- return checkOverrideEquivalentMethods(aClass);
- }
- return errorResult;
- }
-
- public static HighlightInfo checkInterfaceMultipleInheritance(PsiClass aClass) {
- final PsiClassType[] types = aClass.getSuperTypes();
- if (types.length < 2) return null;
- Map<PsiClass, PsiSubstitutor> inheritedClasses = new HashMap<PsiClass, PsiSubstitutor>();
- final TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
- return checkInterfaceMultipleInheritance(aClass,
- PsiSubstitutor.EMPTY, inheritedClasses,
- new HashSet<PsiClass>(), textRange);
- }
-
- private static HighlightInfo checkInterfaceMultipleInheritance(PsiClass aClass,
- PsiSubstitutor derivedSubstitutor,
- Map<PsiClass, PsiSubstitutor> inheritedClasses,
- Set<PsiClass> visited,
- TextRange textRange) {
- final PsiClassType[] superTypes = aClass.getSuperTypes();
- for (PsiClassType superType : superTypes) {
- final PsiClassType.ClassResolveResult result = superType.resolveGenerics();
- final PsiClass superClass = result.getElement();
- if (superClass == null || visited.contains(superClass)) continue;
- PsiSubstitutor superTypeSubstitutor = result.getSubstitutor();
- superTypeSubstitutor = MethodSignatureUtil.combineSubstitutors(superTypeSubstitutor, derivedSubstitutor);
-
- final PsiSubstitutor inheritedSubstitutor = inheritedClasses.get(superClass);
- if (inheritedSubstitutor != null) {
- final PsiTypeParameter[] typeParameters = superClass.getTypeParameters();
- for (PsiTypeParameter typeParameter : typeParameters) {
- PsiType type1 = GenericsUtil.eliminateWildcards(inheritedSubstitutor.substitute(typeParameter));
- PsiType type2 = GenericsUtil.eliminateWildcards(superTypeSubstitutor.substitute(typeParameter));
-
- if (!Comparing.equal(type1, type2)) {
- String description = JavaErrorMessages.message("generics.cannot.be.inherited.with.different.type.arguments",
- HighlightUtil.formatClass(superClass),
- JavaHighlightUtil.formatType(type1),
- JavaHighlightUtil.formatType(type2));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- }
- }
- }
- inheritedClasses.put(superClass, superTypeSubstitutor);
- visited.add(superClass);
- final HighlightInfo highlightInfo = checkInterfaceMultipleInheritance(superClass, superTypeSubstitutor, inheritedClasses, visited, textRange);
- visited.remove(superClass);
-
- if (highlightInfo != null) return highlightInfo;
- }
- return null;
- }
-
- public static HighlightInfo checkOverrideEquivalentMethods(@NotNull PsiClass aClass) {
- final Collection<HierarchicalMethodSignature> signaturesWithSupers = aClass.getVisibleSignatures();
- PsiManager manager = aClass.getManager();
- Map<MethodSignature, MethodSignatureBackedByPsiMethod> sameErasureMethods =
- new THashMap<MethodSignature, MethodSignatureBackedByPsiMethod>(MethodSignatureUtil.METHOD_PARAMETERS_ERASURE_EQUALITY);
-
- for (HierarchicalMethodSignature signature : signaturesWithSupers) {
- HighlightInfo info = checkSameErasureNotSubSignatureInner(signature, manager, aClass, sameErasureMethods);
- if (info != null) return info;
- }
-
- final PsiIdentifier classIdentifier = aClass.getNameIdentifier();
- if (PsiUtil.isLanguageLevel8OrHigher(aClass) && classIdentifier != null) {
- final HighlightInfo info = checkUnrelatedDefaultMethods(aClass, signaturesWithSupers, classIdentifier);
- if (info != null) return info;
- }
-
- return null;
- }
-
- private static HighlightInfo checkUnrelatedDefaultMethods(PsiClass aClass,
- Collection<HierarchicalMethodSignature> signaturesWithSupers,
- PsiIdentifier classIdentifier) {
- for (HierarchicalMethodSignature methodSignature : signaturesWithSupers) {
- final PsiMethod method = methodSignature.getMethod();
- if (method.hasModifierProperty(PsiModifier.DEFAULT)) {
- final PsiClass containingClass = method.getContainingClass();
- List<HierarchicalMethodSignature> superSignatures = methodSignature.getSuperSignatures();
- if (!superSignatures.isEmpty()) {
- for (HierarchicalMethodSignature signature : superSignatures) {
- final PsiMethod superMethod = signature.getMethod();
- final PsiClass superContainingClass = superMethod.getContainingClass();
- if (containingClass != null && superContainingClass != null && !InheritanceUtil
- .isInheritorOrSelf(containingClass, superContainingClass, true)) {
- if (superMethod.hasModifierProperty(PsiModifier.DEFAULT)) {
- final String inheritUnrelatedDefaultsMessage = HighlightUtil.formatClass(aClass) + " inherits unrelated defaults for " +
- JavaHighlightUtil.formatMethod(method) + " from types " + HighlightUtil.formatClass(containingClass) +
- " and " + HighlightUtil.formatClass(superContainingClass);
- return HighlightInfo
- .newHighlightInfo(HighlightInfoType.ERROR).range(classIdentifier).descriptionAndTooltip(inheritUnrelatedDefaultsMessage).create();
- }
- if (!aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
- final String message = JavaErrorMessages.message(
- aClass instanceof PsiEnumConstantInitializer ? "enum.constant.should.implement.method" : "class.must.be.abstract",
- HighlightUtil.formatClass(superContainingClass),
- JavaHighlightUtil.formatMethod(superMethod),
- HighlightUtil.formatClass(superContainingClass, false));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(classIdentifier).descriptionAndTooltip(message).create();
- }
- }
- }
- }
- }
- }
- return null;
- }
-
- @Nullable
- private static HighlightInfo checkSameErasureNotSubSignatureInner(@NotNull HierarchicalMethodSignature signature,
- @NotNull PsiManager manager,
- @NotNull PsiClass aClass,
- @NotNull Map<MethodSignature, MethodSignatureBackedByPsiMethod> sameErasureMethods) {
- PsiMethod method = signature.getMethod();
- JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
- if (!facade.getResolveHelper().isAccessible(method, aClass, null)) return null;
- MethodSignature signatureToErase = method.getSignature(PsiSubstitutor.EMPTY);
- MethodSignatureBackedByPsiMethod sameErasure = sameErasureMethods.get(signatureToErase);
- HighlightInfo info;
- if (sameErasure != null) {
- if (aClass instanceof PsiTypeParameter ||
- MethodSignatureUtil.findMethodBySuperMethod(aClass, sameErasure.getMethod(), false) != null ||
- !(InheritanceUtil.isInheritorOrSelf(sameErasure.getMethod().getContainingClass(), method.getContainingClass(), true) ||
- InheritanceUtil.isInheritorOrSelf(method.getContainingClass(), sameErasure.getMethod().getContainingClass(), true))) {
- info = checkSameErasureNotSubSignatureOrSameClass(sameErasure, signature, aClass, method);
- if (info != null) return info;
- }
- }
- else {
- sameErasureMethods.put(signatureToErase, signature);
- }
- List<HierarchicalMethodSignature> supers = signature.getSuperSignatures();
- for (HierarchicalMethodSignature superSignature : supers) {
- info = checkSameErasureNotSubSignatureInner(superSignature, manager, aClass, sameErasureMethods);
- if (info != null) return info;
-
- if (superSignature.isRaw() && !signature.isRaw()) {
- final PsiType[] parameterTypes = signature.getParameterTypes();
- PsiType[] erasedTypes = superSignature.getErasedParameterTypes();
- for (int i = 0; i < erasedTypes.length; i++) {
- if (!Comparing.equal(parameterTypes[i], erasedTypes[i])) {
- return getSameErasureMessage(false, method, superSignature.getMethod(),
- HighlightNamesUtil.getClassDeclarationTextRange(aClass));
- }
- }
- }
-
- }
- return null;
- }
-
- @Nullable
- private static HighlightInfo checkSameErasureNotSubSignatureOrSameClass(final MethodSignatureBackedByPsiMethod signatureToCheck,
- final HierarchicalMethodSignature superSignature,
- final PsiClass aClass,
- final PsiMethod superMethod) {
- final PsiMethod checkMethod = signatureToCheck.getMethod();
- if (superMethod.equals(checkMethod)) return null;
- PsiClass checkContainingClass = checkMethod.getContainingClass();
- LOG.assertTrue(checkContainingClass != null);
- PsiClass superContainingClass = superMethod.getContainingClass();
- boolean checkEqualsSuper = checkContainingClass.equals(superContainingClass);
- if (checkMethod.isConstructor()) {
- if (!superMethod.isConstructor() || !checkEqualsSuper) return null;
- }
- else if (superMethod.isConstructor()) return null;
-
- final boolean atLeast17 = JavaVersionService.getInstance().isAtLeast(aClass, JavaSdkVersion.JDK_1_7);
- if (checkMethod.hasModifierProperty(PsiModifier.STATIC) && !checkEqualsSuper && !atLeast17) {
- return null;
- }
-
- final PsiType retErasure1 = TypeConversionUtil.erasure(checkMethod.getReturnType());
- final PsiType retErasure2 = TypeConversionUtil.erasure(superMethod.getReturnType());
-
- boolean differentReturnTypeErasure = !Comparing.equal(retErasure1, retErasure2);
- if (checkEqualsSuper && atLeast17) {
- if (retErasure1 != null && retErasure2 != null) {
- differentReturnTypeErasure = !TypeConversionUtil.isAssignable(retErasure1, retErasure2);
- }
- else {
- differentReturnTypeErasure = !(retErasure1 == null && retErasure2 == null);
- }
- }
-
- if (differentReturnTypeErasure &&
- !TypeConversionUtil.isVoidType(retErasure1) &&
- !TypeConversionUtil.isVoidType(retErasure2) &&
- !(checkEqualsSuper && Arrays.equals(superSignature.getParameterTypes(), signatureToCheck.getParameterTypes())) &&
- !atLeast17) {
- int idx = 0;
- final PsiType[] erasedTypes = signatureToCheck.getErasedParameterTypes();
- boolean erasure = erasedTypes.length > 0;
- for (PsiType type : superSignature.getParameterTypes()) {
- erasure &= Comparing.equal(type, erasedTypes[idx]);
- idx++;
- }
-
- if (!erasure) return null;
- }
-
- if (!checkEqualsSuper && MethodSignatureUtil.isSubsignature(superSignature, signatureToCheck)) {
- return null;
- }
- if (superContainingClass != null && !superContainingClass.isInterface() && checkContainingClass.isInterface() && !aClass.equals(superContainingClass)) return null;
- if (aClass.equals(checkContainingClass)) {
- boolean sameClass = aClass.equals(superContainingClass);
- return getSameErasureMessage(sameClass, checkMethod, superMethod, HighlightNamesUtil.getMethodDeclarationTextRange(checkMethod));
- }
- else {
- return getSameErasureMessage(false, checkMethod, superMethod, HighlightNamesUtil.getClassDeclarationTextRange(aClass));
- }
- }
-
- private static HighlightInfo getSameErasureMessage(final boolean sameClass, @NotNull PsiMethod method, @NotNull PsiMethod superMethod,
- TextRange textRange) {
- @NonNls final String key = sameClass ? "generics.methods.have.same.erasure" :
- method.hasModifierProperty(PsiModifier.STATIC) ?
- "generics.methods.have.same.erasure.hide" :
- "generics.methods.have.same.erasure.override";
- String description = JavaErrorMessages.message(key, HighlightMethodUtil.createClashMethodMessage(method, superMethod, !sameClass));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- }
-
- public static HighlightInfo checkTypeParameterInstantiation(PsiNewExpression expression) {
- PsiJavaCodeReferenceElement classReference = expression.getClassOrAnonymousClassReference();
- if (classReference == null) return null;
- final JavaResolveResult result = classReference.advancedResolve(false);
- final PsiElement element = result.getElement();
- if (element instanceof PsiTypeParameter) {
- String description = JavaErrorMessages.message("generics.type.parameter.cannot.be.instantiated",
- HighlightUtil.formatClass((PsiTypeParameter)element));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(classReference).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
- public static HighlightInfo checkWildcardUsage(PsiTypeElement typeElement) {
- PsiType type = typeElement.getType();
- if (type instanceof PsiWildcardType) {
- if (typeElement.getParent() instanceof PsiReferenceParameterList) {
- PsiElement parent = typeElement.getParent().getParent();
- LOG.assertTrue(parent instanceof PsiJavaCodeReferenceElement, parent);
- PsiElement refParent = parent.getParent();
- if (refParent instanceof PsiAnonymousClass) refParent = refParent.getParent();
- if (refParent instanceof PsiNewExpression) {
- PsiNewExpression newExpression = (PsiNewExpression)refParent;
- if (!(newExpression.getType() instanceof PsiArrayType)) {
- String description = JavaErrorMessages.message("wildcard.type.cannot.be.instantiated", JavaHighlightUtil.formatType(type));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement).descriptionAndTooltip(description).create();
- }
- }
- else if (refParent instanceof PsiReferenceList) {
- PsiElement refPParent = refParent.getParent();
- if (!(refPParent instanceof PsiTypeParameter) || refParent != ((PsiTypeParameter)refPParent).getExtendsList()) {
- String description = JavaErrorMessages.message("generics.wildcard.not.expected");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement).descriptionAndTooltip(description).create();
- }
- }
- }
- else {
- String description = JavaErrorMessages.message("generics.wildcards.may.be.used.only.as.reference.parameters");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement).descriptionAndTooltip(description).create();
- }
- }
-
- return null;
- }
-
- public static HighlightInfo checkReferenceTypeUsedAsTypeArgument(final PsiTypeElement typeElement) {
- final PsiType type = typeElement.getType();
- if (type != PsiType.NULL && type instanceof PsiPrimitiveType ||
- type instanceof PsiWildcardType && ((PsiWildcardType)type).getBound() instanceof PsiPrimitiveType) {
- final PsiElement element = new PsiMatcherImpl(typeElement)
- .parent(PsiMatchers.hasClass(PsiReferenceParameterList.class))
- .parent(PsiMatchers.hasClass(PsiJavaCodeReferenceElement.class, PsiNewExpression.class))
- .getElement();
- if (element == null) return null;
-
- String description = JavaErrorMessages.message("generics.type.argument.cannot.be.of.primitive.type");
- final HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement).descriptionAndTooltip(description).create();
- PsiType toConvert = type;
- if (type instanceof PsiWildcardType) {
- toConvert = ((PsiWildcardType)type).getBound();
- }
- if (toConvert instanceof PsiPrimitiveType) {
- final PsiClassType boxedType = ((PsiPrimitiveType)toConvert).getBoxedType(typeElement);
- if (boxedType != null) {
- QuickFixAction.registerQuickFixAction(highlightInfo,
- new ReplacePrimitiveWithBoxedTypeAction(typeElement, toConvert.getPresentableText(), ((PsiPrimitiveType)toConvert).getBoxedTypeName()));
- }
- }
- return highlightInfo;
- }
-
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkForeachLoopParameterType(PsiForeachStatement statement) {
- final PsiParameter parameter = statement.getIterationParameter();
- final PsiExpression expression = statement.getIteratedValue();
- if (expression == null || expression.getType() == null) return null;
- final PsiType itemType = JavaGenericsUtil.getCollectionItemType(expression);
- if (itemType == null) {
- String description = JavaErrorMessages.message("foreach.not.applicable",
- JavaHighlightUtil.formatType(expression.getType()));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
- }
- final int start = parameter.getTextRange().getStartOffset();
- final int end = expression.getTextRange().getEndOffset();
- final PsiType parameterType = parameter.getType();
- HighlightInfo highlightInfo = HighlightUtil.checkAssignability(parameterType, itemType, null, new TextRange(start, end), 0);
- if (highlightInfo != null) {
- HighlightUtil.registerChangeVariableTypeFixes(parameter, itemType, highlightInfo);
- }
- return highlightInfo;
- }
-
- //http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9.2
- @Nullable
- public static HighlightInfo checkAccessStaticFieldFromEnumConstructor(@NotNull PsiReferenceExpression expr, @NotNull JavaResolveResult result) {
- final PsiElement resolved = result.getElement();
-
- if (!(resolved instanceof PsiField)) return null;
- if (!((PsiModifierListOwner)resolved).hasModifierProperty(PsiModifier.STATIC)) return null;
- final PsiMember constructorOrInitializer = PsiUtil.findEnclosingConstructorOrInitializer(expr);
- if (constructorOrInitializer == null) return null;
- if (constructorOrInitializer.hasModifierProperty(PsiModifier.STATIC)) return null;
- final PsiClass aClass = constructorOrInitializer.getContainingClass();
- if (aClass == null || !(aClass.isEnum() || aClass instanceof PsiEnumConstantInitializer)) return null;
- final PsiField field = (PsiField)resolved;
- if (aClass instanceof PsiEnumConstantInitializer) {
- if (field.getContainingClass() != aClass.getSuperClass()) return null;
- } else if (field.getContainingClass() != aClass) return null;
-
-
- if (!JavaVersionService.getInstance().isAtLeast(field, JavaSdkVersion.JDK_1_6)) {
- final PsiType type = field.getType();
- if (type instanceof PsiClassType && ((PsiClassType)type).resolve() == aClass) return null;
- }
-
- if (PsiUtil.isCompileTimeConstant(field)) return null;
-
- String description = JavaErrorMessages.message(
- "illegal.to.access.static.member.from.enum.constructor.or.instance.initializer",
- HighlightMessageUtil.getSymbolName(resolved, result.getSubstitutor())
- );
-
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expr).descriptionAndTooltip(description).create();
- }
-
- @Nullable
- public static HighlightInfo checkEnumInstantiation(PsiNewExpression expression) {
- final PsiType type = expression.getType();
- if (type instanceof PsiClassType) {
- final PsiClass aClass = ((PsiClassType)type).resolve();
- if (aClass != null && aClass.isEnum()) {
- String description = JavaErrorMessages.message("enum.types.cannot.be.instantiated");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
- }
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkGenericArrayCreation(PsiElement element, PsiType type) {
- if (type instanceof PsiArrayType) {
- if (!JavaGenericsUtil.isReifiableType(((PsiArrayType)type).getComponentType())) {
- String description = JavaErrorMessages.message("generic.array.creation");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
- }
- }
-
- return null;
- }
-
- private static final MethodSignature ourValuesEnumSyntheticMethod = MethodSignatureUtil.createMethodSignature("values",
- PsiType.EMPTY_ARRAY,
- PsiTypeParameter.EMPTY_ARRAY,
- PsiSubstitutor.EMPTY);
-
- public static boolean isEnumSyntheticMethod(MethodSignature methodSignature, Project project) {
- if (methodSignature.equals(ourValuesEnumSyntheticMethod)) return true;
- final PsiType javaLangString = PsiType.getJavaLangString(PsiManager.getInstance(project), GlobalSearchScope.allScope(project));
- final MethodSignature valueOfMethod = MethodSignatureUtil.createMethodSignature("valueOf", new PsiType[]{javaLangString}, PsiTypeParameter.EMPTY_ARRAY,
- PsiSubstitutor.EMPTY);
- return valueOfMethod.equals(methodSignature);
- }
-
- @Nullable
- public static HighlightInfo checkTypeParametersList(PsiTypeParameterList parameterList, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- PsiTypeParameter[] typeParameters = parameterList.getTypeParameters();
- if (typeParameters.length == 0) return null;
- HighlightInfo info = HighlightUtil.checkGenericsFeature(parameterList, typeParameters.length, languageLevel, containingFile);
- if (info != null) return info;
-
- final PsiElement parent = parameterList.getParent();
- if (parent instanceof PsiClass && ((PsiClass)parent).isEnum()) {
- String description = JavaErrorMessages.message("generics.enum.may.not.have.type.parameters");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parameterList).descriptionAndTooltip(description).create();
- }
- if (PsiUtil.isAnnotationMethod(parent)) {
- String description = JavaErrorMessages.message("generics.annotation.members.may.not.have.type.parameters");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parameterList).descriptionAndTooltip(description).create();
- }
- if (parent instanceof PsiClass && ((PsiClass)parent).isAnnotationType()) {
- String description = JavaErrorMessages.message("annotation.may.not.have.type.parameters");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parameterList).descriptionAndTooltip(description).create();
- }
-
- for (int i = 0; i < typeParameters.length; i++) {
- final PsiTypeParameter typeParameter1 = typeParameters[i];
- final HighlightInfo cyclicInheritance = HighlightClassUtil.checkCyclicInheritance(typeParameter1);
- if (cyclicInheritance != null) return cyclicInheritance;
- String name1 = typeParameter1.getName();
- for (int j = i + 1; j < typeParameters.length; j++) {
- final PsiTypeParameter typeParameter2 = typeParameters[j];
- String name2 = typeParameter2.getName();
- if (Comparing.strEqual(name1, name2)) {
- String message = JavaErrorMessages.message("generics.duplicate.type.parameter", name1);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeParameter2).descriptionAndTooltip(message).create();
- }
- }
- if (!languageLevel.isAtLeast(LanguageLevel.JDK_1_7)) {
- for (PsiJavaCodeReferenceElement referenceElement : typeParameter1.getExtendsList().getReferenceElements()) {
- final PsiElement resolve = referenceElement.resolve();
- if (resolve instanceof PsiTypeParameter && ArrayUtilRt.find(typeParameters, resolve) > i) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(referenceElement.getTextRange()).descriptionAndTooltip("Illegal forward reference").create();
- }
- }
- }
- }
- return null;
- }
-
- @Nullable
- public static Collection<HighlightInfo> checkCatchParameterIsClass(PsiParameter parameter) {
- if (!(parameter.getDeclarationScope() instanceof PsiCatchSection)) return null;
- final Collection<HighlightInfo> result = ContainerUtil.newArrayList();
-
- final List<PsiTypeElement> typeElements = PsiUtil.getParameterTypeElements(parameter);
- for (PsiTypeElement typeElement : typeElements) {
- final PsiClass aClass = PsiUtil.resolveClassInClassTypeOnly(typeElement.getType());
- if (aClass instanceof PsiTypeParameter) {
- final String message = JavaErrorMessages.message("generics.cannot.catch.type.parameters");
- result.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement).descriptionAndTooltip(message).create());
- }
- }
-
- return result;
- }
-
- public static HighlightInfo checkInstanceOfGenericType(PsiInstanceOfExpression expression) {
- final PsiTypeElement checkTypeElement = expression.getCheckType();
- if (checkTypeElement == null) return null;
- PsiElement ref = checkTypeElement.getInnermostComponentReferenceElement();
- while (ref instanceof PsiJavaCodeReferenceElement) {
- final HighlightInfo result = isIllegalForInstanceOf((PsiJavaCodeReferenceElement)ref, checkTypeElement);
- if (result != null) return result;
- ref = ((PsiQualifiedReference)ref).getQualifier();
- }
- return null;
- }
-
- private static HighlightInfo isIllegalForInstanceOf(PsiJavaCodeReferenceElement ref, final PsiTypeElement typeElement) {
- final PsiElement resolved = ref.resolve();
- if (resolved instanceof PsiTypeParameter) {
- String description = JavaErrorMessages.message("generics.cannot.instanceof.type.parameters");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref).descriptionAndTooltip(description).create();
- }
-
- if (resolved instanceof PsiClass) {
- final PsiClass containingClass = ((PsiClass)resolved).getContainingClass();
- if (containingClass != null &&
- ref.getQualifier() == null &&
- containingClass.getTypeParameters().length > 0 &&
- !((PsiClass)resolved).hasModifierProperty(PsiModifier.STATIC) &&
- ((PsiClass)resolved).getTypeParameters().length == 0) {
- String description = JavaErrorMessages.message("illegal.generic.type.for.instanceof");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement).descriptionAndTooltip(description).create();
- }
- }
-
- final PsiType[] parameters = ref.getTypeParameters();
- for (PsiType parameterType : parameters) {
- if (parameterType != null &&
- !(parameterType instanceof PsiWildcardType && ((PsiWildcardType)parameterType).getBound() == null)) {
- String description = JavaErrorMessages.message("illegal.generic.type.for.instanceof");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement).descriptionAndTooltip(description).create();
- }
- }
-
- return null;
- }
-
- public static HighlightInfo checkClassObjectAccessExpression(PsiClassObjectAccessExpression expression) {
- PsiType type = expression.getOperand().getType();
- if (type instanceof PsiClassType) {
- return canSelectFrom((PsiClassType)type, expression.getOperand());
- }
- if (type instanceof PsiArrayType) {
- final PsiType arrayComponentType = type.getDeepComponentType();
- if (arrayComponentType instanceof PsiClassType) {
- return canSelectFrom((PsiClassType)arrayComponentType, expression.getOperand());
- }
- }
-
- return null;
- }
-
- @Nullable
- private static HighlightInfo canSelectFrom(PsiClassType type, PsiTypeElement operand) {
- PsiClass aClass = type.resolve();
- if (aClass instanceof PsiTypeParameter) {
- String description = JavaErrorMessages.message("cannot.select.dot.class.from.type.variable");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(operand).descriptionAndTooltip(description).create();
- }
- if (type.getParameters().length > 0) {
- return HighlightInfo
- .newHighlightInfo(HighlightInfoType.ERROR).range(operand).descriptionAndTooltip("Cannot select from parameterized type").create();
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkOverrideAnnotation(PsiMethod method, final LanguageLevel languageLevel) {
- PsiModifierList list = method.getModifierList();
- final PsiAnnotation overrideAnnotation = list.findAnnotation("java.lang.Override");
- if (overrideAnnotation == null) {
- return null;
- }
- try {
- MethodSignatureBackedByPsiMethod superMethod = SuperMethodsSearch.search(method, null, true, false).findFirst();
- if (superMethod != null && method.getContainingClass().isInterface() && "clone".equals(superMethod.getName())) {
- final PsiClass containingClass = superMethod.getMethod().getContainingClass();
- if (containingClass != null && CommonClassNames.JAVA_LANG_OBJECT.equals(containingClass.getQualifiedName())) {
- superMethod = null;
- }
- }
- if (superMethod == null) {
- String description = JavaErrorMessages.message("method.does.not.override.super");
- HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(overrideAnnotation).descriptionAndTooltip(description).create();
- PullAsAbstractUpFix.registerQuickFix(highlightInfo, method);
- return highlightInfo;
- }
- PsiClass superClass = superMethod.getMethod().getContainingClass();
- if (languageLevel.equals(LanguageLevel.JDK_1_5) &&
- superClass != null &&
- superClass.isInterface()) {
- String description = JavaErrorMessages.message("override.not.allowed.in.interfaces");
- HighlightInfo info =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(overrideAnnotation).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(info, new IncreaseLanguageLevelFix(LanguageLevel.JDK_1_6));
- return info;
- }
- return null;
- }
- catch (IndexNotReadyException e) {
- return null;
- }
- }
-
- @Nullable
- public static HighlightInfo checkSafeVarargsAnnotation(PsiMethod method) {
- PsiModifierList list = method.getModifierList();
- final PsiAnnotation safeVarargsAnnotation = list.findAnnotation("java.lang.SafeVarargs");
- if (safeVarargsAnnotation == null) {
- return null;
- }
- try {
- if (!method.isVarArgs()) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(safeVarargsAnnotation).descriptionAndTooltip(
- "@SafeVarargs is not allowed on methods with fixed arity").create();
- }
- if (!method.hasModifierProperty(PsiModifier.STATIC) && !method.hasModifierProperty(PsiModifier.FINAL) && !method.isConstructor()) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(safeVarargsAnnotation).descriptionAndTooltip(
- "@SafeVarargs is not allowed on non-final instance methods").create();
- }
-
- final PsiParameter varParameter = method.getParameterList().getParameters()[method.getParameterList().getParametersCount() - 1];
-
- for (PsiReference reference : ReferencesSearch.search(varParameter)) {
- final PsiElement element = reference.getElement();
- if (element instanceof PsiExpression && !PsiUtil.isAccessedForReading((PsiExpression)element)) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.WARNING).range(element).descriptionAndTooltip(
- "@SafeVarargs do not suppress potentially unsafe operations").create();
- }
- }
-
-
- LOG.assertTrue(varParameter.isVarArgs());
- final PsiEllipsisType ellipsisType = (PsiEllipsisType)varParameter.getType();
- final PsiType componentType = ellipsisType.getComponentType();
- if (JavaGenericsUtil.isReifiableType(componentType)) {
- PsiElement element = varParameter.getTypeElement();
- return HighlightInfo.newHighlightInfo(HighlightInfoType.WARNING).range(element).descriptionAndTooltip(
- "@SafeVarargs is not applicable for reifiable types").create();
- }
- return null;
- }
- catch (IndexNotReadyException e) {
- return null;
- }
- }
-
- static void checkEnumConstantForConstructorProblems(PsiEnumConstant enumConstant,
- final HighlightInfoHolder holder,
- @NotNull JavaSdkVersion javaSdkVersion) {
- PsiClass containingClass = enumConstant.getContainingClass();
- if (enumConstant.getInitializingClass() == null) {
- HighlightInfo highlightInfo = HighlightClassUtil.checkInstantiationOfAbstractClass(containingClass, enumConstant.getNameIdentifier());
- if (highlightInfo != null) {
- QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createImplementMethodsFix(enumConstant));
- holder.add(highlightInfo);
- return;
- }
- highlightInfo = HighlightClassUtil.checkClassWithAbstractMethods(enumConstant.getContainingClass(), enumConstant, enumConstant.getNameIdentifier().getTextRange());
- if (highlightInfo != null) {
- holder.add(highlightInfo);
- return;
- }
- }
- PsiClassType type = JavaPsiFacade.getInstance(holder.getProject()).getElementFactory().createType(containingClass);
-
- HighlightMethodUtil.checkConstructorCall(type.resolveGenerics(), enumConstant, type, null, holder, javaSdkVersion);
- }
-
- @Nullable
- public static HighlightInfo checkEnumSuperConstructorCall(PsiMethodCallExpression expr) {
- PsiReferenceExpression methodExpression = expr.getMethodExpression();
- final PsiElement refNameElement = methodExpression.getReferenceNameElement();
- if (refNameElement != null && PsiKeyword.SUPER.equals(refNameElement.getText())) {
- final PsiMember constructor = PsiUtil.findEnclosingConstructorOrInitializer(expr);
- if (constructor instanceof PsiMethod) {
- final PsiClass aClass = constructor.getContainingClass();
- if (aClass != null && aClass.isEnum()) {
- final String message = JavaErrorMessages.message("call.to.super.is.not.allowed.in.enum.constructor");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expr).descriptionAndTooltip(message).create();
- }
- }
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkVarArgParameterIsLast(@NotNull PsiParameter parameter, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- PsiElement declarationScope = parameter.getDeclarationScope();
- if (declarationScope instanceof PsiMethod) {
- PsiParameter[] params = ((PsiMethod)declarationScope).getParameterList().getParameters();
- if (parameter.isVarArgs()) {
- HighlightInfo info = HighlightUtil.checkVarargFeature(parameter, languageLevel,containingFile);
- if (info != null) return info;
-
- if (params[params.length - 1] != parameter) {
- String description = JavaErrorMessages.message("vararg.not.last.parameter");
- info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parameter).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(info, new MakeVarargParameterLastFix(parameter));
- return info;
- }
- }
- }
- return null;
- }
-
- @Nullable
- public static List<HighlightInfo> checkEnumConstantModifierList(PsiModifierList modifierList) {
- List<HighlightInfo> list = null;
- PsiElement[] children = modifierList.getChildren();
- for (PsiElement child : children) {
- if (child instanceof PsiKeyword) {
- if (list == null) {
- list = new ArrayList<HighlightInfo>();
- }
- String description = JavaErrorMessages.message("modifiers.for.enum.constants");
- list.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(child).descriptionAndTooltip(description).create());
- }
- }
- return list;
- }
-
- @Nullable
- public static HighlightInfo checkParametersAllowed(PsiReferenceParameterList refParamList, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- HighlightInfo info = HighlightUtil.checkGenericsFeature(refParamList, refParamList.getTypeParameterElements().length,
- languageLevel, containingFile);
- if (info != null) return info;
-
- if (refParamList.getTextLength() != 0) {
- final PsiElement parent = refParamList.getParent();
- if (parent instanceof PsiReferenceExpression) {
- final PsiElement grandParent = parent.getParent();
- if (!(grandParent instanceof PsiMethodCallExpression) && !(parent instanceof PsiMethodReferenceExpression)) {
- final String message = JavaErrorMessages.message("generics.reference.parameters.not.allowed");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(refParamList).descriptionAndTooltip(message).create();
- }
- }
- }
-
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkParametersOnRaw(PsiReferenceParameterList refParamList) {
- if (refParamList.getTypeArguments().length == 0) return null;
- JavaResolveResult resolveResult = null;
- PsiElement parent = refParamList.getParent();
- PsiElement qualifier = null;
- if (parent instanceof PsiJavaCodeReferenceElement) {
- resolveResult = ((PsiJavaCodeReferenceElement)parent).advancedResolve(false);
- qualifier = ((PsiJavaCodeReferenceElement)parent).getQualifier();
- }
- else if (parent instanceof PsiCallExpression) {
- resolveResult = ((PsiCallExpression)parent).resolveMethodGenerics();
- if (parent instanceof PsiMethodCallExpression) {
- final PsiReferenceExpression methodExpression = ((PsiMethodCallExpression)parent).getMethodExpression();
- qualifier = methodExpression.getQualifier();
- }
- }
- if (resolveResult != null) {
- PsiElement element = resolveResult.getElement();
- if (!(element instanceof PsiTypeParameterListOwner)) return null;
- if (((PsiModifierListOwner)element).hasModifierProperty(PsiModifier.STATIC)) return null;
- if (qualifier instanceof PsiJavaCodeReferenceElement && ((PsiJavaCodeReferenceElement)qualifier).resolve() instanceof PsiTypeParameter) return null;
- PsiClass containingClass = ((PsiMember)element).getContainingClass();
- if (containingClass != null && PsiUtil.isRawSubstitutor(containingClass, resolveResult.getSubstitutor())) {
- if ((parent instanceof PsiCallExpression || parent instanceof PsiMethodReferenceExpression) && PsiUtil.isLanguageLevel7OrHigher(parent)) {
- return null;
- }
-
- if (element instanceof PsiMethod) {
- if (((PsiMethod)element).findSuperMethods().length > 0) return null;
- if (qualifier instanceof PsiReferenceExpression){
- final PsiClass typeParameter = PsiUtil.resolveClassInType(((PsiReferenceExpression)qualifier).getType());
- if (typeParameter instanceof PsiTypeParameter) {
- if (JavaVersionService.getInstance().isAtLeast(containingClass, JavaSdkVersion.JDK_1_7)) return null;
- for (PsiClassType classType : typeParameter.getExtendsListTypes()) {
- final PsiClass resolve = classType.resolve();
- if (resolve != null) {
- final PsiMethod[] superMethods = resolve.findMethodsBySignature((PsiMethod)element, true);
- for (PsiMethod superMethod : superMethods) {
- if (!PsiUtil.isRawSubstitutor(superMethod, resolveResult.getSubstitutor())) {
- return null;
- }
- }
- }
- }
- }
- }
- }
- final String message = element instanceof PsiClass
- ? JavaErrorMessages.message("generics.type.arguments.on.raw.type")
- : JavaErrorMessages.message("generics.type.arguments.on.raw.method");
-
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(refParamList).descriptionAndTooltip(message).create();
- }
- }
- return null;
- }
-
- public static HighlightInfo checkCannotInheritFromEnum(PsiClass superClass, PsiElement elementToHighlight) {
- HighlightInfo errorResult = null;
- if (Comparing.strEqual("java.lang.Enum", superClass.getQualifiedName())) {
- String message = JavaErrorMessages.message("classes.extends.enum");
- errorResult =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(elementToHighlight).descriptionAndTooltip(message).create();
- }
- return errorResult;
- }
-
- public static HighlightInfo checkGenericCannotExtendException(PsiReferenceList list) {
- PsiElement parent = list.getParent();
- if (!(parent instanceof PsiClass)) return null;
- PsiClass aClass = (PsiClass)parent;
-
- if (!aClass.hasTypeParameters() || aClass.getExtendsList() != list) return null;
- PsiJavaCodeReferenceElement[] referenceElements = list.getReferenceElements();
- PsiClass throwableClass = null;
- for (PsiJavaCodeReferenceElement referenceElement : referenceElements) {
- PsiElement resolved = referenceElement.resolve();
- if (!(resolved instanceof PsiClass)) continue;
- if (throwableClass == null) {
- throwableClass = JavaPsiFacade.getInstance(aClass.getProject()).findClass("java.lang.Throwable", aClass.getResolveScope());
- }
- if (InheritanceUtil.isInheritorOrSelf((PsiClass)resolved, throwableClass, true)) {
- String message = JavaErrorMessages.message("generic.extend.exception");
- HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(referenceElement).descriptionAndTooltip(message).create();
- PsiClassType classType = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createType((PsiClass)resolved);
- QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createExtendsListFix(aClass, classType, false));
- return highlightInfo;
- }
- }
- return null;
- }
-
- public static HighlightInfo checkEnumMustNotBeLocal(final PsiClass aClass) {
- if (!aClass.isEnum()) return null;
- PsiElement parent = aClass.getParent();
- if (!(parent instanceof PsiClass || parent instanceof PsiFile)) {
- String description = JavaErrorMessages.message("local.enum");
- TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
- public static HighlightInfo checkSelectStaticClassFromParameterizedType(final PsiElement resolved, final PsiJavaCodeReferenceElement ref) {
- if (resolved instanceof PsiClass && ((PsiClass)resolved).hasModifierProperty(PsiModifier.STATIC)) {
- final PsiElement qualifier = ref.getQualifier();
- if (qualifier instanceof PsiJavaCodeReferenceElement) {
- final PsiReferenceParameterList parameterList = ((PsiJavaCodeReferenceElement)qualifier).getParameterList();
- if (parameterList != null && parameterList.getTypeArguments().length > 0) {
- final String message = JavaErrorMessages.message("generics.select.static.class.from.parameterized.type",
- HighlightUtil.formatClass((PsiClass)resolved));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parameterList).descriptionAndTooltip(message).create();
- }
- }
- }
- return null;
- }
-
- public static HighlightInfo checkCannotInheritFromTypeParameter(final PsiClass superClass, final PsiJavaCodeReferenceElement toHighlight) {
- if (superClass instanceof PsiTypeParameter) {
- String description = JavaErrorMessages.message("class.cannot.inherit.from.its.type.parameter");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(toHighlight).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
- /**
- * http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.8
- */
- @Nullable
- public static HighlightInfo checkRawOnParameterizedType(PsiReferenceParameterList list) {
- if (list.getTypeArguments().length > 0) return null;
- final PsiElement parent = list.getParent();
- if (parent instanceof PsiJavaCodeReferenceElement) {
- final PsiElement qualifier = ((PsiJavaCodeReferenceElement)parent).getQualifier();
- if (qualifier instanceof PsiJavaCodeReferenceElement) {
- if (((PsiJavaCodeReferenceElement)qualifier).getTypeParameters().length > 0) {
- final PsiElement resolve = ((PsiJavaCodeReferenceElement)parent).resolve();
- if (resolve instanceof PsiTypeParameterListOwner
- && ((PsiTypeParameterListOwner)resolve).hasTypeParameters()
- && !((PsiTypeParameterListOwner)resolve).hasModifierProperty(PsiModifier.STATIC)) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parent).descriptionAndTooltip(
- "Improper formed type; some type parameters are missing").create();
- }
- }
- }
- }
- return null;
- }
-
- public static HighlightInfo checkCannotPassInner(PsiJavaCodeReferenceElement ref) {
- if (ref.getParent() instanceof PsiTypeElement) {
- final PsiClass psiClass = PsiTreeUtil.getParentOfType(ref, PsiClass.class);
- if (psiClass == null) return null;
- if (PsiTreeUtil.isAncestor(psiClass.getExtendsList(), ref, false) ||
- PsiTreeUtil.isAncestor(psiClass.getImplementsList(), ref, false)) {
- final PsiElement qualifier = ref.getQualifier();
- if (qualifier instanceof PsiJavaCodeReferenceElement && ((PsiJavaCodeReferenceElement)qualifier).resolve() == psiClass) {
- final PsiJavaCodeReferenceElement referenceElement = PsiTreeUtil.getParentOfType(ref, PsiJavaCodeReferenceElement.class);
- if (referenceElement == null) return null;
- final PsiElement typeClass = referenceElement.resolve();
- if (!(typeClass instanceof PsiClass)) return null;
- final PsiElement resolve = ref.resolve();
- final PsiClass containingClass = resolve != null ? ((PsiClass)resolve).getContainingClass() : null;
- if (containingClass == null) return null;
- if (psiClass.isInheritor(containingClass, true) ||
- unqualifiedNestedClassReferenceAccessedViaContainingClassInheritance((PsiClass)typeClass, ((PsiClass)resolve).getExtendsList()) ||
- unqualifiedNestedClassReferenceAccessedViaContainingClassInheritance((PsiClass)typeClass, ((PsiClass)resolve).getImplementsList())) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).descriptionAndTooltip(((PsiClass)resolve).getName() + " is not accessible in current context").range(ref).create();
- }
- }
- }
- }
- return null;
- }
-
- private static boolean unqualifiedNestedClassReferenceAccessedViaContainingClassInheritance(PsiClass containingClass,
- PsiReferenceList referenceList) {
- if (referenceList != null) {
- for (PsiJavaCodeReferenceElement referenceElement : referenceList.getReferenceElements()) {
- if (!referenceElement.isQualified()) {
- final PsiElement superClass = referenceElement.resolve();
- if (superClass instanceof PsiClass) {
- final PsiClass superContainingClass = ((PsiClass)superClass).getContainingClass();
- if (superContainingClass != null && InheritanceUtil.isInheritorOrSelf(containingClass, superContainingClass, true)) {
- return true;
- }
- }
- }
- }
- }
- return false;
- }
-}
-
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightClassUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightClassUtil.java
deleted file mode 100644
index e2babf5f8531..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightClassUtil.java
+++ /dev/null
@@ -1,1030 +0,0 @@
-/*
- * Copyright 2000-2013 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/*
- * Checks and Highlights problems with classes
- * User: cdr
- * Date: Aug 19, 2002
- */
-package com.intellij.codeInsight.daemon.impl.analysis;
-
-import com.intellij.codeInsight.ClassUtil;
-import com.intellij.codeInsight.ExceptionUtil;
-import com.intellij.codeInsight.FileModificationService;
-import com.intellij.codeInsight.daemon.JavaErrorMessages;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
-import com.intellij.codeInsight.daemon.impl.RefCountHolder;
-import com.intellij.codeInsight.daemon.impl.quickfix.*;
-import com.intellij.codeInsight.generation.OverrideImplementUtil;
-import com.intellij.codeInsight.generation.PsiMethodMember;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.codeInsight.intention.QuickFixFactory;
-import com.intellij.ide.util.MemberChooser;
-import com.intellij.openapi.application.ApplicationManager;
-import com.intellij.openapi.application.Result;
-import com.intellij.openapi.command.WriteCommandAction;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.fileTypes.StdFileTypes;
-import com.intellij.openapi.module.Module;
-import com.intellij.openapi.module.ModuleUtilCore;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.Comparing;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.openapi.util.io.FileUtil;
-import com.intellij.openapi.vfs.VirtualFile;
-import com.intellij.psi.*;
-import com.intellij.psi.search.GlobalSearchScope;
-import com.intellij.psi.util.*;
-import com.intellij.refactoring.util.RefactoringChangeUtil;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.*;
-
-public class HighlightClassUtil {
- private static final QuickFixFactory QUICK_FIX_FACTORY = QuickFixFactory.getInstance();
-
- /**
- * new ref(...) or new ref(..) { ... } where ref is abstract class
- */
- @Nullable
- static HighlightInfo checkAbstractInstantiation(PsiJavaCodeReferenceElement ref, PsiElement resolved) {
- PsiElement parent = ref.getParent();
- HighlightInfo highlightInfo = null;
- if (parent instanceof PsiNewExpression && !PsiUtilCore.hasErrorElementChild(parent)) {
- if (((PsiNewExpression)parent).getType() instanceof PsiArrayType) return null;
- if (resolved instanceof PsiClass) {
- highlightInfo = checkInstantiationOfAbstractClass((PsiClass)resolved, ref);
- }
- }
- else if (parent instanceof PsiAnonymousClass
- && parent.getParent() instanceof PsiNewExpression
- && !PsiUtilCore.hasErrorElementChild(parent.getParent())) {
- PsiAnonymousClass aClass = (PsiAnonymousClass)parent;
- highlightInfo = checkClassWithAbstractMethods(aClass, ref.getTextRange());
- }
- return highlightInfo;
- }
-
- @Nullable
- static HighlightInfo checkClassWithAbstractMethods(PsiClass aClass, TextRange range) {
- return checkClassWithAbstractMethods(aClass, aClass, range);
- }
-
- @Nullable
- static HighlightInfo checkClassWithAbstractMethods(PsiClass aClass, PsiElement implementsFixElement, TextRange range) {
- PsiMethod abstractMethod = ClassUtil.getAnyAbstractMethod(aClass);
-
- if (abstractMethod == null) {
- return null;
- }
-
- final PsiClass superClass = abstractMethod.getContainingClass();
- if (superClass == null) {
- return null;
- }
-
- String baseClassName = HighlightUtil.formatClass(aClass, false);
- String methodName = JavaHighlightUtil.formatMethod(abstractMethod);
- String message = JavaErrorMessages.message(aClass instanceof PsiEnumConstantInitializer || implementsFixElement instanceof PsiEnumConstant ? "enum.constant.should.implement.method" : "class.must.be.abstract",
- baseClassName,
- methodName,
- HighlightUtil.formatClass(superClass, false));
-
- HighlightInfo errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range).descriptionAndTooltip(message).create();
- final PsiMethod anyMethodToImplement = ClassUtil.getAnyMethodToImplement(aClass);
- if (anyMethodToImplement != null) {
- if (!anyMethodToImplement.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) ||
- JavaPsiFacade.getInstance(aClass.getProject()).arePackagesTheSame(aClass, superClass)) {
- QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createImplementMethodsFix(implementsFixElement));
- } else {
- QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createModifierListFix(anyMethodToImplement, PsiModifier.PROTECTED, true, true));
- QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createModifierListFix(anyMethodToImplement, PsiModifier.PUBLIC, true, true));
- }
- }
- if (!(aClass instanceof PsiAnonymousClass)
- && HighlightUtil.getIncompatibleModifier(PsiModifier.ABSTRACT, aClass.getModifierList()) == null) {
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(aClass, PsiModifier.ABSTRACT, true, false);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- }
- return errorResult;
- }
-
- @Nullable
- static HighlightInfo checkClassMustBeAbstract(final PsiClass aClass, final TextRange textRange) {
- if (aClass.hasModifierProperty(PsiModifier.ABSTRACT) || aClass.getRBrace() == null || aClass.isEnum() && hasEnumConstants(aClass)) {
- return null;
- }
- return checkClassWithAbstractMethods(aClass, textRange);
- }
-
- @Nullable
- public static HighlightInfo checkInstantiationOfAbstractClass(PsiClass aClass, PsiElement highlightElement) {
- HighlightInfo errorResult = null;
- if (aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
- String baseClassName = aClass.getName();
- String message = JavaErrorMessages.message("abstract.cannot.be.instantiated", baseClassName);
- PsiElement parent = highlightElement.getParent();
- if (parent instanceof PsiNewExpression) {
- errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parent).descriptionAndTooltip(message).create();
- } else {
- errorResult =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(highlightElement).descriptionAndTooltip(message).create();
- }
- final PsiMethod anyAbstractMethod = ClassUtil.getAnyAbstractMethod(aClass);
- if (!aClass.isInterface() && anyAbstractMethod == null) {
- // suggest to make not abstract only if possible
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(aClass, PsiModifier.ABSTRACT, false, false);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- }
- if (anyAbstractMethod != null && parent instanceof PsiNewExpression && ((PsiNewExpression)parent).getClassReference() != null) {
- QuickFixAction.registerQuickFixAction(errorResult, new ImplementAbstractClassMethodsFix(parent));
- }
- }
- return errorResult;
- }
-
- private static boolean hasEnumConstants(PsiClass aClass) {
- PsiField[] fields = aClass.getFields();
- for (PsiField field : fields) {
- if (field instanceof PsiEnumConstant) return true;
- }
- return false;
- }
-
- @Nullable
- static HighlightInfo checkDuplicateTopLevelClass(PsiClass aClass) {
- if (!(aClass.getParent() instanceof PsiFile)) return null;
- String qualifiedName = aClass.getQualifiedName();
- if (qualifiedName == null) return null;
- int numOfClassesToFind = 2;
- if (qualifiedName.contains("$")) {
- qualifiedName = qualifiedName.replaceAll("\\$", ".");
- numOfClassesToFind = 1;
- }
- PsiManager manager = aClass.getManager();
- Module module = ModuleUtilCore.findModuleForPsiElement(aClass);
- if (module == null) return null;
-
- PsiClass[] classes = JavaPsiFacade.getInstance(aClass.getProject()).findClasses(qualifiedName, GlobalSearchScope.moduleScope(module));
- if (classes.length < numOfClassesToFind) return null;
- String dupFileName = null;
- for (PsiClass dupClass : classes) {
- // do not use equals
- if (dupClass != aClass) {
- VirtualFile file = dupClass.getContainingFile().getVirtualFile();
- if (file != null && manager.isInProject(dupClass)) {
- dupFileName = FileUtil.toSystemDependentName(file.getPath());
- break;
- }
- }
- }
- if (dupFileName == null) return null;
- String message = JavaErrorMessages.message("duplicate.class.in.other.file", dupFileName);
- TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
-
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(message).create();
- }
-
- @Nullable
- static HighlightInfo checkDuplicateNestedClass(PsiClass aClass) {
- if (aClass == null) return null;
- PsiElement parent = aClass;
- if (aClass.getParent() instanceof PsiDeclarationStatement) {
- parent = aClass.getParent();
- }
- String name = aClass.getName();
- if (name == null) return null;
- boolean duplicateFound = false;
- boolean checkSiblings = true;
- while (parent != null) {
- if (parent instanceof PsiFile) break;
- PsiElement element = checkSiblings ? parent.getPrevSibling() : null;
- if (element == null) {
- element = parent.getParent();
- // JLS 14.3:
- // The name of a local class C may not be redeclared
- // as a local class of the directly enclosing method, constructor, or initializer block within the scope of C
- // , or a compile-time error occurs.
- // However, a local class declaration may be shadowed (?6.3.1)
- // anywhere inside a class declaration nested within the local class declaration's scope.
- if (element instanceof PsiMethod || element instanceof PsiClass ||
- element instanceof PsiCodeBlock && element.getParent() instanceof PsiClassInitializer) {
- checkSiblings = false;
- }
- }
- parent = element;
-
- if (element instanceof PsiDeclarationStatement) element = PsiTreeUtil.getChildOfType(element, PsiClass.class);
- if (element instanceof PsiClass && name.equals(((PsiClass)element).getName())) {
- duplicateFound = true;
- break;
- }
- }
-
- if (duplicateFound) {
- String message = JavaErrorMessages.message("duplicate.class", name);
- TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(message).create();
- }
- return null;
- }
-
- @Nullable
- static HighlightInfo checkPublicClassInRightFile(PsiKeyword keyword, PsiModifierList psiModifierList) {
- // most test case classes are located in wrong files
- if (ApplicationManager.getApplication().isUnitTestMode()) return null;
-
- if (new PsiMatcherImpl(keyword)
- .dot(PsiMatchers.hasText(PsiModifier.PUBLIC))
- .parent(PsiMatchers.hasClass(PsiModifierList.class))
- .parent(PsiMatchers.hasClass(PsiClass.class))
- .parent(PsiMatchers.hasClass(PsiJavaFile.class))
- .getElement() == null) {
- return null;
- }
-
- PsiClass aClass = (PsiClass)keyword.getParent().getParent();
- PsiJavaFile file = (PsiJavaFile)aClass.getContainingFile();
- VirtualFile virtualFile = file.getVirtualFile();
- HighlightInfo errorResult = null;
- if (virtualFile != null && !aClass.getName().equals(virtualFile.getNameWithoutExtension())) {
- String message = JavaErrorMessages.message("public.class.should.be.named.after.file", aClass.getName());
- TextRange range = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
- errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range).descriptionAndTooltip(message).create();
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(psiModifierList, PsiModifier.PUBLIC, false, false);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- PsiClass[] classes = file.getClasses();
- if (classes.length > 1) {
- QuickFixAction.registerQuickFixAction(errorResult, new MoveClassToSeparateFileFix(aClass));
- }
- for (PsiClass otherClass : classes) {
- if (!otherClass.getManager().areElementsEquivalent(otherClass, aClass) &&
- otherClass.hasModifierProperty(PsiModifier.PUBLIC) &&
- otherClass.getName().equals(virtualFile.getNameWithoutExtension())) {
- return errorResult;
- }
- }
- QuickFixAction.registerQuickFixAction(errorResult, new RenameFileFix(aClass.getName() + "." + StdFileTypes.JAVA.getDefaultExtension()));
- QuickFixAction.registerQuickFixAction(errorResult, new RenameElementFix(aClass));
- }
- return errorResult;
- }
-
- @Nullable
- static HighlightInfo checkClassAndPackageConflict(@NotNull PsiClass aClass) {
- String name = aClass.getQualifiedName();
-
- if (CommonClassNames.DEFAULT_PACKAGE.equals(name)) {
- String message = JavaErrorMessages.message("class.clashes.with.package", name);
- TextRange range = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range).descriptionAndTooltip(message).create();
- }
-
- PsiElement file = aClass.getParent();
- if (file instanceof PsiJavaFile && !((PsiJavaFile)file).getPackageName().isEmpty()) {
- PsiElement directory = file.getParent();
- if (directory instanceof PsiDirectory) {
- String simpleName = aClass.getName();
- PsiDirectory subDirectory = ((PsiDirectory)directory).findSubdirectory(simpleName);
- if (subDirectory != null && simpleName.equals(subDirectory.getName())) {
- String message = JavaErrorMessages.message("class.clashes.with.package", name);
- TextRange range = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range).descriptionAndTooltip(message).create();
- }
- }
- }
-
- return null;
- }
-
- @Nullable
- private static HighlightInfo checkStaticFieldDeclarationInInnerClass(PsiKeyword keyword) {
- if (getEnclosingStaticClass(keyword, PsiField.class) == null) {
- return null;
- }
-
- PsiField field = (PsiField)keyword.getParent().getParent();
- if (PsiUtilCore.hasErrorElementChild(field) || PsiUtil.isCompileTimeConstant(field)) {
- return null;
- }
-
- String message = JavaErrorMessages.message("static.declaration.in.inner.class");
- HighlightInfo result = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(keyword).descriptionAndTooltip(message).create();
-
- QuickFixAction.registerQuickFixAction(result, QUICK_FIX_FACTORY.createModifierListFix(field, PsiModifier.STATIC, false, false));
-
- PsiClass aClass = field.getContainingClass();
- if (aClass != null) {
- QuickFixAction.registerQuickFixAction(result, QUICK_FIX_FACTORY.createModifierListFix(aClass, PsiModifier.STATIC, true, false));
- }
-
- return result;
- }
-
- @Nullable
- private static HighlightInfo checkStaticMethodDeclarationInInnerClass(PsiKeyword keyword) {
- if (getEnclosingStaticClass(keyword, PsiMethod.class) == null) {
- return null;
- }
- PsiMethod method = (PsiMethod)keyword.getParent().getParent();
- if (PsiUtilCore.hasErrorElementChild(method)) return null;
- String message = JavaErrorMessages.message("static.declaration.in.inner.class");
- HighlightInfo result = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(keyword).descriptionAndTooltip(message).create();
- IntentionAction fix1 = QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.STATIC, false, false);
- QuickFixAction.registerQuickFixAction(result, fix1);
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix((PsiClass)keyword.getParent().getParent().getParent(), PsiModifier.STATIC, true, false);
- QuickFixAction.registerQuickFixAction(result, fix);
- return result;
- }
-
- @Nullable
- private static HighlightInfo checkStaticInitializerDeclarationInInnerClass(PsiKeyword keyword) {
- if (getEnclosingStaticClass(keyword, PsiClassInitializer.class) == null) {
- return null;
- }
- PsiClassInitializer initializer = (PsiClassInitializer)keyword.getParent().getParent();
- if (PsiUtilCore.hasErrorElementChild(initializer)) return null;
- String message = JavaErrorMessages.message("static.declaration.in.inner.class");
- HighlightInfo result = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(keyword).descriptionAndTooltip(message).create();
- IntentionAction fix1 = QUICK_FIX_FACTORY.createModifierListFix(initializer, PsiModifier.STATIC, false, false);
- QuickFixAction.registerQuickFixAction(result, fix1);
- PsiClass owner = (PsiClass)keyword.getParent().getParent().getParent();
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(owner, PsiModifier.STATIC, true, false);
- QuickFixAction.registerQuickFixAction(result, fix);
- return result;
- }
-
- private static PsiElement getEnclosingStaticClass(PsiKeyword keyword, Class<?> parentClass) {
- return new PsiMatcherImpl(keyword)
- .dot(PsiMatchers.hasText(PsiModifier.STATIC))
- .parent(PsiMatchers.hasClass(PsiModifierList.class))
- .parent(PsiMatchers.hasClass(parentClass))
- .parent(PsiMatchers.hasClass(PsiClass.class))
- .dot(JavaMatchers.hasModifier(PsiModifier.STATIC, false))
- .parent(PsiMatchers.hasClass(PsiClass.class, PsiDeclarationStatement.class, PsiNewExpression.class, PsiEnumConstant.class))
- .getElement();
- }
-
- @Nullable
- private static HighlightInfo checkStaticClassDeclarationInInnerClass(PsiKeyword keyword) {
- // keyword points to 'class' or 'interface' or 'enum'
- if (new PsiMatcherImpl(keyword)
- .parent(PsiMatchers.hasClass(PsiClass.class))
- .dot(JavaMatchers.hasModifier(PsiModifier.STATIC, true))
- .parent(PsiMatchers.hasClass(PsiClass.class))
- .dot(JavaMatchers.hasModifier(PsiModifier.STATIC, false))
- .parent(PsiMatchers.hasClass(PsiClass.class, PsiDeclarationStatement.class, PsiNewExpression.class, PsiEnumConstant.class))
- .getElement() == null) {
- return null;
- }
- PsiClass aClass = (PsiClass)keyword.getParent();
- if (PsiUtilCore.hasErrorElementChild(aClass) || aClass.getQualifiedName() == null && !aClass.isInterface()) return null;
- // highlight 'static' keyword if any, or class or interface if not
- PsiElement context = null;
- PsiModifierList modifierList = aClass.getModifierList();
- PsiElement[] children = modifierList.getChildren();
- for (PsiElement element : children) {
- if (Comparing.equal(element.getText(), PsiModifier.STATIC)) {
- context = element;
- break;
- }
- }
- TextRange range = context == null ? null : context.getTextRange();
- if (range == null) {
- range = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
- }
- String message = JavaErrorMessages.message("static.declaration.in.inner.class");
- HighlightInfo errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range).descriptionAndTooltip(message).create();
- if (context != keyword) {
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(aClass, PsiModifier.STATIC, false, false);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- }
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(aClass.getContainingClass(), PsiModifier.STATIC, true, false);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- return errorResult;
- }
-
- @Nullable
- static HighlightInfo checkStaticDeclarationInInnerClass(PsiKeyword keyword) {
- HighlightInfo errorResult = checkStaticFieldDeclarationInInnerClass(keyword);
- if (errorResult != null) return errorResult;
- errorResult = checkStaticMethodDeclarationInInnerClass(keyword);
- if (errorResult != null) return errorResult;
- errorResult = checkStaticClassDeclarationInInnerClass(keyword);
- if (errorResult != null) return errorResult;
- errorResult = checkStaticInitializerDeclarationInInnerClass(keyword);
- if (errorResult != null) return errorResult;
- return null;
- }
-
- @Nullable
- static HighlightInfo checkExtendsAllowed(PsiReferenceList list) {
- if (list.getParent() instanceof PsiClass) {
- PsiClass aClass = (PsiClass)list.getParent();
- if (aClass.isEnum()) {
- boolean isExtends = list.equals(aClass.getExtendsList());
- if (isExtends) {
- String description = JavaErrorMessages.message("extends.after.enum");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).descriptionAndTooltip(description).create();
- }
- }
- }
- return null;
- }
-
- @Nullable
- static HighlightInfo checkImplementsAllowed(PsiReferenceList list) {
- if (list.getParent() instanceof PsiClass) {
- PsiClass aClass = (PsiClass)list.getParent();
- if (aClass.isInterface()) {
- boolean isImplements = list.equals(aClass.getImplementsList());
- if (isImplements) {
- String description = JavaErrorMessages.message("implements.after.interface");
- HighlightInfo result =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).descriptionAndTooltip(description).create();
- final PsiClassType[] referencedTypes = list.getReferencedTypes();
- if (referencedTypes.length > 0) {
- QuickFixAction.registerQuickFixAction(result, new ChangeExtendsToImplementsFix(aClass, referencedTypes[0]));
- }
- return result;
- }
- }
- }
- return null;
- }
-
- @Nullable
- static HighlightInfo checkExtendsClassAndImplementsInterface(PsiReferenceList referenceList,
- JavaResolveResult resolveResult,
- PsiJavaCodeReferenceElement ref) {
- PsiClass aClass = (PsiClass)referenceList.getParent();
- boolean isImplements = referenceList.equals(aClass.getImplementsList());
- boolean isInterface = aClass.isInterface();
- if (isInterface && isImplements) return null;
- boolean mustBeInterface = isImplements || isInterface;
- HighlightInfo errorResult = null;
- PsiClass extendFrom = (PsiClass)resolveResult.getElement();
- if (extendFrom.isInterface() != mustBeInterface) {
- String message = JavaErrorMessages.message(mustBeInterface ? "interface.expected" : "no.interface.expected");
- errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref).descriptionAndTooltip(message).create();
- PsiClassType type =
- JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createType(ref);
- QuickFixAction.registerQuickFixAction(errorResult, new ChangeExtendsToImplementsFix(aClass, type));
- }
- return errorResult;
- }
-
- @Nullable
- static HighlightInfo checkCannotInheritFromFinal(PsiClass superClass, PsiElement elementToHighlight) {
- HighlightInfo errorResult = null;
- if (superClass.hasModifierProperty(PsiModifier.FINAL) || superClass.isEnum()) {
- String message = JavaErrorMessages.message("inheritance.from.final.class", superClass.getQualifiedName());
- errorResult =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(elementToHighlight).descriptionAndTooltip(message).create();
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(superClass, PsiModifier.FINAL, false, false);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- }
- return errorResult;
- }
-
- @Nullable
- static HighlightInfo checkAnonymousInheritFinal(PsiNewExpression expression) {
- PsiAnonymousClass aClass = PsiTreeUtil.getChildOfType(expression, PsiAnonymousClass.class);
- if (aClass == null) return null;
- PsiClassType baseClassReference = aClass.getBaseClassType();
- PsiClass baseClass = baseClassReference.resolve();
- if (baseClass == null) return null;
- return checkCannotInheritFromFinal(baseClass, aClass.getBaseClassReference());
- }
-
- @Nullable
- private static String checkDefaultConstructorThrowsException(PsiMethod constructor, @NotNull PsiClassType[] handledExceptions) {
- PsiClassType[] referencedTypes = constructor.getThrowsList().getReferencedTypes();
- List<PsiClassType> exceptions = new ArrayList<PsiClassType>();
- for (PsiClassType referencedType : referencedTypes) {
- if (!ExceptionUtil.isUncheckedException(referencedType) && !ExceptionUtil.isHandledBy(referencedType, handledExceptions)) {
- exceptions.add(referencedType);
- }
- }
- if (!exceptions.isEmpty()) {
- return HighlightUtil.getUnhandledExceptionsDescriptor(exceptions);
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkClassDoesNotCallSuperConstructorOrHandleExceptions(PsiClass aClass,
- RefCountHolder refCountHolder,
- final PsiResolveHelper resolveHelper) {
- if (aClass.isEnum()) return null;
- // check only no-ctr classes. Problem with specific constructor will be highlighted inside it
- if (aClass.getConstructors().length != 0) return null;
- // find no-args base class ctr
- TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
- return checkBaseClassDefaultConstructorProblem(aClass, refCountHolder, resolveHelper, textRange, PsiClassType.EMPTY_ARRAY);
- }
-
- public static HighlightInfo checkBaseClassDefaultConstructorProblem(PsiClass aClass,
- RefCountHolder refCountHolder,
- PsiResolveHelper resolveHelper,
- TextRange range,
- @NotNull PsiClassType[] handledExceptions) {
- PsiClass baseClass = aClass.getSuperClass();
- if (baseClass == null) return null;
- PsiMethod[] constructors = baseClass.getConstructors();
- if (constructors.length == 0) return null;
-
- for (PsiMethod constructor : constructors) {
- if (resolveHelper.isAccessible(constructor, aClass, null)) {
- if (constructor.getParameterList().getParametersCount() == 0 ||
- constructor.getParameterList().getParametersCount() == 1 && constructor.isVarArgs()
- ) {
- // it is an error if base ctr throws exceptions
- String description = checkDefaultConstructorThrowsException(constructor, handledExceptions);
- if (description != null) {
- HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(info, new CreateConstructorMatchingSuperFix(aClass));
- return info;
- }
- if (refCountHolder != null) {
- refCountHolder.registerLocallyReferenced(constructor);
- }
- return null;
- }
- }
- }
-
- String description = JavaErrorMessages.message("no.default.constructor.available", HighlightUtil.formatClass(baseClass));
-
- HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(info, new CreateConstructorMatchingSuperFix(aClass));
-
- return info;
- }
-
- @Nullable
- static HighlightInfo checkInterfaceCannotBeLocal(PsiClass aClass) {
- if (PsiUtil.isLocalClass(aClass)) {
- TextRange range = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
- String description = JavaErrorMessages.message("interface.cannot.be.local");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkCyclicInheritance(PsiClass aClass) {
- PsiClass circularClass = getCircularClass(aClass, new HashSet<PsiClass>());
- if (circularClass != null) {
- String description = JavaErrorMessages.message("cyclic.inheritance", HighlightUtil.formatClass(circularClass));
- TextRange range = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
- @Nullable
- private static PsiClass getCircularClass(PsiClass aClass, Collection<PsiClass> usedClasses) {
- if (usedClasses.contains(aClass)) {
- return aClass;
- }
- try {
- usedClasses.add(aClass);
- PsiClass[] superTypes = aClass.getSupers();
- for (PsiElement superType : superTypes) {
- while (superType instanceof PsiClass) {
- if (!CommonClassNames.JAVA_LANG_OBJECT.equals(((PsiClass)superType).getQualifiedName())) {
- PsiClass circularClass = getCircularClass((PsiClass)superType, usedClasses);
- if (circularClass != null) return circularClass;
- }
- // check class qualifier
- superType = superType.getParent();
- }
- }
- }
- finally {
- usedClasses.remove(aClass);
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkExtendsDuplicate(PsiJavaCodeReferenceElement element, PsiElement resolved, @NotNull PsiFile containingFile) {
- if (!(element.getParent() instanceof PsiReferenceList)) return null;
- PsiReferenceList list = (PsiReferenceList)element.getParent();
- if (!(list.getParent() instanceof PsiClass)) return null;
- if (!(resolved instanceof PsiClass)) return null;
- PsiClass aClass = (PsiClass)resolved;
- PsiClassType[] referencedTypes = list.getReferencedTypes();
- int dupCount = 0;
- PsiManager manager = containingFile.getManager();
- for (PsiClassType referencedType : referencedTypes) {
- PsiClass resolvedElement = referencedType.resolve();
- if (resolvedElement != null && manager.areElementsEquivalent(resolvedElement, aClass)) {
- dupCount++;
- }
- }
- if (dupCount > 1) {
- String description = JavaErrorMessages.message("duplicate.class", HighlightUtil.formatClass(aClass));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkClassAlreadyImported(PsiClass aClass, PsiElement elementToHighlight) {
- PsiFile file = aClass.getContainingFile();
- if (!(file instanceof PsiJavaFile)) return null;
- PsiJavaFile javaFile = (PsiJavaFile)file;
- // check only top-level classes conflicts
- if (aClass.getParent() != javaFile) return null;
- PsiImportList importList = javaFile.getImportList();
- if (importList == null) return null;
- PsiImportStatementBase[] importStatements = importList.getAllImportStatements();
- for (PsiImportStatementBase importStatement : importStatements) {
- if (importStatement.isOnDemand()) continue;
- PsiElement resolved = importStatement.resolve();
- if (resolved instanceof PsiClass && !resolved.equals(aClass) && Comparing.equal(aClass.getName(), ((PsiClass)resolved).getName(), true)) {
- String description = JavaErrorMessages.message("class.already.imported", HighlightUtil.formatClass(aClass, false));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(elementToHighlight).descriptionAndTooltip(description).create();
- }
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkClassExtendsOnlyOneClass(PsiReferenceList list) {
- PsiClassType[] referencedTypes = list.getReferencedTypes();
- PsiElement parent = list.getParent();
- if (!(parent instanceof PsiClass)) return null;
-
- PsiClass aClass = (PsiClass)parent;
- if (!aClass.isInterface()
- && referencedTypes.length > 1
- && aClass.getExtendsList() == list) {
- String description = JavaErrorMessages.message("class.cannot.extend.multiple.classes");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).descriptionAndTooltip(description).create();
- }
-
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkThingNotAllowedInInterface(PsiElement element, PsiClass aClass) {
- if (aClass == null || !aClass.isInterface()) return null;
- String description = JavaErrorMessages.message("not.allowed.in.interface");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
- }
-
- @Nullable
- public static HighlightInfo checkQualifiedNew(PsiNewExpression expression) {
- PsiExpression qualifier = expression.getQualifier();
- if (qualifier == null) return null;
- PsiType type = expression.getType();
- if (type instanceof PsiArrayType) {
- String description = JavaErrorMessages.message("invalid.qualified.new");
- HighlightInfo info =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(info, new RemoveNewQualifierFix(expression, null));
- return info;
- }
- PsiClass aClass = PsiUtil.resolveClassInType(type);
- HighlightInfo info = null;
- if (aClass != null) {
- if (aClass.hasModifierProperty(PsiModifier.STATIC)) {
- String description = JavaErrorMessages.message("qualified.new.of.static.class");
- info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
- if (!aClass.isEnum()) {
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(aClass, PsiModifier.STATIC, false, false);
- QuickFixAction.registerQuickFixAction(info, fix);
- }
-
- } else if (aClass instanceof PsiAnonymousClass) {
- final PsiClass baseClass = PsiUtil.resolveClassInType(((PsiAnonymousClass)aClass).getBaseClassType());
- if (baseClass != null && baseClass.isInterface()) {
- info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression)
- .descriptionAndTooltip("Anonymous class implements interface; cannot have qualifier for new").create();
- }
- }
- QuickFixAction.registerQuickFixAction(info, new RemoveNewQualifierFix(expression, aClass));
- }
- return info;
- }
-
-
- /**
- * class c extends foreign.inner {}
- *
- * @param extendRef points to the class in the extends list
- * @param resolved extendRef resolved
- */
- @Nullable
- public static HighlightInfo checkClassExtendsForeignInnerClass(final PsiJavaCodeReferenceElement extendRef, final PsiElement resolved) {
- PsiElement parent = extendRef.getParent();
- if (!(parent instanceof PsiReferenceList)) {
- return null;
- }
- PsiElement grand = parent.getParent();
- if (!(grand instanceof PsiClass)) {
- return null;
- }
- final PsiClass aClass = (PsiClass)grand;
- final PsiClass containerClass;
- if (aClass instanceof PsiTypeParameter) {
- final PsiTypeParameterListOwner owner = ((PsiTypeParameter)aClass).getOwner();
- if (!(owner instanceof PsiClass)) {
- return null;
- }
- containerClass = (PsiClass)owner;
- } else {
- containerClass = aClass;
- }
- if (aClass.getExtendsList() != parent && aClass.getImplementsList() != parent) {
- return null;
- }
- if (!(resolved instanceof PsiClass)) {
- String description = JavaErrorMessages.message("class.name.expected");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(extendRef).descriptionAndTooltip(description).create();
- }
- final HighlightInfo[] infos = new HighlightInfo[1];
- extendRef.accept(new JavaRecursiveElementWalkingVisitor() {
- @Override
- public void visitElement(PsiElement element) {
- if (infos[0] != null) return;
- super.visitElement(element);
- }
-
- @Override
- public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
- super.visitReferenceElement(reference);
- final PsiElement resolve = reference.resolve();
- if (resolve instanceof PsiClass) {
- final PsiClass base = (PsiClass)resolve;
- final PsiClass baseClass = base.getContainingClass();
- if (baseClass != null && base.hasModifierProperty(PsiModifier.PRIVATE) && baseClass == containerClass) {
- String description = JavaErrorMessages.message("private.symbol",
- HighlightUtil.formatClass(base),
- HighlightUtil.formatClass(baseClass));
- infos[0] = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(extendRef).descriptionAndTooltip(description).create();
- return;
- }
-
- // must be inner class
- if (!PsiUtil.isInnerClass(base)) return;
-
- if (resolve == resolved && baseClass != null && (!PsiTreeUtil.isAncestor(baseClass, extendRef, true) || aClass.hasModifierProperty(PsiModifier.STATIC)) &&
- !hasEnclosingInstanceInScope(baseClass, extendRef, !aClass.hasModifierProperty(PsiModifier.STATIC), true) && !qualifiedNewCalledInConstructors(aClass, baseClass)) {
- String description = JavaErrorMessages.message("no.enclosing.instance.in.scope", HighlightUtil.formatClass(baseClass));
- infos[0] = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(extendRef).descriptionAndTooltip(description).create();
- }
- }
- }
- });
-
- return infos[0];
- }
-
- private static boolean qualifiedNewCalledInConstructors(final PsiClass aClass, final PsiClass baseClass) {
- PsiMethod[] constructors = aClass.getConstructors();
- if (constructors.length == 0) return false;
- for (PsiMethod constructor : constructors) {
- PsiCodeBlock body = constructor.getBody();
- if (body == null) return false;
- PsiStatement[] statements = body.getStatements();
- if (statements.length == 0) return false;
- PsiStatement firstStatement = statements[0];
- if (!(firstStatement instanceof PsiExpressionStatement)) return false;
- PsiExpression expression = ((PsiExpressionStatement)firstStatement).getExpression();
- if (!RefactoringChangeUtil.isSuperOrThisMethodCall(expression)) return false;
- PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)expression;
- if (PsiKeyword.THIS.equals(methodCallExpression.getMethodExpression().getReferenceName())) continue;
- PsiReferenceExpression referenceExpression = methodCallExpression.getMethodExpression();
- PsiExpression qualifierExpression = PsiUtil.skipParenthesizedExprDown(referenceExpression.getQualifierExpression());
- if (!(qualifierExpression instanceof PsiReferenceExpression) && !(qualifierExpression instanceof PsiNewExpression)) return false;
- PsiType type = qualifierExpression.getType();
- if (!(type instanceof PsiClassType)) return false;
- PsiClass resolved = ((PsiClassType)type).resolve();
- if (resolved != baseClass) return false;
- }
- return true;
- }
-
- public static boolean hasEnclosingInstanceInScope(PsiClass aClass,
- PsiElement scope,
- final boolean isSuperClassAccepted,
- boolean isTypeParamsAccepted) {
- PsiManager manager = aClass.getManager();
- PsiElement place = scope;
- while (place != null && place != aClass && !(place instanceof PsiFile)) {
- if (place instanceof PsiClass) {
- if (isSuperClassAccepted) {
- if (InheritanceUtil.isInheritorOrSelf((PsiClass)place, aClass, true)) return true;
- }
- else {
- if (manager.areElementsEquivalent(place, aClass)) return true;
- }
- if (isTypeParamsAccepted && place instanceof PsiTypeParameter) {
- return true;
- }
- }
- if (place instanceof PsiModifierListOwner) {
- final PsiModifierList modifierList = ((PsiModifierListOwner)place).getModifierList();
- if (modifierList != null && modifierList.hasModifierProperty(PsiModifier.STATIC)) {
- return false;
- }
- }
- place = place.getParent();
- }
- return place == aClass;
- }
-
- @Nullable
- public static HighlightInfo checkCreateInnerClassFromStaticContext(PsiNewExpression expression) {
- PsiType type = expression.getType();
- PsiExpression qualifier = expression.getQualifier();
- if (type == null || type instanceof PsiArrayType || type instanceof PsiPrimitiveType) return null;
- PsiClass aClass = PsiUtil.resolveClassInType(type);
- if (aClass == null) return null;
- if (aClass instanceof PsiAnonymousClass) {
- aClass = ((PsiAnonymousClass)aClass).getBaseClassType().resolve();
- if (aClass == null) return null;
- }
-
- return checkCreateInnerClassFromStaticContext(expression, qualifier, aClass);
- }
-
- @Nullable
- public static HighlightInfo checkCreateInnerClassFromStaticContext(PsiElement element,
- @Nullable PsiExpression qualifier,
- PsiClass aClass) {
- PsiElement placeToSearchEnclosingFrom;
- if (qualifier != null) {
- PsiType qType = qualifier.getType();
- placeToSearchEnclosingFrom = PsiUtil.resolveClassInType(qType);
- }
- else {
- placeToSearchEnclosingFrom = element;
- }
- return checkCreateInnerClassFromStaticContext(element, placeToSearchEnclosingFrom, aClass);
- }
-
- @Nullable
- public static HighlightInfo checkCreateInnerClassFromStaticContext(PsiElement element,
- PsiElement placeToSearchEnclosingFrom,
- PsiClass aClass) {
- if (aClass == null || !PsiUtil.isInnerClass(aClass)) return null;
- PsiClass outerClass = aClass.getContainingClass();
- if (outerClass == null) return null;
-
- if (outerClass instanceof PsiSyntheticClass || hasEnclosingInstanceInScope(outerClass, placeToSearchEnclosingFrom, true, false)) return null;
- return reportIllegalEnclosingUsage(placeToSearchEnclosingFrom, aClass, outerClass, element);
- }
-
- @Nullable
- public static HighlightInfo checkSuperQualifierType(@NotNull Project project, @NotNull PsiMethodCallExpression superCall) {
- if (!RefactoringChangeUtil.isSuperMethodCall(superCall)) return null;
- PsiMethod ctr = PsiTreeUtil.getParentOfType(superCall, PsiMethod.class, true, PsiMember.class);
- if (ctr == null) return null;
- final PsiClass aClass = ctr.getContainingClass();
- if (aClass == null) return null;
- PsiClass targetClass = aClass.getSuperClass();
- if (targetClass == null) return null;
- PsiExpression qualifier = superCall.getMethodExpression().getQualifierExpression();
- if (qualifier != null) {
- if (PsiUtil.isInnerClass(targetClass)) {
- PsiClass outerClass = targetClass.getContainingClass();
- if (outerClass != null) {
- PsiClassType outerType = JavaPsiFacade.getInstance(project).getElementFactory().createType(outerClass);
- return HighlightUtil.checkAssignability(outerType, null, qualifier, qualifier);
- }
- } else {
- String description = "'" + HighlightUtil.formatClass(targetClass) + "' is not an inner class";
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(qualifier).descriptionAndTooltip(description).create();
- }
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo reportIllegalEnclosingUsage(PsiElement place,
- @Nullable PsiClass aClass,
- PsiClass outerClass,
- PsiElement elementToHighlight) {
- if (outerClass != null && !PsiTreeUtil.isContextAncestor(outerClass, place, false)) {
- String description = JavaErrorMessages.message("is.not.an.enclosing.class", HighlightUtil.formatClass(outerClass));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(elementToHighlight).descriptionAndTooltip(description).create();
- }
- PsiModifierListOwner staticParent = PsiUtil.getEnclosingStaticElement(place, outerClass);
- if (staticParent != null) {
- String element = outerClass == null ? "" : HighlightUtil.formatClass(outerClass) + "." +
- (place instanceof PsiSuperExpression ? PsiKeyword.SUPER : PsiKeyword.THIS);
- String description = JavaErrorMessages.message("cannot.be.referenced.from.static.context", element);
- HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(elementToHighlight).descriptionAndTooltip(description).create();
- // make context not static or referenced class static
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(staticParent, PsiModifier.STATIC, false, false);
- QuickFixAction.registerQuickFixAction(highlightInfo, fix);
- if (aClass != null && HighlightUtil.getIncompatibleModifier(PsiModifier.STATIC, aClass.getModifierList()) == null) {
- IntentionAction fix2 = QUICK_FIX_FACTORY.createModifierListFix(aClass, PsiModifier.STATIC, true, false);
- QuickFixAction.registerQuickFixAction(highlightInfo, fix2);
- }
- return highlightInfo;
- }
- return null;
- }
-
- private static class ImplementAbstractClassMethodsFix extends ImplementMethodsFix {
- public ImplementAbstractClassMethodsFix(PsiElement highlightElement) {
- super(highlightElement);
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project,
- @NotNull PsiFile file,
- @NotNull PsiElement startElement,
- @NotNull PsiElement endElement) {
- if (startElement instanceof PsiNewExpression) {
- final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
- String startElementText = startElement.getText();
- try {
- PsiNewExpression newExpression =
- (PsiNewExpression)elementFactory.createExpressionFromText(startElementText + "{}", startElement);
- if (newExpression.getAnonymousClass() == null) {
- try {
- newExpression = (PsiNewExpression)elementFactory.createExpressionFromText(startElementText + "){}", startElement);
- }
- catch (IncorrectOperationException e) {
- return false;
- }
- if (newExpression.getAnonymousClass() == null) return false;
- }
- }
- catch (IncorrectOperationException e) {
- return false;
- }
- return true;
- }
- return false;
- }
-
- @Override
- public void invoke(@NotNull final Project project,
- @NotNull PsiFile file,
- @Nullable("is null when called from inspection") final Editor editor,
- @NotNull final PsiElement startElement,
- @NotNull PsiElement endElement) {
- final PsiFile containingFile = startElement.getContainingFile();
- if (editor == null || !FileModificationService.getInstance().prepareFileForWrite(containingFile)) return;
- PsiJavaCodeReferenceElement classReference = ((PsiNewExpression)startElement).getClassReference();
- if (classReference == null) return;
- final PsiClass psiClass = (PsiClass)classReference.resolve();
- if (psiClass == null) return;
- final MemberChooser<PsiMethodMember> chooser = chooseMethodsToImplement(editor, startElement, psiClass, false);
- if (chooser == null) return;
-
- final List<PsiMethodMember> selectedElements = chooser.getSelectedElements();
- if (selectedElements == null || selectedElements.isEmpty()) return;
-
- new WriteCommandAction(project, file) {
- @Override
- protected void run(final Result result) throws Throwable {
- PsiNewExpression newExpression =
- (PsiNewExpression)JavaPsiFacade.getElementFactory(project).createExpressionFromText(startElement.getText() + "{}", startElement);
- newExpression = (PsiNewExpression)startElement.replace(newExpression);
- final PsiClass psiClass = newExpression.getAnonymousClass();
- if (psiClass == null) return;
- Map<PsiClass, PsiSubstitutor> subst = new HashMap<PsiClass, PsiSubstitutor>();
- for (PsiMethodMember selectedElement : selectedElements) {
- final PsiClass baseClass = selectedElement.getElement().getContainingClass();
- if (baseClass != null) {
- PsiSubstitutor substitutor = subst.get(baseClass);
- if (substitutor == null) {
- substitutor = TypeConversionUtil.getSuperClassSubstitutor(baseClass, psiClass, PsiSubstitutor.EMPTY);
- subst.put(baseClass, substitutor);
- }
- selectedElement.setSubstitutor(substitutor);
- }
- }
- OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(editor, psiClass, selectedElements, chooser.isCopyJavadoc(),
- chooser.isInsertOverrideAnnotation());
- }
- }.execute();
- }
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightControlFlowUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightControlFlowUtil.java
deleted file mode 100644
index 7cc7879c2206..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightControlFlowUtil.java
+++ /dev/null
@@ -1,750 +0,0 @@
-/*
- * Copyright 2000-2013 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.analysis;
-
-import com.intellij.codeInsight.daemon.JavaErrorMessages;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
-import com.intellij.codeInsight.daemon.impl.quickfix.*;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.codeInsight.intention.QuickFixFactory;
-import com.intellij.openapi.project.IndexNotReadyException;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.pom.java.LanguageLevel;
-import com.intellij.psi.*;
-import com.intellij.psi.controlFlow.*;
-import com.intellij.psi.search.LocalSearchScope;
-import com.intellij.psi.search.searches.ReferencesSearch;
-import com.intellij.psi.tree.IElementType;
-import com.intellij.psi.util.PsiTreeUtil;
-import com.intellij.psi.util.PsiUtil;
-import com.intellij.util.Processor;
-import com.intellij.psi.util.FileTypeUtils;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.*;
-
-/**
- * @author cdr
- * @since Aug 8, 2002
- */
-public class HighlightControlFlowUtil {
- private static final QuickFixFactory QUICK_FIX_FACTORY = QuickFixFactory.getInstance();
-
- private HighlightControlFlowUtil() { }
-
- @Nullable
- public static HighlightInfo checkMissingReturnStatement(PsiCodeBlock body, PsiType returnType) {
- if (body == null || returnType == null || PsiType.VOID.equals(returnType.getDeepComponentType())) {
- return null;
- }
-
- // do not compute constant expressions for if() statement condition
- // see JLS 14.20 Unreachable Statements
- try {
- ControlFlow controlFlow = getControlFlowNoConstantEvaluate(body);
- if (!ControlFlowUtil.returnPresent(controlFlow)) {
- PsiJavaToken rBrace = body.getRBrace();
- PsiElement context = rBrace == null ? body.getLastChild() : rBrace;
- String message = JavaErrorMessages.message("missing.return.statement");
- HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(context).descriptionAndTooltip(message).create();
- PsiElement parent = body.getParent();
- if (parent instanceof PsiMethod) {
- PsiMethod method = (PsiMethod)parent;
- QuickFixAction.registerQuickFixAction(info, new AddReturnFix(method));
- IntentionAction fix = QUICK_FIX_FACTORY.createMethodReturnFix(method, PsiType.VOID, true);
- QuickFixAction.registerQuickFixAction(info, fix);
- }
- return info;
- }
- }
- catch (AnalysisCanceledException ignored) { }
-
- return null;
- }
-
- public static ControlFlow getControlFlowNoConstantEvaluate(PsiElement body) throws AnalysisCanceledException {
- LocalsOrMyInstanceFieldsControlFlowPolicy policy = LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance();
- return ControlFlowFactory.getInstance(body.getProject()).getControlFlow(body, policy, false);
- }
-
- private static ControlFlow getControlFlow(PsiElement context) throws AnalysisCanceledException {
- LocalsOrMyInstanceFieldsControlFlowPolicy policy = LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance();
- return ControlFlowFactory.getInstance(context.getProject()).getControlFlow(context, policy);
- }
-
- public static HighlightInfo checkUnreachableStatement(PsiCodeBlock codeBlock) {
- if (codeBlock == null) return null;
- // do not compute constant expressions for if() statement condition
- // see JLS 14.20 Unreachable Statements
- try {
- final ControlFlow controlFlow = getControlFlowNoConstantEvaluate(codeBlock);
- final PsiElement unreachableStatement = ControlFlowUtil.getUnreachableStatement(controlFlow);
- if (unreachableStatement != null) {
- String description = JavaErrorMessages.message("unreachable.statement");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(unreachableStatement).descriptionAndTooltip(description).create();
- }
- }
- catch (AnalysisCanceledException e) {
- // incomplete code
- }
- catch (IndexNotReadyException ignored) {
- }
- return null;
- }
-
- private static boolean isFinalFieldInitialized(PsiField field) {
- if (field.hasInitializer()) return true;
- final boolean isFieldStatic = field.hasModifierProperty(PsiModifier.STATIC);
- final PsiClass aClass = field.getContainingClass();
- if (aClass != null) {
- // field might be assigned in the other field initializers
- if (isFieldInitializedInOtherFieldInitializer(aClass, field, isFieldStatic)) return true;
- }
- final PsiClassInitializer[] initializers;
- if (aClass != null) {
- initializers = aClass.getInitializers();
- }
- else {
- return false;
- }
- for (PsiClassInitializer initializer : initializers) {
- if (initializer.hasModifierProperty(PsiModifier.STATIC) == isFieldStatic
- && variableDefinitelyAssignedIn(field, initializer.getBody())) {
- return true;
- }
- }
- if (isFieldStatic) {
- return false;
- }
- else {
- // instance field should be initialized at the end of the each constructor
- final PsiMethod[] constructors = aClass.getConstructors();
-
- if (constructors.length == 0) return false;
- nextConstructor:
- for (PsiMethod constructor : constructors) {
- PsiCodeBlock ctrBody = constructor.getBody();
- if (ctrBody == null) return false;
- final List<PsiMethod> redirectedConstructors = JavaHighlightUtil.getChainedConstructors(constructor);
- for (int j = 0; redirectedConstructors != null && j < redirectedConstructors.size(); j++) {
- PsiMethod redirectedConstructor = redirectedConstructors.get(j);
- final PsiCodeBlock body = redirectedConstructor.getBody();
- if (body != null && variableDefinitelyAssignedIn(field, body)) continue nextConstructor;
- }
- if (!ctrBody.isValid() || variableDefinitelyAssignedIn(field, ctrBody)) {
- continue;
- }
- return false;
- }
- return true;
- }
- }
-
- private static boolean isFieldInitializedInOtherFieldInitializer(final PsiClass aClass,
- PsiField field,
- final boolean fieldStatic) {
- PsiField[] fields = aClass.getFields();
- for (PsiField psiField : fields) {
- if (psiField != field
- && psiField.hasModifierProperty(PsiModifier.STATIC) == fieldStatic
- && variableDefinitelyAssignedIn(field, psiField)) {
- return true;
- }
- }
- return false;
- }
-
- public static boolean isRecursivelyCalledConstructor(PsiMethod constructor) {
- final JavaHighlightUtil.ConstructorVisitorInfo info = new JavaHighlightUtil.ConstructorVisitorInfo();
- JavaHighlightUtil.visitConstructorChain(constructor, info);
- if (info.recursivelyCalledConstructor == null) return false;
- // our constructor is reached from some other constructor by constructor chain
- return info.visitedConstructors.indexOf(info.recursivelyCalledConstructor) <=
- info.visitedConstructors.indexOf(constructor);
- }
-
- public static boolean isAssigned(final PsiParameter parameter) {
- ParamWriteProcessor processor = new ParamWriteProcessor();
- ReferencesSearch.search(parameter, new LocalSearchScope(parameter.getDeclarationScope()), true).forEach(processor);
- return processor.isWriteRefFound();
- }
-
- private static class ParamWriteProcessor implements Processor<PsiReference> {
- private volatile boolean myIsWriteRefFound = false;
- @Override
- public boolean process(PsiReference reference) {
- final PsiElement element = reference.getElement();
- if (element instanceof PsiReferenceExpression && PsiUtil.isAccessedForWriting((PsiExpression)element)) {
- myIsWriteRefFound = true;
- return false;
- }
- return true;
- }
-
- public boolean isWriteRefFound() {
- return myIsWriteRefFound;
- }
- }
-
- /**
- * see JLS chapter 16
- * @return true if variable assigned (maybe more than once)
- */
- private static boolean variableDefinitelyAssignedIn(PsiVariable variable, PsiElement context) {
- try {
- ControlFlow controlFlow = getControlFlow(context);
- return ControlFlowUtil.isVariableDefinitelyAssigned(variable, controlFlow);
- }
- catch (AnalysisCanceledException e) {
- return false;
- }
- }
-
- private static boolean variableDefinitelyNotAssignedIn(PsiVariable variable, PsiElement context) {
- try {
- ControlFlow controlFlow = getControlFlow(context);
- return ControlFlowUtil.isVariableDefinitelyNotAssigned(variable, controlFlow);
- }
- catch (AnalysisCanceledException e) {
- return false;
- }
- }
-
-
- @Nullable
- public static HighlightInfo checkFinalFieldInitialized(PsiField field) {
- if (!field.hasModifierProperty(PsiModifier.FINAL)) return null;
- if (isFinalFieldInitialized(field)) return null;
-
- String description = JavaErrorMessages.message("variable.not.initialized", field.getName());
- TextRange range = HighlightNamesUtil.getFieldDeclarationTextRange(field);
- HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(highlightInfo, HighlightMethodUtil.getFixRange(field), new CreateConstructorParameterFromFieldFix(field));
- QuickFixAction.registerQuickFixAction(highlightInfo, HighlightMethodUtil.getFixRange(field), new InitializeFinalFieldInConstructorFix(field));
- final PsiClass containingClass = field.getContainingClass();
- if (containingClass != null && !containingClass.isInterface()) {
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(field, PsiModifier.FINAL, false, false);
- QuickFixAction.registerQuickFixAction(highlightInfo, fix);
- }
- QuickFixAction.registerQuickFixAction(highlightInfo, new AddVariableInitializerFix(field));
- return highlightInfo;
- }
-
-
- @Nullable
- public static HighlightInfo checkVariableInitializedBeforeUsage(PsiReferenceExpression expression,
- PsiVariable variable,
- Map<PsiElement, Collection<PsiReferenceExpression>> uninitializedVarProblems, @NotNull PsiFile containingFile) {
- if (variable instanceof ImplicitVariable) return null;
- if (!PsiUtil.isAccessedForReading(expression)) return null;
- final int startOffset = expression.getTextRange().getStartOffset();
- final PsiElement topBlock;
- if (variable.hasInitializer()) {
- topBlock = PsiUtil.getVariableCodeBlock(variable, variable);
- if (topBlock == null) return null;
- }
- else {
- PsiElement scope = variable instanceof PsiField
- ? variable.getContainingFile()
- : variable.getParent() != null ? variable.getParent().getParent() : null;
- if (scope instanceof PsiCodeBlock && scope.getParent() instanceof PsiSwitchStatement) {
- scope = PsiTreeUtil.getParentOfType(scope, PsiCodeBlock.class);
- }
-
- topBlock = FileTypeUtils.isInServerPageFile(scope) && scope instanceof PsiFile ? scope : PsiUtil.getTopLevelEnclosingCodeBlock(expression, scope);
- if (variable instanceof PsiField) {
- // non final field already initialized with default value
- if (!variable.hasModifierProperty(PsiModifier.FINAL)) return null;
- // final field may be initialized in ctor or class initializer only
- // if we're inside non-ctr method, skip it
- if (PsiUtil.findEnclosingConstructorOrInitializer(expression) == null
- && HighlightUtil.findEnclosingFieldInitializer(expression) == null) {
- return null;
- }
- // access to final fields from inner classes always allowed
- if (inInnerClass(expression, ((PsiField)variable).getContainingClass(),containingFile)) return null;
- if (topBlock == null) return null;
- final PsiElement parent = topBlock.getParent();
- final PsiCodeBlock block;
- final PsiClass aClass;
- if (parent instanceof PsiMethod) {
- PsiMethod constructor = (PsiMethod)parent;
- if (!containingFile.getManager().areElementsEquivalent(constructor.getContainingClass(), ((PsiField)variable).getContainingClass())) return null;
- // static variables already initialized in class initializers
- if (variable.hasModifierProperty(PsiModifier.STATIC)) return null;
- // as a last chance, field may be initialized in this() call
- final List<PsiMethod> redirectedConstructors = JavaHighlightUtil.getChainedConstructors(constructor);
- for (int j = 0; redirectedConstructors != null && j < redirectedConstructors.size(); j++) {
- PsiMethod redirectedConstructor = redirectedConstructors.get(j);
- // variable must be initialized before its usage
- //???
- //if (startOffset < redirectedConstructor.getTextRange().getStartOffset()) continue;
- if (redirectedConstructor.getBody() != null
- && variableDefinitelyAssignedIn(variable, redirectedConstructor.getBody())) {
- return null;
- }
- }
- block = constructor.getBody();
- aClass = constructor.getContainingClass();
- }
- else if (parent instanceof PsiClassInitializer) {
- final PsiClassInitializer classInitializer = (PsiClassInitializer)parent;
- if (!containingFile.getManager().areElementsEquivalent(classInitializer.getContainingClass(), ((PsiField)variable).getContainingClass())) return null;
- block = classInitializer.getBody();
- aClass = classInitializer.getContainingClass();
- }
- else {
- // field reference outside code block
- // check variable initialized before its usage
- final PsiField field = (PsiField)variable;
-
- aClass = field.getContainingClass();
- if (aClass == null || isFieldInitializedInOtherFieldInitializer(aClass, field, field.hasModifierProperty(PsiModifier.STATIC))) {
- return null;
- }
- block = null;
- // initializers will be checked later
- final PsiMethod[] constructors = aClass.getConstructors();
- for (PsiMethod constructor : constructors) {
- // variable must be initialized before its usage
- if (startOffset < constructor.getTextRange().getStartOffset()) continue;
- if (constructor.getBody() != null
- && variableDefinitelyAssignedIn(variable, constructor.getBody())) {
- return null;
- }
- // as a last chance, field may be initialized in this() call
- final List<PsiMethod> redirectedConstructors = JavaHighlightUtil.getChainedConstructors(constructor);
- for (int j = 0; redirectedConstructors != null && j < redirectedConstructors.size(); j++) {
- PsiMethod redirectedConstructor = redirectedConstructors.get(j);
- // variable must be initialized before its usage
- if (startOffset < redirectedConstructor.getTextRange().getStartOffset()) continue;
- if (redirectedConstructor.getBody() != null
- && variableDefinitelyAssignedIn(variable, redirectedConstructor.getBody())) {
- return null;
- }
- }
- }
- }
-
- if (aClass != null) {
- // field may be initialized in class initializer
- final PsiClassInitializer[] initializers = aClass.getInitializers();
- for (PsiClassInitializer initializer : initializers) {
- PsiCodeBlock body = initializer.getBody();
- if (body == block) break;
- // variable referenced in initializer must be initialized in initializer preceding assignment
- // variable referenced in field initializer or in class initializer
- boolean shouldCheckInitializerOrder = block == null || block.getParent() instanceof PsiClassInitializer;
- if (shouldCheckInitializerOrder && startOffset < initializer.getTextRange().getStartOffset()) continue;
- if (initializer.hasModifierProperty(PsiModifier.STATIC)
- == variable.hasModifierProperty(PsiModifier.STATIC)) {
- if (variableDefinitelyAssignedIn(variable, body)) return null;
- }
- }
- }
- }
- }
- if (topBlock == null) return null;
- Collection<PsiReferenceExpression> codeBlockProblems = uninitializedVarProblems.get(topBlock);
- if (codeBlockProblems == null) {
- try {
- final ControlFlow controlFlow = getControlFlow(topBlock);
- codeBlockProblems = ControlFlowUtil.getReadBeforeWriteLocals(controlFlow);
- }
- catch (AnalysisCanceledException e) {
- codeBlockProblems = Collections.emptyList();
- }
- catch (IndexNotReadyException e) {
- codeBlockProblems = Collections.emptyList();
- }
- uninitializedVarProblems.put(topBlock, codeBlockProblems);
- }
- if (codeBlockProblems.contains(expression)) {
- final String name = expression.getElement().getText();
- String description = JavaErrorMessages.message("variable.not.initialized", name);
- HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(highlightInfo, new AddVariableInitializerFix(variable));
- return highlightInfo;
- }
-
- return null;
- }
-
- private static boolean inInnerClass(PsiElement element, PsiClass containingClass, @NotNull PsiFile containingFile) {
- while (element != null) {
- if (element instanceof PsiClass) return !containingFile.getManager().areElementsEquivalent(element, containingClass);
- element = element.getParent();
- }
- return false;
- }
-
- public static boolean isReassigned(PsiVariable variable,
- Map<PsiElement, Collection<ControlFlowUtil.VariableInfo>> finalVarProblems) {
- if (variable instanceof PsiLocalVariable) {
- final PsiElement parent = variable.getParent();
- if (parent == null) return false;
- final PsiElement declarationScope = parent.getParent();
- Collection<ControlFlowUtil.VariableInfo> codeBlockProblems = getFinalVariableProblemsInBlock(finalVarProblems, declarationScope);
- return codeBlockProblems.contains(new ControlFlowUtil.VariableInfo(variable, null));
- }
- else if (variable instanceof PsiParameter) {
- final PsiParameter parameter = (PsiParameter)variable;
- return isAssigned(parameter);
- }
- else {
- return false;
- }
- }
-
-
- @Nullable
- public static HighlightInfo checkFinalVariableMightAlreadyHaveBeenAssignedTo(PsiVariable variable,
- PsiReferenceExpression expression,
- Map<PsiElement, Collection<ControlFlowUtil.VariableInfo>> finalVarProblems) {
- if (!PsiUtil.isAccessedForWriting(expression)) return null;
-
- final PsiElement scope = variable instanceof PsiField ? variable.getParent() :
- variable.getParent() == null ? null : variable.getParent().getParent();
- PsiElement codeBlock = PsiUtil.getTopLevelEnclosingCodeBlock(expression, scope);
- if (codeBlock == null) return null;
- Collection<ControlFlowUtil.VariableInfo> codeBlockProblems = getFinalVariableProblemsInBlock(finalVarProblems, codeBlock);
-
- boolean alreadyAssigned = false;
- for (ControlFlowUtil.VariableInfo variableInfo : codeBlockProblems) {
- if (variableInfo.expression == expression) {
- alreadyAssigned = true;
- break;
- }
- }
-
- if (!alreadyAssigned) {
- if (!(variable instanceof PsiField)) return null;
- final PsiField field = (PsiField)variable;
- final PsiClass aClass = field.getContainingClass();
- if (aClass == null) return null;
- // field can get assigned in other field initializers
- final PsiField[] fields = aClass.getFields();
- boolean isFieldStatic = field.hasModifierProperty(PsiModifier.STATIC);
- for (PsiField psiField : fields) {
- PsiExpression initializer = psiField.getInitializer();
- if (psiField != field
- && psiField.hasModifierProperty(PsiModifier.STATIC) == isFieldStatic
- && initializer != null
- && initializer != codeBlock
- && !variableDefinitelyNotAssignedIn(field, initializer)) {
- alreadyAssigned = true;
- break;
- }
- }
-
- if (!alreadyAssigned) {
- // field can get assigned in class initializers
- final PsiMember enclosingConstructorOrInitializer = PsiUtil.findEnclosingConstructorOrInitializer(expression);
- if (enclosingConstructorOrInitializer == null
- || !aClass.getManager().areElementsEquivalent(enclosingConstructorOrInitializer.getContainingClass(), aClass)) {
- return null;
- }
- final PsiClassInitializer[] initializers = aClass.getInitializers();
- for (PsiClassInitializer initializer : initializers) {
- if (initializer.hasModifierProperty(PsiModifier.STATIC)
- == field.hasModifierProperty(PsiModifier.STATIC)) {
- final PsiCodeBlock body = initializer.getBody();
- if (body == codeBlock) return null;
- try {
- final ControlFlow controlFlow = getControlFlow(body);
- if (!ControlFlowUtil.isVariableDefinitelyNotAssigned(field, controlFlow)) {
- alreadyAssigned = true;
- break;
- }
- }
- catch (AnalysisCanceledException e) {
- // incomplete code
- return null;
- }
- }
- }
- }
-
- if (!alreadyAssigned
- && !field.hasModifierProperty(PsiModifier.STATIC)) {
- // then check if instance field already assigned in other constructor
- final PsiMethod ctr = codeBlock.getParent() instanceof PsiMethod ?
- (PsiMethod)codeBlock.getParent() : null;
- // assignment to final field in several constructors threatens us only if these are linked (there is this() call in the beginning)
- final List<PsiMethod> redirectedConstructors = ctr != null && ctr.isConstructor() ? JavaHighlightUtil.getChainedConstructors(ctr) : null;
- for (int j = 0; redirectedConstructors != null && j < redirectedConstructors.size(); j++) {
- PsiMethod redirectedConstructor = redirectedConstructors.get(j);
- if (redirectedConstructor.getBody() != null &&
- variableDefinitelyAssignedIn(variable, redirectedConstructor.getBody())) {
- alreadyAssigned = true;
- break;
- }
- }
- }
- }
-
- if (alreadyAssigned) {
- String description = JavaErrorMessages.message("variable.already.assigned", variable.getName());
- final HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(variable, PsiModifier.FINAL, false, false);
- QuickFixAction.registerQuickFixAction(highlightInfo, fix);
- QuickFixAction.registerQuickFixAction(highlightInfo, new DeferFinalAssignmentFix(variable, expression));
- return highlightInfo;
- }
-
- return null;
- }
-
- @NotNull
- public static Collection<ControlFlowUtil.VariableInfo> getFinalVariableProblemsInBlock(Map<PsiElement,Collection<ControlFlowUtil.VariableInfo>> finalVarProblems, PsiElement codeBlock) {
- Collection<ControlFlowUtil.VariableInfo> codeBlockProblems = finalVarProblems.get(codeBlock);
- if (codeBlockProblems == null) {
- try {
- final ControlFlow controlFlow = getControlFlow(codeBlock);
- codeBlockProblems = ControlFlowUtil.getInitializedTwice(controlFlow);
- }
- catch (AnalysisCanceledException e) {
- codeBlockProblems = Collections.emptyList();
- }
- finalVarProblems.put(codeBlock, codeBlockProblems);
- }
- return codeBlockProblems;
- }
-
-
- @Nullable
- public static HighlightInfo checkFinalVariableInitializedInLoop(PsiReferenceExpression expression, PsiElement resolved) {
- if (ControlFlowUtil.isVariableAssignedInLoop(expression, resolved)) {
- String description = JavaErrorMessages.message("variable.assigned.in.loop", ((PsiVariable)resolved).getName());
- final HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix((PsiVariable)resolved, PsiModifier.FINAL, false, false);
- QuickFixAction.registerQuickFixAction(highlightInfo, fix);
- return highlightInfo;
- }
- return null;
- }
-
-
- @Nullable
- public static HighlightInfo checkCannotWriteToFinal(PsiExpression expression, @NotNull PsiFile containingFile) {
- PsiReferenceExpression reference = null;
- if (expression instanceof PsiAssignmentExpression) {
- final PsiExpression left = ((PsiAssignmentExpression)expression).getLExpression();
- if (left instanceof PsiReferenceExpression) {
- reference = (PsiReferenceExpression)left;
- }
- }
- else if (expression instanceof PsiPostfixExpression) {
- final PsiExpression operand = ((PsiPostfixExpression)expression).getOperand();
- final IElementType sign = ((PsiPostfixExpression)expression).getOperationTokenType();
- if (operand instanceof PsiReferenceExpression && (sign == JavaTokenType.PLUSPLUS || sign == JavaTokenType.MINUSMINUS)) {
- reference = (PsiReferenceExpression)operand;
- }
- }
- else if (expression instanceof PsiPrefixExpression) {
- final PsiExpression operand = ((PsiPrefixExpression)expression).getOperand();
- final IElementType sign = ((PsiPrefixExpression)expression).getOperationTokenType();
- if (operand instanceof PsiReferenceExpression && (sign == JavaTokenType.PLUSPLUS || sign == JavaTokenType.MINUSMINUS)) {
- reference = (PsiReferenceExpression)operand;
- }
- }
- final PsiElement resolved = reference == null ? null : reference.resolve();
- PsiVariable variable = resolved instanceof PsiVariable ? (PsiVariable)resolved : null;
- if (variable == null || !variable.hasModifierProperty(PsiModifier.FINAL)) return null;
- if (!canWriteToFinal(variable, expression, reference,containingFile)) {
- final String name = variable.getName();
- String description = JavaErrorMessages.message("assignment.to.final.variable", name);
- final HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(reference.getTextRange()).descriptionAndTooltip(description).create();
- final PsiClass innerClass = getInnerClassVariableReferencedFrom(variable, expression);
- if (innerClass == null || variable instanceof PsiField) {
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(variable, PsiModifier.FINAL, false, false);
- QuickFixAction.registerQuickFixAction(highlightInfo, fix);
- }
- else {
- QuickFixAction.registerQuickFixAction(highlightInfo, new VariableAccessFromInnerClassFix(variable, innerClass));
- }
- return highlightInfo;
- }
-
- return null;
- }
-
- private static boolean canWriteToFinal(PsiVariable variable, PsiExpression expression, final PsiReferenceExpression reference, @NotNull PsiFile containingFile) {
- if (variable.hasInitializer()) return false;
- if (variable instanceof PsiParameter) return false;
- PsiClass innerClass = getInnerClassVariableReferencedFrom(variable, expression);
- if (variable instanceof PsiField) {
- // if inside some field initializer
- if (HighlightUtil.findEnclosingFieldInitializer(expression) != null) return true;
- // assignment from within inner class is illegal always
- PsiField field = (PsiField)variable;
- if (innerClass != null && !containingFile.getManager().areElementsEquivalent(innerClass, field.getContainingClass())) return false;
- final PsiMember enclosingCtrOrInitializer = PsiUtil.findEnclosingConstructorOrInitializer(expression);
- return enclosingCtrOrInitializer != null && isSameField(variable, enclosingCtrOrInitializer, field, reference,containingFile);
- }
- if (variable instanceof PsiLocalVariable) {
- boolean isAccessedFromOtherClass = innerClass != null;
- if (isAccessedFromOtherClass) {
- return false;
- }
- }
- return true;
- }
-
- private static boolean isSameField(final PsiVariable variable,
- final PsiMember enclosingCtrOrInitializer,
- final PsiField field,
- final PsiReferenceExpression reference, @NotNull PsiFile containingFile) {
-
- if (!containingFile.getManager().areElementsEquivalent(enclosingCtrOrInitializer.getContainingClass(), field.getContainingClass())) return false;
- PsiExpression qualifierExpression = reference.getQualifierExpression();
- return qualifierExpression == null || qualifierExpression instanceof PsiThisExpression;
- }
-
-
- @Nullable
- static HighlightInfo checkVariableMustBeFinal(PsiVariable variable,
- PsiJavaCodeReferenceElement context,
- @NotNull LanguageLevel languageLevel) {
- if (variable.hasModifierProperty(PsiModifier.FINAL)) return null;
- final PsiClass innerClass = getInnerClassVariableReferencedFrom(variable, context);
- if (innerClass != null) {
- if (variable instanceof PsiParameter) {
- final PsiElement parent = variable.getParent();
- if (parent instanceof PsiParameterList && parent.getParent() instanceof PsiLambdaExpression &&
- notAccessedForWriting(variable, new LocalSearchScope(((PsiParameter)variable).getDeclarationScope()))) {
- return null;
- }
- }
- if (languageLevel.isAtLeast(LanguageLevel.JDK_1_8) && isEffectivelyFinal(variable, innerClass, context)) {
- return null;
- }
- final String description = JavaErrorMessages.message("variable.must.be.final", context.getText());
-
- final HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(context).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(highlightInfo, new VariableAccessFromInnerClassFix(variable, innerClass));
- return highlightInfo;
- }
- final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(context, PsiLambdaExpression.class);
- if (lambdaExpression != null && !PsiTreeUtil.isAncestor(lambdaExpression, variable, true)) {
- final PsiElement parent = variable.getParent();
- if (parent instanceof PsiParameterList && parent.getParent() == lambdaExpression) {
- return null;
- }
- if (!isEffectivelyFinal(variable, lambdaExpression, context)) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(context).descriptionAndTooltip(
- "Variable used in lambda expression should be effectively final").create();
- }
- }
- return null;
- }
-
- private static boolean isEffectivelyFinal(PsiVariable variable, PsiElement scope, PsiJavaCodeReferenceElement context) {
- boolean effectivelyFinal;
- if (variable instanceof PsiParameter) {
- effectivelyFinal = notAccessedForWriting(variable, new LocalSearchScope(((PsiParameter)variable).getDeclarationScope()));
- } else {
- final ControlFlow controlFlow;
- try {
- controlFlow = getControlFlow(PsiUtil.getVariableCodeBlock(variable, context));
- }
- catch (AnalysisCanceledException e) {
- return true;
- }
-
- if (ControlFlowUtil.isVariableDefinitelyAssigned(variable, controlFlow)) {
- final Collection<ControlFlowUtil.VariableInfo> initializedTwice = ControlFlowUtil.getInitializedTwice(controlFlow);
- effectivelyFinal = !initializedTwice.contains(new ControlFlowUtil.VariableInfo(variable, null));
- if (effectivelyFinal) {
- effectivelyFinal = notAccessedForWriting(variable, new LocalSearchScope(scope));
- }
- } else {
- effectivelyFinal = false;
- }
- }
- return effectivelyFinal;
- }
-
- private static boolean notAccessedForWriting(PsiVariable variable, final LocalSearchScope searchScope) {
- for (PsiReference reference : ReferencesSearch.search(variable, searchScope)) {
- final PsiElement element = reference.getElement();
- if (element instanceof PsiExpression && PsiUtil.isAccessedForWriting((PsiExpression)element)) {
- return false;
- }
- }
- return true;
- }
-
- @Nullable
- public static PsiClass getInnerClassVariableReferencedFrom(PsiVariable variable, PsiElement context) {
- final PsiElement[] scope;
- if (variable instanceof PsiResourceVariable) {
- scope = ((PsiResourceVariable)variable).getDeclarationScope();
- }
- else if (variable instanceof PsiLocalVariable) {
- final PsiElement parent = variable.getParent();
- scope = new PsiElement[]{parent != null ? parent.getParent() : null}; // code block or for statement
- }
- else if (variable instanceof PsiParameter) {
- scope = new PsiElement[]{((PsiParameter)variable).getDeclarationScope()};
- }
- else {
- scope = new PsiElement[]{variable.getParent()};
- }
- if (scope.length < 1 || scope[0] == null || scope[0].getContainingFile() != context.getContainingFile()) return null;
-
- PsiElement parent = context.getParent();
- PsiElement prevParent = context;
- outer:
- while (parent != null) {
- for (PsiElement scopeElement : scope) {
- if (parent.equals(scopeElement)) break outer;
- }
- if (parent instanceof PsiClass && !(prevParent instanceof PsiExpressionList && parent instanceof PsiAnonymousClass)) {
- return (PsiClass)parent;
- }
- prevParent = parent;
- parent = parent.getParent();
- }
- return null;
- }
-
-
- @Nullable
- public static HighlightInfo checkInitializerCompleteNormally(PsiClassInitializer initializer) {
- final PsiCodeBlock body = initializer.getBody();
- // unhandled exceptions already reported
- try {
- final ControlFlow controlFlow = getControlFlowNoConstantEvaluate(body);
- final int completionReasons = ControlFlowUtil.getCompletionReasons(controlFlow, 0, controlFlow.getSize());
- if ((completionReasons & ControlFlowUtil.NORMAL_COMPLETION_REASON) == 0) {
- String description = JavaErrorMessages.message("initializer.must.be.able.to.complete.normally");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(body).descriptionAndTooltip(description).create();
- }
- }
- catch (AnalysisCanceledException e) {
- // incomplete code
- }
- return null;
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java
deleted file mode 100644
index 3266e9685dc2..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java
+++ /dev/null
@@ -1,1405 +0,0 @@
-/*
- * Copyright 2000-2013 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.analysis;
-
-import com.intellij.codeInsight.ExceptionUtil;
-import com.intellij.codeInsight.daemon.JavaErrorMessages;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
-import com.intellij.codeInsight.daemon.impl.RefCountHolder;
-import com.intellij.codeInsight.daemon.impl.quickfix.*;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.codeInsight.intention.QuickFixFactory;
-import com.intellij.codeInspection.LocalQuickFixOnPsiElementAsIntentionAdapter;
-import com.intellij.openapi.application.ApplicationManager;
-import com.intellij.openapi.project.IndexNotReadyException;
-import com.intellij.openapi.projectRoots.JavaSdkVersion;
-import com.intellij.openapi.util.Comparing;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.pom.java.LanguageLevel;
-import com.intellij.psi.*;
-import com.intellij.psi.infos.CandidateInfo;
-import com.intellij.psi.infos.MethodCandidateInfo;
-import com.intellij.psi.util.*;
-import com.intellij.refactoring.util.RefactoringChangeUtil;
-import com.intellij.ui.ColorUtil;
-import com.intellij.util.containers.MostlySingularMultiMap;
-import com.intellij.util.ui.UIUtil;
-import com.intellij.xml.util.XmlStringUtil;
-import org.intellij.lang.annotations.Language;
-import org.jetbrains.annotations.NonNls;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
-/**
- * Highlight method problems
- *
- * @author cdr
- * Date: Aug 14, 2002
- */
-public class HighlightMethodUtil {
- private static final QuickFixFactory QUICK_FIX_FACTORY = QuickFixFactory.getInstance();
-
- private HighlightMethodUtil() { }
-
- public static String createClashMethodMessage(PsiMethod method1, PsiMethod method2, boolean showContainingClasses) {
- @NonNls String pattern = showContainingClasses ? "clash.methods.message.show.classes" : "clash.methods.message";
- return JavaErrorMessages.message(pattern,
- JavaHighlightUtil.formatMethod(method1),
- JavaHighlightUtil.formatMethod(method2),
- HighlightUtil.formatClass(method1.getContainingClass()),
- HighlightUtil.formatClass(method2.getContainingClass()));
- }
-
- static HighlightInfo checkMethodWeakerPrivileges(@NotNull MethodSignatureBackedByPsiMethod methodSignature,
- @NotNull List<HierarchicalMethodSignature> superMethodSignatures,
- boolean includeRealPositionInfo,
- @NotNull PsiFile containingFile) {
- PsiMethod method = methodSignature.getMethod();
- PsiModifierList modifierList = method.getModifierList();
- if (modifierList.hasModifierProperty(PsiModifier.PUBLIC)) return null;
- int accessLevel = PsiUtil.getAccessLevel(modifierList);
- String accessModifier = PsiUtil.getAccessModifier(accessLevel);
- for (MethodSignatureBackedByPsiMethod superMethodSignature : superMethodSignatures) {
- PsiMethod superMethod = superMethodSignature.getMethod();
- if (method.hasModifierProperty(PsiModifier.ABSTRACT) && !MethodSignatureUtil.isSuperMethod(superMethod, method)) continue;
- if (!PsiUtil.isAccessible(containingFile.getProject(), superMethod, method, null)) continue;
- HighlightInfo info = isWeaker(method, modifierList, accessModifier, accessLevel, superMethod, includeRealPositionInfo);
- if (info != null) return info;
- }
- return null;
- }
-
- private static HighlightInfo isWeaker(final PsiMethod method, final PsiModifierList modifierList, final String accessModifier, final int accessLevel,
- final PsiMethod superMethod,
- final boolean includeRealPositionInfo) {
- int superAccessLevel = PsiUtil.getAccessLevel(superMethod.getModifierList());
- if (accessLevel < superAccessLevel) {
- String description = JavaErrorMessages.message("weaker.privileges",
- createClashMethodMessage(method, superMethod, true),
- accessModifier,
- PsiUtil.getAccessModifier(superAccessLevel));
- TextRange textRange;
- if (includeRealPositionInfo) {
- if (modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
- textRange = method.getNameIdentifier().getTextRange();
- }
- else {
- PsiElement keyword = PsiUtil.findModifierInList(modifierList, accessModifier);
- textRange = keyword.getTextRange();
- }
- }
- else {
- textRange = TextRange.EMPTY_RANGE;
- }
- HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(method, PsiUtil.getAccessModifier(superAccessLevel), true, false);
- QuickFixAction.registerQuickFixAction(highlightInfo, fix);
- return highlightInfo;
- }
- return null;
- }
-
-
- static HighlightInfo checkMethodIncompatibleReturnType(MethodSignatureBackedByPsiMethod methodSignature,
- List<HierarchicalMethodSignature> superMethodSignatures,
- boolean includeRealPositionInfo) {
- PsiMethod method = methodSignature.getMethod();
- PsiType returnType = methodSignature.getSubstitutor().substitute(method.getReturnType());
- PsiClass aClass = method.getContainingClass();
- if (aClass == null) return null;
- for (MethodSignatureBackedByPsiMethod superMethodSignature : superMethodSignatures) {
- PsiMethod superMethod = superMethodSignature.getMethod();
- PsiType declaredReturnType = superMethod.getReturnType();
- PsiType superReturnType = declaredReturnType;
- if (superMethodSignature.isRaw()) superReturnType = TypeConversionUtil.erasure(declaredReturnType);
- if (returnType == null || superReturnType == null || method == superMethod) continue;
- PsiClass superClass = superMethod.getContainingClass();
- if (superClass == null) continue;
- HighlightInfo highlightInfo = checkSuperMethodSignature(superMethod, superMethodSignature, superReturnType, method, methodSignature,
- returnType, includeRealPositionInfo, JavaErrorMessages.message("incompatible.return.type"), method);
- if (highlightInfo != null) return highlightInfo;
- }
-
- return null;
- }
-
- private static HighlightInfo checkSuperMethodSignature(PsiMethod superMethod,
- MethodSignatureBackedByPsiMethod superMethodSignature,
- PsiType superReturnType,
- PsiMethod method,
- MethodSignatureBackedByPsiMethod methodSignature,
- PsiType returnType,
- boolean includeRealPositionInfo,
- String detailMessage,
- PsiMethod methodToHighlight) {
- if (superReturnType == null) return null;
- if ("clone".equals(method.getName())) {
- final PsiClass containingClass = method.getContainingClass();
- final PsiClass superContainingClass = superMethod.getContainingClass();
- if (containingClass != null && superContainingClass != null && containingClass.isInterface() && !superContainingClass.isInterface()) {
- return null;
- }
- }
-
- PsiType substitutedSuperReturnType;
- final boolean isJdk15 = PsiUtil.isLanguageLevel5OrHigher(method);
- if (isJdk15 && !superMethodSignature.isRaw() && superMethodSignature.equals(methodSignature)) { //see 8.4.5
- PsiSubstitutor unifyingSubstitutor = MethodSignatureUtil.getSuperMethodSignatureSubstitutor(methodSignature,
- superMethodSignature);
- substitutedSuperReturnType = unifyingSubstitutor == null
- ? superReturnType
- : unifyingSubstitutor.substitute(superReturnType);
- }
- else {
- substitutedSuperReturnType = TypeConversionUtil.erasure(superMethodSignature.getSubstitutor().substitute(superReturnType));
- }
-
- if (returnType.equals(substitutedSuperReturnType)) return null;
- if (!(returnType instanceof PsiPrimitiveType) && substitutedSuperReturnType.getDeepComponentType() instanceof PsiClassType) {
- if (isJdk15 && TypeConversionUtil.isAssignable(substitutedSuperReturnType, returnType)) {
- return null;
- }
- }
-
- return createIncompatibleReturnTypeMessage(methodToHighlight, method, superMethod, includeRealPositionInfo,
- substitutedSuperReturnType, returnType, detailMessage);
- }
-
- private static HighlightInfo createIncompatibleReturnTypeMessage(PsiMethod methodToHighlight,
- PsiMethod method,
- PsiMethod superMethod,
- boolean includeRealPositionInfo,
- PsiType substitutedSuperReturnType,
- PsiType returnType,
- String detailMessage) {
- String description = MessageFormat.format("{0}; {1}", createClashMethodMessage(method, superMethod, true), detailMessage);
- TextRange textRange = includeRealPositionInfo ? methodToHighlight.getReturnTypeElement().getTextRange() : TextRange.EMPTY_RANGE;
- HighlightInfo errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- IntentionAction fix = QUICK_FIX_FACTORY.createMethodReturnFix(method, substitutedSuperReturnType, false);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- QuickFixAction.registerQuickFixAction(errorResult, new SuperMethodReturnFix(superMethod, returnType));
-
- return errorResult;
- }
-
-
- static HighlightInfo checkMethodOverridesFinal(MethodSignatureBackedByPsiMethod methodSignature,
- List<HierarchicalMethodSignature> superMethodSignatures) {
- PsiMethod method = methodSignature.getMethod();
- for (MethodSignatureBackedByPsiMethod superMethodSignature : superMethodSignatures) {
- PsiMethod superMethod = superMethodSignature.getMethod();
- HighlightInfo info = checkSuperMethodIsFinal(method, superMethod);
- if (info != null) return info;
- }
- return null;
- }
-
- private static HighlightInfo checkSuperMethodIsFinal(PsiMethod method, PsiMethod superMethod) {
- // strange things happen when super method is from Object and method from interface
- if (superMethod.hasModifierProperty(PsiModifier.FINAL)) {
- String description = JavaErrorMessages.message("final.method.override",
- JavaHighlightUtil.formatMethod(method),
- JavaHighlightUtil.formatMethod(superMethod),
- HighlightUtil.formatClass(superMethod.getContainingClass()));
- TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
- HighlightInfo errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(superMethod, PsiModifier.FINAL, false, true);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- return errorResult;
- }
- return null;
- }
-
- static HighlightInfo checkMethodIncompatibleThrows(MethodSignatureBackedByPsiMethod methodSignature,
- List<HierarchicalMethodSignature> superMethodSignatures,
- boolean includeRealPositionInfo, PsiClass analyzedClass) {
- PsiMethod method = methodSignature.getMethod();
- PsiClass aClass = method.getContainingClass();
- if (aClass == null) return null;
- PsiSubstitutor superSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(aClass, analyzedClass, PsiSubstitutor.EMPTY);
- PsiClassType[] exceptions = method.getThrowsList().getReferencedTypes();
- PsiJavaCodeReferenceElement[] referenceElements;
- List<PsiElement> exceptionContexts;
- if (includeRealPositionInfo) {
- exceptionContexts = new ArrayList<PsiElement>();
- referenceElements = method.getThrowsList().getReferenceElements();
- }
- else {
- exceptionContexts = null;
- referenceElements = null;
- }
- List<PsiClassType> checkedExceptions = new ArrayList<PsiClassType>();
- for (int i = 0; i < exceptions.length; i++) {
- PsiClassType exception = exceptions[i];
- if (!ExceptionUtil.isUncheckedException(exception)) {
- checkedExceptions.add(exception);
- if (includeRealPositionInfo && i < referenceElements.length) {
- PsiJavaCodeReferenceElement exceptionRef = referenceElements[i];
- exceptionContexts.add(exceptionRef);
- }
- }
- }
- for (MethodSignatureBackedByPsiMethod superMethodSignature : superMethodSignatures) {
- PsiMethod superMethod = superMethodSignature.getMethod();
- int index = getExtraExceptionNum(methodSignature, superMethodSignature, checkedExceptions, superSubstitutor);
- if (index != -1) {
- if (aClass.isInterface()) {
- final PsiClass superContainingClass = superMethod.getContainingClass();
- if (superContainingClass != null && !superContainingClass.isInterface()) continue;
- if (superContainingClass != null && !aClass.isInheritor(superContainingClass, true)) continue;
- }
- PsiClassType exception = checkedExceptions.get(index);
- String description = JavaErrorMessages.message("overridden.method.does.not.throw",
- createClashMethodMessage(method, superMethod, true),
- JavaHighlightUtil.formatType(exception));
- TextRange textRange;
- if (includeRealPositionInfo) {
- PsiElement exceptionContext = exceptionContexts.get(index);
- textRange = exceptionContext.getTextRange();
- }
- else {
- textRange = TextRange.EMPTY_RANGE;
- }
- HighlightInfo errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(errorResult, new LocalQuickFixOnPsiElementAsIntentionAdapter(QUICK_FIX_FACTORY.createMethodThrowsFix(method, exception, false, false)));
- QuickFixAction.registerQuickFixAction(errorResult, new LocalQuickFixOnPsiElementAsIntentionAdapter(QUICK_FIX_FACTORY.createMethodThrowsFix(superMethod, exception, true, true)));
- return errorResult;
- }
- }
- return null;
- }
-
- // return number of exception which was not declared in super method or -1
- private static int getExtraExceptionNum(final MethodSignature methodSignature,
- final MethodSignatureBackedByPsiMethod superSignature,
- List<PsiClassType> checkedExceptions, PsiSubstitutor substitutorForDerivedClass) {
- PsiMethod superMethod = superSignature.getMethod();
- PsiSubstitutor substitutorForMethod = MethodSignatureUtil.getSuperMethodSignatureSubstitutor(methodSignature, superSignature);
- for (int i = 0; i < checkedExceptions.size(); i++) {
- final PsiClassType checkedEx = checkedExceptions.get(i);
- final PsiType substituted = substitutorForMethod != null ? substitutorForMethod.substitute(checkedEx) : TypeConversionUtil.erasure(checkedEx);
- PsiType exception = substitutorForDerivedClass.substitute(substituted);
- if (!isMethodThrows(superMethod, substitutorForMethod, exception, substitutorForDerivedClass)) {
- return i;
- }
- }
- return -1;
- }
-
- private static boolean isMethodThrows(PsiMethod method, @Nullable PsiSubstitutor substitutorForMethod, PsiType exception, PsiSubstitutor substitutorForDerivedClass) {
- PsiClassType[] thrownExceptions = method.getThrowsList().getReferencedTypes();
- for (PsiClassType thrownException1 : thrownExceptions) {
- PsiType thrownException = substitutorForMethod != null ? substitutorForMethod.substitute(thrownException1) : TypeConversionUtil.erasure(thrownException1);
- thrownException = substitutorForDerivedClass.substitute(thrownException);
- if (TypeConversionUtil.isAssignable(thrownException, exception)) return true;
- }
- return false;
- }
-
- @Nullable
- static HighlightInfo checkMethodCall(@NotNull PsiMethodCallExpression methodCall,
- @NotNull PsiResolveHelper resolveHelper,
- @NotNull LanguageLevel languageLevel,
- @NotNull JavaSdkVersion javaSdkVersion) {
- PsiExpressionList list = methodCall.getArgumentList();
- PsiReferenceExpression referenceToMethod = methodCall.getMethodExpression();
- JavaResolveResult[] results = referenceToMethod.multiResolve(true);
- JavaResolveResult resolveResult = results.length == 1 ? results[0] : JavaResolveResult.EMPTY;
- PsiElement resolved = resolveResult.getElement();
-
- boolean isDummy = isDummyConstructorCall(methodCall, resolveHelper, list, referenceToMethod);
- if (isDummy) return null;
- HighlightInfo highlightInfo;
-
- PsiSubstitutor substitutor = resolveResult.getSubstitutor();
- if (resolved instanceof PsiMethod && resolveResult.isValidResult()) {
- TextRange fixRange = getFixRange(methodCall);
- highlightInfo = HighlightUtil.checkUnhandledExceptions(methodCall, fixRange);
- if (highlightInfo == null && !LambdaUtil.isValidQualifier4InterfaceStaticMethodCall((PsiMethod)resolved, methodCall.getMethodExpression(), languageLevel)) {
- highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).descriptionAndTooltip("Static method may be invoked on containing interface class only").range(fixRange).create();
- }
- }
- else {
- PsiMethod resolvedMethod = null;
- MethodCandidateInfo candidateInfo = null;
- if (resolveResult instanceof MethodCandidateInfo) {
- candidateInfo = (MethodCandidateInfo)resolveResult;
- resolvedMethod = candidateInfo.getElement();
- }
-
- if (!resolveResult.isAccessible() || !resolveResult.isStaticsScopeCorrect()) {
- //highlightInfo = checkAmbiguousMethodCall(referenceToMethod, results, list, element, resolveResult, methodCall, resolveHelper);
- highlightInfo = null;
- }
- else if (candidateInfo != null && !candidateInfo.isApplicable()) {
- if (candidateInfo.isTypeArgumentsApplicable()) {
- String methodName = HighlightMessageUtil.getSymbolName(resolved, substitutor);
- PsiElement parent = resolved.getParent();
- String containerName = parent == null ? "" : HighlightMessageUtil.getSymbolName(parent, substitutor);
- String argTypes = buildArgTypesList(list);
- String description = JavaErrorMessages.message("wrong.method.arguments", methodName, containerName, argTypes);
- String toolTip = parent instanceof PsiClass && !ApplicationManager.getApplication().isUnitTestMode() ?
- createMismatchedArgumentsHtmlTooltip(candidateInfo, list) : description;
- highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).description(description).escapedToolTip(toolTip).navigationShift(+1).create();
- if (highlightInfo != null) {
- registerMethodCallIntentions(highlightInfo, methodCall, list, resolveHelper);
- }
- }
- else {
- PsiReferenceExpression methodExpression = methodCall.getMethodExpression();
- PsiReferenceParameterList typeArgumentList = methodCall.getTypeArgumentList();
- if (typeArgumentList.getTypeArguments().length == 0 && resolvedMethod != null && resolvedMethod.hasTypeParameters()) {
- highlightInfo = GenericsHighlightUtil.checkInferredTypeArguments(resolvedMethod, methodCall, substitutor);
- }
- else {
- highlightInfo = GenericsHighlightUtil.checkParameterizedReferenceTypeArguments(resolved, methodExpression, substitutor, javaSdkVersion);
- }
- }
- }
- else {
- String description = JavaErrorMessages.message("method.call.expected");
- highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(methodCall).descriptionAndTooltip(description).create();
- if (resolved instanceof PsiClass) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new InsertNewFix(methodCall, (PsiClass)resolved));
- }
- else {
- TextRange range = getFixRange(methodCall);
- QuickFixAction.registerQuickFixAction(highlightInfo, range, new CreateMethodFromUsageFix(methodCall));
- QuickFixAction.registerQuickFixAction(highlightInfo, range, new CreateAbstractMethodFromUsageFix(methodCall));
- QuickFixAction.registerQuickFixAction(highlightInfo, range, new CreatePropertyFromUsageFix(methodCall));
- }
- }
- }
- if (highlightInfo == null) {
- highlightInfo = GenericsHighlightUtil.checkParameterizedReferenceTypeArguments(resolved, referenceToMethod, substitutor,
- javaSdkVersion);
- }
- return highlightInfo;
- }
-
- static boolean isDummyConstructorCall(PsiMethodCallExpression methodCall,
- PsiResolveHelper resolveHelper,
- PsiExpressionList list,
- PsiReferenceExpression referenceToMethod) {
- boolean isDummy = false;
- boolean isThisOrSuper = referenceToMethod.getReferenceNameElement() instanceof PsiKeyword;
- if (isThisOrSuper) {
- // super(..) or this(..)
- if (list.getExpressions().length == 0) { // implicit ctr call
- CandidateInfo[] candidates = resolveHelper.getReferencedMethodCandidates(methodCall, true);
- if (candidates.length == 1 && !candidates[0].getElement().isPhysical()) {
- isDummy = true;// dummy constructor
- }
- }
- }
- return isDummy;
- }
-
- @Nullable
- static HighlightInfo checkAmbiguousMethodCall(final PsiReferenceExpression referenceToMethod,
- JavaResolveResult[] resolveResults,
- @NotNull PsiExpressionList list,
- final PsiElement element,
- final JavaResolveResult resolveResult,
- final PsiMethodCallExpression methodCall, final PsiResolveHelper resolveHelper) {
- MethodCandidateInfo methodCandidate1 = null;
- MethodCandidateInfo methodCandidate2 = null;
- for (JavaResolveResult result : resolveResults) {
- if (!(result instanceof MethodCandidateInfo)) continue;
- MethodCandidateInfo candidate = (MethodCandidateInfo)result;
- if (candidate.isApplicable() && !candidate.getElement().isConstructor()) {
- if (methodCandidate1 == null) {
- methodCandidate1 = candidate;
- }
- else {
- methodCandidate2 = candidate;
- break;
- }
- }
- }
- MethodCandidateInfo[] candidates = toMethodCandidates(resolveResults);
-
- String description;
- String toolTip;
- PsiElement elementToHighlight;
- HighlightInfoType highlightInfoType = HighlightInfoType.ERROR;
- if (methodCandidate2 != null) {
- String m1 = PsiFormatUtil.formatMethod(methodCandidate1.getElement(),
- methodCandidate1.getSubstitutor(),
- PsiFormatUtilBase.SHOW_CONTAINING_CLASS | PsiFormatUtilBase.SHOW_NAME |
- PsiFormatUtilBase.SHOW_PARAMETERS,
- PsiFormatUtilBase.SHOW_TYPE);
- String m2 = PsiFormatUtil.formatMethod(methodCandidate2.getElement(),
- methodCandidate2.getSubstitutor(),
- PsiFormatUtilBase.SHOW_CONTAINING_CLASS | PsiFormatUtilBase.SHOW_NAME |
- PsiFormatUtilBase.SHOW_PARAMETERS,
- PsiFormatUtilBase.SHOW_TYPE);
- description = JavaErrorMessages.message("ambiguous.method.call", m1, m2);
- toolTip = createAmbiguousMethodHtmlTooltip(new MethodCandidateInfo[]{methodCandidate1, methodCandidate2});
- elementToHighlight = list;
- }
- else {
- if (element != null && !resolveResult.isAccessible()) {
- description = HighlightUtil.buildProblemWithAccessDescription(referenceToMethod, resolveResult);
- elementToHighlight = referenceToMethod.getReferenceNameElement();
- }
- else if (element != null && !resolveResult.isStaticsScopeCorrect()) {
- description = HighlightUtil.buildProblemWithStaticDescription(element);
- elementToHighlight = referenceToMethod.getReferenceNameElement();
- }
- else {
- String methodName = referenceToMethod.getReferenceName() + buildArgTypesList(list);
- description = JavaErrorMessages.message("cannot.resolve.method", methodName);
- if (candidates.length == 0) {
- elementToHighlight = referenceToMethod.getReferenceNameElement();
- highlightInfoType = HighlightInfoType.WRONG_REF;
- }
- else {
- elementToHighlight = list;
- }
- }
- toolTip = XmlStringUtil.escapeString(description);
- }
- HighlightInfo info =
- HighlightInfo.newHighlightInfo(highlightInfoType).range(elementToHighlight).description(description).escapedToolTip(toolTip).create();
- if (methodCandidate2 == null) {
- registerMethodCallIntentions(info, methodCall, list, resolveHelper);
- }
- if (!resolveResult.isAccessible() && resolveResult.isStaticsScopeCorrect() && methodCandidate2 != null) {
- HighlightUtil.registerAccessQuickFixAction((PsiMember)element, referenceToMethod, info, resolveResult.getCurrentFileResolveScope());
- }
- if (element != null && !resolveResult.isStaticsScopeCorrect()) {
- HighlightUtil.registerStaticProblemQuickFixAction(element, info, referenceToMethod);
- }
-
- TextRange fixRange = getFixRange(elementToHighlight);
- CastMethodArgumentFix.REGISTRAR.registerCastActions(candidates, methodCall, info, fixRange);
- PermuteArgumentsFix.registerFix(info, methodCall, candidates, fixRange);
- WrapExpressionFix.registerWrapAction(candidates, list.getExpressions(), info);
- ChangeParameterClassFix.registerQuickFixActions(methodCall, list, info);
- return info;
- }
-
- private static MethodCandidateInfo[] toMethodCandidates(JavaResolveResult[] resolveResults) {
- List<MethodCandidateInfo> candidateList = new ArrayList<MethodCandidateInfo>();
-
- for (JavaResolveResult result : resolveResults) {
- if (!(result instanceof MethodCandidateInfo)) continue;
- MethodCandidateInfo candidate = (MethodCandidateInfo)result;
- if (candidate.isAccessible()) candidateList.add(candidate);
- }
- return candidateList.toArray(new MethodCandidateInfo[candidateList.size()]);
- }
-
- private static void registerMethodCallIntentions(@Nullable HighlightInfo highlightInfo,
- PsiMethodCallExpression methodCall,
- PsiExpressionList list,
- PsiResolveHelper resolveHelper) {
- TextRange fixRange = getFixRange(methodCall);
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new CreateMethodFromUsageFix(methodCall));
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new CreateAbstractMethodFromUsageFix(methodCall));
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new CreateConstructorFromSuperFix(methodCall));
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new CreateConstructorFromThisFix(methodCall));
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new CreatePropertyFromUsageFix(methodCall));
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new CreateGetterSetterPropertyFromUsageFix(methodCall));
- CandidateInfo[] methodCandidates = resolveHelper.getReferencedMethodCandidates(methodCall, false);
- CastMethodArgumentFix.REGISTRAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
- PermuteArgumentsFix.registerFix(highlightInfo, methodCall, methodCandidates, fixRange);
- AddTypeArgumentsFix.REGISTRAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
- registerMethodAccessLevelIntentions(methodCandidates, methodCall, list, highlightInfo);
- ChangeMethodSignatureFromUsageFix.registerIntentions(methodCandidates, list, highlightInfo, fixRange);
- RemoveRedundantArgumentsFix.registerIntentions(methodCandidates, list, highlightInfo, fixRange);
- ConvertDoubleToFloatFix.registerIntentions(methodCandidates, list, highlightInfo, fixRange);
- WrapExpressionFix.registerWrapAction(methodCandidates, list.getExpressions(), highlightInfo);
- ChangeParameterClassFix.registerQuickFixActions(methodCall, list, highlightInfo);
- if (methodCandidates.length == 0) {
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new StaticImportMethodFix(methodCall));
- }
- VariableTypeFromCallFix.registerQuickFixActions(methodCall, list, highlightInfo);
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new ReplaceAddAllArrayToCollectionFix(methodCall));
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new SurroundWithArrayFix(methodCall));
- QualifyThisArgumentFix.registerQuickFixAction(methodCandidates, methodCall, highlightInfo, fixRange);
-
- CandidateInfo[] candidates = resolveHelper.getReferencedMethodCandidates(methodCall, true);
- ChangeStringLiteralToCharInMethodCallFix.registerFixes(candidates, methodCall, highlightInfo);
- }
-
- private static void registerMethodAccessLevelIntentions(CandidateInfo[] methodCandidates,
- PsiMethodCallExpression methodCall,
- PsiExpressionList exprList,
- HighlightInfo highlightInfo) {
- for (CandidateInfo methodCandidate : methodCandidates) {
- PsiMethod method = (PsiMethod)methodCandidate.getElement();
- if (!methodCandidate.isAccessible() && PsiUtil.isApplicable(method, methodCandidate.getSubstitutor(), exprList)) {
- HighlightUtil.registerAccessQuickFixAction(method, methodCall.getMethodExpression(), highlightInfo, methodCandidate.getCurrentFileResolveScope());
- }
- }
- }
-
- @NotNull
- private static String createAmbiguousMethodHtmlTooltip(MethodCandidateInfo[] methodCandidates) {
- return JavaErrorMessages.message("ambiguous.method.html.tooltip",
- Integer.valueOf(methodCandidates[0].getElement().getParameterList().getParametersCount() + 2),
- createAmbiguousMethodHtmlTooltipMethodRow(methodCandidates[0]),
- getContainingClassName(methodCandidates[0]),
- createAmbiguousMethodHtmlTooltipMethodRow(methodCandidates[1]),
- getContainingClassName(methodCandidates[1]));
- }
-
- private static String getContainingClassName(final MethodCandidateInfo methodCandidate) {
- PsiMethod method = methodCandidate.getElement();
- PsiClass containingClass = method.getContainingClass();
- return containingClass == null ? method.getContainingFile().getName() : HighlightUtil.formatClass(containingClass, false);
- }
-
- @Language("HTML")
- private static String createAmbiguousMethodHtmlTooltipMethodRow(final MethodCandidateInfo methodCandidate) {
- PsiMethod method = methodCandidate.getElement();
- PsiParameter[] parameters = method.getParameterList().getParameters();
- PsiSubstitutor substitutor = methodCandidate.getSubstitutor();
- @NonNls @Language("HTML") String ms = "<td><b>" + method.getName() + "</b></td>";
-
- for (int j = 0; j < parameters.length; j++) {
- PsiParameter parameter = parameters[j];
- PsiType type = substitutor.substitute(parameter.getType());
- ms += "<td><b>" + (j == 0 ? "(" : "") +
- XmlStringUtil.escapeString(type.getPresentableText())
- + (j == parameters.length - 1 ? ")" : ",") + "</b></td>";
- }
- if (parameters.length == 0) {
- ms += "<td><b>()</b></td>";
- }
- return ms;
- }
-
- private static String createMismatchedArgumentsHtmlTooltip(MethodCandidateInfo info, PsiExpressionList list) {
- PsiMethod method = info.getElement();
- PsiSubstitutor substitutor = info.getSubstitutor();
- PsiClass aClass = method.getContainingClass();
- PsiParameter[] parameters = method.getParameterList().getParameters();
- String methodName = method.getName();
- return createMismatchedArgumentsHtmlTooltip(list, parameters, methodName, substitutor, aClass);
- }
-
- private static String createShortMismatchedArgumentsHtmlTooltip(PsiExpressionList list,
- PsiParameter[] parameters,
- String methodName,
- PsiSubstitutor substitutor,
- PsiClass aClass) {
- PsiExpression[] expressions = list.getExpressions();
- int cols = Math.max(parameters.length, expressions.length);
-
- @Language("HTML")
- @NonNls String parensizedName = methodName + (parameters.length == 0 ? "(&nbsp;)&nbsp;" : "");
- return JavaErrorMessages.message(
- "argument.mismatch.html.tooltip",
- Integer.valueOf(cols - parameters.length + 1), parensizedName,
- HighlightUtil.formatClass(aClass, false),
- createMismatchedArgsHtmlTooltipParamsRow(parameters, substitutor, expressions),
- createMismatchedArgsHtmlTooltipArgumentsRow(expressions, parameters, substitutor, cols)
- );
- }
-
- private static String esctrim(@NotNull String s) {
- return XmlStringUtil.escapeString(trimNicely(s));
- }
-
- private static String trimNicely(String s) {
- if (s.length() <= 40) return s;
-
- List<TextRange> wordIndices = StringUtil.getWordIndicesIn(s);
- if (wordIndices.size() > 2) {
- int firstWordEnd = wordIndices.get(0).getEndOffset();
-
- // try firstWord...remainder
- for (int i = 1; i<wordIndices.size();i++) {
- int stringLength = firstWordEnd + s.length() - wordIndices.get(i).getStartOffset();
- if (stringLength <= 40) {
- return s.substring(0, firstWordEnd) + "..." + s.substring(wordIndices.get(i).getStartOffset());
- }
- }
- }
- // maybe one last word will fit?
- if (!wordIndices.isEmpty() && s.length() - wordIndices.get(wordIndices.size()-1).getStartOffset() <= 40) {
- return "..." + s.substring(wordIndices.get(wordIndices.size()-1).getStartOffset());
- }
-
- return StringUtil.last(s, 40, true).toString();
- }
-
- private static String createMismatchedArgumentsHtmlTooltip(PsiExpressionList list,
- PsiParameter[] parameters,
- String methodName,
- PsiSubstitutor substitutor,
- PsiClass aClass) {
- return Math.max(parameters.length, list.getExpressions().length) <= 2
- ? createShortMismatchedArgumentsHtmlTooltip(list, parameters, methodName, substitutor, aClass)
- : createLongMismatchedArgumentsHtmlTooltip(list, parameters, methodName, substitutor, aClass);
- }
-
- @Language("HTML")
- private static String createLongMismatchedArgumentsHtmlTooltip(PsiExpressionList list,
- PsiParameter[] parameters,
- String methodName,
- PsiSubstitutor substitutor,
- PsiClass aClass) {
- PsiExpression[] expressions = list.getExpressions();
-
- @NonNls
- String s = "<html><body><table border=0>" +
- "<tr><td colspan=3>" +
- "<nobr><b>" + methodName + "()</b> in <b>" + HighlightUtil.formatClass(aClass, false) +"</b> cannot be applied to:</nobr>" +
- "</td></tr>"+
- "<tr><td colspan=2 align=left>Expected<br>Parameters:</td><td align=left>Actual<br>Arguments:</td></tr>"+
- "<tr><td colspan=3><hr></td></tr>"
- ;
-
- for (int i = 0; i < Math.max(parameters.length,expressions.length); i++) {
- PsiParameter parameter = i < parameters.length ? parameters[i] : null;
- PsiExpression expression = i < expressions.length ? expressions[i] : null;
- boolean showShort = showShortType(i, parameters, expressions, substitutor);
- @NonNls String mismatchColor = showShort ? null : UIUtil.isUnderDarcula() ? "FF6B68" : "red";
-
- s += "<tr" + (i % 2 == 0 ? " style='background-color: #"
- + (UIUtil.isUnderDarcula() ? ColorUtil.toHex(ColorUtil.shift(UIUtil.getToolTipBackground(), 1.1)) : "eeeeee")
- + "'" : "") + ">";
- s += "<td><b><nobr>";
- if (parameter != null) {
- String name = parameter.getName();
- if (name != null) {
- s += esctrim(name) +":";
- }
- }
- s += "</nobr></b></td>";
-
- s += "<td><b><nobr>";
- if (parameter != null) {
- PsiType type = substitutor.substitute(parameter.getType());
- s += "<font " + (mismatchColor == null ? "" : "color=" + mismatchColor) + ">" +
- esctrim(showShort ? type.getPresentableText() : JavaHighlightUtil.formatType(type))
- + "</font>"
- ;
- }
- s += "</nobr></b></td>";
-
- s += "<td><b><nobr>";
- if (expression != null) {
- PsiType type = expression.getType();
- s += "<font " + (mismatchColor == null ? "" : "color='" + mismatchColor + "'") + ">" +
- esctrim(expression.getText()) + "&nbsp;&nbsp;"+
- (mismatchColor == null || type == null || type == PsiType.NULL ? "" : "("+esctrim(JavaHighlightUtil.formatType(type))+")")
- + "</font>"
- ;
-
- }
- s += "</nobr></b></td>";
-
- s += "</tr>";
- }
-
- s+= "</table></body></html>";
- return s;
- }
-
- @Language("HTML")
- private static String createMismatchedArgsHtmlTooltipArgumentsRow(final PsiExpression[] expressions, final PsiParameter[] parameters,
- final PsiSubstitutor substitutor, final int cols) {
- @Language("HTML")
-
- @NonNls String ms = "";
- for (int i = 0; i < expressions.length; i++) {
- PsiExpression expression = expressions[i];
- PsiType type = expression.getType();
-
- boolean showShort = showShortType(i, parameters, expressions, substitutor);
- @NonNls String mismatchColor = showShort ? null : UIUtil.isUnderDarcula() ? "ff6464" : "red";
- ms += "<td> " + "<b><nobr>" + (i == 0 ? "(" : "")
- + "<font " + (showShort ? "" : "color=" + mismatchColor) + ">" +
- XmlStringUtil.escapeString(showShort ? type.getPresentableText() : JavaHighlightUtil.formatType(type))
- + "</font>"
- + (i == expressions.length - 1 ? ")" : ",") + "</nobr></b></td>";
- }
- for (int i = expressions.length; i < cols + 1; i++) {
- ms += "<td>" + (i == 0 ? "<b>()</b>" : "") +
- "&nbsp;</td>";
- }
- return ms;
- }
-
- @Language("HTML")
- private static String createMismatchedArgsHtmlTooltipParamsRow(final PsiParameter[] parameters,
- final PsiSubstitutor substitutor,
- final PsiExpression[] expressions) {
- @NonNls String ms = "";
- for (int i = 0; i < parameters.length; i++) {
- PsiParameter parameter = parameters[i];
- PsiType type = substitutor.substitute(parameter.getType());
- ms += "<td><b><nobr>" + (i == 0 ? "(" : "") +
- XmlStringUtil.escapeString(showShortType(i, parameters, expressions, substitutor)
- ? type.getPresentableText()
- : JavaHighlightUtil.formatType(type))
- + (i == parameters.length - 1 ? ")" : ",") + "</nobr></b></td>";
- }
- return ms;
- }
-
- private static boolean showShortType(int i,
- PsiParameter[] parameters,
- PsiExpression[] expressions,
- PsiSubstitutor substitutor) {
- PsiExpression expression = i < expressions.length ? expressions[i] : null;
- if (expression == null) return true;
- PsiType paramType = i < parameters.length && parameters[i] != null
- ? substitutor.substitute(parameters[i].getType())
- : null;
- return paramType != null && TypeConversionUtil.areTypesAssignmentCompatible(paramType, expression);
- }
-
-
- static HighlightInfo checkMethodMustHaveBody(PsiMethod method, PsiClass aClass) {
- HighlightInfo errorResult = null;
- if (method.getBody() == null
- && !method.hasModifierProperty(PsiModifier.ABSTRACT)
- && !method.hasModifierProperty(PsiModifier.NATIVE)
- && aClass != null
- && !aClass.isInterface()
- && !PsiUtilCore.hasErrorElementChild(method)) {
- int start = method.getModifierList().getTextRange().getStartOffset();
- int end = method.getTextRange().getEndOffset();
-
- String description = JavaErrorMessages.message("missing.method.body");
- errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(start, end).descriptionAndTooltip(description).create();
- if (HighlightUtil.getIncompatibleModifier(PsiModifier.ABSTRACT, method.getModifierList()) == null) {
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.ABSTRACT, true, false);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- }
- QuickFixAction.registerQuickFixAction(errorResult, new AddMethodBodyFix(method));
- }
- return errorResult;
- }
-
-
- static HighlightInfo checkAbstractMethodInConcreteClass(PsiMethod method, PsiElement elementToHighlight) {
- HighlightInfo errorResult = null;
- PsiClass aClass = method.getContainingClass();
- if (method.hasModifierProperty(PsiModifier.ABSTRACT)
- && aClass != null
- && !aClass.hasModifierProperty(PsiModifier.ABSTRACT)
- && !aClass.isEnum()
- && !PsiUtilCore.hasErrorElementChild(method)) {
- String description = JavaErrorMessages.message("abstract.method.in.non.abstract.class");
- errorResult =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(elementToHighlight).descriptionAndTooltip(description).create();
- if (method.getBody() != null) {
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.ABSTRACT, false, false);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- }
- QuickFixAction.registerQuickFixAction(errorResult, new AddMethodBodyFix(method));
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(aClass, PsiModifier.ABSTRACT, true, false);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- }
- return errorResult;
- }
-
-
- static HighlightInfo checkConstructorName(PsiMethod method) {
- String methodName = method.getName();
- PsiClass aClass = method.getContainingClass();
- HighlightInfo errorResult = null;
-
- if (aClass != null) {
- String className = aClass instanceof PsiAnonymousClass ? null : aClass.getName();
- if (className == null || !Comparing.strEqual(methodName, className)) {
- PsiElement element = method.getNameIdentifier();
- String description = JavaErrorMessages.message("missing.return.type");
- errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
- if (className != null) {
- QuickFixAction.registerQuickFixAction(errorResult, new RenameElementFix(method, className));
- }
- }
- }
- return errorResult;
- }
-
- @Nullable
- static HighlightInfo checkDuplicateMethod(PsiClass aClass,
- @NotNull PsiMethod method,
- @NotNull MostlySingularMultiMap<MethodSignature, PsiMethod> duplicateMethods) {
- if (aClass == null || method instanceof ExternallyDefinedPsiElement) return null;
- MethodSignature methodSignature = method.getSignature(PsiSubstitutor.EMPTY);
- int methodCount = 1;
- List<PsiMethod> methods = (List<PsiMethod>)duplicateMethods.get(methodSignature);
- if (methods.size() > 1) {
- methodCount++;
- }
-
- if (methodCount == 1 && aClass.isEnum() &&
- GenericsHighlightUtil.isEnumSyntheticMethod(methodSignature, aClass.getProject())) {
- methodCount++;
- }
- if (methodCount > 1) {
- String description = JavaErrorMessages.message("duplicate.method",
- JavaHighlightUtil.formatMethod(method),
- HighlightUtil.formatClass(aClass));
- TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
- @Nullable
- static HighlightInfo checkMethodCanHaveBody(PsiMethod method, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- PsiClass aClass = method.getContainingClass();
- boolean hasNoBody = method.getBody() == null;
- boolean isInterface = aClass != null && aClass.isInterface();
- boolean isExtension = method.hasModifierProperty(PsiModifier.DEFAULT);
- boolean isStatic = method.hasModifierProperty(PsiModifier.STATIC);
-
- final List<IntentionAction> additionalFixes = new ArrayList<IntentionAction>();
- String description = null;
- if (hasNoBody) {
- if (isExtension) {
- description = JavaErrorMessages.message("extension.method.should.have.a.body");
- additionalFixes.add(new AddMethodBodyFix(method));
- }
- else if (isInterface && isStatic) {
- description = "Static methods in interfaces should have a body";
- }
- }
- else if (isInterface) {
- if (!isExtension && !isStatic) {
- description = JavaErrorMessages.message("interface.methods.cannot.have.body");
- if (PsiUtil.isLanguageLevel8OrHigher(method)) {
- additionalFixes.add(QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.DEFAULT, true, false));
- additionalFixes.add(QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.STATIC, true, false));
- }
- }
- else if (isExtension) {
- return HighlightUtil.checkExtensionMethodsFeature(method, languageLevel,containingFile);
- }
- }
- else if (isExtension) {
- description = JavaErrorMessages.message("extension.method.in.class");
- }
- else if (method.hasModifierProperty(PsiModifier.ABSTRACT)) {
- description = JavaErrorMessages.message("abstract.methods.cannot.have.a.body");
- }
- else if (method.hasModifierProperty(PsiModifier.NATIVE)) {
- description = JavaErrorMessages.message("native.methods.cannot.have.a.body");
- }
- if (description == null) return null;
-
- TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
- HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- if (!hasNoBody) {
- QuickFixAction.registerQuickFixAction(info, new DeleteMethodBodyFix(method));
- }
- if (method.hasModifierProperty(PsiModifier.ABSTRACT) && !isInterface) {
- QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.ABSTRACT, false, false));
- }
- for (IntentionAction intentionAction : additionalFixes) {
- QuickFixAction.registerQuickFixAction(info, intentionAction);
- }
- return info;
- }
-
- @Nullable
- static HighlightInfo checkConstructorCallMustBeFirstStatement(PsiReferenceExpression expression) {
- PsiElement methodCall = expression.getParent();
- if (!RefactoringChangeUtil.isSuperOrThisMethodCall(methodCall)) return null;
- PsiElement codeBlock = methodCall.getParent().getParent();
- if (codeBlock instanceof PsiCodeBlock
- && codeBlock.getParent() instanceof PsiMethod
- && ((PsiMethod)codeBlock.getParent()).isConstructor()) {
- PsiElement prevSibling = methodCall.getParent().getPrevSibling();
- while (true) {
- if (prevSibling == null) return null;
- if (prevSibling instanceof PsiStatement) break;
- prevSibling = prevSibling.getPrevSibling();
- }
- }
- String message = JavaErrorMessages.message("constructor.call.must.be.first.statement", expression.getText() + "()");
- PsiElement element = expression.getParent();
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(message).create();
- }
-
-
- static HighlightInfo checkAbstractMethodDirectCall(PsiSuperExpression expr) {
- if (expr.getParent() instanceof PsiReferenceExpression
- && expr.getParent().getParent() instanceof PsiMethodCallExpression) {
- PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)expr.getParent().getParent();
- PsiMethod method = methodCallExpression.resolveMethod();
- if (method != null && method.hasModifierProperty(PsiModifier.ABSTRACT)) {
- String message = JavaErrorMessages.message("direct.abstract.method.access", JavaHighlightUtil.formatMethod(method));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(methodCallExpression).descriptionAndTooltip(message).create();
- }
- }
- return null;
- }
-
-
- static HighlightInfo checkConstructorCallsBaseClassConstructor(PsiMethod constructor,
- RefCountHolder refCountHolder,
- PsiResolveHelper resolveHelper) {
- if (!constructor.isConstructor()) return null;
- PsiClass aClass = constructor.getContainingClass();
- if (aClass == null) return null;
- if (aClass.isEnum()) return null;
- PsiCodeBlock body = constructor.getBody();
- if (body == null) return null;
-
- // check whether constructor call super(...) or this(...)
- PsiElement element = new PsiMatcherImpl(body)
- .firstChild(PsiMatchers.hasClass(PsiExpressionStatement.class))
- .firstChild(PsiMatchers.hasClass(PsiMethodCallExpression.class))
- .firstChild(PsiMatchers.hasClass(PsiReferenceExpression.class))
- .firstChild(PsiMatchers.hasClass(PsiKeyword.class))
- .getElement();
- if (element != null) return null;
- TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(constructor);
- PsiClassType[] handledExceptions = constructor.getThrowsList().getReferencedTypes();
- HighlightInfo info = HighlightClassUtil.checkBaseClassDefaultConstructorProblem(aClass, refCountHolder, resolveHelper, textRange, handledExceptions);
- if (info != null) {
- QuickFixAction.registerQuickFixAction(info, new InsertSuperFix(constructor));
- QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createAddDefaultConstructorFix(aClass.getSuperClass()));
- }
- return info;
- }
-
-
- /**
- * @return error if static method overrides instance method or
- * instance method overrides static. see JLS 8.4.6.1, 8.4.6.2
- */
- static HighlightInfo checkStaticMethodOverride(@NotNull PsiMethod method,@NotNull PsiFile containingFile) {
- // constructors are not members and therefor don't override class methods
- if (method.isConstructor()) {
- return null;
- }
-
- PsiClass aClass = method.getContainingClass();
- if (aClass == null) return null;
- PsiClass superClass = aClass.getSuperClass();
- PsiMethod superMethod = superClass == null
- ? null
- : MethodSignatureUtil.findMethodBySignature(superClass, method, true);
-
- boolean isStatic = method.hasModifierProperty(PsiModifier.STATIC);
- HighlightInfo highlightInfo = checkStaticMethodOverride(aClass, method, isStatic,superClass, superMethod,containingFile);
- if (highlightInfo != null) return highlightInfo;
- if (!isStatic) {
- // all methods in interface are instance, so no possible errors in this case
- return null;
- }
- PsiClass[] interfaces = aClass.getInterfaces();
- for (PsiClass aInterfaces : interfaces) {
- superClass = aInterfaces;
- superMethod = MethodSignatureUtil.findMethodInSuperClassBySignatureInDerived(aClass, superClass, method.getSignature(PsiSubstitutor.EMPTY), true);
- highlightInfo = checkStaticMethodOverride(aClass, method, true, superClass, superMethod,containingFile);
- if (highlightInfo != null) return highlightInfo;
- }
- return null;
- }
-
- private static HighlightInfo checkStaticMethodOverride(PsiClass aClass, PsiMethod method, boolean isMethodStatic, PsiClass superClass, PsiMethod superMethod,@NotNull PsiFile containingFile) {
- if (superMethod == null) return null;
- PsiManager manager = containingFile.getManager();
- PsiModifierList superModifierList = superMethod.getModifierList();
- PsiModifierList modifierList = method.getModifierList();
- if (superModifierList.hasModifierProperty(PsiModifier.PRIVATE)) return null;
- if (superModifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)
- && !JavaPsiFacade.getInstance(manager.getProject()).arePackagesTheSame(aClass, superClass)) {
- return null;
- }
- boolean isSuperMethodStatic = superModifierList.hasModifierProperty(PsiModifier.STATIC);
- if (isMethodStatic != isSuperMethodStatic) {
- TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
- @NonNls final String messageKey = isMethodStatic
- ? "static.method.cannot.override.instance.method"
- : "instance.method.cannot.override.static.method";
-
- String description = JavaErrorMessages.message(messageKey,
- JavaHighlightUtil.formatMethod(method),
- HighlightUtil.formatClass(aClass),
- JavaHighlightUtil.formatMethod(superMethod),
- HighlightUtil.formatClass(superClass));
-
- HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- if (!isSuperMethodStatic || HighlightUtil.getIncompatibleModifier(PsiModifier.STATIC, modifierList) == null) {
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.STATIC, isSuperMethodStatic, false);
- QuickFixAction.registerQuickFixAction(info, fix);
- }
- if (manager.isInProject(superMethod) &&
- (!isMethodStatic || HighlightUtil.getIncompatibleModifier(PsiModifier.STATIC, superModifierList) == null)) {
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(superMethod, PsiModifier.STATIC, isMethodStatic, true);
- QuickFixAction.registerQuickFixAction(info, fix);
- }
- return info;
- }
-
- if (isMethodStatic) {
- int accessLevel = PsiUtil.getAccessLevel(modifierList);
- String accessModifier = PsiUtil.getAccessModifier(accessLevel);
- HighlightInfo info = isWeaker(method, modifierList, accessModifier, accessLevel, superMethod, true);
- if (info != null) return info;
- info = checkSuperMethodIsFinal(method, superMethod);
- if (info != null) return info;
- }
- return null;
- }
-
- private static HighlightInfo checkInterfaceInheritedMethodsReturnTypes(List<? extends MethodSignatureBackedByPsiMethod> superMethodSignatures) {
- if (superMethodSignatures.size() < 2) return null;
- MethodSignatureBackedByPsiMethod returnTypeSubstitutable = superMethodSignatures.get(0);
- for (int i = 1; i < superMethodSignatures.size(); i++) {
- PsiMethod currentMethod = returnTypeSubstitutable.getMethod();
- PsiType currentType = returnTypeSubstitutable.getSubstitutor().substitute(currentMethod.getReturnType());
-
- MethodSignatureBackedByPsiMethod otherSuperSignature = superMethodSignatures.get(i);
- PsiMethod otherSuperMethod = otherSuperSignature.getMethod();
- PsiType otherSuperReturnType = otherSuperSignature.getSubstitutor().substitute(otherSuperMethod.getReturnType());
-
- PsiSubstitutor unifyingSubstitutor = MethodSignatureUtil.getSuperMethodSignatureSubstitutor(returnTypeSubstitutable,
- otherSuperSignature);
- if (unifyingSubstitutor != null) {
- otherSuperReturnType = unifyingSubstitutor.substitute(otherSuperReturnType);
- currentType = unifyingSubstitutor.substitute(currentType);
- }
-
- if (otherSuperReturnType == null || currentType == null || otherSuperReturnType.equals(currentType)) continue;
-
- if (PsiUtil.isLanguageLevel5OrHigher(currentMethod)) {
- //http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8 Example 8.1.5-3
- if (!(otherSuperReturnType instanceof PsiPrimitiveType || currentType instanceof PsiPrimitiveType)) {
- if (otherSuperReturnType.isAssignableFrom(currentType)) continue;
- if (currentType.isAssignableFrom(otherSuperReturnType)) {
- returnTypeSubstitutable = otherSuperSignature;
- continue;
- }
- }
- }
- return createIncompatibleReturnTypeMessage(currentMethod, currentMethod, otherSuperMethod, false, otherSuperReturnType,
- currentType, JavaErrorMessages.message("unrelated.overriding.methods.return.types"));
- }
- return null;
- }
-
- static HighlightInfo checkOverrideEquivalentInheritedMethods(PsiClass aClass, PsiFile containingFile) {
- String description = null;
- final Collection<HierarchicalMethodSignature> visibleSignatures = aClass.getVisibleSignatures();
- PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(aClass.getProject()).getResolveHelper();
- Ultimate:
- for (HierarchicalMethodSignature signature : visibleSignatures) {
- PsiMethod method = signature.getMethod();
- if (!resolveHelper.isAccessible(method, aClass, null)) continue;
- List<HierarchicalMethodSignature> superSignatures = signature.getSuperSignatures();
-
- boolean allAbstracts = method.hasModifierProperty(PsiModifier.ABSTRACT);
- final PsiClass containingClass = method.getContainingClass();
- if (aClass.equals(containingClass)) continue; //to be checked at method level
-
- if (aClass.isInterface() && !containingClass.isInterface()) continue;
- HighlightInfo highlightInfo = null;
- if (allAbstracts) {
- superSignatures = new ArrayList<HierarchicalMethodSignature>(superSignatures);
- superSignatures.add(signature);
- highlightInfo = checkInterfaceInheritedMethodsReturnTypes(superSignatures);
- }
- else {
- highlightInfo = checkMethodIncompatibleReturnType(signature, superSignatures, false);
- }
- if (highlightInfo != null) description = highlightInfo.getDescription();
-
- if (method.hasModifierProperty(PsiModifier.STATIC)) {
- for (HierarchicalMethodSignature superSignature : superSignatures) {
- PsiMethod superMethod = superSignature.getMethod();
- if (!superMethod.hasModifierProperty(PsiModifier.STATIC)) {
- description = JavaErrorMessages.message("static.method.cannot.override.instance.method",
- JavaHighlightUtil.formatMethod(method),
- HighlightUtil.formatClass(containingClass),
- JavaHighlightUtil.formatMethod(superMethod),
- HighlightUtil.formatClass(superMethod.getContainingClass()));
- break Ultimate;
- }
- }
- continue;
- }
-
- if (description == null) {
- highlightInfo = checkMethodIncompatibleThrows(signature, superSignatures, false, aClass);
- if (highlightInfo != null) description = highlightInfo.getDescription();
- }
-
- if (description == null) {
- highlightInfo = checkMethodWeakerPrivileges(signature, superSignatures, false, containingFile);
- if (highlightInfo != null) description = highlightInfo.getDescription();
- }
-
- if (description != null) break;
- }
-
-
- if (description != null) {
- // show error info at the class level
- TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
-
- static HighlightInfo checkConstructorHandleSuperClassExceptions(PsiMethod method) {
- if (!method.isConstructor()) {
- return null;
- }
- PsiCodeBlock body = method.getBody();
- PsiStatement[] statements = body == null ? null : body.getStatements();
- if (statements == null) return null;
-
- // if we have unhandled exception inside method body, we could not have been called here,
- // so the only problem it can catch here is with super ctr only
- Collection<PsiClassType> unhandled = ExceptionUtil.collectUnhandledExceptions(method, method.getContainingClass());
- if (unhandled.isEmpty()) return null;
- String description = HighlightUtil.getUnhandledExceptionsDescriptor(unhandled);
- TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
- HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- for (PsiClassType exception : unhandled) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new LocalQuickFixOnPsiElementAsIntentionAdapter(QUICK_FIX_FACTORY.createMethodThrowsFix(method, exception, true, false)));
- }
- return highlightInfo;
- }
-
-
- static HighlightInfo checkRecursiveConstructorInvocation(PsiMethod method) {
- if (HighlightControlFlowUtil.isRecursivelyCalledConstructor(method)) {
- TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
- String description = JavaErrorMessages.message("recursive.constructor.invocation");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
- @NotNull
- public static TextRange getFixRange(@NotNull PsiElement element) {
- TextRange range = element.getTextRange();
- int start = range.getStartOffset();
- int end = range.getEndOffset();
-
- PsiElement nextSibling = element.getNextSibling();
- if (nextSibling instanceof PsiJavaToken && ((PsiJavaToken)nextSibling).getTokenType() == JavaTokenType.SEMICOLON) {
- return new TextRange(start, end + 1);
- }
- return range;
- }
-
-
- static void checkNewExpression(@NotNull PsiNewExpression expression, @NotNull HighlightInfoHolder holder, @NotNull JavaSdkVersion javaSdkVersion) {
- PsiType type = expression.getType();
- if (!(type instanceof PsiClassType)) return;
- PsiClassType.ClassResolveResult typeResult = ((PsiClassType)type).resolveGenerics();
- PsiClass aClass = typeResult.getElement();
- if (aClass == null) return;
- if (aClass instanceof PsiAnonymousClass) {
- type = ((PsiAnonymousClass)aClass).getBaseClassType();
- typeResult = ((PsiClassType)type).resolveGenerics();
- aClass = typeResult.getElement();
- if (aClass == null) return;
- }
-
- PsiJavaCodeReferenceElement classReference = expression.getClassOrAnonymousClassReference();
- checkConstructorCall(typeResult, expression, type, classReference, holder, javaSdkVersion);
- }
-
-
- public static void checkConstructorCall(PsiClassType.ClassResolveResult typeResolveResult,
- PsiConstructorCall constructorCall,
- PsiType type,
- PsiJavaCodeReferenceElement classReference,
- final HighlightInfoHolder holder,
- @NotNull JavaSdkVersion javaSdkVersion) {
- PsiExpressionList list = constructorCall.getArgumentList();
- if (list == null) return;
- PsiClass aClass = typeResolveResult.getElement();
- if (aClass == null) return;
- final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(holder.getProject()).getResolveHelper();
- PsiClass accessObjectClass = null;
- if (constructorCall instanceof PsiNewExpression) {
- PsiExpression qualifier = ((PsiNewExpression)constructorCall).getQualifier();
- if (qualifier != null) {
- accessObjectClass = (PsiClass)PsiUtil.getAccessObjectClass(qualifier).getElement();
- }
- }
- if (classReference != null && !resolveHelper.isAccessible(aClass, constructorCall, accessObjectClass)) {
- String description = HighlightUtil.buildProblemWithAccessDescription(classReference, typeResolveResult);
- PsiElement element = classReference.getReferenceNameElement();
- HighlightInfo info =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
- HighlightUtil.registerAccessQuickFixAction(aClass, classReference, info, null);
- holder.add(info);
- return;
- }
- PsiMethod[] constructors = aClass.getConstructors();
-
- if (constructors.length == 0) {
- if (list.getExpressions().length != 0) {
- String constructorName = aClass.getName();
- String argTypes = buildArgTypesList(list);
- String description = JavaErrorMessages.message("wrong.constructor.arguments", constructorName+"()", argTypes);
- String tooltip = createMismatchedArgumentsHtmlTooltip(list, PsiParameter.EMPTY_ARRAY, constructorName, PsiSubstitutor.EMPTY, aClass);
- HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).description(description).escapedToolTip(tooltip).navigationShift(+1).create();
- QuickFixAction.registerQuickFixAction(info, constructorCall.getTextRange(), new CreateConstructorFromCallFix(constructorCall));
- if (classReference != null) {
- ConstructorParametersFixer.registerFixActions(classReference, constructorCall, info,getFixRange(list));
- }
- holder.add(info);
- return;
- }
- if (classReference != null && aClass.hasModifierProperty(PsiModifier.PROTECTED) && callingProtectedConstructorFromDerivedClass(constructorCall, aClass)) {
- holder.add(buildAccessProblem(classReference, typeResolveResult, aClass));
- } else if (aClass.isInterface() && constructorCall instanceof PsiNewExpression) {
- final PsiReferenceParameterList typeArgumentList = ((PsiNewExpression)constructorCall).getTypeArgumentList();
- if (typeArgumentList.getTypeArguments().length > 0) {
- holder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeArgumentList)
- .descriptionAndTooltip("Anonymous class implements interface; cannot have type arguments").create());
- }
- }
- }
- else {
- PsiElement place = list;
- if (constructorCall instanceof PsiNewExpression) {
- final PsiAnonymousClass anonymousClass = ((PsiNewExpression)constructorCall).getAnonymousClass();
- if (anonymousClass != null) place = anonymousClass;
- }
-
- JavaResolveResult[] results = resolveHelper.multiResolveConstructor((PsiClassType)type, list, place);
- MethodCandidateInfo result = null;
- if (results.length == 1) result = (MethodCandidateInfo)results[0];
-
- PsiMethod constructor = result == null ? null : result.getElement();
-
- boolean applicable = true;
- try {
- applicable = constructor != null && result.isApplicable();
- }
- catch (IndexNotReadyException e) {
- // ignore
- }
-
- if (constructor == null) {
- String name = aClass.getName();
- name += buildArgTypesList(list);
- String description = JavaErrorMessages.message("cannot.resolve.constructor", name);
- HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).descriptionAndTooltip(description).navigationShift(+1).create();
- QuickFixAction.registerQuickFixAction(info, constructorCall.getTextRange(), new CreateConstructorFromCallFix(constructorCall));
- if (classReference != null) {
- ConstructorParametersFixer.registerFixActions(classReference, constructorCall, info,getFixRange(list));
- PermuteArgumentsFix.registerFix(info, constructorCall, toMethodCandidates(results), getFixRange(list));
- }
- WrapExpressionFix.registerWrapAction(results, list.getExpressions(), info);
- holder.add(info);
- ChangeStringLiteralToCharInMethodCallFix.registerFixes(constructors, constructorCall, info);
- }
- else {
- if (classReference != null && (!result.isAccessible() ||
- constructor.hasModifierProperty(PsiModifier.PROTECTED) && callingProtectedConstructorFromDerivedClass(constructorCall, aClass))) {
- holder.add(buildAccessProblem(classReference, result, constructor));
- }
- else if (!applicable) {
- String constructorName = HighlightMessageUtil.getSymbolName(constructor, result.getSubstitutor());
- String containerName = HighlightMessageUtil.getSymbolName(constructor.getContainingClass(), result.getSubstitutor());
- String argTypes = buildArgTypesList(list);
- String description = JavaErrorMessages.message("wrong.method.arguments", constructorName, containerName, argTypes);
- String toolTip = createMismatchedArgumentsHtmlTooltip(result, list);
- PsiElement infoElement = list.getTextLength() > 0 ? list : constructorCall;
- HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(infoElement).description(description).escapedToolTip(toolTip).navigationShift(+1).create();
- if (info != null) {
- QuickFixAction.registerQuickFixAction(info, constructorCall.getTextRange(), new CreateConstructorFromCallFix(constructorCall));
- if (classReference != null) {
- ConstructorParametersFixer.registerFixActions(classReference, constructorCall, info, getFixRange(infoElement));
- ChangeMethodSignatureFromUsageFix.registerIntentions(results, list, info, null);
- ChangeTypeArgumentsFix.registerIntentions(results, list, info, aClass);
- ConvertDoubleToFloatFix.registerIntentions(results, list, info, null);
- PermuteArgumentsFix.registerFix(info, constructorCall, toMethodCandidates(results), getFixRange(list));
- ChangeParameterClassFix.registerQuickFixActions(constructorCall, list, info);
- QuickFixAction.registerQuickFixAction(info, getFixRange(list), new SurroundWithArrayFix(constructorCall));
- }
- holder.add(info);
- ChangeStringLiteralToCharInMethodCallFix.registerFixes(constructors, constructorCall, info);
- }
- }
- else {
- if (constructorCall instanceof PsiNewExpression) {
- PsiReferenceParameterList typeArgumentList = ((PsiNewExpression)constructorCall).getTypeArgumentList();
- HighlightInfo info = GenericsHighlightUtil.checkReferenceTypeArgumentList(constructor, typeArgumentList, result.getSubstitutor(), false, javaSdkVersion);
- if (info != null) {
- holder.add(info);
- }
- }
- }
- }
- }
- }
-
- private static HighlightInfo buildAccessProblem(@NotNull PsiJavaCodeReferenceElement classReference, JavaResolveResult result, PsiMember elementToFix) {
- String description = HighlightUtil.buildProblemWithAccessDescription(classReference, result);
- HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(classReference).descriptionAndTooltip(
- description).navigationShift(+1).create();
- if (result.isStaticsScopeCorrect()) {
- HighlightUtil.registerAccessQuickFixAction(elementToFix, classReference, info, result.getCurrentFileResolveScope());
- }
- return info;
- }
-
- private static boolean callingProtectedConstructorFromDerivedClass(PsiConstructorCall place, PsiClass constructorClass) {
- if (constructorClass == null) return false;
- // indirect instantiation via anonymous class is ok
- if (place instanceof PsiNewExpression && ((PsiNewExpression)place).getAnonymousClass() != null) return false;
- PsiElement curElement = place;
- PsiClass containingClass = constructorClass.getContainingClass();
- while (true) {
- PsiClass aClass = PsiTreeUtil.getParentOfType(curElement, PsiClass.class);
- if (aClass == null) return false;
- curElement = aClass;
- if ((aClass.isInheritor(constructorClass, true) || containingClass != null && aClass.isInheritor(containingClass, true))
- && !JavaPsiFacade.getInstance(aClass.getProject()).arePackagesTheSame(aClass, constructorClass)) {
- return true;
- }
- }
- }
-
- private static String buildArgTypesList(PsiExpressionList list) {
- StringBuilder builder = new StringBuilder();
- builder.append("(");
- PsiExpression[] args = list.getExpressions();
- for (int i = 0; i < args.length; i++) {
- if (i > 0) {
- builder.append(", ");
- }
- PsiType argType = args[i].getType();
- builder.append(argType != null ? JavaHighlightUtil.formatType(argType) : "?");
- }
- builder.append(")");
- return builder.toString();
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightNamesUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightNamesUtil.java
deleted file mode 100644
index b9ea90cc1dc7..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightNamesUtil.java
+++ /dev/null
@@ -1,305 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * @author cdr
- */
-package com.intellij.codeInsight.daemon.impl.analysis;
-
-import com.intellij.application.options.colors.ScopeAttributesUtil;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
-import com.intellij.lang.ASTNode;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.editor.colors.TextAttributesKey;
-import com.intellij.openapi.editor.colors.TextAttributesScheme;
-import com.intellij.openapi.editor.markup.TextAttributes;
-import com.intellij.openapi.util.Pair;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.packageDependencies.DependencyValidationManager;
-import com.intellij.packageDependencies.DependencyValidationManagerImpl;
-import com.intellij.psi.*;
-import com.intellij.psi.impl.source.tree.ElementType;
-import com.intellij.psi.impl.source.tree.TreeUtil;
-import com.intellij.psi.search.scope.packageSet.NamedScope;
-import com.intellij.psi.search.scope.packageSet.NamedScopesHolder;
-import com.intellij.psi.search.scope.packageSet.PackageSet;
-import com.intellij.psi.util.PsiTreeUtil;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.List;
-
-public class HighlightNamesUtil {
- private static final Logger LOG = Logger.getInstance("#" + HighlightNamesUtil.class.getName());
-
- @Nullable
- public static HighlightInfo highlightMethodName(@NotNull PsiMethod method,
- final PsiElement elementToHighlight,
- final boolean isDeclaration,
- @NotNull TextAttributesScheme colorsScheme) {
- return highlightMethodName(method, elementToHighlight, elementToHighlight.getTextRange(), colorsScheme, isDeclaration);
- }
-
- @Nullable
- public static HighlightInfo highlightMethodName(@NotNull PsiMethod method,
- final PsiElement elementToHighlight,
- TextRange range, @NotNull TextAttributesScheme colorsScheme, final boolean isDeclaration) {
- boolean isInherited = false;
-
- if (!isDeclaration) {
- if (isCalledOnThis(elementToHighlight)) {
- PsiClass enclosingClass = PsiTreeUtil.getParentOfType(elementToHighlight, PsiClass.class);
- isInherited = enclosingClass != null && enclosingClass.isInheritor(method.getContainingClass(), true);
- }
- }
-
- HighlightInfoType type = getMethodNameHighlightType(method, isDeclaration, isInherited);
- if (type != null && elementToHighlight != null) {
- TextAttributes attributes = mergeWithScopeAttributes(method, type, colorsScheme);
- HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(type).range(range);
- if (attributes != null) {
- builder.textAttributes(attributes);
- }
- return builder.createUnconditionally();
- }
- return null;
- }
-
- private static boolean isCalledOnThis(PsiElement elementToHighlight) {
- PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(elementToHighlight, PsiMethodCallExpression.class);
- if (methodCallExpression != null) {
- PsiElement qualifier = methodCallExpression.getMethodExpression().getQualifier();
- if (qualifier == null || qualifier instanceof PsiThisExpression) {
- return true;
- }
- }
- return false;
- }
-
- private static TextAttributes mergeWithScopeAttributes(final PsiElement element,
- @NotNull HighlightInfoType type,
- @NotNull TextAttributesScheme colorsScheme) {
- TextAttributes regularAttributes = HighlightInfo.getAttributesByType(element, type, colorsScheme);
- if (element == null) return regularAttributes;
- TextAttributes scopeAttributes = getScopeAttributes(element, colorsScheme);
- return TextAttributes.merge(scopeAttributes, regularAttributes);
- }
-
- @Nullable
- public static HighlightInfo highlightClassName(PsiClass aClass, PsiElement elementToHighlight, @NotNull TextAttributesScheme colorsScheme) {
- HighlightInfoType type = getClassNameHighlightType(aClass, elementToHighlight);
- if (elementToHighlight != null) {
- TextAttributes attributes = mergeWithScopeAttributes(aClass, type, colorsScheme);
- TextRange range = elementToHighlight.getTextRange();
- if (elementToHighlight instanceof PsiJavaCodeReferenceElement) {
- final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement)elementToHighlight;
- PsiReferenceParameterList parameterList = referenceElement.getParameterList();
- if (parameterList != null) {
- final TextRange paramListRange = parameterList.getTextRange();
- if (paramListRange.getEndOffset() > paramListRange.getStartOffset()) {
- range = new TextRange(range.getStartOffset(), paramListRange.getStartOffset());
- }
- }
- }
-
- // This will highlight @ sign in annotation as well.
- final PsiElement parent = elementToHighlight.getParent();
- if (parent instanceof PsiAnnotation) {
- final PsiAnnotation psiAnnotation = (PsiAnnotation)parent;
- range = new TextRange(psiAnnotation.getTextRange().getStartOffset(), range.getEndOffset());
- }
-
- HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(type).range(range);
- if (attributes != null) {
- builder.textAttributes(attributes);
- }
- return builder.createUnconditionally();
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo highlightVariableName(final PsiVariable variable,
- final PsiElement elementToHighlight,
- @NotNull TextAttributesScheme colorsScheme) {
- HighlightInfoType varType = getVariableNameHighlightType(variable);
- if (varType != null) {
- if (variable instanceof PsiField) {
- TextAttributes attributes = mergeWithScopeAttributes(variable, varType, colorsScheme);
- HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(varType).range(elementToHighlight.getTextRange());
- if (attributes != null) {
- builder.textAttributes(attributes);
- }
- return builder.createUnconditionally();
- }
- return HighlightInfo.newHighlightInfo(varType).range(elementToHighlight).create();
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo highlightClassNameInQualifier(final PsiJavaCodeReferenceElement element,
- @NotNull TextAttributesScheme colorsScheme) {
- PsiExpression qualifierExpression = null;
- if (element instanceof PsiReferenceExpression) {
- qualifierExpression = ((PsiReferenceExpression)element).getQualifierExpression();
- }
- if (qualifierExpression instanceof PsiJavaCodeReferenceElement) {
- PsiElement resolved = ((PsiJavaCodeReferenceElement)qualifierExpression).resolve();
- if (resolved instanceof PsiClass) {
- return highlightClassName((PsiClass)resolved, qualifierExpression, colorsScheme);
- }
- }
- return null;
- }
-
- private static HighlightInfoType getMethodNameHighlightType(@NotNull PsiMethod method, boolean isDeclaration, boolean isInheritedMethod) {
- if (method.isConstructor()) {
- return isDeclaration ? HighlightInfoType.CONSTRUCTOR_DECLARATION : HighlightInfoType.CONSTRUCTOR_CALL;
- }
- if (isDeclaration) return HighlightInfoType.METHOD_DECLARATION;
- if (method.hasModifierProperty(PsiModifier.STATIC)) {
- return HighlightInfoType.STATIC_METHOD;
- }
- if (isInheritedMethod) return HighlightInfoType.INHERITED_METHOD;
- if(method.hasModifierProperty(PsiModifier.ABSTRACT)) {
- return HighlightInfoType.ABSTRACT_METHOD;
- }
- return HighlightInfoType.METHOD_CALL;
- }
-
- @Nullable
- private static HighlightInfoType getVariableNameHighlightType(PsiVariable var) {
- if (var instanceof PsiLocalVariable
- || var instanceof PsiParameter && ((PsiParameter)var).getDeclarationScope() instanceof PsiForeachStatement) {
- return HighlightInfoType.LOCAL_VARIABLE;
- }
- if (var instanceof PsiField) {
- return var.hasModifierProperty(PsiModifier.STATIC) ? var.hasModifierProperty(PsiModifier.FINAL)
- ? HighlightInfoType.STATIC_FINAL_FIELD
- : HighlightInfoType.STATIC_FIELD : HighlightInfoType.INSTANCE_FIELD;
- }
- if (var instanceof PsiParameter) {
- return HighlightInfoType.PARAMETER;
- }
- return null;
- }
-
- @NotNull
- private static HighlightInfoType getClassNameHighlightType(@Nullable PsiClass aClass, @Nullable PsiElement element) {
- if (element instanceof PsiJavaCodeReferenceElement && element.getParent() instanceof PsiAnonymousClass) {
- return HighlightInfoType.ANONYMOUS_CLASS_NAME;
- }
- if (aClass != null) {
- if (aClass.isAnnotationType()) return HighlightInfoType.ANNOTATION_NAME;
- if (aClass.isInterface()) return HighlightInfoType.INTERFACE_NAME;
- if (aClass.isEnum()) return HighlightInfoType.ENUM_NAME;
- if (aClass instanceof PsiTypeParameter) return HighlightInfoType.TYPE_PARAMETER_NAME;
- final PsiModifierList modList = aClass.getModifierList();
- if (modList != null && modList.hasModifierProperty(PsiModifier.ABSTRACT)) return HighlightInfoType.ABSTRACT_CLASS_NAME;
- }
- // use class by default
- return HighlightInfoType.CLASS_NAME;
- }
-
- @Nullable
- public static HighlightInfo highlightReassignedVariable(PsiVariable variable, PsiElement elementToHighlight) {
- if (variable instanceof PsiLocalVariable) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.REASSIGNED_LOCAL_VARIABLE).range(elementToHighlight).create();
- }
- if (variable instanceof PsiParameter) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.REASSIGNED_PARAMETER).range(elementToHighlight).create();
- }
- return null;
- }
-
- private static TextAttributes getScopeAttributes(@NotNull PsiElement element, @NotNull TextAttributesScheme colorsScheme) {
- PsiFile file = element.getContainingFile();
- if (file == null) return null;
- TextAttributes result = null;
- DependencyValidationManagerImpl validationManager = (DependencyValidationManagerImpl)DependencyValidationManager.getInstance(file.getProject());
- List<Pair<NamedScope,NamedScopesHolder>> scopes = validationManager.getScopeBasedHighlightingCachedScopes();
- for (Pair<NamedScope, NamedScopesHolder> scope : scopes) {
- NamedScope namedScope = scope.getFirst();
- NamedScopesHolder scopesHolder = scope.getSecond();
- PackageSet packageSet = namedScope.getValue();
- if (packageSet != null && packageSet.contains(file, scopesHolder)) {
- TextAttributesKey scopeKey = ScopeAttributesUtil.getScopeTextAttributeKey(namedScope.getName());
- TextAttributes attributes = colorsScheme.getAttributes(scopeKey);
- if (attributes == null || attributes.isEmpty()) {
- continue;
- }
- result = TextAttributes.merge(attributes, result);
- }
- }
- return result;
- }
-
- public static TextRange getMethodDeclarationTextRange(@NotNull PsiMethod method) {
- if (method instanceof SyntheticElement) return TextRange.EMPTY_RANGE;
- int start = stripAnnotationsFromModifierList(method.getModifierList());
- final TextRange throwsRange = method.getThrowsList().getTextRange();
- LOG.assertTrue(throwsRange != null, method);
- int end = throwsRange.getEndOffset();
- return new TextRange(start, end);
- }
-
- public static TextRange getFieldDeclarationTextRange(@NotNull PsiField field) {
- int start = stripAnnotationsFromModifierList(field.getModifierList());
- int end = field.getNameIdentifier().getTextRange().getEndOffset();
- return new TextRange(start, end);
- }
-
- public static TextRange getClassDeclarationTextRange(@NotNull PsiClass aClass) {
- if (aClass instanceof PsiEnumConstantInitializer) {
- return ((PsiEnumConstantInitializer)aClass).getEnumConstant().getNameIdentifier().getTextRange();
- }
- final PsiElement psiElement = aClass instanceof PsiAnonymousClass
- ? ((PsiAnonymousClass)aClass).getBaseClassReference()
- : aClass.getModifierList() == null ? aClass.getNameIdentifier() : aClass.getModifierList();
- if(psiElement == null) return new TextRange(aClass.getTextRange().getStartOffset(), aClass.getTextRange().getStartOffset());
- int start = stripAnnotationsFromModifierList(psiElement);
- PsiElement endElement = aClass instanceof PsiAnonymousClass ?
- ((PsiAnonymousClass)aClass).getBaseClassReference() :
- aClass.getImplementsList();
- if (endElement == null) endElement = aClass.getNameIdentifier();
- TextRange endTextRange = endElement == null ? null : endElement.getTextRange();
- int end = endTextRange == null ? start : endTextRange.getEndOffset();
- return new TextRange(start, end);
- }
-
- private static int stripAnnotationsFromModifierList(@NotNull PsiElement element) {
- TextRange textRange = element.getTextRange();
- if (textRange == null) return 0;
- PsiAnnotation lastAnnotation = null;
- for (PsiElement child : element.getChildren()) {
- if (child instanceof PsiAnnotation) lastAnnotation = (PsiAnnotation)child;
- }
- if (lastAnnotation == null) {
- return textRange.getStartOffset();
- }
- ASTNode node = lastAnnotation.getNode();
- if (node != null) {
- do {
- node = TreeUtil.nextLeaf(node);
- }
- while (node != null && ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(node.getElementType()));
- }
- if (node != null) return node.getTextRange().getStartOffset();
- return textRange.getStartOffset();
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java
deleted file mode 100644
index a3fb8928efef..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java
+++ /dev/null
@@ -1,2755 +0,0 @@
-/*
- * Copyright 2000-2013 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.analysis;
-
-import com.intellij.codeInsight.ContainerProvider;
-import com.intellij.codeInsight.ExceptionUtil;
-import com.intellij.codeInsight.daemon.JavaErrorMessages;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
-import com.intellij.codeInsight.daemon.impl.quickfix.*;
-import com.intellij.codeInsight.highlighting.HighlightUsagesDescriptionLocation;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.codeInsight.intention.QuickFixFactory;
-import com.intellij.codeInsight.quickfix.ChangeVariableTypeQuickFixProvider;
-import com.intellij.codeInsight.quickfix.UnresolvedReferenceQuickFixProvider;
-import com.intellij.codeInspection.LocalQuickFixOnPsiElementAsIntentionAdapter;
-import com.intellij.lang.findUsages.LanguageFindUsages;
-import com.intellij.lang.java.JavaLanguage;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.extensions.Extensions;
-import com.intellij.openapi.module.Module;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.projectRoots.JavaSdkVersion;
-import com.intellij.openapi.roots.ProjectFileIndex;
-import com.intellij.openapi.util.Comparing;
-import com.intellij.openapi.util.Condition;
-import com.intellij.openapi.util.Pair;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.openapi.vfs.VirtualFile;
-import com.intellij.pom.java.LanguageLevel;
-import com.intellij.psi.*;
-import com.intellij.psi.impl.source.resolve.JavaResolveUtil;
-import com.intellij.psi.impl.source.tree.java.PsiLiteralExpressionImpl;
-import com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl;
-import com.intellij.psi.javadoc.PsiDocComment;
-import com.intellij.psi.scope.processor.VariablesNotProcessor;
-import com.intellij.psi.scope.util.PsiScopesUtil;
-import com.intellij.psi.search.GlobalSearchScope;
-import com.intellij.psi.templateLanguages.OuterLanguageElement;
-import com.intellij.psi.tree.IElementType;
-import com.intellij.psi.util.*;
-import com.intellij.refactoring.util.RefactoringChangeUtil;
-import com.intellij.util.ArrayUtil;
-import com.intellij.util.ArrayUtilRt;
-import com.intellij.util.Function;
-import com.intellij.util.IncorrectOperationException;
-import com.intellij.util.containers.ContainerUtil;
-import com.intellij.util.ui.UIUtil;
-import com.intellij.xml.util.XmlStringUtil;
-import com.intellij.psi.util.FileTypeUtils;
-import gnu.trove.THashMap;
-import org.intellij.lang.annotations.Language;
-import org.jetbrains.annotations.NonNls;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-import org.jetbrains.annotations.PropertyKey;
-
-import java.util.*;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * @author cdr
- * @since Jul 30, 2002
- */
-public class HighlightUtil extends HighlightUtilBase {
- private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil");
-
- @NotNull private static final Map<String, Set<String>> ourInterfaceIncompatibleModifiers;
- @NotNull private static final Map<String, Set<String>> ourMethodIncompatibleModifiers;
- @NotNull private static final Map<String, Set<String>> ourFieldIncompatibleModifiers;
- @NotNull private static final Map<String, Set<String>> ourClassIncompatibleModifiers;
- @NotNull private static final Map<String, Set<String>> ourClassInitializerIncompatibleModifiers;
- @NotNull private static final Set<String> ourConstructorNotAllowedModifiers;
-
- @NonNls private static final String SERIAL_PERSISTENT_FIELDS_FIELD_NAME = "serialPersistentFields";
- private static final QuickFixFactory QUICK_FIX_FACTORY = QuickFixFactory.getInstance();
-
- private HighlightUtil() { }
-
- static {
- ourClassIncompatibleModifiers = new THashMap<String, Set<String>>(8);
- ourClassIncompatibleModifiers.put(PsiModifier.ABSTRACT, ContainerUtil.newTroveSet(PsiModifier.FINAL));
- ourClassIncompatibleModifiers.put(PsiModifier.FINAL, ContainerUtil.newTroveSet(PsiModifier.ABSTRACT));
- ourClassIncompatibleModifiers.put(PsiModifier.PACKAGE_LOCAL, ContainerUtil.newTroveSet(PsiModifier.PRIVATE, PsiModifier.PUBLIC, PsiModifier.PROTECTED));
- ourClassIncompatibleModifiers.put(PsiModifier.PRIVATE, ContainerUtil.newTroveSet(PsiModifier.PACKAGE_LOCAL, PsiModifier.PUBLIC, PsiModifier.PROTECTED));
- ourClassIncompatibleModifiers.put(PsiModifier.PUBLIC, ContainerUtil.newTroveSet(PsiModifier.PACKAGE_LOCAL, PsiModifier.PRIVATE, PsiModifier.PROTECTED));
- ourClassIncompatibleModifiers.put(PsiModifier.PROTECTED, ContainerUtil.newTroveSet(PsiModifier.PACKAGE_LOCAL, PsiModifier.PUBLIC, PsiModifier.PRIVATE));
- ourClassIncompatibleModifiers.put(PsiModifier.STRICTFP, Collections.<String>emptySet());
- ourClassIncompatibleModifiers.put(PsiModifier.STATIC, Collections.<String>emptySet());
-
- ourInterfaceIncompatibleModifiers = new THashMap<String, Set<String>>(7);
- ourInterfaceIncompatibleModifiers.put(PsiModifier.ABSTRACT, Collections.<String>emptySet());
- ourInterfaceIncompatibleModifiers.put(PsiModifier.PACKAGE_LOCAL, ContainerUtil.newTroveSet(PsiModifier.PRIVATE, PsiModifier.PUBLIC, PsiModifier.PROTECTED));
- ourInterfaceIncompatibleModifiers.put(PsiModifier.PRIVATE, ContainerUtil.newTroveSet(PsiModifier.PACKAGE_LOCAL, PsiModifier.PUBLIC, PsiModifier.PROTECTED));
- ourInterfaceIncompatibleModifiers.put(PsiModifier.PUBLIC, ContainerUtil.newTroveSet(PsiModifier.PACKAGE_LOCAL, PsiModifier.PRIVATE, PsiModifier.PROTECTED));
- ourInterfaceIncompatibleModifiers.put(PsiModifier.PROTECTED, ContainerUtil.newTroveSet(PsiModifier.PACKAGE_LOCAL, PsiModifier.PUBLIC, PsiModifier.PRIVATE));
- ourInterfaceIncompatibleModifiers.put(PsiModifier.STRICTFP, Collections.<String>emptySet());
- ourInterfaceIncompatibleModifiers.put(PsiModifier.STATIC, Collections.<String>emptySet());
-
- ourMethodIncompatibleModifiers = new THashMap<String, Set<String>>(11);
- ourMethodIncompatibleModifiers.put(PsiModifier.ABSTRACT, ContainerUtil.newTroveSet(PsiModifier.NATIVE, PsiModifier.STATIC, PsiModifier.FINAL, PsiModifier.PRIVATE, PsiModifier.STRICTFP, PsiModifier.SYNCHRONIZED, PsiModifier.DEFAULT));
- ourMethodIncompatibleModifiers.put(PsiModifier.NATIVE, ContainerUtil.newTroveSet(PsiModifier.ABSTRACT, PsiModifier.STRICTFP));
- ourMethodIncompatibleModifiers.put(PsiModifier.PACKAGE_LOCAL, ContainerUtil.newTroveSet(PsiModifier.PRIVATE, PsiModifier.PUBLIC, PsiModifier.PROTECTED));
- ourMethodIncompatibleModifiers.put(PsiModifier.PRIVATE, ContainerUtil.newTroveSet(PsiModifier.PACKAGE_LOCAL, PsiModifier.PUBLIC, PsiModifier.PROTECTED));
- ourMethodIncompatibleModifiers.put(PsiModifier.PUBLIC, ContainerUtil.newTroveSet(PsiModifier.PACKAGE_LOCAL, PsiModifier.PRIVATE, PsiModifier.PROTECTED));
- ourMethodIncompatibleModifiers.put(PsiModifier.PROTECTED, ContainerUtil.newTroveSet(PsiModifier.PACKAGE_LOCAL, PsiModifier.PUBLIC, PsiModifier.PRIVATE));
- ourMethodIncompatibleModifiers.put(PsiModifier.STATIC, ContainerUtil.newTroveSet(PsiModifier.ABSTRACT, PsiModifier.DEFAULT));
- ourMethodIncompatibleModifiers.put(PsiModifier.DEFAULT, ContainerUtil.newTroveSet(PsiModifier.ABSTRACT, PsiModifier.STATIC));
- ourMethodIncompatibleModifiers.put(PsiModifier.SYNCHRONIZED, ContainerUtil.newTroveSet(PsiModifier.ABSTRACT));
- ourMethodIncompatibleModifiers.put(PsiModifier.STRICTFP, ContainerUtil.newTroveSet(PsiModifier.ABSTRACT));
- ourMethodIncompatibleModifiers.put(PsiModifier.FINAL, ContainerUtil.newTroveSet(PsiModifier.ABSTRACT));
-
- ourFieldIncompatibleModifiers = new THashMap<String, Set<String>>(8);
- ourFieldIncompatibleModifiers.put(PsiModifier.FINAL, ContainerUtil.newTroveSet(PsiModifier.VOLATILE));
- ourFieldIncompatibleModifiers.put(PsiModifier.PACKAGE_LOCAL, ContainerUtil.newTroveSet(PsiModifier.PRIVATE, PsiModifier.PUBLIC, PsiModifier.PROTECTED));
- ourFieldIncompatibleModifiers.put(PsiModifier.PRIVATE, ContainerUtil.newTroveSet(PsiModifier.PACKAGE_LOCAL, PsiModifier.PUBLIC, PsiModifier.PROTECTED));
- ourFieldIncompatibleModifiers.put(PsiModifier.PUBLIC, ContainerUtil.newTroveSet(PsiModifier.PACKAGE_LOCAL, PsiModifier.PRIVATE, PsiModifier.PROTECTED));
- ourFieldIncompatibleModifiers.put(PsiModifier.PROTECTED, ContainerUtil.newTroveSet(PsiModifier.PACKAGE_LOCAL, PsiModifier.PUBLIC, PsiModifier.PRIVATE));
- ourFieldIncompatibleModifiers.put(PsiModifier.STATIC, Collections.<String>emptySet());
- ourFieldIncompatibleModifiers.put(PsiModifier.TRANSIENT, Collections.<String>emptySet());
- ourFieldIncompatibleModifiers.put(PsiModifier.VOLATILE, ContainerUtil.newTroveSet(PsiModifier.FINAL));
-
- ourClassInitializerIncompatibleModifiers = new THashMap<String, Set<String>>(1);
- ourClassInitializerIncompatibleModifiers.put(PsiModifier.STATIC, Collections.<String>emptySet());
-
- ourConstructorNotAllowedModifiers = ContainerUtil.newTroveSet(PsiModifier.ABSTRACT, PsiModifier.STATIC, PsiModifier.NATIVE, PsiModifier.FINAL, PsiModifier.STRICTFP, PsiModifier.SYNCHRONIZED);
- }
-
- @Nullable
- public static String getIncompatibleModifier(String modifier,
- @Nullable PsiModifierList modifierList,
- @NotNull Map<String, Set<String>> incompatibleModifiersHash) {
- if (modifierList == null) return null;
-
- // modifier is always incompatible with itself
- PsiElement[] modifiers = modifierList.getChildren();
- int modifierCount = 0;
- for (PsiElement otherModifier : modifiers) {
- if (Comparing.equal(modifier, otherModifier.getText(), true)) modifierCount++;
- }
- if (modifierCount > 1) {
- return modifier;
- }
-
- Set<String> incompatibles = incompatibleModifiersHash.get(modifier);
- if (incompatibles == null) return null;
- final boolean level8OrHigher = PsiUtil.isLanguageLevel8OrHigher(modifierList);
- for (@PsiModifier.ModifierConstant String incompatible : incompatibles) {
- if (level8OrHigher) {
- if (modifier.equals(PsiModifier.STATIC) && incompatible.equals(PsiModifier.ABSTRACT)){
- continue;
- }
- }
- if (modifierList.hasModifierProperty(incompatible)) {
- return incompatible;
- }
- else if (PsiModifier.ABSTRACT.equals(incompatible) && modifierList.hasExplicitModifier(incompatible)) {
- return incompatible;
- }
- }
-
- return null;
- }
-
- /**
- * make element protected/package local/public suggestion
- */
- static void registerAccessQuickFixAction(@NotNull PsiMember refElement,
- @NotNull PsiJavaCodeReferenceElement place,
- @Nullable HighlightInfo errorResult,
- final PsiElement fileResolveScope) {
- if (errorResult == null) return;
- PsiClass accessObjectClass = null;
- PsiElement qualifier = place.getQualifier();
- if (qualifier instanceof PsiExpression) {
- accessObjectClass = (PsiClass)PsiUtil.getAccessObjectClass((PsiExpression)qualifier).getElement();
- }
- ReplaceInaccessibleFieldWithGetterSetterFix.registerQuickFix(refElement, place, accessObjectClass, errorResult);
-
- if (refElement instanceof PsiCompiledElement) return;
- PsiModifierList modifierList = refElement.getModifierList();
- if (modifierList == null) return;
-
- PsiClass packageLocalClassInTheMiddle = getPackageLocalClassInTheMiddle(place);
- if (packageLocalClassInTheMiddle != null) {
- IntentionAction fix =
- QUICK_FIX_FACTORY.createModifierListFix(packageLocalClassInTheMiddle, PsiModifier.PUBLIC, true, true);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- return;
- }
-
- try {
- Project project = refElement.getProject();
- JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
- PsiModifierList modifierListCopy = facade.getElementFactory().createFieldFromText("int a;", null).getModifierList();
- modifierListCopy.setModifierProperty(PsiModifier.STATIC, modifierList.hasModifierProperty(PsiModifier.STATIC));
- String minModifier = PsiModifier.PACKAGE_LOCAL;
- if (refElement.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
- minModifier = PsiModifier.PROTECTED;
- }
- if (refElement.hasModifierProperty(PsiModifier.PROTECTED)) {
- minModifier = PsiModifier.PUBLIC;
- }
- String[] modifiers = {PsiModifier.PACKAGE_LOCAL, PsiModifier.PROTECTED, PsiModifier.PUBLIC,};
- for (int i = ArrayUtil.indexOf(modifiers, minModifier); i < modifiers.length; i++) {
- @PsiModifier.ModifierConstant String modifier = modifiers[i];
- modifierListCopy.setModifierProperty(modifier, true);
- if (facade.getResolveHelper().isAccessible(refElement, modifierListCopy, place, accessObjectClass, fileResolveScope)) {
- IntentionAction fix = QUICK_FIX_FACTORY.createModifierListFix(refElement, modifier, true, true);
- TextRange fixRange = new TextRange(errorResult.startOffset, errorResult.endOffset);
- PsiElement ref = place.getReferenceNameElement();
- if (ref != null) {
- fixRange = fixRange.union(ref.getTextRange());
- }
- QuickFixAction.registerQuickFixAction(errorResult, fixRange, fix);
- }
- }
- }
- catch (IncorrectOperationException e) {
- LOG.error(e);
- }
- }
-
- @Nullable
- private static PsiClass getPackageLocalClassInTheMiddle(@NotNull PsiElement place) {
- if (place instanceof PsiReferenceExpression) {
- // check for package local classes in the middle
- PsiReferenceExpression expression = (PsiReferenceExpression)place;
- while (true) {
- PsiElement resolved = expression.resolve();
- if (resolved instanceof PsiField) {
- PsiField field = (PsiField)resolved;
- PsiClass aClass = field.getContainingClass();
- if (aClass != null && aClass.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) &&
- !JavaPsiFacade.getInstance(aClass.getProject()).arePackagesTheSame(aClass, place)) {
-
- return aClass;
- }
- }
- PsiExpression qualifier = expression.getQualifierExpression();
- if (!(qualifier instanceof PsiReferenceExpression)) break;
- expression = (PsiReferenceExpression)qualifier;
- }
- }
- return null;
- }
-
-
- @Nullable
- static HighlightInfo checkInstanceOfApplicable(@NotNull PsiInstanceOfExpression expression) {
- PsiExpression operand = expression.getOperand();
- PsiTypeElement typeElement = expression.getCheckType();
- if (typeElement == null) return null;
- PsiType checkType = typeElement.getType();
- PsiType operandType = operand.getType();
- if (operandType == null) return null;
- if (operandType instanceof PsiLambdaExpressionType) {
- return HighlightInfo
- .newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip("Lambda expression is not expected here").create();
- }
- if (operandType instanceof PsiMethodReferenceType) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(
- "Method reference expression is not expected here").create();
- }
- if (TypeConversionUtil.isPrimitiveAndNotNull(operandType)
- || TypeConversionUtil.isPrimitiveAndNotNull(checkType)
- || !TypeConversionUtil.areTypesConvertible(operandType, checkType)) {
- String message = JavaErrorMessages.message("inconvertible.type.cast", JavaHighlightUtil.formatType(operandType), JavaHighlightUtil
- .formatType(checkType));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- return null;
- }
-
-
- @Nullable
- static HighlightInfo checkInconvertibleTypeCast(@NotNull PsiTypeCastExpression expression) {
- PsiTypeElement castTypeElement = expression.getCastType();
- if (castTypeElement == null) return null;
- PsiType castType = castTypeElement.getType();
-
- PsiExpression operand = expression.getOperand();
- if (operand == null) return null;
- PsiType operandType = operand.getType();
-
- if (operandType != null &&
- !TypeConversionUtil.areTypesConvertible(operandType, castType) &&
- !RedundantCastUtil.isInPolymorphicCall(expression)) {
- String message = JavaErrorMessages.message("inconvertible.type.cast", JavaHighlightUtil.formatType(operandType), JavaHighlightUtil
- .formatType(castType));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
-
- return null;
- }
-
- @Nullable
- static HighlightInfo checkVariableExpected(@NotNull PsiExpression expression) {
- PsiExpression lValue;
- if (expression instanceof PsiAssignmentExpression) {
- PsiAssignmentExpression assignment = (PsiAssignmentExpression)expression;
- lValue = assignment.getLExpression();
- }
- else if (PsiUtil.isIncrementDecrementOperation(expression)) {
- lValue = expression instanceof PsiPostfixExpression
- ? ((PsiPostfixExpression)expression).getOperand()
- : ((PsiPrefixExpression)expression).getOperand();
- }
- else {
- lValue = null;
- }
- HighlightInfo errorResult = null;
- if (lValue != null && !TypeConversionUtil.isLValue(lValue)) {
- String description = JavaErrorMessages.message("variable.expected");
- errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(lValue).descriptionAndTooltip(description).create();
- }
-
- return errorResult;
- }
-
-
- @Nullable
- static HighlightInfo checkAssignmentOperatorApplicable(@NotNull PsiAssignmentExpression assignment,@NotNull PsiFile containingFile) {
- PsiJavaToken operationSign = assignment.getOperationSign();
- IElementType eqOpSign = operationSign.getTokenType();
- IElementType opSign = TypeConversionUtil.convertEQtoOperation(eqOpSign);
- if (opSign == null) return null;
- final PsiType lType = assignment.getLExpression().getType();
- final PsiExpression rExpression = assignment.getRExpression();
- if (rExpression == null) return null;
- final PsiType rType = rExpression.getType();
- HighlightInfo errorResult = null;
- if (!TypeConversionUtil.isBinaryOperatorApplicable(opSign, lType, rType, true) ||
- PsiType.getJavaLangObject(containingFile.getManager(), assignment.getResolveScope()).equals(lType)) {
- String operatorText = operationSign.getText().substring(0, operationSign.getText().length() - 1);
- String message = JavaErrorMessages.message("binary.operator.not.applicable", operatorText,
- JavaHighlightUtil.formatType(lType),
- JavaHighlightUtil.formatType(rType));
-
- errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(assignment).descriptionAndTooltip(message).create();
- QuickFixAction.registerQuickFixAction(errorResult, new ChangeToAppendFix(eqOpSign, lType, assignment));
- }
- return errorResult;
- }
-
-
- @Nullable
- static HighlightInfo checkAssignmentCompatibleTypes(@NotNull PsiAssignmentExpression assignment) {
- if (!"=".equals(assignment.getOperationSign().getText())) return null;
- PsiExpression lExpr = assignment.getLExpression();
- PsiExpression rExpr = assignment.getRExpression();
- if (rExpr == null) return null;
- PsiType lType = lExpr.getType();
- PsiType rType = rExpr.getType();
- if (rType == null) return null;
-
- HighlightInfo highlightInfo = checkAssignability(lType, rType, rExpr, assignment);
- if (highlightInfo == null) {
- return null;
- }
-
- registerChangeVariableTypeFixes(lExpr, rType, highlightInfo);
- if (lType != null) {
- registerChangeVariableTypeFixes(rExpr, lType, highlightInfo);
- }
-
- return highlightInfo;
- }
-
- private static void registerChangeVariableTypeFixes(@NotNull PsiExpression expression,
- @NotNull PsiType type,
- @Nullable HighlightInfo highlightInfo) {
- if (highlightInfo == null || !(expression instanceof PsiReferenceExpression)) return;
-
- final PsiElement element = ((PsiReferenceExpression)expression).resolve();
- if (element == null || !(element instanceof PsiVariable)) return;
-
- registerChangeVariableTypeFixes((PsiVariable)element, type, highlightInfo);
- }
-
- private static boolean isCastIntentionApplicable(@NotNull PsiExpression expression, @Nullable PsiType toType) {
- while (expression instanceof PsiTypeCastExpression || expression instanceof PsiParenthesizedExpression) {
- if (expression instanceof PsiTypeCastExpression) {
- expression = ((PsiTypeCastExpression)expression).getOperand();
- }
- if (expression instanceof PsiParenthesizedExpression) {
- expression = ((PsiParenthesizedExpression)expression).getExpression();
- }
- }
- if (expression == null) return false;
- PsiType rType = expression.getType();
- return rType != null && toType != null && TypeConversionUtil.areTypesConvertible(rType, toType);
- }
-
-
- @Nullable
- static HighlightInfo checkVariableInitializerType(@NotNull PsiVariable variable) {
- PsiExpression initializer = variable.getInitializer();
- // array initializer checked in checkArrayInitializerApplicable
- if (initializer == null || initializer instanceof PsiArrayInitializerExpression) return null;
- PsiType lType = variable.getType();
- PsiType rType = initializer.getType();
- int start = variable.getTypeElement().getTextRange().getStartOffset();
- int end = variable.getTextRange().getEndOffset();
- HighlightInfo highlightInfo = checkAssignability(lType, rType, initializer, new TextRange(start, end), 0);
- if (highlightInfo != null) {
- registerChangeVariableTypeFixes(variable, rType, highlightInfo);
- }
- return highlightInfo;
- }
-
- @Nullable
- static HighlightInfo checkAssignability(PsiType lType, @Nullable PsiType rType, PsiExpression expression, @NotNull PsiElement elementToHighlight) {
- TextRange textRange = elementToHighlight.getTextRange();
- return checkAssignability(lType, rType, expression, textRange, 0);
- }
-
- @Nullable
- public static HighlightInfo checkAssignability(@Nullable PsiType lType,
- @Nullable PsiType rType,
- @Nullable PsiExpression expression,
- @NotNull TextRange textRange,
- int navigationShift) {
- if (lType == rType) return null;
- if (expression == null) {
- if (rType == null || lType == null || TypeConversionUtil.isAssignable(lType, rType)) return null;
- }
- else if (TypeConversionUtil.areTypesAssignmentCompatible(lType, expression)) {
- return null;
- }
- if (rType == null) {
- rType = expression.getType();
- }
- HighlightInfo highlightInfo = createIncompatibleTypeHighlightInfo(lType, rType, textRange, navigationShift);
- if (rType != null && expression != null && isCastIntentionApplicable(expression, lType)) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new AddTypeCastFix(lType, expression));
- }
- if (expression != null && lType != null) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new WrapExpressionFix(lType, expression));
- AddTypeArgumentsConditionalFix.register(highlightInfo, expression, lType);
- }
- ChangeNewOperatorTypeFix.register(highlightInfo, expression, lType);
- return highlightInfo;
- }
-
-
- @Nullable
- static HighlightInfo checkReturnStatementType(@NotNull PsiReturnStatement statement) {
- PsiMethod method = null;
- PsiLambdaExpression lambda = null;
- PsiElement parent = statement.getParent();
- while (true) {
- if (parent instanceof PsiFile) break;
- if (parent instanceof PsiClassInitializer) break;
- if (parent instanceof PsiLambdaExpression){
- lambda = (PsiLambdaExpression)parent;
- break;
- }
- if (parent instanceof PsiMethod) {
- method = (PsiMethod)parent;
- break;
- }
- parent = parent.getParent();
- }
- String description;
- HighlightInfo errorResult = null;
- if (method == null && lambda != null) {
- //todo check return statements type inside lambda
- }
- else if (method == null && !(parent instanceof ServerPageFile)) {
- description = JavaErrorMessages.message("return.outside.method");
- errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).create();
- }
- else {
- PsiType returnType = method != null ? method.getReturnType() : null/*JSP page returns void*/;
- boolean isMethodVoid = returnType == null || PsiType.VOID.equals(returnType);
- final PsiExpression returnValue = statement.getReturnValue();
- if (returnValue != null) {
- PsiType valueType = returnValue.getType();
- if (isMethodVoid) {
- description = JavaErrorMessages.message("return.from.void.method");
- errorResult =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).create();
- if (valueType != null) {
- IntentionAction fix = QUICK_FIX_FACTORY.createMethodReturnFix(method, valueType, true);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- }
- }
- else {
- TextRange textRange = statement.getTextRange();
- errorResult = checkAssignability(returnType, valueType, returnValue, textRange, returnValue.getStartOffsetInParent());
- if (errorResult != null && valueType != null) {
- IntentionAction fix = QUICK_FIX_FACTORY.createMethodReturnFix(method, valueType, true);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- ChangeParameterClassFix.registerQuickFixAction(returnType, valueType, errorResult);
- if (returnType instanceof PsiArrayType && TypeConversionUtil.isAssignable(((PsiArrayType)returnType).getComponentType(), valueType)) {
- QuickFixAction.registerQuickFixAction(errorResult, new SurroundWithArrayFix(null) {
- @Override
- protected PsiExpression getExpression(final PsiElement element) {
- return returnValue.isValid() ? returnValue : null;
- }
- });
- }
- }
- }
- }
- else {
- if (!isMethodVoid) {
- description = JavaErrorMessages.message("missing.return.value");
- errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).navigationShift(PsiKeyword.RETURN.length()).create();
- IntentionAction fix = QUICK_FIX_FACTORY.createMethodReturnFix(method, PsiType.VOID, true);
- QuickFixAction.registerQuickFixAction(errorResult, fix);
- }
- }
- }
- return errorResult;
- }
-
- @NotNull
- public static String getUnhandledExceptionsDescriptor(@NotNull final Collection<PsiClassType> unhandled) {
- return getUnhandledExceptionsDescriptor(unhandled, null);
- }
-
- @NotNull
- private static String getUnhandledExceptionsDescriptor(@NotNull final Collection<PsiClassType> unhandled, @Nullable final String source) {
- final String exceptions = formatTypes(unhandled);
- return source != null ? JavaErrorMessages.message("unhandled.close.exceptions", exceptions, unhandled.size(), source)
- : JavaErrorMessages.message("unhandled.exceptions", exceptions, unhandled.size());
- }
-
- @NotNull
- private static String formatTypes(@NotNull Collection<PsiClassType> unhandled) {
- return StringUtil.join(unhandled, new Function<PsiClassType, String>() {
- @NotNull
- @Override
- public String fun(PsiClassType type) {
- return JavaHighlightUtil.formatType(type);
- }
- }, ", ");
- }
-
- @Nullable
- static HighlightInfo checkVariableAlreadyDefined(@NotNull PsiVariable variable) {
- if (variable instanceof ExternallyDefinedPsiElement) return null;
- boolean isIncorrect = false;
- PsiElement declarationScope;
- if (variable instanceof PsiLocalVariable ||
- variable instanceof PsiParameter &&
- ((declarationScope = ((PsiParameter)variable).getDeclarationScope()) instanceof PsiCatchSection ||
- declarationScope instanceof PsiForeachStatement ||
- declarationScope instanceof PsiLambdaExpression)) {
- @SuppressWarnings("unchecked")
- PsiElement scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class, PsiResourceList.class);
- VariablesNotProcessor proc = new VariablesNotProcessor(variable, false) {
- @Override
- protected boolean check(final PsiVariable var, final ResolveState state) {
- return (var instanceof PsiLocalVariable || var instanceof PsiParameter) && super.check(var, state);
- }
- };
- PsiIdentifier identifier = variable.getNameIdentifier();
- assert identifier != null : variable;
- PsiScopesUtil.treeWalkUp(proc, identifier, scope);
- if (scope instanceof PsiResourceList && proc.size() == 0) {
- scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class);
- PsiScopesUtil.treeWalkUp(proc, identifier, scope);
- }
- if (proc.size() > 0) {
- isIncorrect = true;
- }
- }
- else if (variable instanceof PsiField) {
- PsiField field = (PsiField)variable;
- PsiClass aClass = field.getContainingClass();
- if (aClass == null) return null;
- PsiField fieldByName = aClass.findFieldByName(variable.getName(), false);
- if (fieldByName != null && fieldByName != field) {
- isIncorrect = true;
- }
- }
- else {
- PsiElement scope = variable.getParent();
- PsiElement[] children = scope.getChildren();
- for (PsiElement child : children) {
- if (child instanceof PsiVariable) {
- if (child.equals(variable)) continue;
- if (variable.getName().equals(((PsiVariable)child).getName())) {
- isIncorrect = true;
- break;
- }
- }
- }
- }
-
- if (isIncorrect) {
- String description = JavaErrorMessages.message("variable.already.defined", variable.getName());
- PsiIdentifier identifier = variable.getNameIdentifier();
- assert identifier != null : variable;
- HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier).descriptionAndTooltip(description).create();
- if (variable instanceof PsiLocalVariable) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new ReuseVariableDeclarationFix((PsiLocalVariable)variable));
- }
- return highlightInfo;
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkUnderscore(@NotNull PsiIdentifier identifier, @NotNull PsiVariable variable) {
- if ("_".equals(variable.getName()) && PsiUtil.isLanguageLevel8OrHigher(variable)) {
- if (variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiLambdaExpression) {
- String message = JavaErrorMessages.message("underscore.lambda.identifier");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier).descriptionAndTooltip(message).create();
- }
- else {
- String message = JavaErrorMessages.message("underscore.identifier");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.WARNING).range(identifier).descriptionAndTooltip(message).create();
- }
- }
-
- return null;
- }
-
- @NotNull
- public static String formatClass(@NotNull PsiClass aClass) {
- return formatClass(aClass, true);
- }
-
- @NotNull
- public static String formatClass(@NotNull PsiClass aClass, boolean fqn) {
- return PsiFormatUtil.formatClass(aClass, PsiFormatUtilBase.SHOW_NAME |
- PsiFormatUtilBase.SHOW_ANONYMOUS_CLASS_VERBOSE | (fqn ? PsiFormatUtilBase.SHOW_FQ_NAME : 0));
- }
-
- @NotNull
- public static String formatField(@NotNull PsiField field) {
- return PsiFormatUtil.formatVariable(field, PsiFormatUtilBase.SHOW_CONTAINING_CLASS | PsiFormatUtilBase.SHOW_NAME, PsiSubstitutor.EMPTY);
- }
-
- @Nullable
- public static HighlightInfo checkUnhandledExceptions(@NotNull final PsiElement element, @Nullable TextRange textRange) {
- final List<PsiClassType> unhandledExceptions = ExceptionUtil.getUnhandledExceptions(element);
- if (unhandledExceptions.isEmpty()) return null;
-
- final HighlightInfoType highlightType = getUnhandledExceptionHighlightType(element);
- if (highlightType == null) return null;
-
- if (textRange == null) textRange = element.getTextRange();
- final String description = getUnhandledExceptionsDescriptor(unhandledExceptions);
- HighlightInfo errorResult = HighlightInfo.newHighlightInfo(highlightType).range(textRange).descriptionAndTooltip(description).create();
- registerUnhandledExceptionFixes(element, errorResult, unhandledExceptions);
- return errorResult;
- }
-
- @Nullable
- public static HighlightInfo checkUnhandledCloserExceptions(@NotNull final PsiResourceVariable resource) {
- final List<PsiClassType> unhandled = ExceptionUtil.getUnhandledCloserExceptions(resource, null);
- if (unhandled.isEmpty()) return null;
-
- final HighlightInfoType highlightType = getUnhandledExceptionHighlightType(resource);
- if (highlightType == null) return null;
-
- final String description = getUnhandledExceptionsDescriptor(unhandled, "auto-closeable resource");
- final HighlightInfo highlight =
- HighlightInfo.newHighlightInfo(highlightType).range(resource).descriptionAndTooltip(description).create();
- registerUnhandledExceptionFixes(resource, highlight, unhandled);
- return highlight;
- }
-
- private static void registerUnhandledExceptionFixes(@NotNull final PsiElement element,
- final HighlightInfo errorResult,
- @NotNull final List<PsiClassType> unhandled) {
- QuickFixAction.registerQuickFixAction(errorResult, new AddExceptionToCatchFix());
- QuickFixAction.registerQuickFixAction(errorResult, new AddExceptionToThrowsFix(element));
- QuickFixAction.registerQuickFixAction(errorResult, new SurroundWithTryCatchFix(element));
- if (unhandled.size() == 1) {
- QuickFixAction.registerQuickFixAction(errorResult, new GeneralizeCatchFix(element, unhandled.get(0)));
- }
- }
-
- @Nullable
- private static HighlightInfoType getUnhandledExceptionHighlightType(final PsiElement element) {
- if (!FileTypeUtils.isInServerPageFile(element)) {
- return HighlightInfoType.UNHANDLED_EXCEPTION;
- }
- PsiMethod targetMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
- if (!(targetMethod instanceof SyntheticElement)) return HighlightInfoType.UNHANDLED_EXCEPTION;
- // ignore JSP top level errors - it handled by UnhandledExceptionInJSP inspection
- return null;
- }
-
-
- @Nullable
- static HighlightInfo checkBreakOutsideLoop(@NotNull PsiBreakStatement statement) {
- if (statement.getLabelIdentifier() == null) {
- if (new PsiMatcherImpl(statement).ancestor(EnclosingLoopOrSwitchMatcherExpression.INSTANCE).getElement() == null) {
- String description = JavaErrorMessages.message("break.outside.switch.or.loop");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).create();
- }
- }
- else {
- // todo labeled
- }
- return null;
- }
-
-
- @Nullable
- static HighlightInfo checkContinueOutsideLoop(@NotNull PsiContinueStatement statement) {
- if (statement.getLabelIdentifier() == null) {
- if (new PsiMatcherImpl(statement).ancestor(EnclosingLoopMatcherExpression.INSTANCE).getElement() == null) {
- String description = JavaErrorMessages.message("continue.outside.loop");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).create();
- }
- }
- else {
- PsiStatement exitedStatement = statement.findContinuedStatement();
- if (exitedStatement == null) return null;
- if (!(exitedStatement instanceof PsiForStatement) && !(exitedStatement instanceof PsiWhileStatement) &&
- !(exitedStatement instanceof PsiDoWhileStatement) && !(exitedStatement instanceof PsiForeachStatement)) {
- String description = JavaErrorMessages.message("not.loop.label", statement.getLabelIdentifier().getText());
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).create();
- }
- }
- return null;
- }
-
- @Nullable
- static HighlightInfo checkIllegalModifierCombination(@NotNull PsiKeyword keyword, @NotNull PsiModifierList modifierList) {
- @PsiModifier.ModifierConstant String modifier = keyword.getText();
- String incompatible = getIncompatibleModifier(modifier, modifierList);
- if (incompatible != null) {
- String message = JavaErrorMessages.message("incompatible.modifiers", modifier, incompatible);
- HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(keyword).descriptionAndTooltip(message).create();
- QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createModifierListFix(modifierList, modifier, false, false));
- return highlightInfo;
- }
-
- return null;
- }
-
- @Nullable
- private static Map<String, Set<String>> getIncompatibleModifierMap(@NotNull PsiModifierList modifierList) {
- PsiElement parent = modifierList.getParent();
- if (parent == null || PsiUtilCore.hasErrorElementChild(parent)) return null;
- return parent instanceof PsiClass
- ? ((PsiClass)parent).isInterface() ? ourInterfaceIncompatibleModifiers : ourClassIncompatibleModifiers
- : parent instanceof PsiMethod
- ? ourMethodIncompatibleModifiers
- : parent instanceof PsiVariable
- ? ourFieldIncompatibleModifiers
- : parent instanceof PsiClassInitializer ? ourClassInitializerIncompatibleModifiers : null;
- }
-
- @Nullable
- public static String getIncompatibleModifier(String modifier, @NotNull PsiModifierList modifierList) {
- PsiElement parent = modifierList.getParent();
- if (parent == null || PsiUtilCore.hasErrorElementChild(parent)) return null;
- final Map<String, Set<String>> incompatibleModifierMap = getIncompatibleModifierMap(modifierList);
- if (incompatibleModifierMap == null) return null;
- return getIncompatibleModifier(modifier, modifierList, incompatibleModifierMap);
- }
-
-
- @Nullable
- public static HighlightInfo checkNotAllowedModifier(@NotNull PsiKeyword keyword, @NotNull PsiModifierList modifierList) {
- PsiElement modifierOwner = modifierList.getParent();
- if (modifierOwner == null) return null;
- if (PsiUtilCore.hasErrorElementChild(modifierOwner)) return null;
-
- @PsiModifier.ModifierConstant String modifier = keyword.getText();
- final Map<String, Set<String>> incompatibleModifierMap = getIncompatibleModifierMap(modifierList);
- if (incompatibleModifierMap == null) return null;
-
- Set<String> incompatibles = incompatibleModifierMap.get(modifier);
- PsiElement modifierOwnerParent = modifierOwner instanceof PsiMember ? ((PsiMember)modifierOwner).getContainingClass() : modifierOwner.getParent();
- if (modifierOwnerParent == null) modifierOwnerParent = modifierOwner.getParent();
- boolean isAllowed = true;
- if (modifierOwner instanceof PsiClass) {
- PsiClass aClass = (PsiClass)modifierOwner;
- if (aClass.isInterface()) {
- if (PsiModifier.STATIC.equals(modifier) || PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) ||
- PsiModifier.PACKAGE_LOCAL.equals(modifier)) {
- isAllowed = modifierOwnerParent instanceof PsiClass;
- }
- }
- else {
- if (PsiModifier.PUBLIC.equals(modifier)) {
- isAllowed = modifierOwnerParent instanceof PsiJavaFile ||
- modifierOwnerParent instanceof PsiClass &&
- (modifierOwnerParent instanceof PsiSyntheticClass || ((PsiClass)modifierOwnerParent).getQualifiedName() != null);
- }
- else if (PsiModifier.STATIC.equals(modifier) || PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) ||
- PsiModifier.PACKAGE_LOCAL.equals(modifier)) {
- isAllowed = modifierOwnerParent instanceof PsiClass &&
- ((PsiClass)modifierOwnerParent).getQualifiedName() != null || FileTypeUtils.isInServerPageFile(modifierOwnerParent);
- }
-
- if (aClass.isEnum()) {
- isAllowed &= !(PsiModifier.FINAL.equals(modifier) || PsiModifier.ABSTRACT.equals(modifier));
- }
-
- if (aClass.getContainingClass() instanceof PsiAnonymousClass) {
- isAllowed &= !(PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier));
- }
- }
- }
- else if (modifierOwner instanceof PsiMethod) {
- PsiMethod method = (PsiMethod)modifierOwner;
- isAllowed = !(method.isConstructor() && ourConstructorNotAllowedModifiers.contains(modifier));
- PsiClass containingClass = method.getContainingClass();
- if ((method.hasModifierProperty(PsiModifier.PUBLIC) || method.hasModifierProperty(PsiModifier.PROTECTED)) && method.isConstructor() &&
- containingClass != null && containingClass.isEnum()) {
- isAllowed = false;
- }
-
- if (PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) || PsiModifier.TRANSIENT.equals(modifier) ||
- PsiModifier.STRICTFP.equals(modifier) || PsiModifier.SYNCHRONIZED.equals(modifier)) {
- isAllowed &= modifierOwnerParent instanceof PsiClass && !((PsiClass)modifierOwnerParent).isInterface();
- }
- }
- else if (modifierOwner instanceof PsiField) {
- if (PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) || PsiModifier.TRANSIENT.equals(modifier) ||
- PsiModifier.STRICTFP.equals(modifier) || PsiModifier.SYNCHRONIZED.equals(modifier)) {
- isAllowed = modifierOwnerParent instanceof PsiClass && !((PsiClass)modifierOwnerParent).isInterface();
- }
- }
- else if (modifierOwner instanceof PsiClassInitializer) {
- isAllowed = PsiModifier.STATIC.equals(modifier);
- }
- else if (modifierOwner instanceof PsiLocalVariable || modifierOwner instanceof PsiParameter) {
- isAllowed = PsiModifier.FINAL.equals(modifier);
- }
-
- isAllowed &= incompatibles != null;
- if (!isAllowed) {
- String message = JavaErrorMessages.message("modifier.not.allowed", modifier);
-
- HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(keyword).descriptionAndTooltip(message).create();
- QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createModifierListFix(modifierList, modifier, false, false));
- return highlightInfo;
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkLiteralExpressionParsingError(@NotNull final PsiLiteralExpression expression,
- @NotNull LanguageLevel languageLevel, @NotNull PsiFile containingFile) {
- PsiElement literal = expression.getFirstChild();
- assert literal instanceof PsiJavaToken : literal;
- IElementType type = ((PsiJavaToken)literal).getTokenType();
- if (type == JavaTokenType.TRUE_KEYWORD || type == JavaTokenType.FALSE_KEYWORD || type == JavaTokenType.NULL_KEYWORD) {
- return null;
- }
-
- boolean isInt = PsiLiteralExpressionImpl.INTEGER_LITERALS.contains(type);
- boolean isFP = PsiLiteralExpressionImpl.REAL_LITERALS.contains(type);
- String text = isInt || isFP ? literal.getText().toLowerCase() : literal.getText();
- Object value = expression.getValue();
-
- if (isFP) {
- if (text.startsWith(PsiLiteralExpressionImpl.HEX_PREFIX)) {
- final HighlightInfo info = checkFeature(expression, Feature.HEX_FP_LITERALS, languageLevel, containingFile);
- if (info != null) return info;
- }
- }
- if (isInt) {
- if (text.startsWith(PsiLiteralExpressionImpl.BIN_PREFIX)) {
- final HighlightInfo info = checkFeature(expression, Feature.BIN_LITERALS, languageLevel, containingFile);
- if (info != null) return info;
- }
- }
- if (isInt || isFP) {
- if (text.contains("_")) {
- HighlightInfo info = checkFeature(expression, Feature.UNDERSCORES, languageLevel, containingFile);
- if (info != null) return info;
- info = checkUnderscores(expression, text, isInt);
- if (info != null) return info;
- }
- }
-
- final PsiElement parent = expression.getParent();
- if (type == JavaTokenType.INTEGER_LITERAL) {
- //literal 2147483648 may appear only as the operand of the unary negation operator -.
- if (!(text.equals(PsiLiteralExpressionImpl._2_IN_31) &&
- parent instanceof PsiPrefixExpression &&
- ((PsiPrefixExpression)parent).getOperationTokenType() == JavaTokenType.MINUS)) {
- if (text.equals(PsiLiteralExpressionImpl.HEX_PREFIX)) {
- final String message = JavaErrorMessages.message("hexadecimal.numbers.must.contain.at.least.one.hexadecimal.digit");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- if (text.equals(PsiLiteralExpressionImpl.BIN_PREFIX)) {
- final String message = JavaErrorMessages.message("binary.numbers.must.contain.at.least.one.hexadecimal.digit");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- if (value == null || text.equals(PsiLiteralExpressionImpl._2_IN_31)) {
- final String message = JavaErrorMessages.message("integer.number.too.large");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- }
- }
- else if (type == JavaTokenType.LONG_LITERAL) {
- final String mText = text.endsWith("l") ? text.substring(0, text.length() - 1) : text;
- //literal 9223372036854775808L may appear only as the operand of the unary negation operator -.
- if (!(mText.equals(PsiLiteralExpressionImpl._2_IN_63) &&
- parent instanceof PsiPrefixExpression &&
- ((PsiPrefixExpression)parent).getOperationTokenType() == JavaTokenType.MINUS)) {
- if (mText.equals(PsiLiteralExpressionImpl.HEX_PREFIX)) {
- final String message = JavaErrorMessages.message("hexadecimal.numbers.must.contain.at.least.one.hexadecimal.digit");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- if (mText.equals(PsiLiteralExpressionImpl.BIN_PREFIX)) {
- final String message = JavaErrorMessages.message("binary.numbers.must.contain.at.least.one.hexadecimal.digit");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- if (value == null || mText.equals(PsiLiteralExpressionImpl._2_IN_63)) {
- final String message = JavaErrorMessages.message("long.number.too.large");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- }
- }
- else if (isFP) {
- if (value == null) {
- final String message = JavaErrorMessages.message("malformed.floating.point.literal");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- }
- else if (type == JavaTokenType.CHARACTER_LITERAL) {
- // todo[r.sh] clean this mess up
- if (value != null) {
- if (!StringUtil.endsWithChar(text, '\'')) {
- final String message = JavaErrorMessages.message("unclosed.char.literal");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- }
- else {
- if (!StringUtil.startsWithChar(text, '\'')) return null;
- if (StringUtil.endsWithChar(text, '\'')) {
- if (text.length() == 1) {
- final String message = JavaErrorMessages.message("illegal.line.end.in.character.literal");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- text = text.substring(1, text.length() - 1);
- }
- else {
- final String message = JavaErrorMessages.message("illegal.line.end.in.character.literal");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- StringBuilder chars = new StringBuilder();
- final boolean success = PsiLiteralExpressionImpl.parseStringCharacters(text, chars, null);
- if (!success) {
- final String message = JavaErrorMessages.message("illegal.escape.character.in.character.literal");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- int length = chars.length();
- if (length > 1) {
- final String message = JavaErrorMessages.message("too.many.characters.in.character.literal");
- final HighlightInfo info =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- QuickFixAction.registerQuickFixAction(info, new ConvertToStringLiteralAction());
- return info;
- }
- else if (length == 0) {
- final String message = JavaErrorMessages.message("empty.character.literal");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- }
- }
- else if (type == JavaTokenType.STRING_LITERAL) {
- if (value == null) {
- for (final PsiElement element : expression.getChildren()) {
- if (element instanceof OuterLanguageElement) {
- return null;
- }
- }
-
- if (!StringUtil.startsWithChar(text, '\"')) return null;
- if (StringUtil.endsWithChar(text, '\"')) {
- if (text.length() == 1) {
- final String message = JavaErrorMessages.message("illegal.line.end.in.string.literal");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- text = text.substring(1, text.length() - 1);
- }
- else {
- final String message = JavaErrorMessages.message("illegal.line.end.in.string.literal");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- StringBuilder chars = new StringBuilder();
- boolean success = PsiLiteralExpressionImpl.parseStringCharacters(text, chars, null);
- if (!success) {
- final String message = JavaErrorMessages.message("illegal.escape.character.in.string.literal");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- }
- }
-
- if (value instanceof Float) {
- final Float number = (Float)value;
- if (number.isInfinite()) {
- final String message = JavaErrorMessages.message("floating.point.number.too.large");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- if (number.floatValue() == 0 && !isFPZero(text)) {
- final String message = JavaErrorMessages.message("floating.point.number.too.small");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- }
- else if (value instanceof Double) {
- final Double number = (Double)value;
- if (number.isInfinite()) {
- final String message = JavaErrorMessages.message("floating.point.number.too.large");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- if (number.doubleValue() == 0 && !isFPZero(text)) {
- final String message = JavaErrorMessages.message("floating.point.number.too.small");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- }
-
- return null;
- }
-
- private static final Pattern FP_LITERAL_PARTS =
- Pattern.compile("(?:" +
- "(?:0x([_\\p{XDigit}]*)\\.?([_\\p{XDigit}]*)p[+-]?([_\\d]*))" +
- "|" +
- "(?:([_\\d]*)\\.?([_\\d]*)e?[+-]?([_\\d]*))" +
- ")[fd]?");
-
- @Nullable
- private static HighlightInfo checkUnderscores(PsiElement expression, String text, boolean isInt) {
- String[] parts = ArrayUtil.EMPTY_STRING_ARRAY;
-
- if (isInt) {
- int start = 0;
- int end = text.length();
- if (text.startsWith(PsiLiteralExpressionImpl.HEX_PREFIX) || text.startsWith(PsiLiteralExpressionImpl.BIN_PREFIX)) start += 2;
- if (StringUtil.endsWithChar(text, 'l')) --end;
- parts = new String[]{text.substring(start, end)};
- }
- else {
- Matcher matcher = FP_LITERAL_PARTS.matcher(text);
- if (matcher.matches()) {
- parts = new String[matcher.groupCount()];
- for (int i = 0; i < matcher.groupCount(); i++) {
- parts[i] = matcher.group(i + 1);
- }
- }
- }
-
- for (String part : parts) {
- if (part != null && (StringUtil.startsWithChar(part, '_') || StringUtil.endsWithChar(part, '_'))) {
- String message = JavaErrorMessages.message("illegal.underscore");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- }
-
- return null;
- }
-
- // true if floating point literal consists of zeros only
- public static boolean isFPZero(@NotNull final String text) {
- for (int i = 0; i < text.length(); i++) {
- final char c = text.charAt(i);
- if (Character.isDigit(c) && c != '0') return false;
- if (Character.toUpperCase(c) == 'E') break;
- }
- return true;
- }
-
- @Nullable
- static HighlightInfo checkMustBeBoolean(@NotNull PsiExpression expr, PsiType type) {
- PsiElement parent = expr.getParent();
- if (parent instanceof PsiIfStatement || parent instanceof PsiWhileStatement ||
- parent instanceof PsiForStatement && expr.equals(((PsiForStatement)parent).getCondition()) ||
- parent instanceof PsiDoWhileStatement && expr.equals(((PsiDoWhileStatement)parent).getCondition())) {
- if (expr.getNextSibling() instanceof PsiErrorElement) return null;
-
- if (!TypeConversionUtil.isBooleanType(type)) {
- final HighlightInfo info = createIncompatibleTypeHighlightInfo(PsiType.BOOLEAN, type, expr.getTextRange(), 0);
- if (expr instanceof PsiMethodCallExpression) {
- final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)expr;
- final PsiMethod method = methodCall.resolveMethod();
- if (method != null && PsiType.VOID.equals(method.getReturnType())) {
- IntentionAction fix = QUICK_FIX_FACTORY.createMethodReturnFix(method, PsiType.BOOLEAN, true);
- QuickFixAction.registerQuickFixAction(info, fix);
- }
- }
- return info;
- }
- }
- return null;
- }
-
-
- @NotNull
- static Set<PsiClassType> collectUnhandledExceptions(@NotNull final PsiTryStatement statement) {
- final Set<PsiClassType> thrownTypes = ContainerUtil.newHashSet();
-
- final PsiCodeBlock tryBlock = statement.getTryBlock();
- if (tryBlock != null) {
- thrownTypes.addAll(ExceptionUtil.collectUnhandledExceptions(tryBlock, tryBlock));
- }
-
- final PsiResourceList resources = statement.getResourceList();
- if (resources != null) {
- thrownTypes.addAll(ExceptionUtil.collectUnhandledExceptions(resources, resources));
- }
-
- return thrownTypes;
- }
-
- @Nullable
- static Collection<HighlightInfo> checkExceptionThrownInTry(@NotNull final PsiParameter parameter, @NotNull final Set<PsiClassType> thrownTypes) {
- final PsiElement declarationScope = parameter.getDeclarationScope();
- if (!(declarationScope instanceof PsiCatchSection)) return null;
-
- final PsiType caughtType = parameter.getType();
- if (caughtType instanceof PsiClassType) {
- return checkSimpleCatchParameter(parameter, thrownTypes, (PsiClassType)caughtType);
- }
- if (caughtType instanceof PsiDisjunctionType) {
- return checkMultiCatchParameter(parameter, thrownTypes);
- }
-
- return null;
- }
-
- @Nullable
- private static Collection<HighlightInfo> checkSimpleCatchParameter(@NotNull final PsiParameter parameter,
- @NotNull final Collection<PsiClassType> thrownTypes,
- @NotNull final PsiClassType caughtType) {
- if (ExceptionUtil.isUncheckedExceptionOrSuperclass(caughtType)) return null;
-
- for (PsiClassType exceptionType : thrownTypes) {
- if (exceptionType.isAssignableFrom(caughtType) || caughtType.isAssignableFrom(exceptionType)) return null;
- }
-
- final String description = JavaErrorMessages.message("exception.never.thrown.try", JavaHighlightUtil.formatType(caughtType));
- final HighlightInfo errorResult =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parameter).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(errorResult, new DeleteCatchFix(parameter));
- return Collections.singleton(errorResult);
- }
-
- @Nullable
- private static Collection<HighlightInfo> checkMultiCatchParameter(@NotNull final PsiParameter parameter,
- @NotNull final Collection<PsiClassType> thrownTypes) {
- final List<PsiTypeElement> typeElements = PsiUtil.getParameterTypeElements(parameter);
- final Collection<HighlightInfo> highlights = ContainerUtil.newArrayListWithCapacity(typeElements.size());
-
- for (final PsiTypeElement typeElement : typeElements) {
- final PsiType catchType = typeElement.getType();
- if (catchType instanceof PsiClassType && ExceptionUtil.isUncheckedExceptionOrSuperclass((PsiClassType)catchType)) continue;
-
- boolean used = false;
- for (PsiClassType exceptionType : thrownTypes) {
- if (exceptionType.isAssignableFrom(catchType) || catchType.isAssignableFrom(exceptionType)) {
- used = true;
- break;
- }
- }
- if (!used) {
- final String description = JavaErrorMessages.message("exception.never.thrown.try", JavaHighlightUtil.formatType(catchType));
- final HighlightInfo highlight =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(highlight, new DeleteMultiCatchFix(typeElement));
- highlights.add(highlight);
- }
- }
-
- return highlights;
- }
-
-
- @Nullable
- static Collection<HighlightInfo> checkWithImprovedCatchAnalysis(@NotNull final PsiParameter parameter,
- @NotNull final Collection<PsiClassType> thrownInTryStatement,@NotNull PsiFile containingFile) {
- final PsiElement scope = parameter.getDeclarationScope();
- if (!(scope instanceof PsiCatchSection)) return null;
-
- final PsiCatchSection catchSection = (PsiCatchSection)scope;
- final PsiCatchSection[] allCatchSections = catchSection.getTryStatement().getCatchSections();
- final int idx = ArrayUtilRt.find(allCatchSections, catchSection);
- if (idx <= 0) return null;
-
- final Collection<PsiClassType> thrownTypes = ContainerUtil.newHashSet(thrownInTryStatement);
- PsiManager manager = containingFile.getManager();
- GlobalSearchScope parameterResolveScope = parameter.getResolveScope();
- thrownTypes.add(PsiType.getJavaLangError(manager, parameterResolveScope));
- thrownTypes.add(PsiType.getJavaLangRuntimeException(manager, parameterResolveScope));
- final Collection<HighlightInfo> result = ContainerUtil.newArrayList();
-
- final List<PsiTypeElement> parameterTypeElements = PsiUtil.getParameterTypeElements(parameter);
- final boolean isMultiCatch = parameterTypeElements.size() > 1;
- for (PsiTypeElement catchTypeElement : parameterTypeElements) {
- final PsiType catchType = catchTypeElement.getType();
- if (ExceptionUtil.isGeneralExceptionType(catchType)) continue;
-
- // collect exceptions which are caught by this type
- Collection<PsiClassType> caught = ContainerUtil.findAll(thrownTypes, new Condition<PsiClassType>() {
- @Override
- public boolean value(@NotNull PsiClassType type) {
- return catchType.isAssignableFrom(type);
- }
- });
- if (caught.isEmpty()) continue;
- final Collection<PsiClassType> caughtCopy = ContainerUtil.newHashSet(caught);
-
- // exclude all which are caught by previous catch sections
- for (int i = 0; i < idx; i++) {
- final PsiParameter prevCatchParameter = allCatchSections[i].getParameter();
- if (prevCatchParameter == null) continue;
- for (PsiTypeElement prevCatchTypeElement : PsiUtil.getParameterTypeElements(prevCatchParameter)) {
- final PsiType prevCatchType = prevCatchTypeElement.getType();
- for (Iterator<PsiClassType> iterator = caught.iterator(); iterator.hasNext(); ) {
- if (prevCatchType.isAssignableFrom(iterator.next())) iterator.remove();
- }
- if (caught.isEmpty()) break;
- }
- }
-
- // check & warn
- if (caught.isEmpty()) {
- final String message = JavaErrorMessages.message("exception.already.caught.warn", formatTypes(caughtCopy), caughtCopy.size());
- final HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.WARNING).range(catchSection).descriptionAndTooltip(message).create();
- if (isMultiCatch) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new DeleteMultiCatchFix(catchTypeElement));
- }
- else {
- QuickFixAction.registerQuickFixAction(highlightInfo, new DeleteCatchFix(parameter));
- }
- result.add(highlightInfo);
- }
- }
-
- return result;
- }
-
-
- @Nullable
- static HighlightInfo checkNotAStatement(@NotNull PsiStatement statement) {
- if (!PsiUtil.isStatement(statement) && !PsiUtilCore.hasErrorElementChild(statement)) {
- String description = JavaErrorMessages.message("not.a.statement");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
-
- @Nullable
- public static HighlightInfo checkSwitchSelectorType(@NotNull PsiSwitchStatement statement) {
- final PsiExpression expression = statement.getExpression();
- PsiType type = expression == null ? null : expression.getType();
- if (type == null) {
- return null;
- }
- HighlightInfo errorResult = null;
- if (!isValidTypeForSwitchSelector(type, PsiUtil.isLanguageLevel7OrHigher(expression))) {
- String message =
- JavaErrorMessages.message("incompatible.types", JavaErrorMessages.message("valid.switch.selector.types"), JavaHighlightUtil.formatType(
- type));
- errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- QuickFixAction.registerQuickFixAction(errorResult, new ConvertSwitchToIfIntention(statement));
- if (PsiType.LONG.equals(type) || PsiType.FLOAT.equals(type) || PsiType.DOUBLE.equals(type)) {
- QuickFixAction.registerQuickFixAction(errorResult, new AddTypeCastFix(PsiType.INT, expression));
- }
- }
- else {
- final PsiClass member = PsiUtil.resolveClassInClassTypeOnly(type);
- if (member != null && !PsiUtil.isAccessible(member.getProject(), member, expression, null)) {
- String message = PsiFormatUtil.formatClass(member, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_FQ_NAME) + " is inaccessible here";
- errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- }
- return errorResult;
- }
-
- public static boolean isValidTypeForSwitchSelector(@NotNull PsiType type, final boolean languageLevel7OrHigher) {
- if (TypeConversionUtil.getTypeRank(type) <= TypeConversionUtil.INT_RANK) return true;
- if (type instanceof PsiClassType) {
- PsiClass psiClass = ((PsiClassType)type).resolve();
- if (psiClass == null) return false;
- if (psiClass.isEnum()) {
- return true;
- }
- if (languageLevel7OrHigher) {
- return Comparing.strEqual(psiClass.getQualifiedName(), CommonClassNames.JAVA_LANG_STRING);
- }
- }
- return false;
- }
-
- @Nullable
- static HighlightInfo checkPolyadicOperatorApplicable(@NotNull PsiPolyadicExpression expression) {
- PsiExpression[] operands = expression.getOperands();
-
- PsiType lType = operands[0].getType();
- IElementType operationSign = expression.getOperationTokenType();
- for (int i = 1; i < operands.length; i++) {
- PsiExpression operand = operands[i];
- PsiType rType = operand.getType();
- if (!TypeConversionUtil.isBinaryOperatorApplicable(operationSign, lType, rType, false)) {
- PsiJavaToken token = expression.getTokenBeforeOperand(operand);
- String message = JavaErrorMessages.message("binary.operator.not.applicable", token.getText(),
- JavaHighlightUtil.formatType(lType),
- JavaHighlightUtil.formatType(rType));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
- }
- lType = TypeConversionUtil.calcTypeForBinaryExpression(lType, rType, operationSign, true);
- }
-
- return null;
- }
-
-
- @Nullable
- static HighlightInfo checkUnaryOperatorApplicable(@Nullable PsiJavaToken token, @Nullable PsiExpression expression) {
- if (token != null && expression != null && !TypeConversionUtil.isUnaryOperatorApplicable(token, expression)) {
- PsiType type = expression.getType();
- if (type == null) return null;
- String message = JavaErrorMessages.message("unary.operator.not.applicable", token.getText(), JavaHighlightUtil.formatType(type));
-
- PsiElement parentExpr = token.getParent();
- HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parentExpr).descriptionAndTooltip(message).create();
- if (parentExpr instanceof PsiPrefixExpression && token.getTokenType() == JavaTokenType.EXCL) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new NegationBroadScopeFix((PsiPrefixExpression)parentExpr));
- }
- return highlightInfo;
- }
- return null;
- }
-
- @Nullable
- static HighlightInfo checkThisOrSuperExpressionInIllegalContext(@NotNull PsiExpression expr,
- @Nullable PsiJavaCodeReferenceElement qualifier) {
- if (expr instanceof PsiSuperExpression) {
- final PsiElement parent = expr.getParent();
- if (!(parent instanceof PsiReferenceExpression)) {
- // like in 'Object o = super;'
- final int o = expr.getTextRange().getEndOffset();
- String description = JavaErrorMessages.message("dot.expected.after.super.or.this");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(o, o + 1).descriptionAndTooltip(description).create();
- }
-
- if (PsiUtil.isLanguageLevel8OrHigher(expr)) {
- final PsiMethod method = PsiTreeUtil.getParentOfType(expr, PsiMethod.class);
- if (method != null && method.hasModifierProperty(PsiModifier.DEFAULT) && qualifier == null) {
- //todo[r.sh] "Add qualifier" quick fix
- String description = JavaErrorMessages.message("unqualified.super.disallowed");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parent).descriptionAndTooltip(description).create();
- }
- }
- }
-
- PsiClass aClass;
- if (qualifier != null) {
- PsiElement resolved = qualifier.advancedResolve(true).getElement();
- if (resolved != null && !(resolved instanceof PsiClass)) {
- String description = JavaErrorMessages.message("class.expected");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(qualifier).descriptionAndTooltip(description).create();
- }
- aClass = (PsiClass)resolved;
- }
- else {
- aClass = PsiTreeUtil.getParentOfType(expr, PsiClass.class);
- }
- if (aClass == null) return null;
-
- if (!HighlightClassUtil.hasEnclosingInstanceInScope(aClass, expr, false, false) &&
- !resolvesToImmediateSuperInterface(expr, qualifier, aClass)) {
- return HighlightClassUtil.reportIllegalEnclosingUsage(expr, null, aClass, expr);
- }
-
- if (expr instanceof PsiThisExpression) {
- final PsiMethod psiMethod = PsiTreeUtil.getParentOfType(expr, PsiMethod.class);
- if (psiMethod == null || psiMethod.getContainingClass() != aClass && !isInsideDefaultMethod(psiMethod, aClass)) {
- if (aClass.isInterface()) {
- return thisNotFoundInInterfaceInfo(expr);
- }
-
- if (aClass instanceof PsiAnonymousClass && PsiTreeUtil.isAncestor(((PsiAnonymousClass)aClass).getArgumentList(), expr, true)) {
- final PsiClass parentClass = PsiTreeUtil.getParentOfType(aClass, PsiClass.class, true);
- if (parentClass != null && parentClass.isInterface()) {
- return thisNotFoundInInterfaceInfo(expr);
- }
- }
- }
- }
- return null;
- }
-
- private static boolean isInsideDefaultMethod(PsiMethod method, PsiClass aClass) {
- while (method != null && method.getContainingClass() != aClass) {
- method = PsiTreeUtil.getParentOfType(method, PsiMethod.class, true);
- }
- return method != null && method.hasModifierProperty(PsiModifier.DEFAULT);
- }
-
- private static HighlightInfo thisNotFoundInInterfaceInfo(@NotNull PsiExpression expr) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expr).descriptionAndTooltip("Cannot find symbol variable this").create();
- }
-
- private static boolean resolvesToImmediateSuperInterface(@NotNull PsiExpression expr,
- @Nullable PsiJavaCodeReferenceElement qualifier,
- @NotNull PsiClass aClass) {
- if (!(expr instanceof PsiSuperExpression) || qualifier == null || !PsiUtil.isLanguageLevel8OrHigher(expr)) return false;
- final PsiType superType = expr.getType();
- if (!(superType instanceof PsiClassType)) return false;
- final PsiClass superClass = ((PsiClassType)superType).resolve();
- return superClass != null && aClass.equals(superClass);
- }
-
- @NotNull
- static String buildProblemWithStaticDescription(@NotNull PsiElement refElement) {
- String type = LanguageFindUsages.INSTANCE.forLanguage(JavaLanguage.INSTANCE).getType(refElement);
- String name = HighlightMessageUtil.getSymbolName(refElement, PsiSubstitutor.EMPTY);
- return JavaErrorMessages.message("non.static.symbol.referenced.from.static.context", type, name);
- }
-
- static void registerStaticProblemQuickFixAction(@NotNull PsiElement refElement, HighlightInfo errorResult, @NotNull PsiJavaCodeReferenceElement place) {
- if (refElement instanceof PsiModifierListOwner) {
- QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createModifierListFix((PsiModifierListOwner)refElement, PsiModifier.STATIC, true, false));
- }
- // make context non static
- PsiModifierListOwner staticParent = PsiUtil.getEnclosingStaticElement(place, null);
- if (staticParent != null && isInstanceReference(place)) {
- QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createModifierListFix(staticParent, PsiModifier.STATIC, false, false));
- }
- if (place instanceof PsiReferenceExpression && refElement instanceof PsiField) {
- QuickFixAction.registerQuickFixAction(errorResult, new CreateFieldFromUsageFix((PsiReferenceExpression)place));
- }
- }
-
- private static boolean isInstanceReference(@NotNull PsiJavaCodeReferenceElement place) {
- PsiElement qualifier = place.getQualifier();
- if (qualifier == null) return true;
- if (!(qualifier instanceof PsiJavaCodeReferenceElement)) return false;
- PsiElement q = ((PsiReference)qualifier).resolve();
- if (q instanceof PsiClass) return false;
- if (q != null) return true;
- String qname = ((PsiJavaCodeReferenceElement)qualifier).getQualifiedName();
- return qname == null || !Character.isLowerCase(qname.charAt(0));
- }
-
- @NotNull
- static String buildProblemWithAccessDescription(@NotNull final PsiElement reference, @NotNull final JavaResolveResult result) {
- return buildProblemWithAccessDescription(reference, result, result.getElement());
- }
-
- @NotNull
- static String buildProblemWithAccessDescription(@NotNull final PsiElement reference,
- @NotNull final JavaResolveResult result,
- @NotNull final PsiElement resolved) {
- assert resolved instanceof PsiModifierListOwner : resolved;
- PsiModifierListOwner refElement = (PsiModifierListOwner)resolved;
- String symbolName = HighlightMessageUtil.getSymbolName(refElement, result.getSubstitutor());
-
- if (refElement.hasModifierProperty(PsiModifier.PRIVATE)) {
- String containerName = getContainerName(refElement, result.getSubstitutor());
- return JavaErrorMessages.message("private.symbol", symbolName, containerName);
- }
- else if (refElement.hasModifierProperty(PsiModifier.PROTECTED)) {
- String containerName = getContainerName(refElement, result.getSubstitutor());
- return JavaErrorMessages.message("protected.symbol", symbolName, containerName);
- }
- else {
- PsiClass packageLocalClass = getPackageLocalClassInTheMiddle(reference);
- if (packageLocalClass != null) {
- refElement = packageLocalClass;
- symbolName = HighlightMessageUtil.getSymbolName(refElement, result.getSubstitutor());
- }
- if (refElement.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) || packageLocalClass != null) {
- String containerName = getContainerName(refElement, result.getSubstitutor());
- return JavaErrorMessages.message("package.local.symbol", symbolName, containerName);
- }
- else {
- String containerName = getContainerName(refElement, result.getSubstitutor());
- return JavaErrorMessages.message("visibility.access.problem", symbolName, containerName);
- }
- }
- }
-
- private static PsiElement getContainer(PsiModifierListOwner refElement) {
- for (ContainerProvider provider : ContainerProvider.EP_NAME.getExtensions()) {
- final PsiElement container = provider.getContainer(refElement);
- if (container != null) return container;
- }
- return refElement.getParent();
- }
-
- private static String getContainerName(PsiModifierListOwner refElement, final PsiSubstitutor substitutor) {
- final PsiElement container = getContainer(refElement);
- return container == null ? "?" : HighlightMessageUtil.getSymbolName(container, substitutor);
- }
-
- @Nullable
- static HighlightInfo checkValidArrayAccessExpression(@NotNull PsiArrayAccessExpression arrayAccessExpression) {
- final PsiExpression arrayExpression = arrayAccessExpression.getArrayExpression();
- final PsiType arrayExpressionType = arrayExpression.getType();
-
- if (arrayExpressionType != null && !(arrayExpressionType instanceof PsiArrayType)) {
- final String description = JavaErrorMessages.message("array.type.expected", JavaHighlightUtil.formatType(arrayExpressionType));
- final HighlightInfo info =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(arrayExpression).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(info, new ReplaceWithListAccessFix(arrayAccessExpression));
- return info;
- }
-
- final PsiExpression indexExpression = arrayAccessExpression.getIndexExpression();
- return indexExpression != null ? checkAssignability(PsiType.INT, indexExpression.getType(), indexExpression, indexExpression) : null;
- }
-
-
- @Nullable
- public static HighlightInfo checkCatchParameterIsThrowable(@NotNull final PsiParameter parameter) {
- if (parameter.getDeclarationScope() instanceof PsiCatchSection) {
- final PsiType type = parameter.getType();
- return checkMustBeThrowable(type, parameter, true);
- }
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkTryResourceIsAutoCloseable(@NotNull final PsiResourceVariable resource) {
- final PsiType type = resource.getType();
- final PsiElementFactory factory = JavaPsiFacade.getInstance(resource.getProject()).getElementFactory();
- final PsiClassType autoCloseable = factory.createTypeByFQClassName(CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE, resource.getResolveScope());
- if (TypeConversionUtil.isAssignable(autoCloseable, type)) return null;
-
- return createIncompatibleTypeHighlightInfo(autoCloseable, type, resource.getTextRange(), 0);
- }
-
- @Nullable
- public static Collection<HighlightInfo> checkArrayInitializer(final PsiExpression initializer, PsiType type) {
- if (!(initializer instanceof PsiArrayInitializerExpression)) return null;
- if (!(type instanceof PsiArrayType)) return null;
-
- final PsiType componentType = ((PsiArrayType)type).getComponentType();
- final PsiArrayInitializerExpression arrayInitializer = (PsiArrayInitializerExpression)initializer;
-
- boolean arrayTypeFixChecked = false;
- VariableArrayTypeFix fix = null;
-
- final Collection<HighlightInfo> result = ContainerUtil.newArrayList();
- final PsiExpression[] initializers = arrayInitializer.getInitializers();
- for (PsiExpression expression : initializers) {
- final HighlightInfo info = checkArrayInitializerCompatibleTypes(expression, componentType);
- if (info != null) {
- result.add(info);
-
- if (!arrayTypeFixChecked) {
- final PsiType checkResult = JavaHighlightUtil.sameType(initializers);
- fix = checkResult != null ? new VariableArrayTypeFix(arrayInitializer, checkResult) : null;
- arrayTypeFixChecked = true;
- }
- if (fix != null) {
- QuickFixAction.registerQuickFixAction(info, new LocalQuickFixOnPsiElementAsIntentionAdapter(fix));
- }
- }
- }
- return result;
- }
-
- @Nullable
- private static HighlightInfo checkArrayInitializerCompatibleTypes(@NotNull PsiExpression initializer, final PsiType componentType) {
- PsiType initializerType = initializer.getType();
- if (initializerType == null) {
- String description = JavaErrorMessages.message("illegal.initializer", JavaHighlightUtil.formatType(componentType));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(initializer).descriptionAndTooltip(description).create();
- }
- PsiExpression expression = initializer instanceof PsiArrayInitializerExpression ? null : initializer;
- return checkAssignability(componentType, initializerType, expression, initializer);
- }
-
- @Nullable
- public static HighlightInfo checkExpressionRequired(@NotNull PsiReferenceExpression expression, @NotNull JavaResolveResult resultForIncompleteCode) {
- if (expression.getNextSibling() instanceof PsiErrorElement) return null;
- PsiElement resolved = resultForIncompleteCode.getElement();
- if (resolved == null) return null;
- PsiElement parent = expression.getParent();
- // String.class or String() are both correct
- if (parent instanceof PsiReferenceExpression || parent instanceof PsiMethodCallExpression) return null;
- if (resolved instanceof PsiVariable) return null;
- String description = JavaErrorMessages.message("expression.expected");
- final HighlightInfo info =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
- UnresolvedReferenceQuickFixProvider.registerReferenceFixes(expression, new QuickFixActionRegistrarImpl(info));
- return info;
- }
-
-
- @Nullable
- public static HighlightInfo checkArrayInitializerApplicable(@NotNull PsiArrayInitializerExpression expression) {
- /*
- JLS 10.6 Array Initializers
- An array initializer may be specified in a declaration, or as part of an array creation expression
- */
- PsiElement parent = expression.getParent();
- if (parent instanceof PsiVariable) {
- PsiVariable variable = (PsiVariable)parent;
- if (variable.getType() instanceof PsiArrayType) return null;
- }
- else if (parent instanceof PsiNewExpression || parent instanceof PsiArrayInitializerExpression) {
- return null;
- }
-
- String description = JavaErrorMessages.message("array.initializer.not.allowed");
- HighlightInfo info =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(info, new AddNewArrayExpressionFix(expression));
- return info;
- }
-
-
- @Nullable
- public static HighlightInfo checkCaseStatement(@NotNull PsiSwitchLabelStatement statement) {
- PsiSwitchStatement switchStatement = statement.getEnclosingSwitchStatement();
- if (switchStatement == null) {
- String description = JavaErrorMessages.message("case.statement.outside.switch");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).create();
- }
- if (switchStatement.getBody() == null) return null;
- PsiExpression switchExpression = switchStatement.getExpression();
- PsiType switchType = switchExpression == null ? PsiType.INT : switchExpression.getType();
- // check constant expression
- PsiExpression caseValue = statement.getCaseValue();
-
- // Every case constant expression associated with a switch statement must be assignable ($5.2) to the type of the switch Expression.
- if (caseValue != null && switchExpression != null) {
- HighlightInfo highlightInfo = checkAssignability(switchType, caseValue.getType(), caseValue, caseValue);
- if (highlightInfo != null) return highlightInfo;
- }
- Object value = null;
-
- boolean isEnumSwitch = false;
- if (!statement.isDefaultCase() && caseValue != null) {
- if (caseValue instanceof PsiReferenceExpression) {
- PsiElement element = ((PsiReferenceExpression)caseValue).resolve();
- if (element instanceof PsiEnumConstant) {
- isEnumSwitch = true;
- value = ((PsiEnumConstant)element).getName();
- if (!(((PsiReferenceExpression)caseValue).getQualifier() == null)) {
- String message = JavaErrorMessages.message("qualified.enum.constant.in.switch");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(caseValue).descriptionAndTooltip(message).create();
- }
- }
- }
- if (!isEnumSwitch) {
- value = ConstantExpressionUtil.computeCastTo(caseValue, switchType);
- }
- if (value == null) {
- String description = JavaErrorMessages.message("constant.expression.required");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(caseValue).descriptionAndTooltip(description).create();
- }
- }
-
- // check duplicate
- PsiStatement[] statements = switchStatement.getBody().getStatements();
- for (PsiStatement st : statements) {
- if (st == statement) continue;
- if (!(st instanceof PsiSwitchLabelStatement)) continue;
- PsiSwitchLabelStatement labelStatement = (PsiSwitchLabelStatement)st;
- if (labelStatement.isDefaultCase() != statement.isDefaultCase()) continue;
- PsiExpression caseExpr = labelStatement.getCaseValue();
- if (isEnumSwitch && caseExpr instanceof PsiReferenceExpression) {
- PsiElement element = ((PsiReferenceExpression)caseExpr).resolve();
- if (!(element instanceof PsiEnumConstant && Comparing.equal(((PsiEnumConstant)element).getName(), value))) continue;
- }
- else {
- // not assignable error already caught
- if (!TypeConversionUtil.areTypesAssignmentCompatible(switchType, caseExpr)) continue;
- if (!Comparing.equal(ConstantExpressionUtil.computeCastTo(caseExpr, switchType), value)) continue;
- }
- String description = statement.isDefaultCase()
- ? JavaErrorMessages.message("duplicate.default.switch.label")
- : JavaErrorMessages.message("duplicate.switch.label", value);
- PsiElement element = value == null ? statement : caseValue;
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
- }
-
- // must be followed with colon
- PsiElement lastChild = statement.getLastChild();
- while (lastChild instanceof PsiComment || lastChild instanceof PsiWhiteSpace) {
- lastChild = lastChild.getPrevSibling();
- }
- if (!(lastChild instanceof PsiJavaToken && ((PsiJavaToken)lastChild).getTokenType() == JavaTokenType.COLON)) {
- int start = statement.getTextRange().getEndOffset();
- int end = statement.getTextRange().getEndOffset() + 1;
- String description = JavaErrorMessages.message("switch.colon.expected.after.case.label");
- CharSequence chars = statement.getContainingFile().getViewProvider().getContents();
- boolean isAfterEndOfLine = end >= chars.length() || chars.charAt(start) == '\n' || chars.charAt(start) == '\r';
- HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(start, end).descriptionAndTooltip(description);
- if (isAfterEndOfLine) {
- builder.endOfLine();
- }
- return builder.create();
- }
- return null;
- }
-
-
- /**
- * see JLS 8.3.2.3
- */
- @Nullable
- public static HighlightInfo checkIllegalForwardReferenceToField(@NotNull PsiReferenceExpression expression, @NotNull PsiField referencedField) {
- PsiClass containingClass = referencedField.getContainingClass();
- if (containingClass == null) return null;
- if (expression.getContainingFile() != referencedField.getContainingFile()) return null;
- if (expression.getTextRange().getStartOffset() >= referencedField.getTextRange().getEndOffset()) return null;
- // only simple reference can be illegal
- if (expression.getQualifierExpression() != null) return null;
- PsiField initField = findEnclosingFieldInitializer(expression);
- PsiClassInitializer classInitializer = findParentClassInitializer(expression);
- if (initField == null && classInitializer == null) return null;
- // instance initializers may access static fields
- boolean isStaticClassInitializer = classInitializer != null && classInitializer.hasModifierProperty(PsiModifier.STATIC);
- boolean isStaticInitField = initField != null && initField.hasModifierProperty(PsiModifier.STATIC);
- boolean inStaticContext = isStaticInitField || isStaticClassInitializer;
- if (!inStaticContext && referencedField.hasModifierProperty(PsiModifier.STATIC)) return null;
- if (PsiUtil.isOnAssignmentLeftHand(expression) && !PsiUtil.isAccessedForReading(expression)) return null;
- if (!containingClass.getManager().areElementsEquivalent(containingClass, PsiTreeUtil.getParentOfType(expression, PsiClass.class))) {
- return null;
- }
- String description = JavaErrorMessages.message("illegal.forward.reference");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
- }
-
- /**
- * @return field that has initializer with this element as subexpression or null if not found
- */
- @Nullable
- static PsiField findEnclosingFieldInitializer(@Nullable PsiElement element) {
- while (element != null) {
- PsiElement parent = element.getParent();
- if (parent instanceof PsiField) {
- PsiField field = (PsiField)parent;
- if (element == field.getInitializer()) return field;
- if (field instanceof PsiEnumConstant && element == ((PsiEnumConstant)field).getArgumentList()) return field;
- }
- if (element instanceof PsiClass || element instanceof PsiMethod || parent instanceof PsiLambdaExpression) return null;
- element = parent;
- }
- return null;
- }
-
- @Nullable
- private static PsiClassInitializer findParentClassInitializer(@Nullable PsiElement element) {
- while (element != null) {
- if (element instanceof PsiClassInitializer) return (PsiClassInitializer)element;
- if (element instanceof PsiClass || element instanceof PsiMethod) return null;
- element = element.getParent();
- }
- return null;
- }
-
-
- @Nullable
- public static HighlightInfo checkIllegalType(@Nullable PsiTypeElement typeElement) {
- if (typeElement == null || typeElement.getParent() instanceof PsiTypeElement) return null;
-
- if (PsiUtil.isInsideJavadocComment(typeElement)) return null;
-
- PsiType type = typeElement.getType();
- PsiType componentType = type.getDeepComponentType();
- if (componentType instanceof PsiClassType) {
- PsiClass aClass = PsiUtil.resolveClassInType(componentType);
- if (aClass == null) {
- String canonicalText = type.getCanonicalText();
- String description = JavaErrorMessages.message("unknown.class", canonicalText);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement).descriptionAndTooltip(description).create();
- }
- }
-
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkIllegalVoidType(@NotNull PsiKeyword type) {
- if (!PsiKeyword.VOID.equals(type.getText())) return null;
-
- PsiElement parent = type.getParent();
- if (parent instanceof PsiTypeElement) {
- PsiElement typeOwner = parent.getParent();
- if (typeOwner != null) {
- // do not highlight incomplete declarations
- if (PsiUtilCore.hasErrorElementChild(typeOwner)) return null;
- }
-
- if (typeOwner instanceof PsiMethod) {
- PsiMethod method = (PsiMethod)typeOwner;
- if (method.getReturnTypeElement() == parent && PsiType.VOID.equals(method.getReturnType())) return null;
- }
- else if (typeOwner instanceof PsiClassObjectAccessExpression) {
- if (TypeConversionUtil.isVoidType(((PsiClassObjectAccessExpression)typeOwner).getOperand().getType())) return null;
- }
- else if (typeOwner instanceof JavaCodeFragment) {
- if (typeOwner.getUserData(PsiUtil.VALID_VOID_TYPE_IN_CODE_FRAGMENT) != null) return null;
- }
- }
-
- String description = JavaErrorMessages.message("illegal.type.void");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(type).descriptionAndTooltip(description).create();
- }
-
- @Nullable
- public static HighlightInfo checkMemberReferencedBeforeConstructorCalled(@NotNull PsiElement expression,
- PsiElement resolved,
- @NotNull PsiFile containingFile) {
- PsiClass referencedClass;
- @NonNls String resolvedName;
- PsiType type;
- if (expression instanceof PsiJavaCodeReferenceElement) {
- // redirected ctr
- if (PsiKeyword.THIS.equals(((PsiJavaCodeReferenceElement)expression).getReferenceName())
- && resolved instanceof PsiMethod
- && ((PsiMethod)resolved).isConstructor()) {
- return null;
- }
- PsiElement qualifier = ((PsiJavaCodeReferenceElement)expression).getQualifier();
- type = qualifier instanceof PsiExpression ? ((PsiExpression)qualifier).getType() : null;
- referencedClass = PsiUtil.resolveClassInType(type);
-
- boolean isSuperCall = RefactoringChangeUtil.isSuperMethodCall(expression.getParent());
- if (resolved == null && isSuperCall) {
- if (qualifier instanceof PsiReferenceExpression) {
- resolved = ((PsiReferenceExpression)qualifier).resolve();
- expression = qualifier;
- type = ((PsiReferenceExpression)qualifier).getType();
- referencedClass = PsiUtil.resolveClassInType(type);
- }
- else if (qualifier == null) {
- resolved = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true, PsiMember.class);
- if (resolved != null) {
- referencedClass = ((PsiMethod)resolved).getContainingClass();
- }
- } else if (qualifier instanceof PsiThisExpression) {
- referencedClass = PsiUtil.resolveClassInType(((PsiThisExpression)qualifier).getType());
- }
- }
- if (resolved instanceof PsiField) {
- PsiField referencedField = (PsiField)resolved;
- if (referencedField.hasModifierProperty(PsiModifier.STATIC)) return null;
- resolvedName = PsiFormatUtil.formatVariable(referencedField, PsiFormatUtilBase.SHOW_CONTAINING_CLASS | PsiFormatUtilBase.SHOW_NAME, PsiSubstitutor.EMPTY);
- referencedClass = referencedField.getContainingClass();
- }
- else if (resolved instanceof PsiMethod) {
- PsiMethod method = (PsiMethod)resolved;
- if (method.hasModifierProperty(PsiModifier.STATIC)) return null;
- PsiElement nameElement = expression instanceof PsiThisExpression ? expression : ((PsiJavaCodeReferenceElement)expression).getReferenceNameElement();
- String name = nameElement == null ? null : nameElement.getText();
- if (isSuperCall) {
- if (referencedClass == null) return null;
- if (qualifier == null) {
- PsiClass superClass = referencedClass.getSuperClass();
- if (superClass != null
- && PsiUtil.isInnerClass(superClass)
- && InheritanceUtil.isInheritorOrSelf(referencedClass, superClass.getContainingClass(), true)) {
- // by default super() is considered this. - qualified
- resolvedName = PsiKeyword.THIS;
- }
- else {
- return null;
- }
- }
- else {
- resolvedName = qualifier.getText();
- }
- }
- else if (PsiKeyword.THIS.equals(name)) {
- resolvedName = PsiKeyword.THIS;
- }
- else {
- resolvedName = PsiFormatUtil.formatMethod(method, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_CONTAINING_CLASS |
- PsiFormatUtilBase.SHOW_NAME, 0);
- if (referencedClass == null) referencedClass = method.getContainingClass();
- }
- }
- else if (resolved instanceof PsiClass) {
- PsiClass aClass = (PsiClass)resolved;
- if (aClass.hasModifierProperty(PsiModifier.STATIC)) return null;
- referencedClass = aClass.getContainingClass();
- if (referencedClass == null) return null;
- resolvedName = PsiFormatUtil.formatClass(aClass, PsiFormatUtilBase.SHOW_NAME);
- }
- else {
- return null;
- }
- }
- else if (expression instanceof PsiThisExpression) {
- PsiThisExpression thisExpression = (PsiThisExpression)expression;
- type = thisExpression.getType();
- referencedClass = PsiUtil.resolveClassInType(type);
- if (thisExpression.getQualifier() != null) {
- resolvedName = referencedClass == null
- ? null
- : PsiFormatUtil.formatClass(referencedClass, PsiFormatUtilBase.SHOW_NAME) + ".this";
- }
- else {
- resolvedName = "this";
- }
- }
- else {
- return null;
- }
- if (referencedClass == null) return null;
- return checkReferenceToOurInstanceInsideThisOrSuper(expression, referencedClass, resolvedName, containingFile);
- }
-
- @Nullable
- private static HighlightInfo checkReferenceToOurInstanceInsideThisOrSuper(@NotNull final PsiElement expression,
- @NotNull PsiClass referencedClass,
- final String resolvedName,
- @NotNull PsiFile containingFile) {
- if (PsiTreeUtil.getParentOfType(expression, PsiReferenceParameterList.class) != null) return null;
- PsiElement element = expression.getParent();
- while (element != null) {
- // check if expression inside super()/this() call
- if (RefactoringChangeUtil.isSuperOrThisMethodCall(element)) {
- PsiElement parentClass = new PsiMatcherImpl(element)
- .parent(PsiMatchers.hasClass(PsiExpressionStatement.class))
- .parent(PsiMatchers.hasClass(PsiCodeBlock.class))
- .parent(PsiMatchers.hasClass(PsiMethod.class))
- .dot(JavaMatchers.isConstructor(true))
- .parent(PsiMatchers.hasClass(PsiClass.class))
- .getElement();
- if (parentClass == null) {
- return null;
- }
-
- // only this class/superclasses instance methods are not allowed to call
- PsiClass aClass = (PsiClass)parentClass;
- if (PsiUtil.isInnerClass(aClass) && referencedClass == aClass.getContainingClass()) return null;
- // field or method should be declared in this class or super
- if (!InheritanceUtil.isInheritorOrSelf(aClass, referencedClass, true)) return null;
- // and point to our instance
- if (expression instanceof PsiReferenceExpression &&
- !thisOrSuperReference(((PsiReferenceExpression)expression).getQualifierExpression(), aClass)) {
- return null;
- }
-
- if (expression instanceof PsiJavaCodeReferenceElement &&
- !aClass.equals(PsiTreeUtil.getParentOfType(expression, PsiClass.class)) &&
- PsiTreeUtil.getParentOfType(expression, PsiTypeElement.class) != null) {
- return null;
- }
-
- final HighlightInfo highlightInfo = createMemberReferencedError(resolvedName, expression.getTextRange());
- if (expression instanceof PsiReferenceExpression && PsiUtil.isInnerClass(aClass)) {
- final String referenceName = ((PsiReferenceExpression)expression).getReferenceName();
- final PsiClass containingClass = aClass.getContainingClass();
- LOG.assertTrue(containingClass != null);
- final PsiField fieldInContainingClass = containingClass.findFieldByName(referenceName, true);
- if (fieldInContainingClass != null && ((PsiReferenceExpression)expression).getQualifierExpression() == null) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new QualifyWithThisFix(containingClass, expression));
- }
- }
-
- return highlightInfo;
- }
-
- if (element instanceof PsiReferenceExpression) {
- final PsiElement resolve;
- if (element instanceof PsiReferenceExpressionImpl) {
- PsiReferenceExpressionImpl referenceExpression = (PsiReferenceExpressionImpl)element;
- JavaResolveResult[] results = JavaResolveUtil
- .resolveWithContainingFile(referenceExpression, PsiReferenceExpressionImpl.OurGenericsResolver.INSTANCE, true, false, containingFile);
- resolve = results.length == 1 ? results[0].getElement() : null;
- }
- else {
- resolve = ((PsiReferenceExpression)element).resolve();
- }
- if (resolve instanceof PsiField && ((PsiField)resolve).hasModifierProperty(PsiModifier.STATIC)) {
- return null;
- }
- }
-
- element = element.getParent();
- if (element instanceof PsiClass && InheritanceUtil.isInheritorOrSelf((PsiClass)element, referencedClass, true)) return null;
- }
- return null;
- }
-
- private static HighlightInfo createMemberReferencedError(@NonNls final String resolvedName, @NotNull TextRange textRange) {
- String description = JavaErrorMessages.message("member.referenced.before.constructor.called", resolvedName);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
- }
-
- @Nullable
- public static HighlightInfo checkImplicitThisReferenceBeforeSuper(@NotNull PsiClass aClass, @NotNull JavaSdkVersion javaSdkVersion) {
- if (javaSdkVersion.isAtLeast(JavaSdkVersion.JDK_1_7)) return null;
- if (aClass instanceof PsiAnonymousClass || aClass instanceof PsiTypeParameter) return null;
- PsiClass superClass = aClass.getSuperClass();
- if (superClass == null || !PsiUtil.isInnerClass(superClass)) return null;
- PsiClass outerClass = superClass.getContainingClass();
- if (!InheritanceUtil.isInheritorOrSelf(aClass, outerClass, true)) {
- return null;
- }
- // 'this' can be used as an (implicit) super() qualifier
- PsiMethod[] constructors = aClass.getConstructors();
- if (constructors.length == 0) {
- TextRange range = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
- return createMemberReferencedError(aClass.getName() + ".this", range);
- }
- for (PsiMethod constructor : constructors) {
- if (!isSuperCalledInConstructor(constructor)) {
- return createMemberReferencedError(aClass.getName() + ".this", HighlightNamesUtil.getMethodDeclarationTextRange(constructor));
- }
- }
- return null;
- }
-
- private static boolean isSuperCalledInConstructor(@NotNull final PsiMethod constructor) {
- final PsiCodeBlock body = constructor.getBody();
- if (body == null) return false;
- final PsiStatement[] statements = body.getStatements();
- if (statements.length == 0) return false;
- final PsiStatement statement = statements[0];
- final PsiElement element = new PsiMatcherImpl(statement)
- .dot(PsiMatchers.hasClass(PsiExpressionStatement.class))
- .firstChild(PsiMatchers.hasClass(PsiMethodCallExpression.class))
- .firstChild(PsiMatchers.hasClass(PsiReferenceExpression.class))
- .firstChild(PsiMatchers.hasClass(PsiKeyword.class))
- .dot(PsiMatchers.hasText(PsiKeyword.SUPER))
- .getElement();
- return element != null;
- }
-
- private static boolean thisOrSuperReference(@Nullable PsiExpression qualifierExpression, PsiClass aClass) {
- if (qualifierExpression == null) return true;
- PsiJavaCodeReferenceElement qualifier;
- if (qualifierExpression instanceof PsiThisExpression) {
- qualifier = ((PsiThisExpression)qualifierExpression).getQualifier();
- }
- else if (qualifierExpression instanceof PsiSuperExpression) {
- qualifier = ((PsiSuperExpression)qualifierExpression).getQualifier();
- }
- else {
- return false;
- }
- if (qualifier == null) return true;
- PsiElement resolved = qualifier.resolve();
- return resolved instanceof PsiClass && InheritanceUtil.isInheritorOrSelf(aClass, (PsiClass)resolved, true);
- }
-
-
- @Nullable
- public static HighlightInfo checkLabelWithoutStatement(@NotNull PsiLabeledStatement statement) {
- if (statement.getStatement() == null) {
- String description = JavaErrorMessages.message("label.without.statement");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
-
- @Nullable
- public static HighlightInfo checkLabelAlreadyInUse(@NotNull PsiLabeledStatement statement) {
- PsiIdentifier identifier = statement.getLabelIdentifier();
- String text = identifier.getText();
- PsiElement element = statement;
- while (element != null) {
- if (element instanceof PsiMethod || element instanceof PsiClass) break;
- if (element instanceof PsiLabeledStatement && element != statement &&
- Comparing.equal(((PsiLabeledStatement)element).getLabelIdentifier().getText(), text)) {
- String description = JavaErrorMessages.message("duplicate.label", text);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier).descriptionAndTooltip(description).create();
- }
- element = element.getParent();
- }
- return null;
- }
-
-
- @Nullable
- public static HighlightInfo checkUnclosedComment(@NotNull PsiComment comment) {
- if (!(comment instanceof PsiDocComment) && !(comment.getTokenType() == JavaTokenType.C_STYLE_COMMENT)) return null;
- if (!comment.getText().endsWith("*/")) {
- int start = comment.getTextRange().getEndOffset() - 1;
- int end = start + 1;
- String description = JavaErrorMessages.message("unclosed.comment");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(start, end).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
-
- @Nullable
- static Collection<HighlightInfo> checkCatchTypeIsDisjoint(@NotNull final PsiParameter parameter) {
- if (!(parameter.getType() instanceof PsiDisjunctionType)) return null;
-
- final Collection<HighlightInfo> result = ContainerUtil.newArrayList();
- final List<PsiTypeElement> typeElements = PsiUtil.getParameterTypeElements(parameter);
- for (int i = 0, size = typeElements.size(); i < size; i++) {
- final PsiClass class1 = PsiUtil.resolveClassInClassTypeOnly(typeElements.get(i).getType());
- if (class1 == null) continue;
- for (int j = i + 1; j < size; j++) {
- final PsiClass class2 = PsiUtil.resolveClassInClassTypeOnly(typeElements.get(j).getType());
- if (class2 == null) continue;
- final boolean sub = InheritanceUtil.isInheritorOrSelf(class1, class2, true);
- final boolean sup = InheritanceUtil.isInheritorOrSelf(class2, class1, true);
- if (sub || sup) {
- final String name1 = PsiFormatUtil.formatClass(class1, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_FQ_NAME);
- final String name2 = PsiFormatUtil.formatClass(class2, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_FQ_NAME);
- final String message = JavaErrorMessages.message("exception.must.be.disjoint", sub ? name1 : name2, sub ? name2 : name1);
- PsiElement element = typeElements.get(sub ? i : j);
- result.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(message).create());
- break;
- }
- }
- }
-
- return result;
- }
-
-
- @Nullable
- static Collection<HighlightInfo> checkExceptionAlreadyCaught(@NotNull final PsiParameter parameter) {
- final PsiElement scope = parameter.getDeclarationScope();
- if (!(scope instanceof PsiCatchSection)) return null;
-
- final PsiCatchSection catchSection = (PsiCatchSection)scope;
- final PsiCatchSection[] allCatchSections = catchSection.getTryStatement().getCatchSections();
- final int startFrom = ArrayUtilRt.find(allCatchSections, catchSection) - 1;
- if (startFrom < 0) return null;
-
- final List<PsiTypeElement> typeElements = PsiUtil.getParameterTypeElements(parameter);
- final boolean isInMultiCatch = typeElements.size() > 1;
- final Collection<HighlightInfo> result = ContainerUtil.newArrayList();
-
- for (PsiTypeElement typeElement : typeElements) {
- final PsiClass catchClass = PsiUtil.resolveClassInClassTypeOnly(typeElement.getType());
- if (catchClass == null) continue;
-
- for (int i = startFrom; i >= 0; i--) {
- final PsiCatchSection upperCatchSection = allCatchSections[i];
- final PsiType upperCatchType = upperCatchSection.getCatchType();
-
- final boolean highlight = upperCatchType instanceof PsiDisjunctionType
- ? checkMultipleTypes(catchClass, ((PsiDisjunctionType)upperCatchType).getDisjunctions())
- : checkSingleType(catchClass, upperCatchType);
- if (highlight) {
- final String className = PsiFormatUtil.formatClass(catchClass, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_FQ_NAME);
- final String description = JavaErrorMessages.message("exception.already.caught", className);
- final HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement).descriptionAndTooltip(description).create();
- result.add(highlightInfo);
-
- QuickFixAction.registerQuickFixAction(highlightInfo, new MoveCatchUpFix(catchSection, upperCatchSection));
- if (isInMultiCatch) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new DeleteMultiCatchFix(typeElement));
- }
- else {
- QuickFixAction.registerQuickFixAction(highlightInfo, new DeleteCatchFix(parameter));
- }
- }
- }
- }
-
- return result.isEmpty() ? null : result;
- }
-
- private static boolean checkMultipleTypes(final PsiClass catchClass, @NotNull final List<PsiType> upperCatchTypes) {
- for (int i = upperCatchTypes.size() - 1; i >= 0; i--) {
- if (checkSingleType(catchClass, upperCatchTypes.get(i))) return true;
- }
- return false;
- }
-
- private static boolean checkSingleType(final PsiClass catchClass, final PsiType upperCatchType) {
- final PsiClass upperCatchClass = PsiUtil.resolveClassInType(upperCatchType);
- return upperCatchClass != null && InheritanceUtil.isInheritorOrSelf(catchClass, upperCatchClass, true);
- }
-
-
- @Nullable
- public static HighlightInfo checkTernaryOperatorConditionIsBoolean(@NotNull PsiExpression expression, PsiType type) {
- if (expression.getParent() instanceof PsiConditionalExpression &&
- ((PsiConditionalExpression)expression.getParent()).getCondition() == expression && !TypeConversionUtil.isBooleanType(type)) {
- return createIncompatibleTypeHighlightInfo(PsiType.BOOLEAN, type, expression.getTextRange(), 0);
- }
- return null;
- }
-
-
- @Nullable
- public static HighlightInfo checkStatementPrependedWithCaseInsideSwitch(@NotNull PsiStatement statement) {
- if (!(statement instanceof PsiSwitchLabelStatement) && statement.getParent() instanceof PsiCodeBlock &&
- statement.getParent().getParent() instanceof PsiSwitchStatement &&
- ((PsiCodeBlock)statement.getParent()).getStatements().length != 0 &&
- statement == ((PsiCodeBlock)statement.getParent()).getStatements()[0]) {
- String description = JavaErrorMessages.message("statement.must.be.prepended.with.case.label");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
-
- @Nullable
- public static HighlightInfo checkAssertOperatorTypes(@NotNull PsiExpression expression, @Nullable PsiType type) {
- if (type == null) return null;
- if (!(expression.getParent() instanceof PsiAssertStatement)) {
- return null;
- }
- PsiAssertStatement assertStatement = (PsiAssertStatement)expression.getParent();
- if (expression == assertStatement.getAssertCondition() && !TypeConversionUtil.isBooleanType(type)) {
- // addTypeCast quickfix is not applicable here since no type can be cast to boolean
- return createIncompatibleTypeHighlightInfo(PsiType.BOOLEAN, type, expression.getTextRange(), 0);
- }
- if (expression == assertStatement.getAssertDescription() && TypeConversionUtil.isVoidType(type)) {
- String description = JavaErrorMessages.message("void.type.is.not.allowed");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
- }
- return null;
- }
-
-
- @Nullable
- public static HighlightInfo checkSynchronizedExpressionType(@NotNull PsiExpression expression, @Nullable PsiType type,@NotNull PsiFile containingFile) {
- if (type == null) return null;
- if (expression.getParent() instanceof PsiSynchronizedStatement) {
- PsiSynchronizedStatement synchronizedStatement = (PsiSynchronizedStatement)expression.getParent();
- if (expression == synchronizedStatement.getLockExpression() &&
- (type instanceof PsiPrimitiveType || TypeConversionUtil.isNullType(type))) {
- PsiClassType objectType = PsiType.getJavaLangObject(containingFile.getManager(), expression.getResolveScope());
- return createIncompatibleTypeHighlightInfo(objectType, type, expression.getTextRange(), 0);
- }
- }
- return null;
- }
-
-
- @Nullable
- public static HighlightInfo checkConditionalExpressionBranchTypesMatch(@NotNull final PsiExpression expression, PsiType type) {
- PsiElement parent = expression.getParent();
- if (!(parent instanceof PsiConditionalExpression)) {
- return null;
- }
- PsiConditionalExpression conditionalExpression = (PsiConditionalExpression)parent;
- // check else branches only
- if (conditionalExpression.getElseExpression() != expression) return null;
- final PsiExpression thenExpression = conditionalExpression.getThenExpression();
- assert thenExpression != null;
- PsiType thenType = thenExpression.getType();
- PsiType elseType = type;
- if (thenType == null || elseType == null) return null;
- if (conditionalExpression.getType() == null) {
- // cannot derive type of conditional expression
- // elseType will never be castable to thenType, so no quick fix here
- return createIncompatibleTypeHighlightInfo(thenType, elseType, expression.getTextRange(), 0);
- }
- return null;
- }
-
- private static HighlightInfo createIncompatibleTypeHighlightInfo(final PsiType lType,
- final PsiType rType,
- @NotNull final TextRange textRange,
- int navigationShift) {
- PsiType lType1 = lType;
- PsiType rType1 = rType;
- PsiTypeParameter[] lTypeParams = PsiTypeParameter.EMPTY_ARRAY;
- PsiSubstitutor lTypeSubstitutor = PsiSubstitutor.EMPTY;
- if (lType1 instanceof PsiClassType) {
- PsiClassType.ClassResolveResult resolveResult = ((PsiClassType)lType1).resolveGenerics();
- lTypeSubstitutor = resolveResult.getSubstitutor();
- PsiClass psiClass = resolveResult.getElement();
- if (psiClass instanceof PsiAnonymousClass) {
- lType1 = ((PsiAnonymousClass)psiClass).getBaseClassType();
- resolveResult = ((PsiClassType)lType1).resolveGenerics();
- lTypeSubstitutor = resolveResult.getSubstitutor();
- psiClass = resolveResult.getElement();
- }
- lTypeParams = psiClass == null ? PsiTypeParameter.EMPTY_ARRAY : psiClass.getTypeParameters();
- }
- PsiTypeParameter[] rTypeParams = PsiTypeParameter.EMPTY_ARRAY;
- PsiSubstitutor rTypeSubstitutor = PsiSubstitutor.EMPTY;
- if (rType1 instanceof PsiClassType) {
- PsiClassType.ClassResolveResult resolveResult = ((PsiClassType)rType1).resolveGenerics();
- rTypeSubstitutor = resolveResult.getSubstitutor();
- PsiClass psiClass = resolveResult.getElement();
- if (psiClass instanceof PsiAnonymousClass) {
- rType1 = ((PsiAnonymousClass)psiClass).getBaseClassType();
- resolveResult = ((PsiClassType)rType1).resolveGenerics();
- rTypeSubstitutor = resolveResult.getSubstitutor();
- psiClass = resolveResult.getElement();
- }
- rTypeParams = psiClass == null ? PsiTypeParameter.EMPTY_ARRAY : psiClass.getTypeParameters();
- }
-
- int typeParamColumns = Math.max(lTypeParams.length, rTypeParams.length);
- @Language("HTML") @NonNls String requiredRow = "";
- @Language("HTML") @NonNls String foundRow = "";
- for (int i = 0; i < typeParamColumns; i++) {
- PsiTypeParameter lTypeParameter = i >= lTypeParams.length ? null : lTypeParams[i];
- PsiTypeParameter rTypeParameter = i >= rTypeParams.length ? null : rTypeParams[i];
- PsiType lSubstitutedType = lTypeParameter == null ? null : lTypeSubstitutor.substitute(lTypeParameter);
- PsiType rSubstitutedType = rTypeParameter == null ? null : rTypeSubstitutor.substitute(rTypeParameter);
- boolean matches = Comparing.equal(lSubstitutedType, rSubstitutedType);
- @NonNls String openBrace = i == 0 ? "&lt;" : "";
- @NonNls String closeBrace = i == typeParamColumns - 1 ? "&gt;" : ",";
- requiredRow += "<td>" + (lTypeParams.length == 0 ? "" : openBrace) + redIfNotMatch(lSubstitutedType, matches) +
- (i < lTypeParams.length ? closeBrace : "") + "</td>";
- foundRow += "<td>" + (rTypeParams.length == 0 ? "" : openBrace) + redIfNotMatch(rSubstitutedType, matches) +
- (i < rTypeParams.length ? closeBrace : "") + "</td>";
- }
- PsiType lRawType = lType1 instanceof PsiClassType ? ((PsiClassType)lType1).rawType() : lType1;
- PsiType rRawType = rType1 instanceof PsiClassType ? ((PsiClassType)rType1).rawType() : rType1;
- boolean assignable = lRawType == null || rRawType == null || TypeConversionUtil.isAssignable(lRawType, rRawType);
-
- String toolTip = JavaErrorMessages.message("incompatible.types.html.tooltip",
- redIfNotMatch(lRawType, assignable), requiredRow,
- redIfNotMatch(rRawType, assignable), foundRow);
-
- String description = JavaErrorMessages.message("incompatible.types", JavaHighlightUtil.formatType(lType1), JavaHighlightUtil
- .formatType(rType1));
-
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).description(description).escapedToolTip(toolTip).navigationShift(navigationShift).create();
- }
-
- @Nullable
- public static HighlightInfo checkSingleImportClassConflict(@NotNull PsiImportStatement statement,
- @NotNull Map<String, Pair<PsiImportStaticReferenceElement, PsiClass>> importedClasses,@NotNull PsiFile containingFile) {
- if (statement.isOnDemand()) return null;
- PsiElement element = statement.resolve();
- if (element instanceof PsiClass) {
- String name = ((PsiClass)element).getName();
- Pair<PsiImportStaticReferenceElement, PsiClass> imported = importedClasses.get(name);
- PsiClass importedClass = imported == null ? null : imported.getSecond();
- if (importedClass != null && !containingFile.getManager().areElementsEquivalent(importedClass, element)) {
- String description = JavaErrorMessages.message("single.import.class.conflict", formatClass(importedClass));
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).create();
- }
- importedClasses.put(name, Pair.create((PsiImportStaticReferenceElement)null, (PsiClass)element));
- }
- return null;
- }
-
-
- @NonNls
- private static String redIfNotMatch(PsiType type, boolean matches) {
- if (matches) return getFQName(type, false);
- String color = UIUtil.isUnderDarcula() ? "FF6B68" : "red";
- return "<font color='" + color +"'><b>" + getFQName(type, true) + "</b></font>";
- }
-
- private static String getFQName(@Nullable PsiType type, boolean longName) {
- if (type == null) return "";
- return XmlStringUtil.escapeString(longName ? type.getInternalCanonicalText() : type.getPresentableText());
- }
-
-
- @Nullable
- static HighlightInfo checkMustBeThrowable(@Nullable PsiType type, @NotNull PsiElement context, boolean addCastIntention) {
- if (type == null) return null;
- PsiElementFactory factory = JavaPsiFacade.getInstance(context.getProject()).getElementFactory();
- PsiClassType throwable = factory.createTypeByFQClassName("java.lang.Throwable", context.getResolveScope());
- if (!TypeConversionUtil.isAssignable(throwable, type)) {
- HighlightInfo highlightInfo = createIncompatibleTypeHighlightInfo(throwable, type, context.getTextRange(), 0);
- if (addCastIntention && TypeConversionUtil.areTypesConvertible(type, throwable)) {
- if (context instanceof PsiExpression) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new AddTypeCastFix(throwable, (PsiExpression)context));
- }
- }
- return highlightInfo;
- }
- return null;
- }
-
-
- @Nullable
- private static HighlightInfo checkMustBeThrowable(@Nullable PsiClass aClass, @NotNull PsiElement context) {
- if (aClass == null) return null;
- PsiClassType type = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createType(aClass);
- return checkMustBeThrowable(type, context, false);
- }
-
-
- @Nullable
- static HighlightInfo checkLabelDefined(@Nullable PsiIdentifier labelIdentifier, @Nullable PsiStatement exitedStatement) {
- if (labelIdentifier == null) return null;
- String label = labelIdentifier.getText();
- if (label == null) return null;
- if (exitedStatement == null) {
- String message = JavaErrorMessages.message("unresolved.label", label);
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(labelIdentifier).descriptionAndTooltip(message).create();
- }
- return null;
- }
-
-
- @Nullable
- static HighlightInfo checkReference(@NotNull final PsiJavaCodeReferenceElement ref,
- @NotNull final JavaResolveResult result,
- @NotNull PsiFile containingFile,
- @NotNull LanguageLevel languageLevel) {
- final PsiElement refName = ref.getReferenceNameElement();
-
- if (!(refName instanceof PsiIdentifier) && !(refName instanceof PsiKeyword)) return null;
- final PsiElement resolved = result.getElement();
-
- HighlightInfo highlightInfo = checkMemberReferencedBeforeConstructorCalled(ref, resolved, containingFile);
- if (highlightInfo != null) return highlightInfo;
-
- PsiElement refParent = ref.getParent();
- PsiElement granny;
- if (refParent instanceof PsiReferenceExpression && (granny = refParent.getParent()) instanceof PsiMethodCallExpression) {
- PsiReferenceExpression referenceToMethod = ((PsiMethodCallExpression)granny).getMethodExpression();
- PsiExpression qualifierExpression = referenceToMethod.getQualifierExpression();
- if (qualifierExpression == ref) {
- @SuppressWarnings("UnnecessaryLocalVariable") PsiElement qualifier = resolved;
- if (qualifier != null && !(qualifier instanceof PsiClass) && !(qualifier instanceof PsiVariable)) {
- return HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(qualifierExpression).descriptionAndTooltip(
- "Qualifier must be an expression").create();
- }
- }
- }
- else if (refParent instanceof PsiMethodCallExpression) {
- return null; // methods checked elsewhere
- }
- if (resolved == null) {
- // do not highlight unknown packages - javac does not care about illegal package names
- if (isInsidePackageStatement(refName)) return null;
- if (result.isPackagePrefixPackageReference()) return null;
- JavaResolveResult[] results = ref.multiResolve(true);
- String description;
- if (results.length > 1) {
- String t1 = format(results[0].getElement());
- String t2 = format(results[1].getElement());
- description = JavaErrorMessages.message("ambiguous.reference", refName.getText(), t1, t2);
- }
- else {
- description = JavaErrorMessages.message("cannot.resolve.symbol", refName.getText());
- }
-
- HighlightInfoType type = HighlightInfoType.WRONG_REF;
- if (PsiUtil.isInsideJavadocComment(ref)) return null;
-
- HighlightInfo info = HighlightInfo.newHighlightInfo(type).range(refName).descriptionAndTooltip(description).create();
-
- UnresolvedReferenceQuickFixProvider.registerReferenceFixes(ref, new QuickFixActionRegistrarImpl(info));
- return info;
- }
-
- if (!result.isValidResult() && !PsiUtil.isInsideJavadocComment(ref)) {
- if (!result.isAccessible()) {
- String description = buildProblemWithAccessDescription(ref, result, resolved);
- HighlightInfo info =
- HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(refName).descriptionAndTooltip(description).create();
- if (result.isStaticsScopeCorrect()) {
- registerAccessQuickFixAction((PsiMember)resolved, ref, info, result.getCurrentFileResolveScope());
- if (ref instanceof PsiReferenceExpression) {
- QuickFixAction.registerQuickFixAction(info, new RenameWrongRefFix((PsiReferenceExpression)ref));
- }
- }
- UnresolvedReferenceQuickFixProvider.registerReferenceFixes(ref, new QuickFixActionRegistrarImpl(info));
- return info;
- }
-
- if (!result.isStaticsScopeCorrect()) {
- String description = buildProblemWithStaticDescription(resolved);
- HighlightInfo info =
- HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(refName).descriptionAndTooltip(description).create();
- registerStaticProblemQuickFixAction(resolved, info, ref);
- if (ref instanceof PsiReferenceExpression) {
- QuickFixAction.registerQuickFixAction(info, new RenameWrongRefFix((PsiReferenceExpression)ref));
- }
- return info;
- }
- }
- if ((resolved instanceof PsiLocalVariable || resolved instanceof PsiParameter) && !(resolved instanceof ImplicitVariable)) {
- highlightInfo = HighlightControlFlowUtil.checkVariableMustBeFinal((PsiVariable)resolved, ref,
- languageLevel);
- }
- else if (resolved instanceof PsiClass) {
- if (Comparing.strEqual(((PsiClass)resolved).getQualifiedName(), ((PsiClass)resolved).getName())) {
- final PsiElement parent = ref.getParent();
- if (parent instanceof PsiImportStaticReferenceElement || parent instanceof PsiImportStatementBase) {
- String description = JavaErrorMessages.message("cannot.resolve.symbol", refName.getText());
- return HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(refName).descriptionAndTooltip(description).create();
- }
- }
- }
- return highlightInfo;
- }
-
- @NotNull
- private static String format(@NotNull PsiElement element) {
- if (element instanceof PsiClass) return formatClass((PsiClass)element);
- if (element instanceof PsiMethod) return JavaHighlightUtil.formatMethod((PsiMethod)element);
- if (element instanceof PsiField) return formatField((PsiField)element);
- return ElementDescriptionUtil.getElementDescription(element, HighlightUsagesDescriptionLocation.INSTANCE);
- }
-
- private static boolean isInsidePackageStatement(@Nullable PsiElement element) {
- while (element != null) {
- if (element instanceof PsiPackageStatement) return true;
- if (!(element instanceof PsiIdentifier) && !(element instanceof PsiJavaCodeReferenceElement)) return false;
- element = element.getParent();
- }
- return false;
- }
-
- @Nullable
- static HighlightInfo checkPackageAndClassConflict(@NotNull PsiJavaCodeReferenceElement ref) {
- if (ref.isQualified() && isInsidePackageStatement(ref)) {
- VirtualFile file = ref.getContainingFile().getVirtualFile();
- if (file != null) {
- Module module = ProjectFileIndex.SERVICE.getInstance(ref.getProject()).getModuleForFile(file);
- if (module != null) {
- GlobalSearchScope scope = module.getModuleWithDependenciesAndLibrariesScope(false);
- PsiClass aClass = JavaPsiFacade.getInstance(ref.getProject()).findClass(ref.getCanonicalText(), scope);
- if (aClass != null) {
- String message = JavaErrorMessages.message("package.clashes.with.class", ref.getText());
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref).descriptionAndTooltip(message).create();
- }
- }
- }
- }
-
- return null;
- }
-
- @Nullable
- static HighlightInfo checkElementInReferenceList(@NotNull PsiJavaCodeReferenceElement ref,
- @NotNull PsiReferenceList referenceList,
- @NotNull JavaResolveResult resolveResult,
- @NotNull LanguageLevel languageLevel) {
- PsiElement resolved = resolveResult.getElement();
- HighlightInfo highlightInfo = null;
- PsiElement refGrandParent = referenceList.getParent();
- if (resolved instanceof PsiClass) {
- PsiClass aClass = (PsiClass)resolved;
- if (refGrandParent instanceof PsiClass) {
- if (refGrandParent instanceof PsiTypeParameter) {
- highlightInfo = GenericsHighlightUtil.checkElementInTypeParameterExtendsList(referenceList, (PsiClass)refGrandParent, resolveResult, ref, languageLevel);
- }
- else {
- highlightInfo = HighlightClassUtil.checkExtendsClassAndImplementsInterface(referenceList, resolveResult, ref);
- if (highlightInfo == null) {
- highlightInfo = HighlightClassUtil.checkCannotInheritFromFinal(aClass, ref);
- }
- if (highlightInfo == null) {
- highlightInfo = GenericsHighlightUtil.checkCannotInheritFromEnum(aClass, ref);
- }
- if (highlightInfo == null) {
- highlightInfo = GenericsHighlightUtil.checkCannotInheritFromTypeParameter(aClass, ref);
- }
- }
- }
- else if (refGrandParent instanceof PsiMethod && ((PsiMethod)refGrandParent).getThrowsList() == referenceList) {
- highlightInfo = checkMustBeThrowable(aClass, ref);
- }
- }
- else if (refGrandParent instanceof PsiMethod && referenceList == ((PsiMethod)refGrandParent).getThrowsList()) {
- String description = JavaErrorMessages.message("class.name.expected");
- highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref).descriptionAndTooltip(description).create();
- }
- return highlightInfo;
- }
-
-
- public static boolean isSerializationImplicitlyUsedField(@NotNull PsiField field) {
- final String name = field.getName();
- if (!SERIAL_VERSION_UID_FIELD_NAME.equals(name) && !SERIAL_PERSISTENT_FIELDS_FIELD_NAME.equals(name)) return false;
- if (!field.hasModifierProperty(PsiModifier.STATIC)) return false;
- PsiClass aClass = field.getContainingClass();
- return aClass == null || JavaHighlightUtil.isSerializable(aClass);
- }
-
- @Nullable
- public static HighlightInfo checkClassReferenceAfterQualifier(@NotNull final PsiReferenceExpression expression, final PsiElement resolved) {
- if (!(resolved instanceof PsiClass)) return null;
- final PsiExpression qualifier = expression.getQualifierExpression();
- if (qualifier == null) return null;
- if (qualifier instanceof PsiReferenceExpression) {
- PsiElement qualifierResolved = ((PsiReferenceExpression)qualifier).resolve();
- if (qualifierResolved instanceof PsiClass || qualifierResolved instanceof PsiPackage) return null;
- }
- String description = JavaErrorMessages.message("expected.class.or.package");
- HighlightInfo info =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(qualifier).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(info, new RemoveQualifierFix(qualifier, expression, (PsiClass)resolved));
- return info;
- }
-
- public static void registerChangeVariableTypeFixes(@NotNull PsiVariable parameter, PsiType itemType, HighlightInfo highlightInfo) {
- if (itemType instanceof PsiMethodReferenceType) return;
- if (itemType != null) {
- for (ChangeVariableTypeQuickFixProvider fixProvider : Extensions.getExtensions(ChangeVariableTypeQuickFixProvider.EP_NAME)) {
- for (IntentionAction action : fixProvider.getFixes(parameter, itemType)) {
- QuickFixAction.registerQuickFixAction(highlightInfo, action);
- }
- }
- }
- ChangeParameterClassFix.registerQuickFixAction(parameter.getType(), itemType, highlightInfo);
- }
-
- @Nullable
- static HighlightInfo checkAnnotationMethodParameters(@NotNull PsiParameterList list) {
- final PsiElement parent = list.getParent();
- if (PsiUtil.isAnnotationMethod(parent) && list.getParametersCount() > 0) {
- final String message = JavaErrorMessages.message("annotation.interface.members.may.not.have.parameters");
- final HighlightInfo highlightInfo =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).descriptionAndTooltip(message).create();
- QuickFixAction.registerQuickFixAction(highlightInfo, new RemoveParameterListFix((PsiMethod)parent));
- return highlightInfo;
- }
- return null;
- }
-
- @Nullable
- static HighlightInfo checkForStatement(@NotNull PsiForStatement statement) {
- PsiStatement init = statement.getInitialization();
- if (!(init == null || init instanceof PsiEmptyStatement ||
- init instanceof PsiDeclarationStatement ||
- init instanceof PsiExpressionStatement || init instanceof PsiExpressionListStatement)) {
- String message = JavaErrorMessages.message("invalid.statement");
- return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(init).descriptionAndTooltip(message).create();
- }
-
- return null;
- }
-
- private enum Feature {
- GENERICS(LanguageLevel.JDK_1_5, "feature.generics"),
- ANNOTATIONS(LanguageLevel.JDK_1_5, "feature.annotations"),
- STATIC_IMPORTS(LanguageLevel.JDK_1_5, "feature.static.imports"),
- FOR_EACH(LanguageLevel.JDK_1_5, "feature.for.each"),
- VARARGS(LanguageLevel.JDK_1_5, "feature.varargs"),
- HEX_FP_LITERALS(LanguageLevel.JDK_1_5, "feature.hex.fp.literals"),
- DIAMOND_TYPES(LanguageLevel.JDK_1_7, "feature.diamond.types"),
- MULTI_CATCH(LanguageLevel.JDK_1_7, "feature.multi.catch"),
- TRY_WITH_RESOURCES(LanguageLevel.JDK_1_7, "feature.try.with.resources"),
- BIN_LITERALS(LanguageLevel.JDK_1_7, "feature.binary.literals"),
- UNDERSCORES(LanguageLevel.JDK_1_7, "feature.underscores.in.literals"),
- EXTENSION_METHODS(LanguageLevel.JDK_1_8, "feature.extension.methods"),
- METHOD_REFERENCES(LanguageLevel.JDK_1_8, "feature.method.references"),
- LAMBDA_EXPRESSIONS(LanguageLevel.JDK_1_8, "feature.lambda.expressions"),
- TYPE_ANNOTATIONS(LanguageLevel.JDK_1_8, "feature.type.annotations");
-
- @NotNull
- private final LanguageLevel level;
- @NotNull
- private final String key;
-
- Feature(@NotNull LanguageLevel level, @NotNull @PropertyKey(resourceBundle = JavaErrorMessages.BUNDLE) final String key) {
- this.level = level;
- this.key = key;
- }
- }
-
- @Nullable
- private static HighlightInfo checkFeature(@NotNull final PsiElement element,
- @NotNull Feature feature,
- @NotNull LanguageLevel languageLevel,
- @NotNull PsiFile containingFile) {
- if (containingFile.getManager().isInProject(containingFile) && !languageLevel.isAtLeast(feature.level)) {
- String message = JavaErrorMessages.message("insufficient.language.level", JavaErrorMessages.message(feature.key));
- HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(message).create();
- QuickFixAction.registerQuickFixAction(info, new IncreaseLanguageLevelFix(feature.level));
- QuickFixAction.registerQuickFixAction(info, new ShowModulePropertiesFix(element));
- return info;
- }
-
- return null;
- }
-
- @Nullable
- public static HighlightInfo checkGenericsFeature(@NotNull PsiElement parameterList,
- int listSize,
- @NotNull LanguageLevel languageLevel,
- @NotNull PsiFile containingFile) {
- return listSize > 0 ? checkFeature(parameterList, Feature.GENERICS, languageLevel, containingFile) : null;
- }
-
- @Nullable
- public static HighlightInfo checkAnnotationFeature(@NotNull PsiElement element,
- @NotNull LanguageLevel languageLevel,
- @NotNull PsiFile containingFile) {
- return checkFeature(element, Feature.ANNOTATIONS, languageLevel, containingFile);
- }
-
- @Nullable
- public static HighlightInfo checkForEachFeature(@NotNull PsiForeachStatement statement, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- return checkFeature(statement, Feature.FOR_EACH, languageLevel, containingFile);
- }
-
- @Nullable
- public static HighlightInfo checkStaticImportFeature(@NotNull PsiImportStaticStatement statement, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- return checkFeature(statement, Feature.STATIC_IMPORTS, languageLevel, containingFile);
- }
-
- @Nullable
- public static HighlightInfo checkVarargFeature(@NotNull PsiParameter parameter, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- return checkFeature(parameter, Feature.VARARGS, languageLevel, containingFile);
- }
-
- @Nullable
- public static HighlightInfo checkDiamondFeature(@NotNull PsiTypeElement typeElement, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- return typeElement.getType() instanceof PsiDiamondType ? checkFeature(typeElement.getParent(), Feature.DIAMOND_TYPES,
- languageLevel, containingFile) : null;
- }
-
- @Nullable
- public static HighlightInfo checkMultiCatchFeature(@NotNull PsiParameter parameter, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- return parameter.getType() instanceof PsiDisjunctionType ? checkFeature(parameter, Feature.MULTI_CATCH,
- languageLevel, containingFile) : null;
- }
-
- @Nullable
- public static HighlightInfo checkTryWithResourcesFeature(@NotNull PsiResourceVariable resourceVariable, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- return checkFeature(resourceVariable.getParent(), Feature.TRY_WITH_RESOURCES, languageLevel, containingFile);
- }
-
- @Nullable
- public static HighlightInfo checkExtensionMethodsFeature(@NotNull PsiMethod method, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- return checkFeature(method, Feature.EXTENSION_METHODS, languageLevel, containingFile);
- }
-
- @Nullable
- public static HighlightInfo checkMethodReferencesFeature(@NotNull PsiMethodReferenceExpression expression, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- return checkFeature(expression, Feature.METHOD_REFERENCES, languageLevel, containingFile);
- }
-
- @Nullable
- public static HighlightInfo checkLambdaFeature(@NotNull PsiLambdaExpression expression, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- return checkFeature(expression, Feature.LAMBDA_EXPRESSIONS, languageLevel, containingFile);
- }
-
- @Nullable
- public static HighlightInfo checkTypeAnnotationFeature(@NotNull PsiAnnotation annotation, @NotNull LanguageLevel languageLevel,@NotNull PsiFile containingFile) {
- return checkFeature(annotation, Feature.TYPE_ANNOTATIONS, languageLevel, containingFile);
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java
deleted file mode 100644
index 40e2eaab4697..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java
+++ /dev/null
@@ -1,1356 +0,0 @@
-/*
- * Copyright 2000-2013 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.analysis;
-
-import com.intellij.codeHighlighting.Pass;
-import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
-import com.intellij.codeInsight.daemon.JavaErrorMessages;
-import com.intellij.codeInsight.daemon.impl.*;
-import com.intellij.codeInsight.daemon.impl.quickfix.QuickFixAction;
-import com.intellij.codeInsight.daemon.impl.quickfix.SetupJDKFix;
-import com.intellij.lang.injection.InjectedLanguageManager;
-import com.intellij.openapi.editor.Document;
-import com.intellij.openapi.editor.colors.TextAttributesScheme;
-import com.intellij.openapi.progress.ProgressIndicator;
-import com.intellij.openapi.progress.ProgressManager;
-import com.intellij.openapi.project.IndexNotReadyException;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.projectRoots.JavaSdkVersion;
-import com.intellij.openapi.projectRoots.JavaVersionService;
-import com.intellij.openapi.util.Pair;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.pom.java.LanguageLevel;
-import com.intellij.psi.*;
-import com.intellij.psi.controlFlow.ControlFlowUtil;
-import com.intellij.psi.impl.source.javadoc.PsiDocMethodOrFieldRef;
-import com.intellij.psi.impl.source.resolve.JavaResolveUtil;
-import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
-import com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl;
-import com.intellij.psi.javadoc.PsiDocComment;
-import com.intellij.psi.javadoc.PsiDocTagValue;
-import com.intellij.psi.util.*;
-import com.intellij.psi.xml.XmlAttributeValue;
-import com.intellij.util.ObjectUtils;
-import com.intellij.util.containers.MostlySingularMultiMap;
-import gnu.trove.THashMap;
-import gnu.trove.TObjectIntHashMap;
-import org.jetbrains.annotations.NotNull;
-
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-public class HighlightVisitorImpl extends JavaElementVisitor implements HighlightVisitor {
- private final PsiResolveHelper myResolveHelper;
-
- private HighlightInfoHolder myHolder;
-
- private RefCountHolder myRefCountHolder;
-
- // map codeBlock->List of PsiReferenceExpression of uninitialized final variables
- private final Map<PsiElement, Collection<PsiReferenceExpression>> myUninitializedVarProblems = new THashMap<PsiElement, Collection<PsiReferenceExpression>>();
- // map codeBlock->List of PsiReferenceExpression of extra initialization of final variable
- private final Map<PsiElement, Collection<ControlFlowUtil.VariableInfo>> myFinalVarProblems = new THashMap<PsiElement, Collection<ControlFlowUtil.VariableInfo>>();
-
- // value==1: no info if the parameter was reassigned (but the parameter is present in current file), value==2: parameter was reassigned
- private final TObjectIntHashMap<PsiParameter> myReassignedParameters = new TObjectIntHashMap<PsiParameter>();
-
- private final Map<String, Pair<PsiImportStaticReferenceElement, PsiClass>> mySingleImportedClasses = new THashMap<String, Pair<PsiImportStaticReferenceElement, PsiClass>>();
- private final Map<String, Pair<PsiImportStaticReferenceElement, PsiField>> mySingleImportedFields = new THashMap<String, Pair<PsiImportStaticReferenceElement, PsiField>>();
- private PsiFile myFile;
- private final PsiElementVisitor REGISTER_REFERENCES_VISITOR = new PsiRecursiveElementWalkingVisitor() {
- @Override public void visitElement(PsiElement element) {
- super.visitElement(element);
- for (PsiReference reference : element.getReferences()) {
- PsiElement resolved = reference.resolve();
- if (resolved instanceof PsiNamedElement) {
- myRefCountHolder.registerLocallyReferenced((PsiNamedElement)resolved);
- }
- }
- }
- };
- private final Map<PsiClass, MostlySingularMultiMap<MethodSignature, PsiMethod>> myDuplicateMethods = new THashMap<PsiClass, MostlySingularMultiMap<MethodSignature, PsiMethod>>();
- private LanguageLevel myLanguageLevel;
- private JavaSdkVersion myJavaSdkVersion;
-
- public HighlightVisitorImpl(@NotNull PsiResolveHelper resolveHelper) {
- myResolveHelper = resolveHelper;
- }
-
- @NotNull
- private MostlySingularMultiMap<MethodSignature, PsiMethod> getDuplicateMethods(PsiClass aClass) {
- MostlySingularMultiMap<MethodSignature, PsiMethod> signatures = myDuplicateMethods.get(aClass);
- if (signatures == null) {
- signatures = new MostlySingularMultiMap<MethodSignature, PsiMethod>();
- for (PsiMethod method : aClass.getMethods()) {
- if (method instanceof ExternallyDefinedPsiElement) continue; // ignore aspectj-weaved methods; they are checked elsewhere
- MethodSignature signature = method.getSignature(PsiSubstitutor.EMPTY);
- signatures.add(signature, method);
- }
-
- myDuplicateMethods.put(aClass, signatures);
- }
- return signatures;
- }
-
- @Override
- @NotNull
- public HighlightVisitorImpl clone() {
- return new HighlightVisitorImpl(myResolveHelper);
- }
-
- @Override
- public int order() {
- return 0;
- }
-
- @Override
- public boolean suitableForFile(@NotNull PsiFile file) {
- return !InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file);
- }
-
- @Override
- public void visit(@NotNull PsiElement element) {
- element.accept(this);
- }
-
- private void registerReferencesFromInjectedFragments(final PsiElement element) {
- InjectedLanguageUtil.enumerate(element, myFile, false, new PsiLanguageInjectionHost.InjectedPsiVisitor() {
- @Override
- public void visit(@NotNull final PsiFile injectedPsi, @NotNull final List<PsiLanguageInjectionHost.Shred> places) {
- injectedPsi.accept(REGISTER_REFERENCES_VISITOR);
- }
- });
- }
-
- @Override
- public boolean analyze(@NotNull final PsiFile file,
- final boolean updateWholeFile,
- @NotNull HighlightInfoHolder holder,
- @NotNull final Runnable action) {
- myFile = file;
- myHolder = holder;
- boolean success = true;
- try {
- myLanguageLevel = PsiUtil.getLanguageLevel(file);
- myJavaSdkVersion = ObjectUtils.notNull(JavaVersionService.getInstance().getJavaSdkVersion(file), JavaSdkVersion.fromLanguageLevel(myLanguageLevel));
- if (updateWholeFile) {
- Project project = file.getProject();
- DaemonCodeAnalyzer daemonCodeAnalyzer = DaemonCodeAnalyzer.getInstance(project);
- FileStatusMap fileStatusMap = ((DaemonCodeAnalyzerImpl)daemonCodeAnalyzer).getFileStatusMap();
- ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
- if (indicator == null) throw new IllegalStateException("Must be run under progress");
- RefCountHolder refCountHolder = RefCountHolder.startUsing(file, indicator);
- myRefCountHolder = refCountHolder;
- Document document = PsiDocumentManager.getInstance(project).getDocument(file);
- TextRange dirtyScope = document == null ? file.getTextRange() : fileStatusMap.getFileDirtyScope(document, Pass.UPDATE_ALL);
- success = refCountHolder.analyze(file, dirtyScope, action, indicator);
- }
- else {
- myRefCountHolder = null;
- action.run();
- }
- }
- finally {
- myUninitializedVarProblems.clear();
- myFinalVarProblems.clear();
- mySingleImportedClasses.clear();
- mySingleImportedFields.clear();
- myReassignedParameters.clear();
-
- myRefCountHolder = null;
- myFile = null;
- myHolder = null;
- myDuplicateMethods.clear();
- }
-
- return success;
- }
-
- @Override
- public void visitElement(final PsiElement element) {
- if (element instanceof XmlAttributeValue) {
- try {
- for (PsiReference reference : element.getReferences()) {
- if(reference instanceof PsiJavaReference && myRefCountHolder != null){
- final PsiJavaReference psiJavaReference = (PsiJavaReference)reference;
- myRefCountHolder.registerReference(psiJavaReference, psiJavaReference.advancedResolve(false));
- }
- }
- }
- catch (IndexNotReadyException ignored) {
- }
- }
- }
-
- @Override
- public void visitAnnotation(PsiAnnotation annotation) {
- super.visitAnnotation(annotation);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkAnnotationFeature(annotation, myLanguageLevel, myFile));
- if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkApplicability(annotation, myLanguageLevel,myFile));
- if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkAnnotationType(annotation));
- if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkMissingAttributes(annotation));
- if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkTargetAnnotationDuplicates(annotation));
- if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkDuplicateAnnotations(annotation));
- if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkForeignInnerClassesUsed(annotation));
- if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkFunctionalInterface(annotation, myLanguageLevel));
- if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkRepeatableAnnotation(annotation));
- }
-
- @Override
- public void visitAnnotationArrayInitializer(PsiArrayInitializerMemberValue initializer) {
- PsiMethod method = null;
- PsiElement parent = initializer.getParent();
- if (parent instanceof PsiNameValuePair) {
- method = (PsiMethod)parent.getReference().resolve();
- }
- else if (PsiUtil.isAnnotationMethod(parent)) {
- method = (PsiMethod)parent;
- }
- if (method != null) {
- PsiType type = method.getReturnType();
- if (type instanceof PsiArrayType) {
- type = ((PsiArrayType)type).getComponentType();
- PsiAnnotationMemberValue[] initializers = initializer.getInitializers();
- for (PsiAnnotationMemberValue initializer1 : initializers) {
- myHolder.add(AnnotationsHighlightUtil.checkMemberValueType(initializer1, type));
- }
- }
- }
- }
-
- @Override
- public void visitAnnotationMethod(PsiAnnotationMethod method) {
- PsiType returnType = method.getReturnType();
- PsiAnnotationMemberValue value = method.getDefaultValue();
- if (returnType != null && value != null) {
- myHolder.add(AnnotationsHighlightUtil.checkMemberValueType(value, returnType));
- }
-
- myHolder.add(AnnotationsHighlightUtil.checkValidAnnotationType(method.getReturnTypeElement()));
- myHolder.add(AnnotationsHighlightUtil.checkCyclicMemberType(method.getReturnTypeElement(), method.getContainingClass()));
- myHolder.add(AnnotationsHighlightUtil.checkClashesWithSuperMethods(method));
- }
-
- @Override
- public void visitArrayInitializerExpression(PsiArrayInitializerExpression expression) {
- super.visitArrayInitializerExpression(expression);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkArrayInitializerApplicable(expression));
- if (!(expression.getParent() instanceof PsiNewExpression)) {
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkGenericArrayCreation(expression, expression.getType()));
- }
- }
-
- @Override
- public void visitAssignmentExpression(PsiAssignmentExpression assignment) {
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkAssignmentCompatibleTypes(assignment));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkAssignmentOperatorApplicable(assignment,myFile));
- if (!myHolder.hasErrorResults()) visitExpression(assignment);
- }
-
- @Override
- public void visitPolyadicExpression(PsiPolyadicExpression expression) {
- super.visitPolyadicExpression(expression);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkPolyadicOperatorApplicable(expression));
- }
-
- @Override
- public void visitLambdaExpression(PsiLambdaExpression expression) {
- myHolder.add(HighlightUtil.checkLambdaFeature(expression, myLanguageLevel,myFile));
- if (!myHolder.hasErrorResults()) {
- if (LambdaUtil.isValidLambdaContext(expression.getParent())) {
- final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
- if (functionalInterfaceType != null) {
- final String notFunctionalMessage = LambdaHighlightingUtil.checkInterfaceFunctional(functionalInterfaceType);
- if (notFunctionalMessage != null) {
- HighlightInfo result =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(notFunctionalMessage)
- .create();
- myHolder.add(result);
- }
- else {
- if (!LambdaUtil.isLambdaFullyInferred(expression, functionalInterfaceType) && !expression.hasFormalParameterTypes()) {
- HighlightInfo result =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip("Cyclic inference")
- .create();
- myHolder.add(result); //todo[ann] append not inferred type params info
- }
- else {
- final String incompatibleReturnTypesMessage = LambdaHighlightingUtil
- .checkReturnTypeCompatible(expression, LambdaUtil.getFunctionalInterfaceReturnType(functionalInterfaceType));
- if (incompatibleReturnTypesMessage != null) {
- HighlightInfo result = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression)
- .descriptionAndTooltip(incompatibleReturnTypesMessage).create();
- myHolder.add(result);
- }
- else {
- final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
- final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(resolveResult);
- if (interfaceMethod != null) {
- final PsiParameter[] parameters = interfaceMethod.getParameterList().getParameters();
- final PsiParameter[] lambdaParameters = expression.getParameterList().getParameters();
- final String incompatibleTypesMessage = "Incompatible parameter types in lambda expression";
- if (lambdaParameters.length != parameters.length) {
- HighlightInfo result = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression)
- .descriptionAndTooltip(incompatibleTypesMessage).create();
- myHolder.add(result);
- }
- else {
- for (int i = 0; i < lambdaParameters.length; i++) {
- PsiParameter lambdaParameter = lambdaParameters[i];
- if (!TypeConversionUtil.isAssignable(lambdaParameter.getType(),
- GenericsUtil.eliminateWildcards(
- LambdaUtil.getSubstitutor(interfaceMethod, resolveResult)
- .substitute(parameters[i].getType())))) {
- HighlightInfo result = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(lambdaParameter)
- .descriptionAndTooltip(incompatibleTypesMessage).create();
- myHolder.add(result);
- break;
- }
- }
- }
- if (!myHolder.hasErrorResults()) {
- final PsiClass samClass = resolveResult.getElement();
- if (!PsiUtil.isAccessible(myFile.getProject(), samClass, expression, null)) {
- myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression)
- .descriptionAndTooltip(HighlightUtil.buildProblemWithAccessDescription(expression, resolveResult)).create());
- }
- }
- }
- }
- }
- }
- }
- }
- else {
- HighlightInfo result = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression)
- .descriptionAndTooltip("Lambda expression not expected here").create();
- myHolder.add(result);
- }
- if (!myHolder.hasErrorResults()) {
- final PsiElement body = expression.getBody();
- if (body instanceof PsiCodeBlock) {
- myHolder.add(HighlightControlFlowUtil.checkUnreachableStatement((PsiCodeBlock)body));
- }
- }
- }
- }
-
- @Override
- public void visitBreakStatement(PsiBreakStatement statement) {
- super.visitBreakStatement(statement);
- if (!myHolder.hasErrorResults()) {
- myHolder.add(HighlightUtil.checkLabelDefined(statement.getLabelIdentifier(), statement.findExitedStatement()));
- }
- }
-
- @Override
- public void visitClass(PsiClass aClass) {
- super.visitClass(aClass);
- if (aClass instanceof PsiSyntheticClass) return;
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkInterfaceMultipleInheritance(aClass));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkDuplicateTopLevelClass(aClass));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkEnumMustNotBeLocal(aClass));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkImplicitThisReferenceBeforeSuper(aClass, myJavaSdkVersion));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkClassAndPackageConflict(aClass));
- }
-
- @Override
- public void visitClassInitializer(PsiClassInitializer initializer) {
- super.visitClassInitializer(initializer);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightControlFlowUtil.checkInitializerCompleteNormally(initializer));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightControlFlowUtil.checkUnreachableStatement(initializer.getBody()));
- if (!myHolder.hasErrorResults()) {
- myHolder.add(HighlightClassUtil.checkThingNotAllowedInInterface(initializer, initializer.getContainingClass()));
- }
- }
-
- @Override
- public void visitClassObjectAccessExpression(PsiClassObjectAccessExpression expression) {
- super.visitClassObjectAccessExpression(expression);
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkClassObjectAccessExpression(expression));
- }
-
- @Override
- public void visitComment(PsiComment comment) {
- super.visitComment(comment);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkUnclosedComment(comment));
- if (myRefCountHolder != null && !myHolder.hasErrorResults()) registerReferencesFromInjectedFragments(comment);
- }
-
- @Override
- public void visitContinueStatement(PsiContinueStatement statement) {
- super.visitContinueStatement(statement);
- if (!myHolder.hasErrorResults()) {
- myHolder.add(HighlightUtil.checkLabelDefined(statement.getLabelIdentifier(), statement.findContinuedStatement()));
- }
- }
-
- @Override
- public void visitJavaToken(PsiJavaToken token) {
- super.visitJavaToken(token);
- if (!myHolder.hasErrorResults()
- && token.getTokenType() == JavaTokenType.RBRACE
- && token.getParent() instanceof PsiCodeBlock) {
-
- final PsiElement gParent = token.getParent().getParent();
- final PsiCodeBlock codeBlock;
- final PsiType returnType;
- if (gParent instanceof PsiMethod) {
- PsiMethod method = (PsiMethod)gParent;
- codeBlock = method.getBody();
- returnType = method.getReturnType();
- }
- else if (gParent instanceof PsiLambdaExpression) {
- final PsiElement body = ((PsiLambdaExpression)gParent).getBody();
- if (!(body instanceof PsiCodeBlock)) return;
- codeBlock = (PsiCodeBlock)body;
- returnType = LambdaUtil.getFunctionalInterfaceReturnType((PsiLambdaExpression)gParent);
- }
- else {
- return;
- }
- myHolder.add(HighlightControlFlowUtil.checkMissingReturnStatement(codeBlock, returnType));
- }
- }
-
- @Override
- public void visitDocComment(PsiDocComment comment) {
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkUnclosedComment(comment));
- }
-
- @Override
- public void visitDocTagValue(PsiDocTagValue value) {
- PsiReference reference = value.getReference();
- if (reference != null) {
- PsiElement element = reference.resolve();
- final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
- if (element instanceof PsiMethod) {
- myHolder.add(HighlightNamesUtil.highlightMethodName((PsiMethod)element, ((PsiDocMethodOrFieldRef)value).getNameElement(), false,
- colorsScheme));
- }
- else if (element instanceof PsiParameter) {
- myHolder.add(HighlightNamesUtil.highlightVariableName((PsiVariable)element, value.getNavigationElement(), colorsScheme));
- }
- }
- }
-
- @Override
- public void visitEnumConstant(PsiEnumConstant enumConstant) {
- super.visitEnumConstant(enumConstant);
- if (!myHolder.hasErrorResults()) GenericsHighlightUtil.checkEnumConstantForConstructorProblems(enumConstant, myHolder, myJavaSdkVersion);
- if (!myHolder.hasErrorResults()) registerConstructorCall(enumConstant);
- }
-
- @Override
- public void visitEnumConstantInitializer(PsiEnumConstantInitializer enumConstantInitializer) {
- super.visitEnumConstantInitializer(enumConstantInitializer);
- if (!myHolder.hasErrorResults()) {
- TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(enumConstantInitializer);
- myHolder.add(HighlightClassUtil.checkClassMustBeAbstract(enumConstantInitializer, textRange));
- }
- }
-
- @Override
- public void visitExpression(PsiExpression expression) {
- ProgressManager.checkCanceled(); // visitLiteralExpression is invoked very often in array initializers
-
- super.visitExpression(expression);
- PsiType type = expression.getType();
- if (myHolder.add(HighlightUtil.checkMustBeBoolean(expression, type))) return;
-
- if (expression instanceof PsiArrayAccessExpression) {
- myHolder.add(HighlightUtil.checkValidArrayAccessExpression((PsiArrayAccessExpression)expression));
- }
-
- if (expression.getParent() instanceof PsiNewExpression
- && ((PsiNewExpression)expression.getParent()).getQualifier() != expression
- && ((PsiNewExpression)expression.getParent()).getArrayInitializer() != expression) {
- // like in 'new String["s"]'
- myHolder.add(HighlightUtil.checkAssignability(PsiType.INT, expression.getType(), expression, expression));
- }
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightControlFlowUtil.checkCannotWriteToFinal(expression,myFile));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkVariableExpected(expression));
- if (!myHolder.hasErrorResults()) myHolder.addAll(HighlightUtil.checkArrayInitializer(expression, type));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkTernaryOperatorConditionIsBoolean(expression, type));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkAssertOperatorTypes(expression, type));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkSynchronizedExpressionType(expression, type,myFile));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkConditionalExpressionBranchTypesMatch(expression, type));
- if (!myHolder.hasErrorResults()
- && expression.getParent() instanceof PsiThrowStatement
- && ((PsiThrowStatement)expression.getParent()).getException() == expression) {
- myHolder.add(HighlightUtil.checkMustBeThrowable(type, expression, true));
- }
-
- if (!myHolder.hasErrorResults()) {
- myHolder.add(AnnotationsHighlightUtil.checkConstantExpression(expression));
- }
- }
-
- @Override
- public void visitField(PsiField field) {
- super.visitField(field);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightControlFlowUtil.checkFinalFieldInitialized(field));
- }
-
- @Override
- public void visitForStatement(PsiForStatement statement) {
- myHolder.add(HighlightUtil.checkForStatement(statement));
- }
-
- @Override
- public void visitForeachStatement(final PsiForeachStatement statement) {
- myHolder.add(HighlightUtil.checkForEachFeature(statement, myLanguageLevel, myFile));
- }
-
- @Override
- public void visitImportStaticStatement(final PsiImportStaticStatement statement) {
- myHolder.add(HighlightUtil.checkStaticImportFeature(statement, myLanguageLevel, myFile));
- }
-
- @Override
- public void visitIdentifier(final PsiIdentifier identifier) {
- TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
-
- PsiElement parent = identifier.getParent();
- if (parent instanceof PsiVariable) {
- PsiVariable variable = (PsiVariable)parent;
- myHolder.add(HighlightUtil.checkVariableAlreadyDefined(variable));
-
- if (variable.getInitializer() == null) {
- final PsiElement child = variable.getLastChild();
- if (child instanceof PsiErrorElement && child.getPrevSibling() == identifier) return;
- }
-
- boolean isMethodParameter = variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiMethod;
- if (!isMethodParameter) { // method params are highlighted in visitMethod since we should make sure the method body was visited before
- if (HighlightControlFlowUtil.isReassigned(variable, myFinalVarProblems)) {
- myHolder.add(HighlightNamesUtil.highlightReassignedVariable(variable, identifier));
- }
- else {
- myHolder.add(HighlightNamesUtil.highlightVariableName(variable, identifier, colorsScheme));
- }
- }
- else {
- myReassignedParameters.put((PsiParameter)variable, 1); // mark param as present in current file
- }
-
- myHolder.add(HighlightUtil.checkUnderscore(identifier, variable));
- }
- else if (parent instanceof PsiClass) {
- PsiClass aClass = (PsiClass)parent;
- if (aClass.isAnnotationType()) {
- myHolder.add(HighlightUtil.checkAnnotationFeature(identifier, myLanguageLevel, myFile));
- }
-
- myHolder.add(HighlightClassUtil.checkClassAlreadyImported(aClass, identifier));
- if (!(parent instanceof PsiAnonymousClass) && aClass.getNameIdentifier() == identifier) {
- myHolder.add(HighlightNamesUtil.highlightClassName(aClass, identifier, colorsScheme));
- }
- }
- else if (parent instanceof PsiMethod) {
- PsiMethod method = (PsiMethod)parent;
- if (method.isConstructor()) {
- myHolder.add(HighlightMethodUtil.checkConstructorName(method));
- }
- myHolder.add(HighlightNamesUtil.highlightMethodName(method, identifier, true, colorsScheme));
- }
- else {
- visitParentReference(parent);
- }
-
- super.visitIdentifier(identifier);
- }
-
- private void visitParentReference(PsiElement parent) {
- if (parent instanceof PsiJavaCodeReferenceElement && !(parent.getParent() instanceof PsiJavaCodeReferenceElement) &&
- !((PsiJavaCodeReferenceElement)parent).isQualified()) {
- PsiJavaCodeReferenceElement ref = (PsiJavaCodeReferenceElement)parent;
- JavaResolveResult result;
- try {
- result = ref.advancedResolve(true);
- }
- catch (IndexNotReadyException e) {
- return;
- }
- myHolder.add(HighlightUtil.checkReference(ref, result, myFile, myLanguageLevel));
- if (myRefCountHolder != null) {
- myRefCountHolder.registerReference(ref, result);
- }
- }
- }
-
- @Override
- public void visitImportStatement(final PsiImportStatement statement) {
- if (!myHolder.hasErrorResults()) {
- myHolder.add(HighlightUtil.checkSingleImportClassConflict(statement, mySingleImportedClasses,myFile));
- }
- }
-
- @Override
- public void visitImportStaticReferenceElement(final PsiImportStaticReferenceElement ref) {
- final String refName = ref.getReferenceName();
- final JavaResolveResult[] results = ref.multiResolve(false);
-
- if (results.length == 0) {
- final String description = JavaErrorMessages.message("cannot.resolve.symbol", refName);
- final PsiElement nameElement = ref.getReferenceNameElement();
- assert nameElement != null : ref;
- final HighlightInfo info =
- HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(nameElement).descriptionAndTooltip(description).create();
- QuickFixAction.registerQuickFixAction(info, SetupJDKFix.getInstance());
- myHolder.add(info);
- }
- else {
- final PsiManager manager = ref.getManager();
- for (JavaResolveResult result : results) {
- final PsiElement element = result.getElement();
-
- String description = null;
- if (element instanceof PsiClass) {
- final Pair<PsiImportStaticReferenceElement, PsiClass> imported = mySingleImportedClasses.get(refName);
- final PsiClass aClass = imported == null ? null : imported.getSecond();
- if (aClass != null && !manager.areElementsEquivalent(aClass, element)) {
- description = imported.first == null
- ? JavaErrorMessages.message("single.import.class.conflict", refName)
- : imported.first.equals(ref)
- ? JavaErrorMessages.message("class.is.ambiguous.in.single.static.import", refName)
- : JavaErrorMessages.message("class.is.already.defined.in.single.static.import", refName);
- }
- mySingleImportedClasses.put(refName, Pair.create(ref, (PsiClass)element));
- }
- else if (element instanceof PsiField) {
- final Pair<PsiImportStaticReferenceElement, PsiField> imported = mySingleImportedFields.get(refName);
- final PsiField field = imported == null ? null : imported.getSecond();
- if (field != null && !manager.areElementsEquivalent(field, element)) {
- description = imported.first.equals(ref)
- ? JavaErrorMessages.message("field.is.ambiguous.in.single.static.import", refName)
- : JavaErrorMessages.message("field.is.already.defined.in.single.static.import", refName);
- }
- mySingleImportedFields.put(refName, Pair.create(ref, (PsiField)element));
- }
-
- if (description != null) {
- myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref).descriptionAndTooltip(description).create());
- }
- }
- }
- }
-
- @Override
- public void visitInstanceOfExpression(PsiInstanceOfExpression expression) {
- super.visitInstanceOfExpression(expression);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkInstanceOfApplicable(expression));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkInstanceOfGenericType(expression));
- }
-
- @Override
- public void visitKeyword(PsiKeyword keyword) {
- super.visitKeyword(keyword);
- PsiElement parent = keyword.getParent();
- String text = keyword.getText();
- if (parent instanceof PsiModifierList) {
- PsiModifierList psiModifierList = (PsiModifierList)parent;
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkNotAllowedModifier(keyword, psiModifierList));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkIllegalModifierCombination(keyword, psiModifierList));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkPublicClassInRightFile(keyword, psiModifierList));
- if (PsiModifier.ABSTRACT.equals(text) && psiModifierList.getParent() instanceof PsiMethod) {
- if (!myHolder.hasErrorResults()) {
- myHolder.add(HighlightMethodUtil.checkAbstractMethodInConcreteClass((PsiMethod)psiModifierList.getParent(), keyword));
- }
- }
- }
- else if (PsiKeyword.CONTINUE.equals(text) && parent instanceof PsiContinueStatement) {
- PsiContinueStatement statement = (PsiContinueStatement)parent;
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkContinueOutsideLoop(statement));
- }
- else if (PsiKeyword.BREAK.equals(text) && parent instanceof PsiBreakStatement) {
- PsiBreakStatement statement = (PsiBreakStatement)parent;
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkBreakOutsideLoop(statement));
- }
- else if (PsiKeyword.INTERFACE.equals(text) && parent instanceof PsiClass) {
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkInterfaceCannotBeLocal((PsiClass)parent));
- }
- else {
- visitParentReference(parent);
- }
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkStaticDeclarationInInnerClass(keyword));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkIllegalVoidType(keyword));
-
- if (PsiTreeUtil.getParentOfType(keyword, PsiDocTagValue.class) != null) {
- HighlightInfo result = HighlightInfo.newHighlightInfo(JavaHighlightInfoTypes.JAVA_KEYWORD).range(keyword).create();
- myHolder.add(result);
- }
- }
-
- @Override
- public void visitLabeledStatement(PsiLabeledStatement statement) {
- super.visitLabeledStatement(statement);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkLabelWithoutStatement(statement));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkLabelAlreadyInUse(statement));
- }
-
- @Override
- public void visitLiteralExpression(PsiLiteralExpression expression) {
- super.visitLiteralExpression(expression);
- if (myHolder.hasErrorResults()) return;
- myHolder.add(HighlightUtil.checkLiteralExpressionParsingError(expression, myLanguageLevel,myFile));
- if (myRefCountHolder != null && !myHolder.hasErrorResults()) registerReferencesFromInjectedFragments(expression);
- }
-
- @Override
- public void visitMethod(PsiMethod method) {
- super.visitMethod(method);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightControlFlowUtil.checkUnreachableStatement(method.getBody()));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkConstructorHandleSuperClassExceptions(method));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkRecursiveConstructorInvocation(method));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkOverrideAnnotation(method, myLanguageLevel));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkSafeVarargsAnnotation(method));
- if (!myHolder.hasErrorResults() && method.isConstructor()) {
- myHolder.add(HighlightClassUtil.checkThingNotAllowedInInterface(method, method.getContainingClass()));
- }
-
- // method params are highlighted in visitMethod since we should make sure the method body was visited before
- PsiParameter[] parameters = method.getParameterList().getParameters();
- final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
-
- for (PsiParameter parameter : parameters) {
- int info = myReassignedParameters.get(parameter);
- if (info == 0) continue; // out of this file
- if (info == 2) {// reassigned
- myHolder.add(HighlightNamesUtil.highlightReassignedVariable(parameter, parameter.getNameIdentifier()));
- }
- else {
- myHolder.add(HighlightNamesUtil.highlightVariableName(parameter, parameter.getNameIdentifier(), colorsScheme));
- }
- }
- }
-
- private void highlightReferencedMethodOrClassName(PsiJavaCodeReferenceElement element, PsiElement resolved) {
- PsiElement parent = element.getParent();
- if (parent instanceof PsiReferenceExpression || parent instanceof PsiJavaCodeReferenceElement) {
- return;
- }
- final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
- if (parent instanceof PsiMethodCallExpression) {
- PsiMethod method = ((PsiMethodCallExpression)parent).resolveMethod();
- PsiElement methodNameElement = element.getReferenceNameElement();
- if (method != null && methodNameElement != null&& !(methodNameElement instanceof PsiKeyword)) {
- myHolder.add(HighlightNamesUtil.highlightMethodName(method, methodNameElement, false, colorsScheme));
- myHolder.add(HighlightNamesUtil.highlightClassNameInQualifier(element, colorsScheme));
- }
- }
- else if (parent instanceof PsiConstructorCall) {
- try {
- PsiMethod method = ((PsiConstructorCall)parent).resolveConstructor();
- if (method == null) {
- if (resolved instanceof PsiClass) {
- myHolder.add(HighlightNamesUtil.highlightClassName((PsiClass)resolved, element, colorsScheme));
- }
- }
- else {
- final PsiElement referenceNameElement = element.getReferenceNameElement();
- if(referenceNameElement != null) {
- // exclude type parameters from the highlighted text range
- TextRange range = new TextRange(element.getTextRange().getStartOffset(), referenceNameElement.getTextRange().getEndOffset());
- myHolder.add(HighlightNamesUtil.highlightMethodName(method, referenceNameElement, range, colorsScheme, false));
- }
- }
- }
- catch (IndexNotReadyException ignored) {
- }
- }
- else if (parent instanceof PsiImportStatement && ((PsiImportStatement)parent).isOnDemand()) {
- // highlight on demand import as class
- myHolder.add(HighlightNamesUtil.highlightClassName(null, element, colorsScheme));
- }
- else if (resolved instanceof PsiClass) {
- myHolder.add(HighlightNamesUtil.highlightClassName((PsiClass)resolved, element, colorsScheme));
- }
- }
-
- @Override
- public void visitMethodCallExpression(PsiMethodCallExpression expression) {
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkEnumSuperConstructorCall(expression));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkSuperQualifierType(myFile.getProject(), expression));
- // in case of JSP synthetic method call, do not check
- if (myFile.isPhysical() && !myHolder.hasErrorResults()) {
- try {
- myHolder.add(HighlightMethodUtil.checkMethodCall(expression, myResolveHelper, myLanguageLevel,myJavaSdkVersion));
- }
- catch (IndexNotReadyException ignored) {
- }
- }
-
- if (!myHolder.hasErrorResults()) visitExpression(expression);
- }
-
- @Override
- public void visitModifierList(PsiModifierList list) {
- super.visitModifierList(list);
- PsiElement parent = list.getParent();
- if (parent instanceof PsiMethod) {
- PsiMethod method = (PsiMethod)parent;
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkMethodCanHaveBody(method, myLanguageLevel,myFile));
- MethodSignatureBackedByPsiMethod methodSignature = MethodSignatureBackedByPsiMethod.create(method, PsiSubstitutor.EMPTY);
- if (!method.isConstructor()) {
- try {
- List<HierarchicalMethodSignature> superMethodSignatures = method.getHierarchicalMethodSignature().getSuperSignatures();
- if (!superMethodSignatures.isEmpty()) {
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkMethodIncompatibleReturnType(methodSignature, superMethodSignatures, true));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkMethodIncompatibleThrows(methodSignature, superMethodSignatures, true, method.getContainingClass()));
- if (!method.hasModifierProperty(PsiModifier.STATIC)) {
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkMethodWeakerPrivileges(methodSignature, superMethodSignatures, true, myFile));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkMethodOverridesFinal(methodSignature, superMethodSignatures));
- }
- }
- }
- catch (IndexNotReadyException ignored) {
- }
- }
- PsiClass aClass = method.getContainingClass();
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkMethodMustHaveBody(method, aClass));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkDuplicateMethod(aClass, method, getDuplicateMethods(aClass)));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkConstructorCallsBaseClassConstructor(method, myRefCountHolder, myResolveHelper));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkStaticMethodOverride(method,myFile));
- }
- else if (parent instanceof PsiClass) {
- PsiClass aClass = (PsiClass)parent;
- try {
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkDuplicateNestedClass(aClass));
- if (!myHolder.hasErrorResults()) {
- TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
- myHolder.add(HighlightClassUtil.checkClassMustBeAbstract(aClass, textRange));
- }
- if (!myHolder.hasErrorResults()) {
- myHolder.add(HighlightClassUtil.checkClassDoesNotCallSuperConstructorOrHandleExceptions(aClass, myRefCountHolder, myResolveHelper));
- }
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkOverrideEquivalentInheritedMethods(aClass, myFile));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkOverrideEquivalentMethods(aClass));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkCyclicInheritance(aClass));
- }
- catch (IndexNotReadyException ignored) {
- }
- }
- else if (parent instanceof PsiEnumConstant) {
- if (!myHolder.hasErrorResults()) myHolder.addAll(GenericsHighlightUtil.checkEnumConstantModifierList(list));
- }
- }
-
- @Override
- public void visitNameValuePair(PsiNameValuePair pair) {
- myHolder.add(AnnotationsHighlightUtil.checkNameValuePair(pair));
- if (!myHolder.hasErrorResults()) {
- PsiIdentifier nameId = pair.getNameIdentifier();
- if (nameId != null) {
- HighlightInfo result = HighlightInfo.newHighlightInfo(HighlightInfoType.ANNOTATION_ATTRIBUTE_NAME).range(nameId).create();
- myHolder.add(result);
- }
- }
- }
-
- @Override
- public void visitNewExpression(PsiNewExpression expression) {
- myHolder.add(HighlightUtil.checkUnhandledExceptions(expression, null));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkAnonymousInheritFinal(expression));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkQualifiedNew(expression));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkCreateInnerClassFromStaticContext(expression));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkTypeParameterInstantiation(expression));
- try {
- if (!myHolder.hasErrorResults()) HighlightMethodUtil.checkNewExpression(expression, myHolder, myJavaSdkVersion);
- }
- catch (IndexNotReadyException ignored) {
- }
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkEnumInstantiation(expression));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkGenericArrayCreation(expression, expression.getType()));
- if (!myHolder.hasErrorResults()) registerConstructorCall(expression);
-
- if (!myHolder.hasErrorResults()) visitExpression(expression);
- }
-
- @Override
- public void visitPackageStatement(PsiPackageStatement statement) {
- super.visitPackageStatement(statement);
- myHolder.add(AnnotationsHighlightUtil.checkPackageAnnotationContainingFile(statement));
- }
-
- @Override
- public void visitParameter(PsiParameter parameter) {
- super.visitParameter(parameter);
-
- final PsiElement parent = parameter.getParent();
- if (parent instanceof PsiParameterList) {
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkVarArgParameterIsLast(parameter,
- myLanguageLevel,myFile));
- }
- else if (parent instanceof PsiForeachStatement) {
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkForeachLoopParameterType((PsiForeachStatement)parent));
- }
- else if (parent instanceof PsiCatchSection) {
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkMultiCatchFeature(parameter, myLanguageLevel,myFile));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkCatchParameterIsThrowable(parameter));
- if (!myHolder.hasErrorResults()) myHolder.addAll(GenericsHighlightUtil.checkCatchParameterIsClass(parameter));
- if (!myHolder.hasErrorResults()) myHolder.addAll(HighlightUtil.checkCatchTypeIsDisjoint(parameter));
- }
- }
-
- @Override
- public void visitParameterList(PsiParameterList list) {
- super.visitParameterList(list);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkAnnotationMethodParameters(list));
- }
-
- @Override
- public void visitPostfixExpression(PsiPostfixExpression expression) {
- super.visitPostfixExpression(expression);
- if (!myHolder.hasErrorResults()) {
- myHolder.add(HighlightUtil.checkUnaryOperatorApplicable(expression.getOperationSign(), expression.getOperand()));
- }
- }
-
- @Override
- public void visitPrefixExpression(PsiPrefixExpression expression) {
- super.visitPrefixExpression(expression);
- if (!myHolder.hasErrorResults()) {
- myHolder.add(HighlightUtil.checkUnaryOperatorApplicable(expression.getOperationSign(), expression.getOperand()));
- }
- }
-
- private void registerConstructorCall(PsiConstructorCall constructorCall) {
- if (myRefCountHolder != null) {
- JavaResolveResult resolveResult = constructorCall.resolveMethodGenerics();
- final PsiElement resolved = resolveResult.getElement();
- if (resolved instanceof PsiNamedElement) {
- myRefCountHolder.registerLocallyReferenced((PsiNamedElement)resolved);
- }
- }
- }
-
- @Override
- public void visitReferenceElement(PsiJavaCodeReferenceElement ref) {
- doVisitReferenceElement(ref);
- }
-
- private JavaResolveResult doVisitReferenceElement(@NotNull PsiJavaCodeReferenceElement ref) {
- JavaResolveResult result;
- try {
- if (ref instanceof PsiReferenceExpressionImpl) {
- PsiReferenceExpressionImpl referenceExpression = (PsiReferenceExpressionImpl)ref;
- JavaResolveResult[] results = JavaResolveUtil.resolveWithContainingFile(referenceExpression,
- PsiReferenceExpressionImpl.OurGenericsResolver.INSTANCE,
- true, true,
- myFile);
- result = results.length == 1 ? results[0] : JavaResolveResult.EMPTY;
- }
- else {
- result = ref.advancedResolve(true);
- }
- }
- catch (IndexNotReadyException e) {
- return null;
- }
- PsiElement resolved = result.getElement();
- PsiElement parent = ref.getParent();
-
- if (parent instanceof PsiJavaCodeReferenceElement || ref.isQualified()) {
- if (myRefCountHolder != null) {
- myRefCountHolder.registerReference(ref, result);
- }
- myHolder.add(HighlightUtil.checkReference(ref, result, myFile, myLanguageLevel));
- if (!myHolder.hasErrorResults() && resolved instanceof PsiTypeParameter) {
- boolean cannotSelectFromTypeParameter = !myJavaSdkVersion.isAtLeast(JavaSdkVersion.JDK_1_7);
- if (!cannotSelectFromTypeParameter) {
- final PsiClass containingClass = PsiTreeUtil.getParentOfType(ref, PsiClass.class);
- if (containingClass != null) {
- if (PsiTreeUtil.isAncestor(containingClass.getExtendsList(), ref, false) ||
- PsiTreeUtil.isAncestor(containingClass.getImplementsList(), ref, false)) {
- cannotSelectFromTypeParameter = true;
- }
- }
- }
- if (cannotSelectFromTypeParameter) {
- myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).descriptionAndTooltip("Cannot select from a type parameter").range(ref).create());
- }
- }
- }
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkAbstractInstantiation(ref, resolved));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkExtendsDuplicate(ref, resolved,myFile));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkClassExtendsForeignInnerClass(ref, resolved));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkSelectStaticClassFromParameterizedType(resolved, ref));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkParameterizedReferenceTypeArguments(resolved, ref,
- result.getSubstitutor(),
- myJavaSdkVersion));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkCannotPassInner(ref));
-
- if (resolved != null && parent instanceof PsiReferenceList) {
- if (!myHolder.hasErrorResults()) {
- PsiReferenceList referenceList = (PsiReferenceList)parent;
- myHolder.add(HighlightUtil.checkElementInReferenceList(ref, referenceList, result, myLanguageLevel));
- }
- }
-
- if (parent instanceof PsiAnonymousClass && ref.equals(((PsiAnonymousClass)parent).getBaseClassReference())) {
- myHolder.add(GenericsHighlightUtil.checkOverrideEquivalentMethods((PsiClass)parent));
- }
-
- if (resolved instanceof PsiVariable) {
- PsiVariable variable = (PsiVariable)resolved;
-
- final PsiClass containingClass = PsiTreeUtil.getParentOfType(ref, PsiClass.class);
- if (containingClass instanceof PsiAnonymousClass &&
- !PsiTreeUtil.isAncestor(containingClass, variable, false) &&
- !(variable instanceof PsiField)) {
- if (!PsiTreeUtil.isAncestor(((PsiAnonymousClass) containingClass).getArgumentList(), ref, false)) {
- myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.IMPLICIT_ANONYMOUS_CLASS_PARAMETER).range(ref).create());
- }
- }
-
- if (variable instanceof PsiParameter && ref instanceof PsiExpression && PsiUtil.isAccessedForWriting((PsiExpression)ref)) {
- myReassignedParameters.put((PsiParameter)variable, 2);
- }
-
- final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
- if (!variable.hasModifierProperty(PsiModifier.FINAL) && isReassigned(variable)) {
- myHolder.add(HighlightNamesUtil.highlightReassignedVariable(variable, ref));
- }
- else {
- myHolder.add(HighlightNamesUtil.highlightVariableName(variable, ref.getReferenceNameElement(), colorsScheme));
- }
- myHolder.add(HighlightNamesUtil.highlightClassNameInQualifier(ref, colorsScheme));
- }
- else {
- highlightReferencedMethodOrClassName(ref, resolved);
- }
-
- if (parent instanceof PsiNewExpression && !(resolved instanceof PsiClass) && resolved instanceof PsiNamedElement && ((PsiNewExpression)parent).getClassOrAnonymousClassReference() == ref) {
- myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref)
- .descriptionAndTooltip("Cannot find symbol " + ((PsiNamedElement)resolved).getName()).create());
- }
- if (!myHolder.hasErrorResults() && resolved instanceof PsiClass) {
- final PsiClass aClass = ((PsiClass)resolved).getContainingClass();
- if (aClass != null) {
- final PsiElement qualifier = ref.getQualifier();
- final PsiElement place;
- if (qualifier instanceof PsiJavaCodeReferenceElement) {
- place = ((PsiJavaCodeReferenceElement)qualifier).resolve();
- }
- else {
- if (parent instanceof PsiNewExpression) {
- final PsiExpression newQualifier = ((PsiNewExpression)parent).getQualifier();
- place = newQualifier == null ? ref : PsiUtil.resolveClassInType(newQualifier.getType());
- }
- else {
- place = ref;
- }
- }
- if (PsiTreeUtil.isAncestor(aClass, place, false) && aClass.hasTypeParameters()) {
- myHolder.add(HighlightClassUtil.checkCreateInnerClassFromStaticContext(ref, place, (PsiClass)resolved));
- }
- } else if (resolved instanceof PsiTypeParameter) {
- final PsiTypeParameterListOwner owner = ((PsiTypeParameter)resolved).getOwner();
- if (owner instanceof PsiClass) {
- final PsiClass outerClass = (PsiClass)owner;
- if (!HighlightClassUtil.hasEnclosingInstanceInScope(outerClass, ref, true, false)) {
- myHolder.add(HighlightClassUtil.reportIllegalEnclosingUsage(ref, aClass, (PsiClass)owner, ref));
- }
- }
- }
- }
-
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkPackageAndClassConflict(ref));
-
- return result;
- }
-
- @Override
- public void visitReferenceExpression(PsiReferenceExpression expression) {
- JavaResolveResult resultForIncompleteCode = doVisitReferenceElement(expression);
- if (!myHolder.hasErrorResults()) {
- visitExpression(expression);
- if (myHolder.hasErrorResults()) return;
- }
- JavaResolveResult result;
- JavaResolveResult[] results;
- try {
- if (expression instanceof PsiReferenceExpressionImpl) {
- PsiReferenceExpressionImpl referenceExpression = (PsiReferenceExpressionImpl)expression;
- results = JavaResolveUtil.resolveWithContainingFile(referenceExpression,
- PsiReferenceExpressionImpl.OurGenericsResolver.INSTANCE, true, true,
- myFile);
- }
- else {
- results = expression.multiResolve(true);
- }
- result = results.length == 1 ? results[0] : JavaResolveResult.EMPTY;
- }
- catch (IndexNotReadyException e) {
- return;
- }
- PsiElement resolved = result.getElement();
- if (resolved instanceof PsiVariable && resolved.getContainingFile() == expression.getContainingFile()) {
- if (!myHolder.hasErrorResults()) {
- try {
- myHolder.add(HighlightControlFlowUtil.checkVariableInitializedBeforeUsage(expression, (PsiVariable)resolved, myUninitializedVarProblems,myFile));
- }
- catch (IndexNotReadyException ignored) {
- }
- }
- PsiVariable variable = (PsiVariable)resolved;
- boolean isFinal = variable.hasModifierProperty(PsiModifier.FINAL);
- if (isFinal && !variable.hasInitializer()) {
- if (!myHolder.hasErrorResults()) {
- myHolder.add(HighlightControlFlowUtil.checkFinalVariableMightAlreadyHaveBeenAssignedTo(variable, expression, myFinalVarProblems));
- }
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightControlFlowUtil.checkFinalVariableInitializedInLoop(expression, resolved));
- }
- }
-
- PsiElement parent = expression.getParent();
- if (parent instanceof PsiMethodCallExpression && ((PsiMethodCallExpression)parent).getMethodExpression() == expression && (!result.isAccessible() || !result.isStaticsScopeCorrect())) {
- PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)parent;
- PsiExpressionList list = methodCallExpression.getArgumentList();
- if (!HighlightMethodUtil.isDummyConstructorCall(methodCallExpression, myResolveHelper, list, expression)) {
- try {
- HighlightInfo info = HighlightMethodUtil.checkAmbiguousMethodCall(expression, results, list, resolved, result, methodCallExpression, myResolveHelper);
- myHolder.add(info);
- }
- catch (IndexNotReadyException ignored) {
- }
- }
- }
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkExpressionRequired(expression, resultForIncompleteCode));
- if (!myHolder.hasErrorResults() && resolved instanceof PsiField) {
- try {
- myHolder.add(HighlightUtil.checkIllegalForwardReferenceToField(expression, (PsiField)resolved));
- }
- catch (IndexNotReadyException ignored) {
- }
- }
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkConstructorCallMustBeFirstStatement(expression));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkAccessStaticFieldFromEnumConstructor(expression, result));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkClassReferenceAfterQualifier(expression, resolved));
- }
-
- @Override
- public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) {
- myHolder.add(HighlightUtil.checkMethodReferencesFeature(expression, myLanguageLevel,myFile));
- JavaResolveResult result;
- try {
- result = expression.advancedResolve(true);
- }
- catch (IndexNotReadyException e) {
- return;
- }
- if (myRefCountHolder != null) {
- myRefCountHolder.registerReference(expression, result);
- }
- final PsiElement method = result.getElement();
- if (method != null && !result.isAccessible()) {
- final String accessProblem = HighlightUtil.buildProblemWithAccessDescription(expression, result);
- HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(accessProblem).create();
- myHolder.add(info);
- }
- if (!myHolder.hasErrorResults()) {
- final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
- if (functionalInterfaceType != null && LambdaUtil.dependsOnTypeParams(functionalInterfaceType, functionalInterfaceType, expression)) {
- HighlightInfo result1 =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip("Cyclic inference").create();
- myHolder.add(result1); //todo[ann] append not inferred type params info
- } else {
- final PsiElement referenceNameElement = expression.getReferenceNameElement();
- if (referenceNameElement instanceof PsiKeyword) {
- if (!PsiMethodReferenceUtil.isValidQualifier(expression)) {
- final PsiElement qualifier = expression.getQualifier();
- String description = "Cannot find class " + qualifier.getText();
- HighlightInfo result1 =
- HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(qualifier).descriptionAndTooltip(description).create();
- myHolder.add(result1);
- }
- }
- }
- if (!myHolder.hasErrorResults()) {
- final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
- final PsiClass psiClass = resolveResult.getElement();
- if (psiClass != null && !PsiUtil.isAccessible(myFile.getProject(), psiClass, expression, null)) {
- myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression)
- .descriptionAndTooltip(HighlightUtil.buildProblemWithAccessDescription(expression, resolveResult)).create());
- }
- }
- }
- if (!myHolder.hasErrorResults()) {
- myHolder.add(HighlightUtil.checkUnhandledExceptions(expression, expression.getTextRange()));
- }
- if (!myHolder.hasErrorResults()) {
- if (method instanceof PsiMethod && ((PsiMethod)method).hasModifierProperty(PsiModifier.ABSTRACT)) {
- final PsiElement qualifier = expression.getQualifier();
- if (qualifier instanceof PsiSuperExpression) {
- myHolder.add(HighlightInfo
- .newHighlightInfo(HighlightInfoType.ERROR)
- .range(expression.getReferenceNameElement())
- .descriptionAndTooltip("Abstract method '" + ((PsiMethod)method).getName() + "' cannot be accessed directly").create());
- }
- }
- }
- }
-
- @Override
- public void visitReferenceList(PsiReferenceList list) {
- if (list.getFirstChild() == null) return;
- PsiElement parent = list.getParent();
- if (!(parent instanceof PsiTypeParameter)) {
- myHolder.add(AnnotationsHighlightUtil.checkAnnotationDeclaration(parent, list));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkExtendsAllowed(list));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkImplementsAllowed(list));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkClassExtendsOnlyOneClass(list));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkGenericCannotExtendException(list));
- }
- }
-
- @Override
- public void visitReferenceParameterList(PsiReferenceParameterList list) {
- myHolder.add(GenericsHighlightUtil.checkParametersAllowed(list, myLanguageLevel,myFile));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkParametersOnRaw(list));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkRawOnParameterizedType(list));
- }
-
- @Override
- public void visitReturnStatement(PsiReturnStatement statement) {
- try {
- myHolder.add(HighlightUtil.checkReturnStatementType(statement));
- }
- catch (IndexNotReadyException ignore) {
- }
- }
-
- @Override
- public void visitStatement(PsiStatement statement) {
- super.visitStatement(statement);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkNotAStatement(statement));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkStatementPrependedWithCaseInsideSwitch(statement));
- }
-
- @Override
- public void visitSuperExpression(PsiSuperExpression expr) {
- myHolder.add(HighlightUtil.checkThisOrSuperExpressionInIllegalContext(expr, expr.getQualifier()));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkAbstractMethodDirectCall(expr));
- if (!myHolder.hasErrorResults()) visitExpression(expr);
- }
-
- @Override
- public void visitSwitchLabelStatement(PsiSwitchLabelStatement statement) {
- super.visitSwitchLabelStatement(statement);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkCaseStatement(statement));
- }
-
- @Override
- public void visitSwitchStatement(PsiSwitchStatement statement) {
- super.visitSwitchStatement(statement);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkSwitchSelectorType(statement));
- }
-
- @Override
- public void visitThisExpression(PsiThisExpression expr) {
- myHolder.add(HighlightUtil.checkThisOrSuperExpressionInIllegalContext(expr, expr.getQualifier()));
- if (!myHolder.hasErrorResults()) {
- myHolder.add(HighlightUtil.checkMemberReferencedBeforeConstructorCalled(expr, null, myFile));
- }
- if (!myHolder.hasErrorResults()) {
- visitExpression(expr);
- }
- }
-
- @Override
- public void visitThrowStatement(PsiThrowStatement statement) {
- myHolder.add(HighlightUtil.checkUnhandledExceptions(statement, null));
- if (!myHolder.hasErrorResults()) visitStatement(statement);
- }
-
- @Override
- public void visitTryStatement(PsiTryStatement statement) {
- super.visitTryStatement(statement);
- if (!myHolder.hasErrorResults()) {
- final Set<PsiClassType> thrownTypes = HighlightUtil.collectUnhandledExceptions(statement);
- for (PsiParameter parameter : statement.getCatchBlockParameters()) {
- boolean added = myHolder.addAll(HighlightUtil.checkExceptionAlreadyCaught(parameter));
- if (!added) {
- added = myHolder.addAll(HighlightUtil.checkExceptionThrownInTry(parameter, thrownTypes));
- }
- if (!added) {
- myHolder.addAll(HighlightUtil.checkWithImprovedCatchAnalysis(parameter, thrownTypes,myFile));
- }
- }
- }
- }
-
- @Override
- public void visitResourceVariable(final PsiResourceVariable resourceVariable) {
- visitVariable(resourceVariable);
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkTryWithResourcesFeature(resourceVariable, myLanguageLevel,myFile));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkTryResourceIsAutoCloseable(resourceVariable));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkUnhandledCloserExceptions(resourceVariable));
- }
-
- @Override
- public void visitTypeElement(final PsiTypeElement type) {
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkDiamondFeature(type, myLanguageLevel,myFile));
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkIllegalType(type));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkReferenceTypeUsedAsTypeArgument(type));
- if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkWildcardUsage(type));
- }
-
- @Override
- public void visitTypeCastExpression(PsiTypeCastExpression typeCast) {
- super.visitTypeCastExpression(typeCast);
- try {
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkInconvertibleTypeCast(typeCast));
- }
- catch (IndexNotReadyException ignore) {
- }
- }
-
- @Override
- public void visitTypeParameterList(PsiTypeParameterList list) {
- myHolder.add(GenericsHighlightUtil.checkTypeParametersList(list, myLanguageLevel,myFile));
- }
-
- @Override
- public void visitVariable(PsiVariable variable) {
- super.visitVariable(variable);
- try {
- if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkVariableInitializerType(variable));
- }
- catch (IndexNotReadyException ignored) {
- }
- }
-
- private boolean isReassigned(@NotNull PsiVariable variable) {
- try {
- boolean reassigned;
- if (variable instanceof PsiParameter) {
- reassigned = myReassignedParameters.get((PsiParameter)variable) == 2;
- }
- else {
- reassigned = HighlightControlFlowUtil.isReassigned(variable, myFinalVarProblems);
- }
-
- return reassigned;
- }
- catch (IndexNotReadyException e) {
- return false;
- }
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/IncreaseLanguageLevelFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/IncreaseLanguageLevelFix.java
deleted file mode 100644
index a3f235e8a638..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/IncreaseLanguageLevelFix.java
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.analysis;
-
-import com.intellij.codeInsight.CodeInsightBundle;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.openapi.application.ApplicationManager;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.module.Module;
-import com.intellij.openapi.module.ModuleUtilCore;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.projectRoots.JavaSdkVersion;
-import com.intellij.openapi.projectRoots.JdkVersionUtil;
-import com.intellij.openapi.projectRoots.Sdk;
-import com.intellij.openapi.roots.*;
-import com.intellij.openapi.vfs.VirtualFile;
-import com.intellij.pom.java.LanguageLevel;
-import com.intellij.psi.PsiFile;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-/**
- * @author cdr
- */
-public class IncreaseLanguageLevelFix implements IntentionAction {
- private static final Logger LOG = Logger.getInstance("#" + IncreaseLanguageLevelFix.class.getName());
-
- private final LanguageLevel myLevel;
-
- public IncreaseLanguageLevelFix(@NotNull LanguageLevel targetLevel) {
- myLevel = targetLevel;
- }
-
- @Override
- @NotNull
- public String getText() {
- return CodeInsightBundle.message("set.language.level.to.0", myLevel.getPresentableText());
- }
-
- @Override
- @NotNull
- public String getFamilyName() {
- return CodeInsightBundle.message("set.language.level");
- }
-
- private static boolean isJdkSupportsLevel(@Nullable final Sdk jdk, @NotNull LanguageLevel level) {
- if (jdk == null) return true;
- String versionString = jdk.getVersionString();
- JavaSdkVersion version = versionString == null ? null : JdkVersionUtil.getVersion(versionString);
- return version != null && version.getMaxLanguageLevel().isAtLeast(level);
- }
-
- @Override
- public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
- final VirtualFile virtualFile = file.getVirtualFile();
- if (virtualFile == null) return false;
- final Module module = ModuleUtilCore.findModuleForFile(virtualFile, project);
- return isLanguageLevelAcceptable(project, module, myLevel);
- }
-
- private static boolean isLanguageLevelAcceptable(@NotNull Project project, Module module, @NotNull LanguageLevel level) {
- return isJdkSupportsLevel(getRelevantJdk(project, module), level);
- }
-
- @Override
- public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
- final VirtualFile virtualFile = file.getVirtualFile();
- LOG.assertTrue(virtualFile != null);
- final Module module = ModuleUtilCore.findModuleForFile(virtualFile, project);
- final LanguageLevel moduleLevel = module == null ? null : LanguageLevelModuleExtension.getInstance(module).getLanguageLevel();
- if (moduleLevel != null && isLanguageLevelAcceptable(project, module, myLevel)) {
- final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();
- rootModel.getModuleExtension(LanguageLevelModuleExtension.class).setLanguageLevel(myLevel);
- ApplicationManager.getApplication().runWriteAction(new Runnable() {
- @Override
- public void run() {
- rootModel.commit();
- }
- });
- }
- else {
- LanguageLevelProjectExtension.getInstance(project).setLanguageLevel(myLevel);
- }
- }
-
- @Nullable
- private static Sdk getRelevantJdk(@NotNull Project project, @Nullable Module module) {
- Sdk projectJdk = ProjectRootManager.getInstance(project).getProjectSdk();
- Sdk moduleJdk = module == null ? null : ModuleRootManager.getInstance(module).getSdk();
- return moduleJdk == null ? projectJdk : moduleJdk;
- }
-
- @Override
- public boolean startInWriteAction() {
- return false;
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/QualifyWithThisFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/QualifyWithThisFix.java
deleted file mode 100644
index bfe2d8c45b33..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/QualifyWithThisFix.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright 2000-2011 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.analysis;
-
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.psi.*;
-import com.intellij.refactoring.util.RefactoringChangeUtil;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NotNull;
-
-/**
-* User: anna
-*/
-class QualifyWithThisFix implements IntentionAction {
- private final PsiClass myContainingClass;
- private final PsiElement myExpression;
-
- public QualifyWithThisFix(PsiClass containingClass, PsiElement expression) {
- myContainingClass = containingClass;
- myExpression = expression;
- }
-
- @NotNull
- @Override
- public String getText() {
- return "Qualify with " + myContainingClass.getName() + ".this";
- }
-
- @NotNull
- @Override
- public String getFamilyName() {
- return getText();
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- return true;
- }
-
- @Override
- public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
- final PsiThisExpression thisExpression =
- RefactoringChangeUtil.createThisExpression(PsiManager.getInstance(project), myContainingClass);
- ((PsiReferenceExpression)myExpression).setQualifierExpression(thisExpression);
- }
-
- @Override
- public boolean startInWriteAction() {
- return true;
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddMethodBodyFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddMethodBodyFix.java
index 235ff4dc8011..d1a4574ddbb6 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddMethodBodyFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddMethodBodyFix.java
@@ -33,7 +33,7 @@ public class AddMethodBodyFix implements IntentionAction {
private final PsiMethod myMethod;
- public AddMethodBodyFix(PsiMethod method) {
+ public AddMethodBodyFix(@NotNull PsiMethod method) {
myMethod = method;
}
@@ -51,11 +51,10 @@ public class AddMethodBodyFix implements IntentionAction {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- return myMethod != null
- && myMethod.isValid()
- && myMethod.getBody() == null
- && myMethod.getContainingClass() != null
- && myMethod.getManager().isInProject(myMethod);
+ return myMethod.isValid() &&
+ myMethod.getBody() == null &&
+ myMethod.getContainingClass() != null &&
+ myMethod.getManager().isInProject(myMethod);
}
@Override
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddReturnFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddReturnFix.java
index ec3ed7a87a58..bee9c89a9f8e 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddReturnFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddReturnFix.java
@@ -34,7 +34,7 @@ public class AddReturnFix implements IntentionAction {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.AddReturnFix");
private final PsiMethod myMethod;
- public AddReturnFix(PsiMethod method) {
+ public AddReturnFix(@NotNull PsiMethod method) {
myMethod = method;
}
@@ -52,11 +52,10 @@ public class AddReturnFix implements IntentionAction {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- return myMethod != null
- && myMethod.isValid()
- && myMethod.getManager().isInProject(myMethod)
- && myMethod.getBody() != null
- && myMethod.getBody().getRBrace() != null
+ return myMethod.isValid() &&
+ myMethod.getManager().isInProject(myMethod) &&
+ myMethod.getBody() != null &&
+ myMethod.getBody().getRBrace() != null
;
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddTypeArgumentsConditionalFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddTypeArgumentsConditionalFix.java
deleted file mode 100644
index 0057749d056b..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddTypeArgumentsConditionalFix.java
+++ /dev/null
@@ -1,153 +0,0 @@
-/*
- * Copyright 2000-2012 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.psi.*;
-import com.intellij.psi.impl.source.resolve.DefaultParameterTypeInferencePolicy;
-import com.intellij.psi.util.TypeConversionUtil;
-import com.intellij.refactoring.util.RefactoringUtil;
-import com.intellij.util.Function;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NotNull;
-
-/**
- * User: anna
- * Date: 2/17/12
- */
-public class AddTypeArgumentsConditionalFix implements IntentionAction {
- private static final Logger LOG = Logger.getInstance("#" + AddTypeArgumentsConditionalFix.class.getName());
-
- private final PsiSubstitutor mySubstitutor;
- private final PsiMethodCallExpression myExpression;
- private final PsiMethod myMethod;
-
- public AddTypeArgumentsConditionalFix(PsiSubstitutor substitutor, PsiMethodCallExpression expression, PsiMethod method) {
- mySubstitutor = substitutor;
- myExpression = expression;
- myMethod = method;
- }
-
- @NotNull
- @Override
- public String getText() {
- return "Add explicit type arguments";
- }
-
- @NotNull
- @Override
- public String getFamilyName() {
- return getText();
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- if (mySubstitutor.isValid() && myExpression.isValid() && myMethod.isValid()) {
- return true;
- }
- return false;
- }
-
- @Override
- public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
- final PsiTypeParameter[] typeParameters = myMethod.getTypeParameters();
- final String typeArguments = "<" + StringUtil.join(typeParameters, new Function<PsiTypeParameter, String>() {
- @Override
- public String fun(PsiTypeParameter parameter) {
- final PsiType substituteTypeParam = mySubstitutor.substitute(parameter);
- LOG.assertTrue(substituteTypeParam != null);
- return substituteTypeParam.getCanonicalText();
- }
- }, ", ") + ">";
- final PsiExpression expression = myExpression.getMethodExpression().getQualifierExpression();
- String withTypeArgsText;
- if (expression != null) {
- withTypeArgsText = expression.getText();
- }
- else {
- if (RefactoringUtil.isInStaticContext(myExpression, null) || myMethod.hasModifierProperty(PsiModifier.STATIC)) {
- final PsiClass aClass = myMethod.getContainingClass();
- LOG.assertTrue(aClass != null);
- withTypeArgsText = aClass.getQualifiedName();
- }
- else {
- withTypeArgsText = "this";
- }
- }
- withTypeArgsText += "." + typeArguments + myExpression.getMethodExpression().getReferenceName();
- final PsiExpression withTypeArgs = JavaPsiFacade.getElementFactory(project).createExpressionFromText(withTypeArgsText + myExpression.getArgumentList().getText(), myExpression);
- myExpression.replace(withTypeArgs);
- }
-
- @Override
- public boolean startInWriteAction() {
- return true;
- }
-
- public static void register(HighlightInfo highlightInfo, PsiExpression expression, @NotNull PsiType lType) {
- if (lType != PsiType.NULL && expression instanceof PsiConditionalExpression) {
- final PsiExpression thenExpression = ((PsiConditionalExpression)expression).getThenExpression();
- final PsiExpression elseExpression = ((PsiConditionalExpression)expression).getElseExpression();
- if (thenExpression != null && elseExpression != null) {
- final PsiType thenType = thenExpression.getType();
- final PsiType elseType = elseExpression.getType();
- if (thenType != null && elseType != null) {
- final boolean thenAssignable = TypeConversionUtil.isAssignable(lType, thenType);
- final boolean elseAssignable = TypeConversionUtil.isAssignable(lType, elseType);
- if (!thenAssignable && thenExpression instanceof PsiMethodCallExpression) {
- inferTypeArgs(highlightInfo, lType, thenExpression);
- }
- if (!elseAssignable && elseExpression instanceof PsiMethodCallExpression) {
- inferTypeArgs(highlightInfo, lType, elseExpression);
- }
- }
- }
- }
- }
-
- private static void inferTypeArgs(HighlightInfo highlightInfo, PsiType lType, PsiExpression thenExpression) {
- final JavaResolveResult result = ((PsiMethodCallExpression)thenExpression).resolveMethodGenerics();
- final PsiMethod method = (PsiMethod)result.getElement();
- if (method != null) {
- final PsiType returnType = method.getReturnType();
- final PsiClass aClass = method.getContainingClass();
- if (returnType != null && aClass != null && aClass.getQualifiedName() != null) {
- final JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(method.getProject());
- final PsiDeclarationStatement variableDeclarationStatement =
- javaPsiFacade.getElementFactory().createVariableDeclarationStatement("xxx", lType, thenExpression);
- final PsiExpression initializer =
- ((PsiLocalVariable)variableDeclarationStatement.getDeclaredElements()[0]).getInitializer();
- LOG.assertTrue(initializer != null);
-
- final PsiSubstitutor substitutor = javaPsiFacade.getResolveHelper()
- .inferTypeArguments(method.getTypeParameters(), method.getParameterList().getParameters(),
- ((PsiMethodCallExpression)thenExpression).getArgumentList().getExpressions(), PsiSubstitutor.EMPTY,
- initializer, DefaultParameterTypeInferencePolicy.INSTANCE);
- PsiType substitutedType = substitutor.substitute(returnType);
- if (substitutedType != null && TypeConversionUtil.isAssignable(lType, substitutedType)) {
- QuickFixAction.registerQuickFixAction(highlightInfo,
- thenExpression.getTextRange(),
- new AddTypeArgumentsConditionalFix(substitutor, (PsiMethodCallExpression)thenExpression, method));
- }
- }
- }
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddTypeArgumentsFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddTypeArgumentsFix.java
deleted file mode 100644
index 805a04528a94..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddTypeArgumentsFix.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.pom.java.LanguageLevel;
-import com.intellij.psi.*;
-import com.intellij.psi.util.PsiUtil;
-import com.intellij.refactoring.util.RefactoringChangeUtil;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NotNull;
-
-public class AddTypeArgumentsFix extends MethodArgumentFix {
- private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.AddTypeArgumentsFix");
-
- private AddTypeArgumentsFix(PsiExpressionList list, int i, PsiType toType, final ArgumentFixerActionFactory factory) {
- super(list, i, toType, factory);
- }
-
- @Override
- @NotNull
- public String getText() {
- if (myArgList.getExpressions().length == 1) {
- return QuickFixBundle.message("add.type.arguments.single.argument.text");
- }
-
- return QuickFixBundle.message("add.type.arguments.text", myIndex + 1);
- }
-
- private static class MyFixerActionFactory extends ArgumentFixerActionFactory {
- @Override
- public AddTypeArgumentsFix createFix(final PsiExpressionList list, final int i, final PsiType toType) {
- return new AddTypeArgumentsFix(list, i, toType, this);
- }
-
- @Override
- protected PsiExpression getModifiedArgument(final PsiExpression expression, final PsiType toType) throws IncorrectOperationException {
- if (!PsiUtil.isLanguageLevel5OrHigher(expression)) return null;
-
- if (expression instanceof PsiMethodCallExpression) {
- final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)expression;
- final PsiReferenceParameterList list = methodCall.getMethodExpression().getParameterList();
- if (list == null || list.getTypeArguments().length > 0) return null;
- final JavaResolveResult resolveResult = methodCall.resolveMethodGenerics();
- final PsiElement element = resolveResult.getElement();
- if (element instanceof PsiMethod) {
- final PsiMethod method = (PsiMethod)element;
- final PsiType returnType = method.getReturnType();
- if (returnType == null) return null;
-
- final PsiTypeParameter[] typeParameters = method.getTypeParameters();
- if (typeParameters.length > 0) {
- PsiType[] mappings = new PsiType[typeParameters.length];
- PsiResolveHelper helper = JavaPsiFacade.getInstance(expression.getProject()).getResolveHelper();
- LanguageLevel level = PsiUtil.getLanguageLevel(expression);
- for (int i = 0; i < typeParameters.length; i++) {
- PsiTypeParameter typeParameter = typeParameters[i];
- final PsiType substitution = helper.getSubstitutionForTypeParameter(typeParameter, returnType, toType, false, level);
- if (substitution == null || PsiType.NULL.equals(substitution)) return null;
- mappings[i] = GenericsUtil.eliminateWildcards(substitution);
- }
-
- final PsiElementFactory factory = JavaPsiFacade.getInstance(expression.getProject()).getElementFactory();
- PsiMethodCallExpression copy = (PsiMethodCallExpression)expression.copy();
- final PsiReferenceExpression methodExpression = copy.getMethodExpression();
- final PsiReferenceParameterList parameterList = methodExpression.getParameterList();
- LOG.assertTrue(parameterList != null);
- for (PsiType mapping : mappings) {
- parameterList.add(factory.createTypeElement(mapping));
- }
- if (methodExpression.getQualifierExpression() == null) {
- final PsiExpression qualifierExpression;
- final PsiClass containingClass = method.getContainingClass();
- LOG.assertTrue(containingClass != null);
- if (method.hasModifierProperty(PsiModifier.STATIC)) {
- qualifierExpression = factory.createReferenceExpression(containingClass);
- } else {
- qualifierExpression = RefactoringChangeUtil.createThisExpression(method.getManager(), null);
- }
- methodExpression.setQualifierExpression(qualifierExpression);
- }
-
- return copy;
- }
- }
- }
-
- return null;
- }
-
- @Override
- public boolean areTypesConvertible(final PsiType exprType, final PsiType parameterType, final PsiElement context) {
- return !(exprType instanceof PsiPrimitiveType) &&
- !(parameterType instanceof PsiPrimitiveType);
- }
- }
-
- public static ArgumentFixerActionFactory REGISTRAR = new AddTypeArgumentsFix.MyFixerActionFactory();
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddTypeCastFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddTypeCastFix.java
deleted file mode 100644
index b9c5d21bcd5e..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddTypeCastFix.java
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/*
- * Created by IntelliJ IDEA.
- * User: mike
- * Date: Jul 26, 2002
- * Time: 2:16:32 PM
- * To change template for new class use
- * Code Style | Class Templates options (Tools | IDE Options).
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.FileModificationService;
-import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.psi.*;
-import com.intellij.psi.codeStyle.CodeStyleManager;
-import com.intellij.psi.util.PsiUtil;
-import com.intellij.psi.util.TypeConversionUtil;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-public class AddTypeCastFix extends LocalQuickFixAndIntentionActionOnPsiElement {
- private final PsiType myType;
-
- public AddTypeCastFix(@NotNull PsiType type, @NotNull PsiExpression expression) {
- super(expression);
- myType = type;
- }
-
- @Override
- @NotNull
- public String getText() {
- return QuickFixBundle.message("add.typecast.text", myType.getCanonicalText());
- }
-
- @Override
- @NotNull
- public String getFamilyName() {
- return QuickFixBundle.message("add.typecast.family");
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project,
- @NotNull PsiFile file,
- @NotNull PsiElement startElement,
- @NotNull PsiElement endElement) {
- return myType.isValid() && startElement.isValid() && startElement.getManager().isInProject(startElement);
- }
-
- @Override
- public void invoke(@NotNull Project project,
- @NotNull PsiFile file,
- @Nullable("is null when called from inspection") Editor editor,
- @NotNull PsiElement startElement,
- @NotNull PsiElement endElement) {
- if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
- addTypeCast(project, (PsiExpression)startElement, myType);
- }
-
- private static void addTypeCast(Project project, PsiExpression originalExpression, PsiType type) throws IncorrectOperationException {
- PsiExpression typeCast = createCastExpression(originalExpression, project, type);
- originalExpression.replace(typeCast);
- }
-
- static PsiExpression createCastExpression(PsiExpression originalExpression, Project project, PsiType type) throws IncorrectOperationException {
- // remove nested casts
- PsiElement element = PsiUtil.deparenthesizeExpression(originalExpression);
- if (element == null){
- return null;
- }
- PsiElementFactory factory = JavaPsiFacade.getInstance(originalExpression.getProject()).getElementFactory();
-
- PsiTypeCastExpression typeCast = (PsiTypeCastExpression)factory.createExpressionFromText("(Type)value", null);
- typeCast = (PsiTypeCastExpression)CodeStyleManager.getInstance(project).reformat(typeCast);
- typeCast.getCastType().replace(factory.createTypeElement(type));
-
- if (element instanceof PsiConditionalExpression) {
- // we'd better cast one branch of ternary expression if we could
- PsiConditionalExpression expression = (PsiConditionalExpression)element.copy();
- PsiExpression thenE = expression.getThenExpression();
- PsiExpression elseE = expression.getElseExpression();
- PsiType thenType = thenE == null ? null : thenE.getType();
- PsiType elseType = elseE == null ? null : elseE.getType();
- if (elseType != null && thenType != null) {
- boolean replaceThen = !TypeConversionUtil.isAssignable(type, thenType);
- boolean replaceElse = !TypeConversionUtil.isAssignable(type, elseType);
- if (replaceThen != replaceElse) {
- if (replaceThen) {
- typeCast.getOperand().replace(thenE);
- thenE.replace(typeCast);
- }
- else {
- typeCast.getOperand().replace(elseE);
- elseE.replace(typeCast);
- }
- return expression;
- }
- }
- }
- typeCast.getOperand().replace(element);
- return typeCast;
- }
-
- @Override
- public boolean startInWriteAction() {
- return true;
- }
-
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddVariableInitializerFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddVariableInitializerFix.java
index 6bb1d537adfd..b1736eb96eb1 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddVariableInitializerFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddVariableInitializerFix.java
@@ -33,7 +33,7 @@ public class AddVariableInitializerFix implements IntentionAction {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.AddReturnFix");
private final PsiVariable myVariable;
- public AddVariableInitializerFix(PsiVariable variable) {
+ public AddVariableInitializerFix(@NotNull PsiVariable variable) {
myVariable = variable;
}
@@ -51,11 +51,10 @@ public class AddVariableInitializerFix implements IntentionAction {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- return myVariable != null
- && myVariable.isValid()
- && myVariable.getManager().isInProject(myVariable)
- && !myVariable.hasInitializer()
- && !(myVariable instanceof PsiParameter)
+ return myVariable.isValid() &&
+ myVariable.getManager().isInProject(myVariable) &&
+ !myVariable.hasInitializer() &&
+ !(myVariable instanceof PsiParameter)
;
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ArgumentFixerActionFactory.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ArgumentFixerActionFactory.java
deleted file mode 100644
index 76bf6ae59e1b..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ArgumentFixerActionFactory.java
+++ /dev/null
@@ -1,116 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.util.Comparing;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.psi.*;
-import com.intellij.psi.infos.CandidateInfo;
-import com.intellij.util.IncorrectOperationException;
-import gnu.trove.THashSet;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Set;
-
-/**
- * @author ven
- */
-public abstract class ArgumentFixerActionFactory {
- private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.ArgumentFixerActionFactory");
-
- @Nullable
- protected abstract PsiExpression getModifiedArgument(PsiExpression expression, final PsiType toType) throws IncorrectOperationException;
-
- public void registerCastActions(CandidateInfo[] candidates, PsiCall call, HighlightInfo highlightInfo, final TextRange fixRange) {
- if (candidates.length == 0) return;
- List<CandidateInfo> methodCandidates = new ArrayList<CandidateInfo>(Arrays.asList(candidates));
- PsiExpressionList list = call.getArgumentList();
- PsiExpression[] expressions = list.getExpressions();
- if (expressions.length == 0) return;
- // filter out not castable candidates
- nextMethod:
- for (int i = methodCandidates.size() - 1; i >= 0; i--) {
- CandidateInfo candidate = methodCandidates.get(i);
- PsiMethod method = (PsiMethod) candidate.getElement();
- PsiSubstitutor substitutor = candidate.getSubstitutor();
- assert method != null;
- PsiParameter[] parameters = method.getParameterList().getParameters();
- if (expressions.length != parameters.length) {
- methodCandidates.remove(i);
- continue;
- }
- for (int j = 0; j < parameters.length; j++) {
- PsiParameter parameter = parameters[j];
- PsiExpression expression = expressions[j];
- // check if we can cast to this method
- PsiType exprType = expression.getType();
- PsiType parameterType = substitutor.substitute(parameter.getType());
- if (exprType == null
- || parameterType == null
- || !areTypesConvertible(exprType, parameterType, call)) {
- methodCandidates.remove(i);
- continue nextMethod;
- }
- }
- }
-
- if (methodCandidates.isEmpty()) return;
-
- try {
- for (int i = 0; i < expressions.length; i++) {
- PsiExpression expression = expressions[i];
- PsiType exprType = expression.getType();
- Set<String> suggestedCasts = new THashSet<String>();
- // find to which type we can cast this param to get valid method call
- for (CandidateInfo candidate : methodCandidates) {
- PsiMethod method = (PsiMethod)candidate.getElement();
- PsiSubstitutor substitutor = candidate.getSubstitutor();
- assert method != null;
- PsiParameter[] parameters = method.getParameterList().getParameters();
- PsiType originalParameterType = parameters[i].getType();
- PsiType parameterType = substitutor.substitute(originalParameterType);
- if (parameterType instanceof PsiWildcardType) continue;
- if (!GenericsUtil.isFromExternalTypeLanguage(parameterType)) continue;
- if (suggestedCasts.contains(parameterType.getCanonicalText())) continue;
- // strict compare since even widening cast may help
- if (Comparing.equal(exprType, parameterType)) continue;
- PsiCall newCall = (PsiCall) call.copy();
- PsiExpression modifiedExpression = getModifiedArgument(expression, parameterType);
- if (modifiedExpression == null) continue;
- newCall.getArgumentList().getExpressions()[i].replace(modifiedExpression);
- JavaResolveResult resolveResult = newCall.resolveMethodGenerics();
- if (resolveResult.getElement() != null && resolveResult.isValidResult()) {
- suggestedCasts.add(parameterType.getCanonicalText());
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, createFix(list, i, parameterType));
- }
- }
- }
- }
- catch (IncorrectOperationException e) {
- LOG.error(e);
- }
- }
-
- public abstract boolean areTypesConvertible(final PsiType exprType, final PsiType parameterType, final PsiElement context);
-
- public abstract MethodArgumentFix createFix(final PsiExpressionList list, final int i, final PsiType parameterType);
-
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CastMethodArgumentFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CastMethodArgumentFix.java
deleted file mode 100644
index 04a7b58179b9..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CastMethodArgumentFix.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * Created by IntelliJ IDEA.
- * User: cdr
- * Date: Nov 13, 2002
- * Time: 3:26:50 PM
- * To change this template use Options | File Templates.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightUtil;
-import com.intellij.psi.*;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NotNull;
-
-public class CastMethodArgumentFix extends MethodArgumentFix {
- private CastMethodArgumentFix(PsiExpressionList list, int i, PsiType toType, final ArgumentFixerActionFactory factory) {
- super(list, i, toType, factory);
- }
-
- @Override
- @NotNull
- public String getText() {
- if (myArgList.getExpressions().length == 1) {
- return QuickFixBundle.message("cast.single.parameter.text", JavaHighlightUtil.formatType(myToType));
- }
-
- return QuickFixBundle.message("cast.parameter.text", myIndex + 1, JavaHighlightUtil.formatType(myToType));
- }
-
- private static class MyFixerActionFactory extends ArgumentFixerActionFactory {
- @Override
- public CastMethodArgumentFix createFix(final PsiExpressionList list, final int i, final PsiType toType) {
- return new CastMethodArgumentFix(list, i, toType, this);
- }
-
- @Override
- protected PsiExpression getModifiedArgument(final PsiExpression expression, PsiType toType) throws IncorrectOperationException {
- final PsiType exprType = expression.getType();
- if (exprType instanceof PsiClassType && toType instanceof PsiPrimitiveType) {
- toType = ((PsiPrimitiveType)toType).getBoxedType(expression);
- assert toType != null;
- }
- return AddTypeCastFix.createCastExpression(expression, expression.getProject(), toType);
- }
-
- @Override
- public boolean areTypesConvertible(PsiType exprType, PsiType parameterType, final PsiElement context) {
- if (exprType instanceof PsiClassType && parameterType instanceof PsiPrimitiveType) {
- parameterType = ((PsiPrimitiveType)parameterType).getBoxedType(context); //unboxing from type of cast expression will take place at runtime
- if (parameterType == null) return false;
- }
- return parameterType.isConvertibleFrom(exprType);
- }
- }
-
- public static final ArgumentFixerActionFactory REGISTRAR = new MyFixerActionFactory();
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeExtendsToImplementsFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeExtendsToImplementsFix.java
index 5c1e4117baa4..8e3fe5158d13 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeExtendsToImplementsFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeExtendsToImplementsFix.java
@@ -31,7 +31,7 @@ import org.jetbrains.annotations.NotNull;
public class ChangeExtendsToImplementsFix extends ExtendsListFix {
private final String myName;
- public ChangeExtendsToImplementsFix(PsiClass aClass, PsiClassType classToExtendFrom) {
+ public ChangeExtendsToImplementsFix(@NotNull PsiClass aClass, @NotNull PsiClassType classToExtendFrom) {
super(aClass, classToExtendFrom, true);
myName = QuickFixBundle.message("exchange.extends.implements.keyword",
aClass.isInterface() == myClassToExtendFrom.isInterface() ? PsiKeyword.IMPLEMENTS : PsiKeyword.EXTENDS,
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageFix.java
index 1fecce120b74..dbf454207cca 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageFix.java
@@ -18,7 +18,6 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.TargetElementUtil;
import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightUtil;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.find.FindManager;
@@ -34,7 +33,6 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
@@ -71,11 +69,11 @@ public class ChangeMethodSignatureFromUsageFix implements IntentionAction/*, Hig
ParameterInfoImpl[] myNewParametersInfo;
private static final Logger LOG = Logger.getInstance(ChangeMethodSignatureFromUsageFix.class);
- ChangeMethodSignatureFromUsageFix(@NotNull PsiMethod targetMethod,
- @NotNull PsiExpression[] expressions,
- @NotNull PsiSubstitutor substitutor,
- @NotNull PsiElement context,
- boolean changeAllUsages, int minUsagesNumberToShowDialog) {
+ public ChangeMethodSignatureFromUsageFix(@NotNull PsiMethod targetMethod,
+ @NotNull PsiExpression[] expressions,
+ @NotNull PsiSubstitutor substitutor,
+ @NotNull PsiElement context,
+ boolean changeAllUsages, int minUsagesNumberToShowDialog) {
myTargetMethod = targetMethod;
myExpressions = expressions;
mySubstitutor = substitutor;
@@ -457,32 +455,6 @@ public class ChangeMethodSignatureFromUsageFix implements IntentionAction/*, Hig
}
}
- public static void registerIntentions(@NotNull JavaResolveResult[] candidates,
- @NotNull PsiExpressionList list,
- @Nullable HighlightInfo highlightInfo,
- TextRange fixRange) {
- if (candidates.length == 0) return;
- PsiExpression[] expressions = list.getExpressions();
- for (JavaResolveResult candidate : candidates) {
- registerIntention(expressions, highlightInfo, fixRange, candidate, list);
- }
- }
-
- private static void registerIntention(@NotNull PsiExpression[] expressions,
- @Nullable HighlightInfo highlightInfo,
- TextRange fixRange,
- @NotNull JavaResolveResult candidate,
- @NotNull PsiElement context) {
- if (!candidate.isStaticsScopeCorrect()) return;
- PsiMethod method = (PsiMethod)candidate.getElement();
- PsiSubstitutor substitutor = candidate.getSubstitutor();
- if (method != null && context.getManager().isInProject(method)) {
- ChangeMethodSignatureFromUsageFix fix = new ChangeMethodSignatureFromUsageFix(method, expressions, substitutor, context, false, 2);
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, fix);
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new ChangeMethodSignatureFromUsageReverseOrderFix(method, expressions, substitutor, context, false, 2));
- }
- }
-
@Override
public boolean startInWriteAction() {
return false;
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageReverseOrderFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageReverseOrderFix.java
index fbc5529cec46..0d7e6c5b1a6d 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageReverseOrderFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeMethodSignatureFromUsageReverseOrderFix.java
@@ -35,12 +35,12 @@ import java.util.Set;
* Date: 2/9/12
*/
public class ChangeMethodSignatureFromUsageReverseOrderFix extends ChangeMethodSignatureFromUsageFix {
- ChangeMethodSignatureFromUsageReverseOrderFix(@NotNull PsiMethod targetMethod,
- @NotNull PsiExpression[] expressions,
- @NotNull PsiSubstitutor substitutor,
- @NotNull PsiElement context,
- boolean changeAllUsages,
- int minUsagesNumberToShowDialog) {
+ public ChangeMethodSignatureFromUsageReverseOrderFix(@NotNull PsiMethod targetMethod,
+ @NotNull PsiExpression[] expressions,
+ @NotNull PsiSubstitutor substitutor,
+ @NotNull PsiElement context,
+ boolean changeAllUsages,
+ int minUsagesNumberToShowDialog) {
super(targetMethod, expressions, substitutor, context, changeAllUsages, minUsagesNumberToShowDialog);
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeNewOperatorTypeFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeNewOperatorTypeFix.java
deleted file mode 100644
index 2e070337f6a8..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeNewOperatorTypeFix.java
+++ /dev/null
@@ -1,203 +0,0 @@
-/*
- * Copyright 2000-2012 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.FileModificationService;
-import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.editor.ScrollType;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.psi.*;
-import com.intellij.psi.impl.PsiDiamondTypeUtil;
-import com.intellij.psi.util.PsiExpressionTrimRenderer;
-import com.intellij.psi.util.PsiTreeUtil;
-import com.intellij.psi.util.PsiUtil;
-import com.intellij.psi.util.TypeConversionUtil;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NonNls;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-public class ChangeNewOperatorTypeFix implements IntentionAction {
- private final PsiType myType;
- private final PsiNewExpression myExpression;
-
- private ChangeNewOperatorTypeFix(PsiType type, PsiNewExpression expression) {
- myType = type;
- myExpression = expression;
- }
-
- @Override
- @NotNull
- public String getText() {
- return QuickFixBundle.message("change.new.operator.type.text", new PsiExpressionTrimRenderer.RenderFunction().fun(myExpression), myType.getPresentableText(), myType instanceof PsiArrayType ? "" : "()");
- }
-
- @Override
- @NotNull
- public String getFamilyName() {
- return QuickFixBundle.message("change.new.operator.type.family");
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- return myType.isValid()
- && myExpression.isValid()
- && myExpression.getManager().isInProject(myExpression)
- && !TypeConversionUtil.isPrimitiveAndNotNull(myType)
- && (myType instanceof PsiArrayType || myExpression.getArgumentList() != null)
- ;
- }
-
- @Override
- public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
- if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
- changeNewOperatorType(myExpression, myType, editor);
- }
-
- private static void changeNewOperatorType(PsiNewExpression originalExpression, PsiType toType, final Editor editor) throws IncorrectOperationException {
- PsiNewExpression newExpression;
- PsiElementFactory factory = JavaPsiFacade.getInstance(originalExpression.getProject()).getElementFactory();
- int caretOffset;
- TextRange selection;
- if (toType instanceof PsiArrayType) {
- final PsiExpression[] originalExpressionArrayDimensions = originalExpression.getArrayDimensions();
- caretOffset = 0;
- @NonNls String text = "new " + toType.getDeepComponentType().getCanonicalText() + "[";
- if (originalExpressionArrayDimensions.length > 0) {
- text += originalExpressionArrayDimensions[0].getText();
- }
- else {
- text += "0";
- caretOffset = -2;
- }
- text += "]";
- for (int i = 1; i < toType.getArrayDimensions(); i++) {
- text += "[";
- String arrayDimension = "";
- if (originalExpressionArrayDimensions.length > i) {
- arrayDimension = originalExpressionArrayDimensions[i].getText();
- text += arrayDimension;
- }
- text += "]";
- if (caretOffset < 0) {
- caretOffset -= arrayDimension.length() + 2;
- }
- }
-
- newExpression = (PsiNewExpression)factory.createExpressionFromText(text, originalExpression);
- if (caretOffset < 0) {
- selection = new TextRange(caretOffset, caretOffset+1);
- } else {
- selection = null;
- }
- }
- else {
- final PsiAnonymousClass anonymousClass = originalExpression.getAnonymousClass();
- newExpression = (PsiNewExpression)factory.createExpressionFromText("new " + toType.getCanonicalText() + "()" + (anonymousClass != null ? "{}" : ""), originalExpression);
- PsiExpressionList argumentList = originalExpression.getArgumentList();
- if (argumentList == null) return;
- newExpression.getArgumentList().replace(argumentList);
- if (anonymousClass == null) { //just to prevent useless inference
- if (PsiDiamondTypeUtil.canCollapseToDiamond(newExpression, originalExpression, toType)) {
- final PsiElement paramList = PsiDiamondTypeUtil.replaceExplicitWithDiamond(newExpression.getClassOrAnonymousClassReference().getParameterList());
- newExpression = PsiTreeUtil.getParentOfType(paramList, PsiNewExpression.class);
- }
- }
-
- if (anonymousClass != null) {
- PsiAnonymousClass newAnonymousClass = newExpression.getAnonymousClass();
- final PsiElement childInside = anonymousClass.getLBrace().getNextSibling();
- if (childInside != null) {
- newAnonymousClass.addRange(childInside, anonymousClass.getRBrace().getPrevSibling());
- }
- }
- selection = null;
- caretOffset = -1;
- }
- PsiElement element = originalExpression.replace(newExpression);
- editor.getCaretModel().moveToOffset(element.getTextRange().getEndOffset() + caretOffset);
- editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
- if (selection != null) {
- selection = selection.shiftRight(element.getTextRange().getEndOffset());
- editor.getSelectionModel().setSelection(selection.getStartOffset(), selection.getEndOffset());
- }
- }
-
- @Override
- public boolean startInWriteAction() {
- return true;
- }
-
- public static void register(final HighlightInfo highlightInfo, PsiExpression expression, final PsiType lType) {
- expression = PsiUtil.deparenthesizeExpression(expression);
- if (!(expression instanceof PsiNewExpression)) return;
- final PsiType rType = expression.getType();
- PsiType newType = lType;
- if (rType instanceof PsiClassType && newType instanceof PsiClassType) {
- final PsiClassType.ClassResolveResult rResolveResult = ((PsiClassType)rType).resolveGenerics();
- PsiClass rClass = rResolveResult.getElement();
- if (rClass instanceof PsiAnonymousClass) {
- rClass = ((PsiAnonymousClass)rClass).getBaseClassType().resolve();
- }
- if (rClass != null) {
- final PsiClassType.ClassResolveResult lResolveResult = ((PsiClassType)newType).resolveGenerics();
- final PsiClass lClass = lResolveResult.getElement();
- if (lClass != null) {
- PsiSubstitutor substitutor = getInheritorSubstitutorForNewExpression(lClass, rClass, lResolveResult.getSubstitutor(), expression);
- if (substitutor != null) {
- newType = JavaPsiFacade.getInstance(lClass.getProject()).getElementFactory().createType(rClass, substitutor);
- }
- }
- }
- }
- PsiNewExpression newExpression = (PsiNewExpression)expression;
- QuickFixAction.registerQuickFixAction(highlightInfo, new ChangeNewOperatorTypeFix(newType, newExpression));
- }
-
- /* Guesswork
- */
- @Nullable
- private static PsiSubstitutor getInheritorSubstitutorForNewExpression(final PsiClass baseClass, final PsiClass inheritor,
- final PsiSubstitutor baseSubstitutor, final PsiElement context) {
- final Project project = baseClass.getProject();
- JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
- final PsiResolveHelper resolveHelper = facade.getResolveHelper();
- PsiSubstitutor superSubstitutor = TypeConversionUtil.getClassSubstitutor(baseClass, inheritor, PsiSubstitutor.EMPTY);
- if (superSubstitutor == null) return null;
- PsiSubstitutor inheritorSubstitutor = PsiSubstitutor.EMPTY;
- for (PsiTypeParameter inheritorParameter : PsiUtil.typeParametersIterable(inheritor)) {
- for (PsiTypeParameter baseParameter : PsiUtil.typeParametersIterable(baseClass)) {
- final PsiType substituted = superSubstitutor.substitute(baseParameter);
- PsiType arg = baseSubstitutor.substitute(baseParameter);
- if (arg instanceof PsiWildcardType) arg = ((PsiWildcardType)arg).getBound();
- PsiType substitution =
- resolveHelper.getSubstitutionForTypeParameter(inheritorParameter, substituted, arg, true, PsiUtil.getLanguageLevel(context));
- if (PsiType.NULL.equals(substitution)) continue;
- if (substitution == null) {
- return facade.getElementFactory().createRawSubstitutor(inheritor);
- }
- inheritorSubstitutor = inheritorSubstitutor.put(inheritorParameter, substitution);
- break;
- }
- }
-
- return inheritorSubstitutor;
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeParameterClassFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeParameterClassFix.java
index b6b13a4cc9e8..80df581fc014 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeParameterClassFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeParameterClassFix.java
@@ -26,17 +26,18 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.CodeInsightUtil;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
+import com.intellij.codeInsight.generation.OverrideImplementExploreUtil;
import com.intellij.codeInsight.generation.OverrideImplementUtil;
import com.intellij.codeInsight.generation.PsiMethodMember;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.undo.UndoUtil;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
-import com.intellij.psi.*;
+import com.intellij.psi.PsiClass;
+import com.intellij.psi.PsiClassType;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiFile;
import com.intellij.psi.infos.CandidateInfo;
-import com.intellij.psi.util.PsiUtil;
-import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
@@ -45,7 +46,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.Collection;
public class ChangeParameterClassFix extends ExtendsListFix {
- private ChangeParameterClassFix(PsiClass aClassToExtend, PsiClassType parameterClass) {
+ public ChangeParameterClassFix(@NotNull PsiClass aClassToExtend, @NotNull PsiClassType parameterClass) {
super(aClassToExtend, parameterClass, true);
}
@@ -86,7 +87,7 @@ public class ChangeParameterClassFix extends ExtendsListFix {
);
final Editor editor1 = CodeInsightUtil.positionCursor(project, myClass.getContainingFile(), myClass);
if (editor1 == null) return;
- final Collection<CandidateInfo> toImplement = OverrideImplementUtil.getMethodsToOverrideImplement(myClass, true);
+ final Collection<CandidateInfo> toImplement = OverrideImplementExploreUtil.getMethodsToOverrideImplement(myClass, true);
if (!toImplement.isEmpty()) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
ApplicationManager.getApplication().runWriteAction(
@@ -114,44 +115,6 @@ public class ChangeParameterClassFix extends ExtendsListFix {
UndoUtil.markPsiFileForUndo(file);
}
- public static void registerQuickFixAction(PsiType lType, PsiType rType, HighlightInfo info) {
- final PsiClass lClass = PsiUtil.resolveClassInClassTypeOnly(lType);
- final PsiClass rClass = PsiUtil.resolveClassInClassTypeOnly(rType);
-
- if (rClass == null || lClass == null) return;
- if (rClass instanceof PsiAnonymousClass) return;
- if (rClass.isInheritor(lClass, true)) return;
- if (lClass.isInheritor(rClass, true)) return;
- if (lClass == rClass) return;
-
- QuickFixAction.registerQuickFixAction(info, new ChangeParameterClassFix(rClass, (PsiClassType)lType));
- }
-
- public static void registerQuickFixActions(PsiCall methodCall, PsiExpressionList list, HighlightInfo highlightInfo) {
- final JavaResolveResult result = methodCall.resolveMethodGenerics();
- PsiMethod method = (PsiMethod)result.getElement();
- final PsiSubstitutor substitutor = result.getSubstitutor();
- PsiExpression[] expressions = list.getExpressions();
- if (method == null) return;
- final PsiParameter[] parameters = method.getParameterList().getParameters();
- if (parameters.length != expressions.length) return;
- for (int i = 0; i < expressions.length; i++) {
- final PsiExpression expression = expressions[i];
- final PsiParameter parameter = parameters[i];
- final PsiType expressionType = expression.getType();
- final PsiType parameterType = substitutor.substitute(parameter.getType());
- if (expressionType == null || expressionType instanceof PsiPrimitiveType || TypeConversionUtil.isNullType(expressionType) || expressionType instanceof PsiArrayType) continue;
- if (parameterType instanceof PsiPrimitiveType || TypeConversionUtil.isNullType(parameterType) || parameterType instanceof PsiArrayType) continue;
- if (parameterType.isAssignableFrom(expressionType)) continue;
- PsiClass parameterClass = PsiUtil.resolveClassInType(parameterType);
- PsiClass expressionClass = PsiUtil.resolveClassInType(expressionType);
- if (parameterClass == null || expressionClass == null) continue;
- if (expressionClass instanceof PsiAnonymousClass) continue;
- if (parameterClass.isInheritor(expressionClass, true)) continue;
- QuickFixAction.registerQuickFixAction(highlightInfo, new ChangeParameterClassFix(expressionClass, (PsiClassType)parameterType));
- }
- }
-
@Override
public boolean startInWriteAction() {
return false;
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeStringLiteralToCharInMethodCallFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeStringLiteralToCharInMethodCallFix.java
deleted file mode 100644
index 19fcc3b9fcc3..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeStringLiteralToCharInMethodCallFix.java
+++ /dev/null
@@ -1,176 +0,0 @@
-/*
- * Copyright 2000-2012 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.FileModificationService;
-import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.Comparing;
-import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.psi.*;
-import com.intellij.psi.infos.CandidateInfo;
-import com.intellij.psi.infos.MethodCandidateInfo;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.HashSet;
-import java.util.Set;
-
-public class ChangeStringLiteralToCharInMethodCallFix implements IntentionAction {
- private final PsiLiteralExpression myLiteral;
- private final PsiCall myCall;
-
- public ChangeStringLiteralToCharInMethodCallFix(final PsiLiteralExpression literal, final PsiCall methodCall) {
- myLiteral = literal;
- myCall = methodCall;
- }
-
- @Override
- @NotNull
- public String getText() {
- final String convertedValue = convertedValue();
- final boolean isString = isString(myLiteral.getType());
- return QuickFixBundle.message("fix.single.character.string.to.char.literal.text", myLiteral.getText(),
- quote(convertedValue, ! isString), isString ? PsiType.CHAR.getCanonicalText() : "String");
- }
-
- @Override
- @NotNull
- public String getFamilyName() {
- return QuickFixBundle.message("fix.single.character.string.to.char.literal.family");
- }
-
- @Override
- public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
- return myCall.isValid() && myLiteral.isValid() && myCall.getManager().isInProject(myCall);
- }
-
- @Override
- public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
- if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
-
- final Object value = myLiteral.getValue();
- if (value != null && value.toString().length() == 1) {
- final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
-
- final PsiExpression newExpression = factory.createExpressionFromText(quote(convertedValue(), ! isString(myLiteral.getType())),
- myLiteral.getParent());
- myLiteral.replace(newExpression);
- }
- }
-
- @Override
- public boolean startInWriteAction() {
- return true;
- }
-
- private static String quote(final String value, final boolean doubleQuotes) {
- final char quote = doubleQuotes ? '"' : '\'';
- return quote + value + quote;
- }
-
- private String convertedValue() {
- String value = String.valueOf(myLiteral.getValue());
- final StringBuilder builder = new StringBuilder();
- StringUtil.escapeStringCharacters(value.length(), value, "\"'", builder);
- return builder.toString();
- }
-
- public static void registerFixes(@NotNull final PsiMethod[] candidates, @NotNull final PsiConstructorCall call,
- @NotNull final HighlightInfo out) {
- final Set<PsiLiteralExpression> literals = new HashSet<PsiLiteralExpression>();
- if (call.getArgumentList() == null) {
- return;
- }
- boolean exactMatch = false;
- for (PsiMethod method : candidates) {
- exactMatch |= findMatchingExpressions(call.getArgumentList().getExpressions(), method, literals);
- }
- if (! exactMatch) {
- processLiterals(literals, call, out);
- }
- }
-
- public static void registerFixes(@NotNull final CandidateInfo[] candidates,
- @NotNull final PsiMethodCallExpression methodCall,
- @Nullable final HighlightInfo info) {
- if (info == null) return;
- final Set<PsiLiteralExpression> literals = new HashSet<PsiLiteralExpression>();
- boolean exactMatch = false;
- for (CandidateInfo candidate : candidates) {
- if (candidate instanceof MethodCandidateInfo) {
- final PsiMethod method = ((MethodCandidateInfo) candidate).getElement();
- exactMatch |= findMatchingExpressions(methodCall.getArgumentList().getExpressions(), method, literals);
- }
- }
- if (!exactMatch) {
- processLiterals(literals, methodCall, info);
- }
- }
-
- private static void processLiterals(@NotNull final Set<PsiLiteralExpression> literals,
- @NotNull final PsiCall call,
- @NotNull final HighlightInfo info) {
- for (PsiLiteralExpression literal : literals) {
- final ChangeStringLiteralToCharInMethodCallFix fix = new ChangeStringLiteralToCharInMethodCallFix(literal, call);
- QuickFixAction.registerQuickFixAction(info, fix);
- }
- }
-
- /**
- * @return <code>true</code> if exact TYPEs match
- */
- private static boolean findMatchingExpressions(final PsiExpression[] arguments, final PsiMethod existingMethod,
- final Set<PsiLiteralExpression> result) {
- final PsiParameterList parameterList = existingMethod.getParameterList();
- final PsiParameter[] parameters = parameterList.getParameters();
-
- if (arguments.length != parameters.length) {
- return false;
- }
-
- boolean typeMatch = true;
- for (int i = 0; i < parameters.length && i < arguments.length; i++) {
- final PsiParameter parameter = parameters[i];
- final PsiType parameterType = parameter.getType();
- final PsiType argumentType = arguments[i].getType();
-
- typeMatch &= Comparing.equal(parameterType, argumentType);
-
- if (arguments[i] instanceof PsiLiteralExpression && ! result.contains(arguments[i]) &&
- (charToString(parameterType, argumentType) || charToString(argumentType, parameterType))) {
-
- final String value = String.valueOf(((PsiLiteralExpression) arguments[i]).getValue());
- if (value != null && value.length() == 1) {
- result.add((PsiLiteralExpression) arguments[i]);
- }
- }
- }
- return typeMatch;
- }
-
- private static boolean charToString(final PsiType firstType, final PsiType secondType) {
- return Comparing.equal(PsiType.CHAR, firstType) && isString(secondType);
- }
-
- private static boolean isString(final PsiType type) {
- return type != null && "java.lang.String".equals(type.getCanonicalText());
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeTypeArgumentsFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeTypeArgumentsFix.java
deleted file mode 100644
index 76580adaa5f4..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ChangeTypeArgumentsFix.java
+++ /dev/null
@@ -1,169 +0,0 @@
-/*
- * Copyright 2000-2012 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * Created by IntelliJ IDEA.
- * User: cdr
- * Date: Nov 13, 2002
- * Time: 3:26:50 PM
- * To change this template use Options | File Templates.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.FileModificationService;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.intention.HighPriorityAction;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.psi.*;
-import com.intellij.psi.impl.source.resolve.DefaultParameterTypeInferencePolicy;
-import com.intellij.psi.util.PsiTreeUtil;
-import com.intellij.psi.util.TypeConversionUtil;
-import com.intellij.util.Function;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-public class ChangeTypeArgumentsFix implements IntentionAction, HighPriorityAction {
- private final PsiMethod myTargetMethod;
- private final PsiClass myPsiClass;
- private final PsiExpression[] myExpressions;
- private static final Logger LOG = Logger.getInstance("#" + ChangeTypeArgumentsFix.class.getName());
- private final PsiNewExpression myNewExpression;
-
- ChangeTypeArgumentsFix(@NotNull PsiMethod targetMethod,
- PsiClass psiClass,
- @NotNull PsiExpression[] expressions,
- @NotNull PsiElement context) {
- myTargetMethod = targetMethod;
- myPsiClass = psiClass;
- myExpressions = expressions;
- myNewExpression = PsiTreeUtil.getParentOfType(context, PsiNewExpression.class);
- }
-
- @Override
- @NotNull
- public String getText() {
- final PsiSubstitutor substitutor = inferTypeArguments();
- return "Change type arguments to <" + StringUtil.join(myPsiClass.getTypeParameters(), new Function<PsiTypeParameter, String>() {
- @Override
- public String fun(PsiTypeParameter typeParameter) {
- final PsiType substituted = substitutor.substitute(typeParameter);
- return substituted != null ? substituted.getPresentableText() : CommonClassNames.JAVA_LANG_OBJECT;
- }
- }, ", ") + ">";
- }
-
-
- @Override
- @NotNull
- public String getFamilyName() {
- return "Change type arguments";
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- final PsiTypeParameter[] typeParameters = myPsiClass.getTypeParameters();
- if (typeParameters.length > 0) {
- if (myNewExpression != null && myNewExpression.isValid() && myNewExpression.getArgumentList() != null) {
- final PsiJavaCodeReferenceElement reference = myNewExpression.getClassOrAnonymousClassReference();
- if (reference != null) {
- final PsiReferenceParameterList parameterList = reference.getParameterList();
- if (parameterList != null) {
- final PsiSubstitutor substitutor = inferTypeArguments();
- final PsiParameter[] parameters = myTargetMethod.getParameterList().getParameters();
- if (parameters.length != myExpressions.length) return false;
- for (int i = 0, length = parameters.length; i < length; i++) {
- PsiParameter parameter = parameters[i];
- final PsiType expectedType = substitutor.substitute(parameter.getType());
- if (!myExpressions[i].isValid()) return false;
- final PsiType actualType = myExpressions[i].getType();
- if (expectedType == null || actualType == null || !TypeConversionUtil.isAssignable(expectedType, actualType)) return false;
- }
- for (PsiTypeParameter parameter : typeParameters) {
- if (substitutor.substitute(parameter) == null) return false;
- }
- return true;
- }
- }
- }
- }
- return false;
- }
-
- @Override
- public void invoke(@NotNull final Project project, Editor editor, final PsiFile file) {
- if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
-
- final PsiTypeParameter[] typeParameters = myPsiClass.getTypeParameters();
- final PsiSubstitutor psiSubstitutor = inferTypeArguments();
- final PsiJavaCodeReferenceElement reference = myNewExpression.getClassOrAnonymousClassReference();
- LOG.assertTrue(reference != null, myNewExpression);
- final PsiReferenceParameterList parameterList = reference.getParameterList();
- LOG.assertTrue(parameterList != null, myNewExpression);
- PsiTypeElement[] elements = parameterList.getTypeParameterElements();
- for (int i = elements.length - 1; i >= 0; i--) {
- PsiTypeElement typeElement = elements[i];
- final PsiType typeArg = psiSubstitutor.substitute(typeParameters[i]);
- typeElement.replace(JavaPsiFacade.getElementFactory(project).createTypeElement(typeArg));
- }
- }
-
- private PsiSubstitutor inferTypeArguments() {
- final JavaPsiFacade facade = JavaPsiFacade.getInstance(myNewExpression.getProject());
- final PsiResolveHelper resolveHelper = facade.getResolveHelper();
- final PsiParameter[] parameters = myTargetMethod.getParameterList().getParameters();
- final PsiExpressionList argumentList = myNewExpression.getArgumentList();
- LOG.assertTrue(argumentList != null);
- final PsiExpression[] expressions = argumentList.getExpressions();
- return resolveHelper.inferTypeArguments(myPsiClass.getTypeParameters(), parameters, expressions,
- PsiSubstitutor.EMPTY,
- myNewExpression.getParent(),
- DefaultParameterTypeInferencePolicy.INSTANCE);
- }
-
-
- public static void registerIntentions(@NotNull JavaResolveResult[] candidates,
- @NotNull PsiExpressionList list,
- @Nullable HighlightInfo highlightInfo,
- PsiClass psiClass) {
- if (candidates.length == 0) return;
- PsiExpression[] expressions = list.getExpressions();
- for (JavaResolveResult candidate : candidates) {
- registerIntention(expressions, highlightInfo, psiClass, candidate, list);
- }
- }
-
- private static void registerIntention(@NotNull PsiExpression[] expressions,
- @Nullable HighlightInfo highlightInfo,
- PsiClass psiClass,
- @NotNull JavaResolveResult candidate,
- @NotNull PsiElement context) {
- if (!candidate.isStaticsScopeCorrect()) return;
- PsiMethod method = (PsiMethod)candidate.getElement();
- if (method != null && context.getManager().isInProject(method)) {
- final ChangeTypeArgumentsFix fix = new ChangeTypeArgumentsFix(method, psiClass, expressions, context);
- QuickFixAction.registerQuickFixAction(highlightInfo, null, fix);
- }
- }
-
- @Override
- public boolean startInWriteAction() {
- return true;
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConstructorParametersFixer.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConstructorParametersFixer.java
deleted file mode 100644
index 44a4c761b18a..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConstructorParametersFixer.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * Propose to cast one argument to corresponding type
- * in the constructor invocation
- * E.g.
- *
- * User: cdr
- * Date: Nov 13, 2002
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.psi.*;
-import com.intellij.psi.infos.CandidateInfo;
-import com.intellij.openapi.util.TextRange;
-import org.jetbrains.annotations.NotNull;
-
-public class ConstructorParametersFixer {
- public static void registerFixActions(@NotNull PsiJavaCodeReferenceElement ctrRef, PsiConstructorCall constructorCall, HighlightInfo highlightInfo,
- final TextRange fixRange) {
- JavaResolveResult resolved = ctrRef.advancedResolve(false);
- PsiClass aClass = (PsiClass) resolved.getElement();
- if (aClass == null) return;
- PsiMethod[] methods = aClass.getConstructors();
- CandidateInfo[] candidates = new CandidateInfo[methods.length];
- for (int i = 0; i < candidates.length; i++) {
- candidates[i] = new CandidateInfo(methods[i], resolved.getSubstitutor());
- }
- CastMethodArgumentFix.REGISTRAR.registerCastActions(candidates, constructorCall, highlightInfo, fixRange);
- AddTypeArgumentsFix.REGISTRAR.registerCastActions(candidates, constructorCall, highlightInfo, fixRange);
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertDoubleToFloatFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertDoubleToFloatFix.java
deleted file mode 100644
index 4a5189ec1935..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertDoubleToFloatFix.java
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * Copyright 2000-2012 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.psi.*;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-/**
- * User: anna
- * Date: 2/10/12
- */
-public class ConvertDoubleToFloatFix implements IntentionAction {
- private final PsiExpression myExpression;
-
- public ConvertDoubleToFloatFix(PsiExpression expression) {
- myExpression = expression;
- }
-
- @NotNull
- @Override
- public String getText() {
- return "Convert '" + myExpression.getText() + "' to float";
- }
-
- @NotNull
- @Override
- public String getFamilyName() {
- return getText();
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- if (myExpression.isValid()) {
- if (!StringUtil.endsWithIgnoreCase(myExpression.getText(), "f")) {
- final PsiLiteralExpression expression = (PsiLiteralExpression)createFloatingPointExpression(project);
- final Object value = expression.getValue();
- return value instanceof Float && !((Float)value).isInfinite() && !(((Float)value).floatValue() == 0 && !HighlightUtil.isFPZero(expression.getText()));
- }
- }
- return false;
- }
-
- @Override
- public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
- myExpression.replace(createFloatingPointExpression(project));
- }
-
- private PsiExpression createFloatingPointExpression(Project project) {
- final String text = myExpression.getText();
- if (StringUtil.endsWithIgnoreCase(text, "d")) {
- return JavaPsiFacade.getElementFactory(project).createExpressionFromText(text.substring(0, text.length() - 1) + "f", myExpression);
- } else {
- return JavaPsiFacade.getElementFactory(project).createExpressionFromText(text + "f", myExpression);
- }
- }
-
- @Override
- public boolean startInWriteAction() {
- return true;
- }
-
- public static void registerIntentions(@NotNull JavaResolveResult[] candidates,
- @NotNull PsiExpressionList list,
- @Nullable HighlightInfo highlightInfo,
- TextRange fixRange) {
- if (candidates.length == 0) return;
- PsiExpression[] expressions = list.getExpressions();
- for (JavaResolveResult candidate : candidates) {
- registerIntention(expressions, highlightInfo, fixRange, candidate, list);
- }
- }
-
- private static void registerIntention(@NotNull PsiExpression[] expressions,
- @Nullable HighlightInfo highlightInfo,
- TextRange fixRange,
- @NotNull JavaResolveResult candidate,
- @NotNull PsiElement context) {
- if (!candidate.isStaticsScopeCorrect()) return;
- PsiMethod method = (PsiMethod)candidate.getElement();
- if (method != null && context.getManager().isInProject(method)) {
- final PsiParameter[] parameters = method.getParameterList().getParameters();
- if (parameters.length == expressions.length) {
- for (int i = 0, length = parameters.length; i < length; i++) {
- PsiParameter parameter = parameters[i];
- final PsiExpression expression = expressions[i];
- if (expression instanceof PsiLiteralExpression && PsiType.FLOAT.equals(parameter.getType()) && PsiType.DOUBLE.equals(expression.getType())) {
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new ConvertDoubleToFloatFix(expression));
- }
- }
- }
- }
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAbstractMethodFromUsageFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAbstractMethodFromUsageFix.java
index 5b51c57e9fb9..c20848cc26a6 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAbstractMethodFromUsageFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAbstractMethodFromUsageFix.java
@@ -24,7 +24,7 @@ import java.util.ArrayList;
import java.util.List;
public class CreateAbstractMethodFromUsageFix extends CreateMethodFromUsageFix {
- public CreateAbstractMethodFromUsageFix(PsiMethodCallExpression methodCall) {
+ public CreateAbstractMethodFromUsageFix(@NotNull PsiMethodCallExpression methodCall) {
super(methodCall);
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAnnotationMethodFromUsageFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAnnotationMethodFromUsageFix.java
index 86f0e4f4d168..487b6f07c707 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAnnotationMethodFromUsageFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateAnnotationMethodFromUsageFix.java
@@ -34,7 +34,7 @@ public class CreateAnnotationMethodFromUsageFix extends CreateFromUsageBaseFix {
private final SmartPsiElementPointer<PsiNameValuePair> myNameValuePair;
- public CreateAnnotationMethodFromUsageFix(PsiNameValuePair valuePair) {
+ public CreateAnnotationMethodFromUsageFix(@NotNull PsiNameValuePair valuePair) {
myNameValuePair = SmartPointerManager.getInstance(valuePair.getProject()).createSmartPsiElementPointer(valuePair);
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromCallFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromCallFix.java
index fa32f9fe9537..efde701fa61f 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromCallFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromCallFix.java
@@ -41,7 +41,7 @@ public class CreateConstructorFromCallFix extends CreateFromUsageBaseFix {
private final PsiConstructorCall myConstructorCall;
- public CreateConstructorFromCallFix(PsiConstructorCall constructorCall) {
+ public CreateConstructorFromCallFix(@NotNull PsiConstructorCall constructorCall) {
myConstructorCall = constructorCall;
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromSuperFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromSuperFix.java
index 43ed9a754d0e..a4a568320299 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromSuperFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromSuperFix.java
@@ -29,7 +29,7 @@ import java.util.List;
public class CreateConstructorFromSuperFix extends CreateConstructorFromThisOrSuperFix {
- public CreateConstructorFromSuperFix(PsiMethodCallExpression methodCall) {
+ public CreateConstructorFromSuperFix(@NotNull PsiMethodCallExpression methodCall) {
super(methodCall);
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromThisFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromThisFix.java
index b03fb7c3eadd..f077c663d428 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromThisFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromThisFix.java
@@ -32,7 +32,7 @@ import java.util.List;
public class CreateConstructorFromThisFix extends CreateConstructorFromThisOrSuperFix {
- public CreateConstructorFromThisFix(PsiMethodCallExpression methodCall) {
+ public CreateConstructorFromThisFix(@NotNull PsiMethodCallExpression methodCall) {
super(methodCall);
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorMatchingSuperFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorMatchingSuperFix.java
index 15c497e112b1..b3c868a562c8 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorMatchingSuperFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorMatchingSuperFix.java
@@ -48,7 +48,7 @@ public class CreateConstructorMatchingSuperFix extends BaseIntentionAction {
private final PsiClass myClass;
- public CreateConstructorMatchingSuperFix(PsiClass aClass) {
+ public CreateConstructorMatchingSuperFix(@NotNull PsiClass aClass) {
myClass = aClass;
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateGetterSetterPropertyFromUsageFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateGetterSetterPropertyFromUsageFix.java
index c5bd0289d4e3..38b604a33ebf 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateGetterSetterPropertyFromUsageFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateGetterSetterPropertyFromUsageFix.java
@@ -21,6 +21,7 @@ import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiMethodCallExpression;
import com.intellij.psi.util.PropertyUtil;
+import org.jetbrains.annotations.NotNull;
import java.util.List;
@@ -29,7 +30,7 @@ import java.util.List;
* Date: 7/12/12
*/
public class CreateGetterSetterPropertyFromUsageFix extends CreatePropertyFromUsageFix {
- public CreateGetterSetterPropertyFromUsageFix(PsiMethodCallExpression methodCall) {
+ public CreateGetterSetterPropertyFromUsageFix(@NotNull PsiMethodCallExpression methodCall) {
super(methodCall);
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateLocalVarFromInstanceofAction.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateLocalVarFromInstanceofAction.java
index 9fc885ee9b79..232505766789 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateLocalVarFromInstanceofAction.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateLocalVarFromInstanceofAction.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2009 JetBrains s.r.o.
+ * Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -342,7 +342,11 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
anchorAfter = newBranch.getCodeBlock().getLBrace();
}
else {
- anchorAfter = ((PsiBlockStatement)thenBranch).getCodeBlock().getLBrace();
+ final PsiJavaToken lBrace = ((PsiBlockStatement)thenBranch).getCodeBlock().getLBrace();
+ if (lBrace != null) {
+ final PsiElement nextSibling = PsiTreeUtil.skipSiblingsForward(lBrace, PsiWhiteSpace.class);
+ anchorAfter = nextSibling instanceof PsiComment ? PsiTreeUtil.skipSiblingsForward(nextSibling, PsiComment.class) : lBrace;
+ }
}
}
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateMethodFromUsageFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateMethodFromUsageFix.java
index d3fb2c1832bb..3f02e6edc72d 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateMethodFromUsageFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateMethodFromUsageFix.java
@@ -17,8 +17,8 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.CodeInsightUtilCore;
import com.intellij.codeInsight.ExpectedTypeInfo;
+import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx;
import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.template.Template;
import com.intellij.codeInsight.template.TemplateBuilderImpl;
@@ -55,7 +55,7 @@ public class CreateMethodFromUsageFix extends CreateFromUsageBaseFix {
private final SmartPsiElementPointer myMethodCall;
- public CreateMethodFromUsageFix(PsiMethodCallExpression methodCall) {
+ public CreateMethodFromUsageFix(@NotNull PsiMethodCallExpression methodCall) {
myMethodCall = SmartPointerManager.getInstance(methodCall.getProject()).createSmartPsiElementPointer(methodCall);
}
@@ -94,15 +94,15 @@ public class CreateMethodFromUsageFix extends CreateFromUsageBaseFix {
PsiExpressionList argumentList = call.getArgumentList();
final TextRange argRange = argumentList.getTextRange();
- return !DaemonCodeAnalyzerImpl.processHighlights(document, project, HighlightSeverity.ERROR,
- //strictly inside arg list
- argRange.getStartOffset()+1,
- argRange.getEndOffset()-1, new Processor<HighlightInfo>() {
- @Override
- public boolean process(HighlightInfo info) {
- return !(info.getActualStartOffset() > argRange.getStartOffset() && info.getActualEndOffset() < argRange.getEndOffset());
- }
- });
+ return !DaemonCodeAnalyzerEx.processHighlights(document, project, HighlightSeverity.ERROR,
+ //strictly inside arg list
+ argRange.getStartOffset() + 1,
+ argRange.getEndOffset() - 1, new Processor<HighlightInfo>() {
+ @Override
+ public boolean process(HighlightInfo info) {
+ return !(info.getActualStartOffset() > argRange.getStartOffset() && info.getActualEndOffset() < argRange.getEndOffset());
+ }
+ });
}
@Override
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreatePropertyFromUsageFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreatePropertyFromUsageFix.java
index 6567a9a478cc..b1cf8282b263 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreatePropertyFromUsageFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreatePropertyFromUsageFix.java
@@ -57,7 +57,7 @@ public class CreatePropertyFromUsageFix extends CreateFromUsageBaseFix implement
@NonNls private static final String IS_PREFIX = "is";
@NonNls private static final String SET_PREFIX = "set";
- public CreatePropertyFromUsageFix(PsiMethodCallExpression methodCall) {
+ public CreatePropertyFromUsageFix(@NotNull PsiMethodCallExpression methodCall) {
myMethodCall = methodCall;
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DefaultQuickFixProvider.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DefaultQuickFixProvider.java
index 06704aaed162..f69714427d29 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DefaultQuickFixProvider.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DefaultQuickFixProvider.java
@@ -18,6 +18,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixActionRegistrar;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightMethodUtil;
import com.intellij.codeInsight.intention.IntentionAction;
+import com.intellij.codeInsight.intention.QuickFixFactory;
import com.intellij.codeInsight.intention.impl.PriorityIntentionActionWrapper;
import com.intellij.codeInsight.quickfix.UnresolvedReferenceQuickFixProvider;
import com.intellij.openapi.util.TextRange;
@@ -34,8 +35,9 @@ import java.util.Map;
public class DefaultQuickFixProvider extends UnresolvedReferenceQuickFixProvider<PsiJavaCodeReferenceElement> {
@Override
public void registerFixes(@NotNull PsiJavaCodeReferenceElement ref, @NotNull QuickFixActionRegistrar registrar) {
+ QuickFixFactory factory = QuickFixFactory.getInstance();
registrar.register(new ImportClassFix(ref));
- registrar.register(SetupJDKFix.getInstance());
+ registrar.register(factory.createSetupJDKFix());
OrderEntryFix.registerFixes(registrar, ref);
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeferFinalAssignmentFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeferFinalAssignmentFix.java
index 94df09dfb49d..dcae6c51d727 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeferFinalAssignmentFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeferFinalAssignmentFix.java
@@ -46,7 +46,7 @@ public class DeferFinalAssignmentFix implements IntentionAction {
private final PsiVariable variable;
private final PsiReferenceExpression expression;
- public DeferFinalAssignmentFix(PsiVariable variable, PsiReferenceExpression expression) {
+ public DeferFinalAssignmentFix(@NotNull PsiVariable variable, @NotNull PsiReferenceExpression expression) {
this.variable = variable;
this.expression = expression;
}
@@ -81,7 +81,7 @@ public class DeferFinalAssignmentFix implements IntentionAction {
deferVariable(codeBlock, field, null);
}
- private PsiCodeBlock getEnclosingCodeBlock(PsiField field, PsiElement element) {
+ private static PsiCodeBlock getEnclosingCodeBlock(PsiField field, PsiElement element) {
PsiClass aClass = field.getContainingClass();
if (aClass == null) return null;
PsiMethod[] constructors = aClass.getConstructors();
@@ -148,7 +148,11 @@ public class DeferFinalAssignmentFix implements IntentionAction {
}
- private boolean insertToDefinitelyReachedPlace(PsiElement codeBlock, PsiStatement finalAssignment, ControlFlow controlFlow, int minOffset, List references) throws IncorrectOperationException {
+ private static boolean insertToDefinitelyReachedPlace(PsiElement codeBlock,
+ PsiStatement finalAssignment,
+ ControlFlow controlFlow,
+ int minOffset,
+ List references) throws IncorrectOperationException {
int offset = ControlFlowUtil.getMinDefinitelyReachedOffset(controlFlow, minOffset, references);
if (offset == controlFlow.getSize()) {
codeBlock.add(finalAssignment);
@@ -200,13 +204,11 @@ public class DeferFinalAssignmentFix implements IntentionAction {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return
- variable != null
- && variable.isValid()
- && !(variable instanceof PsiParameter)
- && !(variable instanceof ImplicitVariable)
- && expression != null
- && expression.isValid()
- && variable.getManager().isInProject(variable)
+ variable.isValid() &&
+ !(variable instanceof PsiParameter) &&
+ !(variable instanceof ImplicitVariable) &&
+ expression.isValid() &&
+ variable.getManager().isInProject(variable)
;
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteMethodBodyFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteMethodBodyFix.java
index e259fdee4d25..db276f1b535d 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteMethodBodyFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteMethodBodyFix.java
@@ -32,7 +32,7 @@ import org.jetbrains.annotations.NotNull;
public class DeleteMethodBodyFix implements IntentionAction {
private final PsiMethod myMethod;
- public DeleteMethodBodyFix(PsiMethod method) {
+ public DeleteMethodBodyFix(@NotNull PsiMethod method) {
myMethod = method;
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ExtendsListFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ExtendsListFix.java
deleted file mode 100644
index 0b987facaea6..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ExtendsListFix.java
+++ /dev/null
@@ -1,177 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.FileModificationService;
-import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
-import com.intellij.openapi.command.undo.UndoUtil;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.psi.*;
-import com.intellij.psi.codeStyle.JavaCodeStyleManager;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NonNls;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-public class ExtendsListFix extends LocalQuickFixAndIntentionActionOnPsiElement {
- private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.ExtendsListFix");
-
- final PsiClass myClassToExtendFrom;
- private final boolean myToAdd;
- private final PsiClassType myTypeToExtendFrom;
- private final String myName;
-
- public ExtendsListFix(@NotNull PsiClass aClass, @NotNull PsiClassType typeToExtendFrom, boolean toAdd) {
- this(aClass, typeToExtendFrom.resolve(), typeToExtendFrom, toAdd);
- }
-
- public ExtendsListFix(@NotNull PsiClass aClass, @NotNull PsiClass classToExtendFrom, boolean toAdd) {
- this(aClass, classToExtendFrom, JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createType(classToExtendFrom), toAdd);
- }
-
- private ExtendsListFix(@NotNull PsiClass aClass,
- PsiClass classToExtendFrom,
- @NotNull PsiClassType typeToExtendFrom,
- boolean toAdd) {
- super(aClass);
- myClassToExtendFrom = classToExtendFrom;
- myToAdd = toAdd;
- myTypeToExtendFrom = (PsiClassType)GenericsUtil.eliminateWildcards(typeToExtendFrom);
-
- @NonNls final String messageKey;
- if (classToExtendFrom != null && aClass.isInterface() == classToExtendFrom.isInterface()) {
- messageKey = toAdd ? "add.class.to.extends.list" : "remove.class.from.extends.list";
- }
- else {
- messageKey = toAdd ? "add.interface.to.implements.list" : "remove.interface.from.implements.list";
- }
-
- myName = QuickFixBundle.message(messageKey, aClass.getName(), classToExtendFrom == null ? "" : classToExtendFrom instanceof PsiTypeParameter ? classToExtendFrom.getName()
- : classToExtendFrom.getQualifiedName());
- }
-
-
- @Override
- @NotNull
- public String getText() {
- return myName;
- }
-
- @Override
- @NotNull
- public String getFamilyName() {
- return QuickFixBundle.message("change.extends.list.family");
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project,
- @NotNull PsiFile file,
- @NotNull PsiElement startElement,
- @NotNull PsiElement endElement) {
- final PsiClass myClass = (PsiClass)startElement;
- return
- myClass.isValid()
- && myClass.getManager().isInProject(myClass)
- && myClassToExtendFrom != null
- && myClassToExtendFrom.isValid()
- && !myClassToExtendFrom.hasModifierProperty(PsiModifier.FINAL)
- && (myClassToExtendFrom.isInterface()
- || !myClass.isInterface()
- && myClass.getExtendsList() != null
- && myClass.getExtendsList().getReferencedTypes().length == 0 == myToAdd)
- ;
-
- }
-
- @Override
- public void invoke(@NotNull Project project,
- @NotNull PsiFile file,
- @Nullable("is null when called from inspection") Editor editor,
- @NotNull PsiElement startElement,
- @NotNull PsiElement endElement) {
- final PsiClass myClass = (PsiClass)startElement;
- invokeImpl(myClass);
- UndoUtil.markPsiFileForUndo(file);
- }
-
- protected void invokeImpl(PsiClass myClass) {
- if (!FileModificationService.getInstance().prepareFileForWrite(myClass.getContainingFile())) return;
- PsiReferenceList extendsList = !(myClass instanceof PsiTypeParameter) &&
- myClass.isInterface() != myClassToExtendFrom.isInterface() ?
- myClass.getImplementsList() : myClass.getExtendsList();
- PsiReferenceList otherList = extendsList == myClass.getImplementsList() ?
- myClass.getExtendsList() : myClass.getImplementsList();
- try {
- if (extendsList != null) {
- modifyList(extendsList, myToAdd, -1);
- }
- if (otherList != null) {
- modifyList(otherList, false, -1);
- }
- }
- catch (IncorrectOperationException e) {
- LOG.error(e);
- }
- }
-
- /**
- * @param position to add new class to or -1 if add to the end
- */
- PsiReferenceList modifyList(@NotNull PsiReferenceList extendsList, boolean add, int position) throws IncorrectOperationException {
- PsiJavaCodeReferenceElement[] referenceElements = extendsList.getReferenceElements();
- boolean alreadyExtends = false;
- for (PsiJavaCodeReferenceElement referenceElement : referenceElements) {
- if (referenceElement.getManager().areElementsEquivalent(myClassToExtendFrom, referenceElement.resolve())) {
- alreadyExtends = true;
- if (!add) {
- referenceElement.delete();
- }
- }
- }
- PsiReferenceList list = extendsList;
- if (add && !alreadyExtends) {
- PsiElement anchor;
- if (position == -1) {
- anchor = referenceElements.length ==0 ? null : referenceElements[referenceElements.length-1];
- }
- else if (position == 0) {
- anchor = null;
- }
- else {
- anchor = referenceElements[position - 1];
- }
- PsiJavaCodeReferenceElement classReferenceElement =
- JavaPsiFacade.getInstance(extendsList.getProject()).getElementFactory().createReferenceElementByType(myTypeToExtendFrom);
- PsiElement element;
- if (anchor == null) {
- if (referenceElements.length == 0) {
- element = extendsList.add(classReferenceElement);
- }
- else {
- element = extendsList.addBefore(classReferenceElement, referenceElements[0]);
- }
- }
- else {
- element = extendsList.addAfter(classReferenceElement, anchor);
- }
- list = (PsiReferenceList) element.getParent();
- }
- return (PsiReferenceList)JavaCodeStyleManager.getInstance(extendsList.getProject()).shortenClassReferences(list);
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImplementAbstractClassMethodsFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImplementAbstractClassMethodsFix.java
new file mode 100644
index 000000000000..d9c2dbbfb19a
--- /dev/null
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImplementAbstractClassMethodsFix.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2000-2013 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.codeInsight.daemon.impl.quickfix;
+
+import com.intellij.codeInsight.FileModificationService;
+import com.intellij.codeInsight.generation.OverrideImplementUtil;
+import com.intellij.codeInsight.generation.PsiMethodMember;
+import com.intellij.ide.util.MemberChooser;
+import com.intellij.openapi.application.Result;
+import com.intellij.openapi.command.WriteCommandAction;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.*;
+import com.intellij.psi.util.TypeConversionUtil;
+import com.intellij.util.IncorrectOperationException;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ImplementAbstractClassMethodsFix extends ImplementMethodsFix {
+ public ImplementAbstractClassMethodsFix(PsiElement highlightElement) {
+ super(highlightElement);
+ }
+
+ @Override
+ public boolean isAvailable(@NotNull Project project,
+ @NotNull PsiFile file,
+ @NotNull PsiElement startElement,
+ @NotNull PsiElement endElement) {
+ if (startElement instanceof PsiNewExpression) {
+ final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
+ String startElementText = startElement.getText();
+ try {
+ PsiNewExpression newExpression =
+ (PsiNewExpression)elementFactory.createExpressionFromText(startElementText + "{}", startElement);
+ if (newExpression.getAnonymousClass() == null) {
+ try {
+ newExpression = (PsiNewExpression)elementFactory.createExpressionFromText(startElementText + "){}", startElement);
+ }
+ catch (IncorrectOperationException e) {
+ return false;
+ }
+ if (newExpression.getAnonymousClass() == null) return false;
+ }
+ }
+ catch (IncorrectOperationException e) {
+ return false;
+ }
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public void invoke(@NotNull final Project project,
+ @NotNull PsiFile file,
+ @Nullable("is null when called from inspection") final Editor editor,
+ @NotNull final PsiElement startElement,
+ @NotNull PsiElement endElement) {
+ final PsiFile containingFile = startElement.getContainingFile();
+ if (editor == null || !FileModificationService.getInstance().prepareFileForWrite(containingFile)) return;
+ PsiJavaCodeReferenceElement classReference = ((PsiNewExpression)startElement).getClassReference();
+ if (classReference == null) return;
+ final PsiClass psiClass = (PsiClass)classReference.resolve();
+ if (psiClass == null) return;
+ final MemberChooser<PsiMethodMember> chooser = chooseMethodsToImplement(editor, startElement, psiClass, false);
+ if (chooser == null) return;
+
+ final List<PsiMethodMember> selectedElements = chooser.getSelectedElements();
+ if (selectedElements == null || selectedElements.isEmpty()) return;
+
+ new WriteCommandAction(project, file) {
+ @Override
+ protected void run(final Result result) throws Throwable {
+ PsiNewExpression newExpression =
+ (PsiNewExpression)JavaPsiFacade.getElementFactory(project).createExpressionFromText(startElement.getText() + "{}", startElement);
+ newExpression = (PsiNewExpression)startElement.replace(newExpression);
+ final PsiClass psiClass = newExpression.getAnonymousClass();
+ if (psiClass == null) return;
+ Map<PsiClass, PsiSubstitutor> subst = new HashMap<PsiClass, PsiSubstitutor>();
+ for (PsiMethodMember selectedElement : selectedElements) {
+ final PsiClass baseClass = selectedElement.getElement().getContainingClass();
+ if (baseClass != null) {
+ PsiSubstitutor substitutor = subst.get(baseClass);
+ if (substitutor == null) {
+ substitutor = TypeConversionUtil.getSuperClassSubstitutor(baseClass, psiClass, PsiSubstitutor.EMPTY);
+ subst.put(baseClass, substitutor);
+ }
+ selectedElement.setSubstitutor(substitutor);
+ }
+ }
+ OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(editor, psiClass, selectedElements, chooser.isCopyJavadoc(),
+ chooser.isInsertOverrideAnnotation());
+ }
+ }.execute();
+ }
+}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImportClassFixBase.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImportClassFixBase.java
index abc31adfaf57..ad235264421d 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImportClassFixBase.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImportClassFixBase.java
@@ -19,10 +19,9 @@ import com.intellij.codeInsight.CodeInsightSettings;
import com.intellij.codeInsight.CodeInsightUtil;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.completion.JavaCompletionUtil;
-import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzerSettings;
import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl;
+import com.intellij.codeInsight.daemon.impl.DaemonListeners;
import com.intellij.codeInsight.daemon.impl.ShowAutoImportPass;
import com.intellij.codeInsight.daemon.impl.actions.AddImportAction;
import com.intellij.codeInsight.hint.HintManager;
@@ -282,8 +281,6 @@ public abstract class ImportClassFixBase<T extends PsiElement, R extends PsiRefe
final QuestionAction action = createAddImportAction(classes, project, editor);
- DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(project);
-
boolean canImportHere = true;
if (classes.length == 1
@@ -291,7 +288,7 @@ public abstract class ImportClassFixBase<T extends PsiElement, R extends PsiRefe
&& (FileTypeUtils.isInServerPageFile(psiFile) ?
CodeInsightSettings.getInstance().JSP_ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY :
CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY)
- && (ApplicationManager.getApplication().isUnitTestMode() || codeAnalyzer.canChangeFileSilently(psiFile))
+ && (ApplicationManager.getApplication().isUnitTestMode() || DaemonListeners.canChangeFileSilently(psiFile))
&& !autoImportWillInsertUnexpectedCharacters(classes[0])
&& !LaterInvocator.isInModalContext()
) {
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/InsertNewFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/InsertNewFix.java
index c8342d406c92..a418efb4da15 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/InsertNewFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/InsertNewFix.java
@@ -32,7 +32,7 @@ public class InsertNewFix implements IntentionAction {
private final PsiMethodCallExpression myMethodCall;
private final PsiClass myClass;
- public InsertNewFix(PsiMethodCallExpression methodCall, PsiClass aClass) {
+ public InsertNewFix(@NotNull PsiMethodCallExpression methodCall, @NotNull PsiClass aClass) {
myMethodCall = methodCall;
myClass = aClass;
}
@@ -51,9 +51,7 @@ public class InsertNewFix implements IntentionAction {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- return myMethodCall != null
- && myMethodCall.isValid()
- && myMethodCall.getManager().isInProject(myMethodCall);
+ return myMethodCall.isValid() && myMethodCall.getManager().isInProject(myMethodCall);
}
@Override
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/InsertSuperFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/InsertSuperFix.java
index 113df122dce4..dd0664c4ead2 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/InsertSuperFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/InsertSuperFix.java
@@ -34,7 +34,7 @@ public class InsertSuperFix implements IntentionAction, HighPriorityAction {
private final PsiMethod myConstructor;
- public InsertSuperFix(PsiMethod constructor) {
+ public InsertSuperFix(@NotNull PsiMethod constructor) {
myConstructor = constructor;
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MakeVarargParameterLastFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MakeVarargParameterLastFix.java
index c7b9579a67f9..4e57a648134d 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MakeVarargParameterLastFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MakeVarargParameterLastFix.java
@@ -29,7 +29,7 @@ import org.jetbrains.annotations.NotNull;
* @author ven
*/
public class MakeVarargParameterLastFix implements IntentionAction {
- public MakeVarargParameterLastFix(PsiParameter parameter) {
+ public MakeVarargParameterLastFix(@NotNull PsiParameter parameter) {
myParameter = parameter;
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MethodArgumentFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MethodArgumentFix.java
deleted file mode 100644
index afed0b1946b7..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MethodArgumentFix.java
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.FileModificationService;
-import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.psi.*;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NotNull;
-
-/**
- * @author ven
- */
-public abstract class MethodArgumentFix implements IntentionAction {
- private static final Logger LOG = Logger.getInstance(MethodArgumentFix.class);
-
- protected final PsiExpressionList myArgList;
- protected final int myIndex;
- private final ArgumentFixerActionFactory myArgumentFixerActionFactory;
- protected final PsiType myToType;
-
- protected MethodArgumentFix(PsiExpressionList list, int i, PsiType toType, ArgumentFixerActionFactory fixerActionFactory) {
- myArgList = list;
- myIndex = i;
- myArgumentFixerActionFactory = fixerActionFactory;
- myToType = toType instanceof PsiEllipsisType ? ((PsiEllipsisType) toType).toArrayType() : toType;
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- return
- myToType != null
- && myToType.isValid()
- && myArgList != null
- && myArgList.getExpressions().length > myIndex
- && myArgList.getExpressions()[myIndex] != null
- && myArgList.getExpressions()[myIndex].isValid();
- }
-
- @Override
- public boolean startInWriteAction() {
- return true;
- }
-
- @Override
- public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
- if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
- PsiExpression expression = myArgList.getExpressions()[myIndex];
-
- try {
- LOG.assertTrue(expression != null && expression.isValid());
- PsiExpression modified = myArgumentFixerActionFactory.getModifiedArgument(expression, myToType);
- LOG.assertTrue(modified != null, myArgumentFixerActionFactory);
- expression.replace(modified);
- }
- catch (IncorrectOperationException e) {
- LOG.error(e);
- }
- }
-
-
- @Override
- @NotNull
- public String getFamilyName() {
- return QuickFixBundle.message("fix.argument.family");
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveBoundClassToFrontFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveBoundClassToFrontFix.java
index 59ae27d6eb75..ca2ecfd62ed5 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveBoundClassToFrontFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveBoundClassToFrontFix.java
@@ -31,7 +31,7 @@ public class MoveBoundClassToFrontFix extends ExtendsListFix {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.MoveBoundClassToFrontFix");
private final String myName;
- public MoveBoundClassToFrontFix(PsiClass aClass, PsiClassType classToExtendFrom) {
+ public MoveBoundClassToFrontFix(@NotNull PsiClass aClass, @NotNull PsiClassType classToExtendFrom) {
super(aClass, classToExtendFrom, true);
myName = QuickFixBundle.message("move.bound.class.to.front.fix.text",
HighlightUtil.formatClass(myClassToExtendFrom),
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveClassToModuleFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveClassToModuleFix.java
index 27b217bf2b01..d8caac1d59a6 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveClassToModuleFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveClassToModuleFix.java
@@ -46,7 +46,9 @@ import org.codehaus.groovy.util.ListHashMap;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
+import java.util.List;
import java.util.Map;
/**
@@ -184,9 +186,9 @@ public class MoveClassToModuleFix implements IntentionAction {
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final Module currentModule = fileIndex.getModuleForFile(classVFile);
if (currentModule == null) return;
- VirtualFile[] sourceRoots = ModuleRootManager.getInstance(currentModule).getSourceRoots();
- if (sourceRoots.length == 0) return;
- final PsiDirectory sourceDirectory = PsiManager.getInstance(project).findDirectory(sourceRoots[0]);
+ List<VirtualFile> sourceRoots = ModuleRootManager.getInstance(currentModule).getSourceRoots(JavaModuleSourceRootTypes.SOURCES);
+ if (sourceRoots.isEmpty()) return;
+ final PsiDirectory sourceDirectory = PsiManager.getInstance(project).findDirectory(sourceRoots.get(0));
if (sourceDirectory == null) return;
VirtualFile vsourceRoot = fileIndex.getSourceRootForFile(classVFile);
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveClassToSeparateFileFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveClassToSeparateFileFix.java
index 36370501f68c..a949408f4a92 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveClassToSeparateFileFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MoveClassToSeparateFileFix.java
@@ -37,7 +37,7 @@ public class MoveClassToSeparateFileFix implements IntentionAction {
private final PsiClass myClass;
- public MoveClassToSeparateFileFix(PsiClass aClass) {
+ public MoveClassToSeparateFileFix(@NotNull PsiClass aClass) {
myClass = aClass;
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/PermuteArgumentsFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/PermuteArgumentsFix.java
deleted file mode 100644
index fe149cfd03e0..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/PermuteArgumentsFix.java
+++ /dev/null
@@ -1,193 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.FileModificationService;
-import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.psi.*;
-import com.intellij.psi.infos.CandidateInfo;
-import com.intellij.psi.infos.MethodCandidateInfo;
-import com.intellij.psi.util.PsiUtil;
-import com.intellij.psi.util.TypeConversionUtil;
-import com.intellij.util.ArrayUtil;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NotNull;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * @author cdr
- */
-public class PermuteArgumentsFix implements IntentionAction {
- private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.PermuteArgumentsFix");
- private final PsiCall myCall;
- private final PsiCall myPermutation;
-
- private PermuteArgumentsFix(@NotNull PsiCall call, @NotNull PsiCall permutation) {
- myCall = call;
- myPermutation = permutation;
- }
-
- @Override
- public boolean startInWriteAction() {
- return true;
- }
-
-
- @Override
- @NotNull
- public String getText() {
- return QuickFixBundle.message("permute.arguments");
- }
-
-
- @Override
- @NotNull
- public String getFamilyName() {
- return getText();
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- return !project.isDisposed() && myCall.isValid() && myCall.getManager().isInProject(myCall);
- }
-
- @Override
- public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
- if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
- myCall.getArgumentList().replace(myPermutation.getArgumentList());
- }
-
- public static void registerFix(HighlightInfo info, PsiCall callExpression, final CandidateInfo[] candidates, final TextRange fixRange) {
- PsiExpression[] expressions = callExpression.getArgumentList().getExpressions();
- if (expressions.length < 2) return;
- List<PsiCall> permutations = new ArrayList<PsiCall>();
-
- for (CandidateInfo candidate : candidates) {
- if (candidate instanceof MethodCandidateInfo) {
- MethodCandidateInfo methodCandidate = (MethodCandidateInfo)candidate;
- PsiMethod method = methodCandidate.getElement();
- PsiSubstitutor substitutor = methodCandidate.getSubstitutor();
-
- PsiParameter[] parameters = method.getParameterList().getParameters();
- if (expressions.length != parameters.length || parameters.length ==0) continue;
- int minIncompatibleIndex = parameters.length;
- int maxIncompatibleIndex = 0;
- int incompatibilitiesCount = 0;
- for (int i = 0; i < parameters.length; i++) {
- PsiParameter parameter = parameters[i];
- PsiType type = substitutor.substitute(parameter.getType());
- if (TypeConversionUtil.areTypesAssignmentCompatible(type, expressions[i])) continue;
- if (minIncompatibleIndex == parameters.length) minIncompatibleIndex = i;
- maxIncompatibleIndex = i;
- incompatibilitiesCount++;
- }
-
- try {
- registerSwapFixes(expressions, callExpression, permutations, methodCandidate, incompatibilitiesCount, minIncompatibleIndex, maxIncompatibleIndex);
- registerShiftFixes(expressions, callExpression, permutations, methodCandidate, minIncompatibleIndex, maxIncompatibleIndex);
- }
- catch (IncorrectOperationException e) {
- LOG.error(e);
- }
- }
- }
- if (permutations.size() == 1) {
- PermuteArgumentsFix fix = new PermuteArgumentsFix(callExpression, permutations.get(0));
- QuickFixAction.registerQuickFixAction(info, fixRange, fix);
- }
- }
-
- private static void registerShiftFixes(final PsiExpression[] expressions, final PsiCall callExpression, final List<PsiCall> permutations,
- final MethodCandidateInfo methodCandidate, final int minIncompatibleIndex, final int maxIncompatibleIndex)
- throws IncorrectOperationException {
- PsiMethod method = methodCandidate.getElement();
- PsiSubstitutor substitutor = methodCandidate.getSubstitutor();
- // shift range should include both incompatible indexes
- for (int i = 0; i <= minIncompatibleIndex; i++) {
- for (int j = Math.max(i+2,maxIncompatibleIndex); j < expressions.length; j++) { // if j=i+1 the shift is equal to swap
- {
- ArrayUtil.rotateLeft(expressions, i, j);
- if (PsiUtil.isApplicable(method, substitutor, expressions)) {
- PsiCall copy = (PsiCall)callExpression.copy();
- PsiExpression[] copyExpressions = copy.getArgumentList().getExpressions();
- for (int k = i; k < copyExpressions.length; k++) {
- copyExpressions[k].replace(expressions[k]);
- }
-
- JavaResolveResult result = copy.resolveMethodGenerics();
- if (result.getElement() != null && result.isValidResult()) {
- permutations.add(copy);
- if (permutations.size() > 1) return;
- }
- }
- ArrayUtil.rotateRight(expressions, i, j);
- }
-
- {
- ArrayUtil.rotateRight(expressions, i, j);
- if (PsiUtil.isApplicable(method, substitutor, expressions)) {
- PsiCall copy = (PsiCall)callExpression.copy();
- PsiExpression[] copyExpressions = copy.getArgumentList().getExpressions();
- for (int k = i; k < copyExpressions.length; k++) {
- copyExpressions[k].replace(expressions[k]);
- }
-
- JavaResolveResult result = copy.resolveMethodGenerics();
- if (result.getElement() != null && result.isValidResult()) {
- permutations.add(copy);
- if (permutations.size() > 1) return;
- }
- }
- ArrayUtil.rotateLeft(expressions, i, j);
- }
- }
- }
- }
-
- private static void registerSwapFixes(final PsiExpression[] expressions, final PsiCall callExpression, final List<PsiCall> permutations,
- MethodCandidateInfo candidate, final int incompatibilitiesCount, final int minIncompatibleIndex,
- final int maxIncompatibleIndex) throws IncorrectOperationException {
- PsiMethod method = candidate.getElement();
- PsiSubstitutor substitutor = candidate.getSubstitutor();
- if (incompatibilitiesCount >= 3) return; // no way we can fix it by swapping
-
- for (int i = minIncompatibleIndex; i < maxIncompatibleIndex; i++) {
- for (int j = i+1; j <= maxIncompatibleIndex; j++) {
- ArrayUtil.swap(expressions, i, j);
- if (PsiUtil.isApplicable(method, substitutor, expressions)) {
- PsiCall copy = (PsiCall)callExpression.copy();
- PsiExpression[] copyExpressions = copy.getArgumentList().getExpressions();
- copyExpressions[i].replace(expressions[i]);
- copyExpressions[j].replace(expressions[j]);
- JavaResolveResult result = copy.resolveMethodGenerics();
- if (result.getElement() != null && result.isValidResult()) {
- permutations.add(copy);
- if (permutations.size() > 1) return;
- }
- }
- ArrayUtil.swap(expressions, i, j);
- }
- }
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/PullAsAbstractUpFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/PullAsAbstractUpFix.java
index 5d5482c07140..c02aa779e12a 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/PullAsAbstractUpFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/PullAsAbstractUpFix.java
@@ -16,7 +16,7 @@
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.FileModificationService;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
+import com.intellij.codeInsight.daemon.QuickFixActionRegistrar;
import com.intellij.codeInsight.intention.impl.RunRefactoringAction;
import com.intellij.codeInsight.navigation.NavigationUtil;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
@@ -135,7 +135,7 @@ public class PullAsAbstractUpFix extends LocalQuickFixAndIntentionActionOnPsiEle
return false;
}
- public static void registerQuickFix(HighlightInfo highlightInfo, PsiMethod methodWithOverrides) {
+ public static void registerQuickFix(@NotNull PsiMethod methodWithOverrides, @NotNull QuickFixActionRegistrar registrar) {
PsiClass containingClass = methodWithOverrides.getContainingClass();
if (containingClass == null) return;
final PsiManager manager = containingClass.getManager();
@@ -164,14 +164,14 @@ public class PullAsAbstractUpFix extends LocalQuickFixAndIntentionActionOnPsiEle
name+= " and make it abstract";
}
}
- QuickFixAction.registerQuickFixAction(highlightInfo, new RunRefactoringAction(new ExtractInterfaceHandler(), "Extract interface"));
- QuickFixAction.registerQuickFixAction(highlightInfo, new RunRefactoringAction(new ExtractSuperclassHandler(), "Extract superclass"));
+ registrar.register(new RunRefactoringAction(new ExtractInterfaceHandler(), "Extract interface"));
+ registrar.register(new RunRefactoringAction(new ExtractSuperclassHandler(), "Extract superclass"));
}
if (canBePulledUp) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new RunRefactoringAction(new JavaPullUpHandler(), "Pull members up"));
+ registrar.register(new RunRefactoringAction(new JavaPullUpHandler(), "Pull members up"));
}
- QuickFixAction.registerQuickFixAction(highlightInfo, new PullAsAbstractUpFix(methodWithOverrides, name));
+ registrar.register(new PullAsAbstractUpFix(methodWithOverrides, name));
}
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/QualifyThisArgumentFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/QualifyThisArgumentFix.java
deleted file mode 100644
index 1fdef3108ea3..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/QualifyThisArgumentFix.java
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * Copyright 2000-2011 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * Created by IntelliJ IDEA.
- * User: cdr
- * Date: Nov 13, 2002
- * Time: 3:26:50 PM
- * To change this template use Options | File Templates.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.psi.*;
-import com.intellij.psi.infos.CandidateInfo;
-import com.intellij.psi.util.PsiTreeUtil;
-import com.intellij.psi.util.PsiUtil;
-import com.intellij.psi.util.TypeConversionUtil;
-import com.intellij.refactoring.util.RefactoringChangeUtil;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NotNull;
-
-import java.util.HashSet;
-import java.util.Set;
-
-public class QualifyThisArgumentFix extends PsiElementBaseIntentionAction {
- private final PsiThisExpression myExpression;
- private final PsiClass myPsiClass;
-
-
- public QualifyThisArgumentFix(@NotNull PsiThisExpression expression, @NotNull PsiClass psiClass) {
- myExpression = expression;
- myPsiClass = psiClass;
- }
-
-
- @Override
- public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
- if (!myExpression.isValid()) return false;
- if (!myPsiClass.isValid()) return false;
- setText("Qualify this expression with \'" + myPsiClass.getQualifiedName() + "\'");
- return true;
- }
-
- @NotNull
- @Override
- public String getFamilyName() {
- return "Qualify this";
- }
-
- @Override
- public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
- myExpression.replace(RefactoringChangeUtil.createThisExpression(PsiManager.getInstance(project), myPsiClass));
- }
-
- public static void registerQuickFixAction(CandidateInfo[] candidates, PsiCall call, HighlightInfo highlightInfo, final TextRange fixRange) {
- if (candidates.length == 0) return;
-
- final Set<PsiClass> containingClasses = new HashSet<PsiClass>();
- PsiClass parentClass = PsiTreeUtil.getParentOfType(call, PsiClass.class);
- while (parentClass != null) {
- if (parentClass.hasModifierProperty(PsiModifier.STATIC)) break;
- if (!(parentClass instanceof PsiAnonymousClass)) {
- containingClasses.add(parentClass);
- }
- parentClass = PsiTreeUtil.getParentOfType(parentClass, PsiClass.class, true);
- }
- if (containingClasses.isEmpty()) return;
-
- final PsiExpressionList list = call.getArgumentList();
- final PsiExpression[] expressions = list.getExpressions();
- if (expressions.length == 0) return;
-
- for (int i1 = 0, expressionsLength = expressions.length; i1 < expressionsLength; i1++) {
- final PsiExpression expression = expressions[i1];
- if (expression instanceof PsiThisExpression) {
- final PsiType exprType = expression.getType();
- for (CandidateInfo candidate : candidates) {
- PsiMethod method = (PsiMethod)candidate.getElement();
- PsiSubstitutor substitutor = candidate.getSubstitutor();
- assert method != null;
- PsiParameter[] parameters = method.getParameterList().getParameters();
- if (expressions.length != parameters.length) {
- continue;
- }
-
- PsiParameter parameter = parameters[i1];
-
- PsiType parameterType = substitutor.substitute(parameter.getType());
- if (exprType == null || parameterType == null) {
- continue;
- }
-
- if (!TypeConversionUtil.isAssignable(parameterType, exprType)) {
- final PsiClass psiClass = PsiUtil.resolveClassInClassTypeOnly(parameterType);
- if (psiClass != null && containingClasses.contains(psiClass)) {
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new QualifyThisArgumentFix((PsiThisExpression)expression, psiClass));
- }
- }
- }
- }
- }
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveNewQualifierFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveNewQualifierFix.java
index 84c2774fbd06..fff92ff01a4c 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveNewQualifierFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveNewQualifierFix.java
@@ -38,7 +38,7 @@ public class RemoveNewQualifierFix implements IntentionAction {
private final PsiNewExpression expression;
private final PsiClass aClass;
- public RemoveNewQualifierFix(PsiNewExpression expression, PsiClass aClass) {
+ public RemoveNewQualifierFix(@NotNull PsiNewExpression expression, PsiClass aClass) {
this.expression = expression;
this.aClass = aClass;
}
@@ -58,10 +58,7 @@ public class RemoveNewQualifierFix implements IntentionAction {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return
- expression != null
- && expression.isValid()
- && (aClass == null || aClass.isValid())
- && expression.getManager().isInProject(expression);
+ expression.isValid() && (aClass == null || aClass.isValid()) && expression.getManager().isInProject(expression);
}
@Override
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveRedundantArgumentsFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveRedundantArgumentsFix.java
deleted file mode 100644
index 8ca41951e5ce..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveRedundantArgumentsFix.java
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * Copyright 2000-2012 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.FileModificationService;
-import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightUtil;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.TextRange;
-import com.intellij.psi.*;
-import com.intellij.psi.util.TypeConversionUtil;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.Arrays;
-
-/**
- * @author Danila Ponomarenko
- */
-public class RemoveRedundantArgumentsFix implements IntentionAction {
- private final PsiMethod myTargetMethod;
- private final PsiExpression[] myArguments;
- private final PsiSubstitutor mySubstitutor;
-
- private RemoveRedundantArgumentsFix(@NotNull PsiMethod targetMethod,
- @NotNull PsiExpression[] arguments,
- @NotNull PsiSubstitutor substitutor) {
- myTargetMethod = targetMethod;
- myArguments = arguments;
- mySubstitutor = substitutor;
- }
-
- @NotNull
- @Override
- public String getText() {
- return QuickFixBundle.message("remove.redundant.arguments.text", JavaHighlightUtil.formatMethod(myTargetMethod));
- }
-
- @NotNull
- @Override
- public String getFamilyName() {
- return QuickFixBundle.message("remove.redundant.arguments.family");
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- if (!myTargetMethod.isValid() || myTargetMethod.getContainingClass() == null) return false;
- for (PsiExpression expression : myArguments) {
- if (!expression.isValid()) return false;
- }
- if (!mySubstitutor.isValid()) return false;
-
- return findRedundantArgument(myArguments, myTargetMethod.getParameterList().getParameters(), mySubstitutor) != null;
- }
-
- @Nullable
- private static PsiExpression[] findRedundantArgument(@NotNull PsiExpression[] arguments,
- @NotNull PsiParameter[] parameters,
- @NotNull PsiSubstitutor substitutor) {
- if (arguments.length <= parameters.length) return null;
-
- for (int i = 0; i < parameters.length; i++) {
- final PsiExpression argument = arguments[i];
- final PsiParameter parameter = parameters[i];
-
- final PsiType argumentType = argument.getType();
- if (argumentType == null) return null;
- final PsiType parameterType = substitutor.substitute(parameter.getType());
-
- if (!TypeConversionUtil.isAssignable(parameterType, argumentType)) {
- return null;
- }
- }
-
- return Arrays.copyOfRange(arguments, parameters.length, arguments.length);
- }
-
- @Override
- public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
- if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
- final PsiExpression[] redundantArguments = findRedundantArgument(myArguments, myTargetMethod.getParameterList().getParameters(), mySubstitutor);
- if (redundantArguments != null) {
- for (PsiExpression argument : redundantArguments) {
- argument.delete();
- }
- }
- }
-
- @Override
- public boolean startInWriteAction() {
- return true;
- }
-
- public static void registerIntentions(@NotNull JavaResolveResult[] candidates,
- @NotNull PsiExpressionList arguments,
- @Nullable HighlightInfo highlightInfo,
- TextRange fixRange) {
- for (JavaResolveResult candidate : candidates) {
- registerIntention(arguments, highlightInfo, fixRange, candidate, arguments);
- }
- }
-
- private static void registerIntention(@NotNull PsiExpressionList arguments,
- @Nullable HighlightInfo highlightInfo,
- TextRange fixRange,
- @NotNull JavaResolveResult candidate,
- @NotNull PsiElement context) {
- if (!candidate.isStaticsScopeCorrect()) return;
- PsiMethod method = (PsiMethod)candidate.getElement();
- PsiSubstitutor substitutor = candidate.getSubstitutor();
- if (method != null && context.getManager().isInProject(method)) {
- QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new RemoveRedundantArgumentsFix(method, arguments.getExpressions(), substitutor));
- }
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveTypeArgumentsFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveTypeArgumentsFix.java
index d0a5eea3c48b..18f100a815af 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveTypeArgumentsFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/RemoveTypeArgumentsFix.java
@@ -31,7 +31,7 @@ import org.jetbrains.annotations.Nullable;
public class RemoveTypeArgumentsFix extends LocalQuickFixAndIntentionActionOnPsiElement implements HighPriorityAction {
private static final Logger LOGGER = Logger.getInstance("#" + RemoveTypeArgumentsFix.class.getName());
- public RemoveTypeArgumentsFix(@Nullable PsiElement element) {
+ public RemoveTypeArgumentsFix(@NotNull PsiElement element) {
super(element);
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceAddAllArrayToCollectionFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceAddAllArrayToCollectionFix.java
index b0c76256c167..57ac922eb361 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceAddAllArrayToCollectionFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceAddAllArrayToCollectionFix.java
@@ -42,7 +42,7 @@ import org.jetbrains.annotations.NotNull;
public class ReplaceAddAllArrayToCollectionFix implements IntentionAction {
private final PsiMethodCallExpression myMethodCall;
- public ReplaceAddAllArrayToCollectionFix(final PsiMethodCallExpression methodCall) {
+ public ReplaceAddAllArrayToCollectionFix(@NotNull PsiMethodCallExpression methodCall) {
myMethodCall = methodCall;
}
@@ -60,7 +60,7 @@ public class ReplaceAddAllArrayToCollectionFix implements IntentionAction {
@Override
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
- if (myMethodCall == null || !myMethodCall.isValid()) return false;
+ if (!myMethodCall.isValid()) return false;
final Module module = ModuleUtilCore.findModuleForPsiElement(file);
if (module == null) return false;
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceInaccessibleFieldWithGetterSetterFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceInaccessibleFieldWithGetterSetterFix.java
index 12176d337f0f..dfe7399ee3f9 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceInaccessibleFieldWithGetterSetterFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceInaccessibleFieldWithGetterSetterFix.java
@@ -16,16 +16,12 @@
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.FileModificationService;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.generation.GenerateMembersUtil;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
-import com.intellij.psi.util.PropertyUtil;
import com.intellij.psi.util.PsiTreeUtil;
-import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -33,7 +29,7 @@ public class ReplaceInaccessibleFieldWithGetterSetterFix extends LocalQuickFixAn
private final String myMethodName;
private final boolean myIsSetter;
- protected ReplaceInaccessibleFieldWithGetterSetterFix(@Nullable PsiElement element, PsiMethod getter, boolean isSetter) {
+ public ReplaceInaccessibleFieldWithGetterSetterFix(@NotNull PsiElement element, @NotNull PsiMethod getter, boolean isSetter) {
super(element);
myMethodName = getter.getName();
myIsSetter = isSetter;
@@ -82,30 +78,4 @@ public class ReplaceInaccessibleFieldWithGetterSetterFix extends LocalQuickFixAn
public String getFamilyName() {
return "Replace with getter/setter";
}
-
- public static void registerQuickFix(PsiMember refElement,
- PsiJavaCodeReferenceElement place,
- PsiClass accessObjectClass,
- HighlightInfo error) {
- if (refElement instanceof PsiField && place instanceof PsiReferenceExpression) {
- final PsiField psiField = (PsiField)refElement;
- final PsiClass containingClass = psiField.getContainingClass();
- if (containingClass != null) {
- if (PsiUtil.isOnAssignmentLeftHand((PsiExpression)place)) {
- final PsiMethod setterPrototype = PropertyUtil.generateSetterPrototype(psiField);
- final PsiMethod setter = containingClass.findMethodBySignature(setterPrototype, true);
- if (setter != null && PsiUtil.isAccessible(setter, place, accessObjectClass)) {
- QuickFixAction.registerQuickFixAction(error, new ReplaceInaccessibleFieldWithGetterSetterFix(place, setter, true));
- }
- }
- else if (PsiUtil.isAccessedForReading((PsiExpression)place)) {
- final PsiMethod getterPrototype = GenerateMembersUtil.generateGetterPrototype(psiField);
- final PsiMethod getter = containingClass.findMethodBySignature(getterPrototype, true);
- if (getter != null && PsiUtil.isAccessible(getter, place, accessObjectClass)) {
- QuickFixAction.registerQuickFixAction(error, new ReplaceInaccessibleFieldWithGetterSetterFix(place, getter, false));
- }
- }
- }
- }
- }
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/ReplacePrimitiveWithBoxedTypeAction.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplacePrimitiveWithBoxedTypeAction.java
index 8f6f42dca2ab..95ed0c7cfb02 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/ReplacePrimitiveWithBoxedTypeAction.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplacePrimitiveWithBoxedTypeAction.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.intellij.codeInsight.daemon.impl.analysis;
+package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.openapi.diagnostic.Logger;
@@ -26,12 +26,12 @@ import org.jetbrains.annotations.Nullable;
/**
* User: anna
*/
-class ReplacePrimitiveWithBoxedTypeAction extends LocalQuickFixAndIntentionActionOnPsiElement {
+public class ReplacePrimitiveWithBoxedTypeAction extends LocalQuickFixAndIntentionActionOnPsiElement {
private final String myPrimitiveName;
private final String myBoxedTypeName;
private static final Logger LOG = Logger.getInstance("#" + ReplacePrimitiveWithBoxedTypeAction.class.getName());
- protected ReplacePrimitiveWithBoxedTypeAction(@Nullable PsiTypeElement element, String typeName, String boxedTypeName) {
+ public ReplacePrimitiveWithBoxedTypeAction(@NotNull PsiTypeElement element, @NotNull String typeName, @NotNull String boxedTypeName) {
super(element);
myPrimitiveName = typeName;
myBoxedTypeName = boxedTypeName;
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SuperMethodReturnFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SuperMethodReturnFix.java
index 6f48890e2f4c..74e4f145bddf 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SuperMethodReturnFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SuperMethodReturnFix.java
@@ -37,7 +37,7 @@ public class SuperMethodReturnFix implements IntentionAction {
private final PsiType mySuperMethodType;
private final PsiMethod mySuperMethod;
- public SuperMethodReturnFix(PsiMethod superMethod, PsiType superMethodType) {
+ public SuperMethodReturnFix(@NotNull PsiMethod superMethod, @NotNull PsiType superMethodType) {
mySuperMethodType = superMethodType;
mySuperMethod = superMethod;
}
@@ -64,11 +64,7 @@ public class SuperMethodReturnFix implements IntentionAction {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return
- mySuperMethod != null
- && mySuperMethod.isValid()
- && mySuperMethod.getManager().isInProject(mySuperMethod)
- && mySuperMethodType != null
- && mySuperMethodType.isValid();
+ mySuperMethod.isValid() && mySuperMethod.getManager().isInProject(mySuperMethod) && mySuperMethodType.isValid();
}
@Override
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithArrayFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithArrayFix.java
index ffa8b82e9eb3..e4c148bb8b2e 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithArrayFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithArrayFix.java
@@ -30,7 +30,7 @@ import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
-import com.intellij.util.ArrayUtil;
+import com.intellij.util.ArrayUtilRt;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -40,9 +40,11 @@ import java.util.Collection;
public class SurroundWithArrayFix extends PsiElementBaseIntentionAction {
private final PsiCall myMethodCall;
+ @Nullable private final PsiExpression myExpression;
- public SurroundWithArrayFix(final PsiCall methodCall) {
+ public SurroundWithArrayFix(@Nullable PsiCall methodCall, @Nullable PsiExpression expression) {
myMethodCall = methodCall;
+ myExpression = expression;
}
@Override
@@ -64,7 +66,9 @@ public class SurroundWithArrayFix extends PsiElementBaseIntentionAction {
@Nullable
protected PsiExpression getExpression(PsiElement element) {
- if (myMethodCall == null || !myMethodCall.isValid()) return null;
+ if (myMethodCall == null || !myMethodCall.isValid()) {
+ return myExpression == null || !myExpression.isValid() ? null : myExpression;
+ }
final PsiElement method = myMethodCall.resolveMethod();
if (method != null) {
final PsiMethod psiMethod = (PsiMethod)method;
@@ -99,7 +103,7 @@ public class SurroundWithArrayFix extends PsiElementBaseIntentionAction {
return expression;
}
final PsiClass psiClass = PsiUtil.resolveClassInType(componentType);
- if (ArrayUtil.find(psiMethod.getTypeParameters(), psiClass) != -1) {
+ if (ArrayUtilRt.find(psiMethod.getTypeParameters(), psiClass) != -1) {
for (PsiClassType superType : psiClass.getSuperTypes()) {
if (TypeConversionUtil.isAssignable(superType, expressionType)) return expression;
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableAccessFromInnerClassFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableAccessFromInnerClassFix.java
index 1070533e94ca..131260f47cf7 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableAccessFromInnerClassFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableAccessFromInnerClassFix.java
@@ -48,7 +48,7 @@ public class VariableAccessFromInnerClassFix implements IntentionAction {
private static final int COPY_TO_FINAL = 2;
private static final Key<Map<PsiVariable,Boolean>>[] VARS = new Key[] {Key.create("VARS_TO_MAKE_FINAL"), Key.create("VARS_TO_TRANSFORM"), Key.create("???")};
- public VariableAccessFromInnerClassFix(PsiVariable variable, PsiClass aClass) {
+ public VariableAccessFromInnerClassFix(@NotNull PsiVariable variable, @NotNull PsiClass aClass) {
myVariable = variable;
myClass = aClass;
myFixType = getQuickFixType(variable);
@@ -86,14 +86,12 @@ public class VariableAccessFromInnerClassFix implements IntentionAction {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- return myClass != null
- && myClass.isValid()
- && myClass.getManager().isInProject(myClass)
- && myVariable != null
- && myVariable.isValid()
- && myFixType != -1
- && !getVariablesToFix().isEmpty()
- && !inOwnInitializer (myVariable, myClass);
+ return myClass.isValid() &&
+ myClass.getManager().isInProject(myClass) &&
+ myVariable.isValid() &&
+ myFixType != -1 &&
+ !getVariablesToFix().isEmpty() &&
+ !inOwnInitializer(myVariable, myClass);
}
private static boolean inOwnInitializer(PsiVariable variable, PsiClass aClass) {
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableParameterizedTypeFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableParameterizedTypeFix.java
deleted file mode 100644
index 60ba6789c85a..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableParameterizedTypeFix.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.daemon.impl.analysis.GenericsHighlightUtil;
-import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil;
-import com.intellij.openapi.project.DumbService;
-import com.intellij.openapi.projectRoots.JavaSdkVersion;
-import com.intellij.openapi.projectRoots.JavaVersionService;
-import com.intellij.psi.*;
-import com.intellij.psi.search.GlobalSearchScope;
-import com.intellij.psi.search.PsiShortNamesCache;
-
-import java.util.HashMap;
-
-public class VariableParameterizedTypeFix {
- public static void registerIntentions(HighlightInfo highlightInfo, PsiVariable variable, PsiReferenceParameterList parameterList) {
- PsiType type = variable.getType();
- if (!(type instanceof PsiClassType)) return;
-
- if (DumbService.getInstance(variable.getProject()).isDumb()) return;
-
- String shortName = ((PsiClassType)type).getClassName();
- PsiManager manager = parameterList.getManager();
- final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
- PsiShortNamesCache shortNamesCache = PsiShortNamesCache.getInstance(parameterList.getProject());
- PsiClass[] classes = shortNamesCache.getClassesByName(shortName, GlobalSearchScope.allScope(manager.getProject()));
- PsiElementFactory factory = facade.getElementFactory();
- JavaSdkVersion version = JavaVersionService.getInstance().getJavaSdkVersion(parameterList);
- for (PsiClass aClass : classes) {
- if (GenericsHighlightUtil.checkReferenceTypeArgumentList(aClass, parameterList, PsiSubstitutor.EMPTY, false, version) == null) {
- PsiType[] actualTypeParameters = parameterList.getTypeArguments();
- PsiTypeParameter[] classTypeParameters = aClass.getTypeParameters();
- HashMap<PsiTypeParameter, PsiType> map = new HashMap<PsiTypeParameter, PsiType>();
- for (int j = 0; j < classTypeParameters.length; j++) {
- PsiTypeParameter classTypeParameter = classTypeParameters[j];
- PsiType actualTypeParameter = actualTypeParameters[j];
- map.put(classTypeParameter, actualTypeParameter);
- }
- PsiSubstitutor substitutor = factory.createSubstitutor(map);
- PsiType suggestedType = factory.createType(aClass, substitutor);
- HighlightUtil.registerChangeVariableTypeFixes(variable, suggestedType, highlightInfo);
- }
- }
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableTypeFromCallFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableTypeFromCallFix.java
index 1b43c1b75008..e2a70d8dfada 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableTypeFromCallFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableTypeFromCallFix.java
@@ -21,7 +21,6 @@
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
@@ -34,15 +33,18 @@ import com.intellij.refactoring.typeMigration.TypeMigrationLabeler;
import com.intellij.refactoring.typeMigration.TypeMigrationProcessor;
import com.intellij.refactoring.typeMigration.TypeMigrationRules;
import com.intellij.usageView.UsageViewUtil;
-import com.intellij.usages.impl.UsageViewImpl;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
public class VariableTypeFromCallFix implements IntentionAction {
private final PsiType myExpressionType;
private final PsiVariable myVar;
- public VariableTypeFromCallFix(PsiClassType type, PsiVariable var) {
+ private VariableTypeFromCallFix(@NotNull PsiClassType type, @NotNull PsiVariable var) {
myExpressionType = type;
myVar = var;
}
@@ -82,13 +84,16 @@ public class VariableTypeFromCallFix implements IntentionAction {
}
- public static void registerQuickFixActions(PsiMethodCallExpression methodCall, PsiExpressionList list, HighlightInfo highlightInfo) {
+ @NotNull
+ public static List<IntentionAction> getQuickFixActions(@NotNull PsiMethodCallExpression methodCall,
+ @NotNull PsiExpressionList list) {
final JavaResolveResult result = methodCall.getMethodExpression().advancedResolve(false);
PsiMethod method = (PsiMethod) result.getElement();
final PsiSubstitutor substitutor = result.getSubstitutor();
PsiExpression[] expressions = list.getExpressions();
- if (method == null || method.getParameterList().getParametersCount() != expressions.length) return;
+ if (method == null || method.getParameterList().getParametersCount() != expressions.length) return Collections.emptyList();
final PsiParameter[] parameters = method.getParameterList().getParameters();
+ List<IntentionAction> actions = new ArrayList<IntentionAction>();
for (int i = 0; i < expressions.length; i++) {
final PsiExpression expression = expressions[i];
PsiType expressionType = expression.getType();
@@ -116,32 +121,35 @@ public class VariableTypeFromCallFix implements IntentionAction {
DefaultParameterTypeInferencePolicy.INSTANCE);
final PsiClassType appropriateVarType = JavaPsiFacade.getElementFactory(expression.getProject()).createType(varClass, psiSubstitutor);
if (!varType.equals(appropriateVarType)) {
- QuickFixAction.registerQuickFixAction(highlightInfo, new VariableTypeFromCallFix(appropriateVarType, (PsiVariable) resolved));
+ actions.add(new VariableTypeFromCallFix(appropriateVarType, (PsiVariable)resolved));
}
break;
}
}
}
- registerParameterTypeChange(highlightInfo, method, expression, parameterType);
+ actions.addAll(getParameterTypeChangeFixes(method, expression, parameterType));
}
+ return actions;
}
- private static void registerParameterTypeChange(HighlightInfo highlightInfo,
- PsiMethod method,
- PsiExpression expression,
- PsiType parameterType) {
- if (expression instanceof PsiReferenceExpression) {
- final PsiManager manager = method.getManager();
- if (manager.isInProject(method)) {
- final PsiMethod[] superMethods = method.findDeepestSuperMethods();
- for (PsiMethod superMethod : superMethods) {
- if (!manager.isInProject(superMethod)) return;
- }
- final PsiElement resolve = ((PsiReferenceExpression)expression).resolve();
- if (resolve instanceof PsiVariable) {
- HighlightUtil.registerChangeVariableTypeFixes((PsiVariable)resolve, parameterType, highlightInfo);
- }
+ private static List<IntentionAction> getParameterTypeChangeFixes(@NotNull PsiMethod method,
+ @NotNull PsiExpression expression,
+ PsiType parameterType) {
+ if (!(expression instanceof PsiReferenceExpression)) {
+ return Collections.emptyList();
+ }
+ List<IntentionAction> result = new ArrayList<IntentionAction>();
+ final PsiManager manager = method.getManager();
+ if (manager.isInProject(method)) {
+ final PsiMethod[] superMethods = method.findDeepestSuperMethods();
+ for (PsiMethod superMethod : superMethods) {
+ if (!manager.isInProject(superMethod)) return Collections.emptyList();
+ }
+ final PsiElement resolve = ((PsiReferenceExpression)expression).resolve();
+ if (resolve instanceof PsiVariable) {
+ result.addAll(HighlightUtil.getChangeVariableTypeFixes((PsiVariable)resolve, parameterType));
}
}
+ return result;
}
}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/WrapExpressionFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/WrapExpressionFix.java
deleted file mode 100644
index eab73d92313e..000000000000
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/WrapExpressionFix.java
+++ /dev/null
@@ -1,175 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.codeInsight.daemon.impl.quickfix;
-
-import com.intellij.codeInsight.FileModificationService;
-import com.intellij.codeInsight.daemon.QuickFixBundle;
-import com.intellij.codeInsight.daemon.impl.HighlightInfo;
-import com.intellij.codeInsight.intention.IntentionAction;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.psi.*;
-import com.intellij.psi.search.GlobalSearchScope;
-import com.intellij.util.IncorrectOperationException;
-import org.jetbrains.annotations.NonNls;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.LinkedHashSet;
-import java.util.Set;
-
-/**
- * @author ven
- */
-public class WrapExpressionFix implements IntentionAction {
-
- private final PsiExpression myExpression;
- private final PsiClassType myExpectedType;
- private final boolean myPrimitiveExpected;
-
- public WrapExpressionFix(PsiType expectedType, PsiExpression expression) {
- myExpression = expression;
- myExpectedType = getClassType(expectedType, expression);
- myPrimitiveExpected = expectedType instanceof PsiPrimitiveType;
- }
-
- @Nullable
- private static PsiClassType getClassType(PsiType type, PsiElement place) {
- if (type instanceof PsiClassType) {
- return (PsiClassType)type;
- }
- else if (type instanceof PsiPrimitiveType) {
- return ((PsiPrimitiveType)type).getBoxedType(place.getManager(), GlobalSearchScope.allScope(place.getProject()));
- }
- return null;
- }
-
- @Override
- @NotNull
- public String getText() {
- final PsiMethod wrapper = myExpression.isValid() && myExpectedType != null ? findWrapper(myExpression.getType(), myExpectedType, myPrimitiveExpected) : null;
- final String methodPresentation = wrapper != null ? wrapper.getContainingClass().getName() + "." + wrapper.getName() : "";
- return QuickFixBundle.message("wrap.expression.using.static.accessor.text", methodPresentation);
- }
-
- @Nullable
- private static PsiMethod findWrapper(PsiType type, @NotNull PsiClassType expectedType, boolean primitiveExpected) {
- PsiClass aClass = expectedType.resolve();
- if (aClass != null) {
- PsiType expectedReturnType = expectedType;
- if (primitiveExpected) {
- expectedReturnType = PsiPrimitiveType.getUnboxedType(expectedType);
- }
- if (expectedReturnType == null) return null;
- PsiMethod[] methods = aClass.getMethods();
- final Set<PsiMethod> wrapperMethods = new LinkedHashSet<PsiMethod>();
- for (PsiMethod method : methods) {
- if (method.hasModifierProperty(PsiModifier.STATIC)
- && method.getParameterList().getParametersCount() == 1
- && method.getParameterList().getParameters()[0].getType().isAssignableFrom(type)
- && method.getReturnType() != null
- && expectedReturnType.equals(method.getReturnType())) {
- final String methodName = method.getName();
- if (methodName.startsWith("parse") || methodName.equals("valueOf")) {
- return method;
- }
- wrapperMethods.add(method);
- }
- }
- if (!wrapperMethods.isEmpty()) return wrapperMethods.iterator().next();
- }
-
- return null;
- }
-
- @Override
- @NotNull
- public String getFamilyName() {
- return QuickFixBundle.message("wrap.expression.using.static.accessor.family");
- }
-
- @Override
- public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- return myExpression.isValid()
- && myExpression.getManager().isInProject(myExpression)
- && myExpectedType != null
- && myExpectedType.isValid()
- && myExpression.getType() != null
- && findWrapper(myExpression.getType(), myExpectedType, myPrimitiveExpected) != null;
- }
-
- @Override
- public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
- if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
- PsiMethod wrapper = findWrapper(myExpression.getType(), myExpectedType, myPrimitiveExpected);
- assert wrapper != null;
- PsiElementFactory factory = JavaPsiFacade.getInstance(file.getProject()).getElementFactory();
- @NonNls String methodCallText = "Foo." + wrapper.getName() + "()";
- PsiMethodCallExpression call = (PsiMethodCallExpression)factory.createExpressionFromText(methodCallText,
- null);
- call.getArgumentList().add(myExpression);
- ((PsiReferenceExpression)call.getMethodExpression().getQualifierExpression()).bindToElement(
- wrapper.getContainingClass());
- myExpression.replace(call);
- }
-
- @Override
- public boolean startInWriteAction() {
- return true;
- }
-
- public static void registerWrapAction(JavaResolveResult[] candidates, PsiExpression[] expressions, HighlightInfo highlightInfo) {
- PsiType expectedType = null;
- PsiExpression expr = null;
-
- nextMethod:
- for (int i = 0; i < candidates.length && expectedType == null; i++) {
- final JavaResolveResult candidate = candidates[i];
- final PsiSubstitutor substitutor = candidate.getSubstitutor();
- final PsiElement element = candidate.getElement();
- assert element != null;
- final PsiMethod method = (PsiMethod)element;
- final PsiParameter[] parameters = method.getParameterList().getParameters();
- if (!method.isVarArgs() && parameters.length != expressions.length) continue;
- for (int j = 0; j < expressions.length; j++) {
- PsiExpression expression = expressions[j];
- final PsiType exprType = expression.getType();
- if (exprType != null) {
- PsiType paramType = parameters[Math.min(j, parameters.length - 1)].getType();
- if (paramType instanceof PsiEllipsisType) {
- paramType = ((PsiEllipsisType)paramType).getComponentType();
- }
- paramType = substitutor != null ? substitutor.substitute(paramType) : paramType;
- if (paramType.isAssignableFrom(exprType)) continue;
- final PsiClassType classType = getClassType(paramType, expression);
- if (expectedType == null && classType != null && findWrapper(exprType, classType, paramType instanceof PsiPrimitiveType) != null) {
- expectedType = paramType;
- expr = expression;
- }
- else {
- expectedType = null;
- expr = null;
- continue nextMethod;
- }
- }
- }
- }
-
- if (expectedType != null) {
- QuickFixAction.registerQuickFixAction(highlightInfo, expr.getTextRange(), new WrapExpressionFix(expectedType, expr));
- }
- }
-}
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/quickFix/CreateClassOrPackageFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/quickFix/CreateClassOrPackageFix.java
index 5534f5fd6cc2..5b8baae9e1a1 100644
--- a/java/java-impl/src/com/intellij/codeInsight/daemon/quickFix/CreateClassOrPackageFix.java
+++ b/java/java-impl/src/com/intellij/codeInsight/daemon/quickFix/CreateClassOrPackageFix.java
@@ -37,6 +37,7 @@ import com.intellij.psi.util.CreateClassUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import java.util.*;
@@ -251,7 +252,8 @@ public class CreateClassOrPackageFix extends LocalQuickFixAndIntentionActionOnPs
}
}
else {
- for (VirtualFile root : ProjectRootManager.getInstance(psiManager.getProject()).getContentSourceRoots()) {
+ for (VirtualFile root : ProjectRootManager.getInstance(psiManager.getProject()).getModuleSourceRoots(
+ JavaModuleSourceRootTypes.SOURCES)) {
PsiDirectory directory = psiManager.findDirectory(root);
if (LOG.isDebugEnabled()) {
LOG.debug("Root: " + root + ", directory: " + directory);
diff --git a/java/java-impl/src/com/intellij/codeInsight/hint/api/impls/MethodParameterInfoHandler.java b/java/java-impl/src/com/intellij/codeInsight/hint/api/impls/MethodParameterInfoHandler.java
index 424c5c55ff2c..2ef5582790ad 100644
--- a/java/java-impl/src/com/intellij/codeInsight/hint/api/impls/MethodParameterInfoHandler.java
+++ b/java/java-impl/src/com/intellij/codeInsight/hint/api/impls/MethodParameterInfoHandler.java
@@ -33,6 +33,7 @@ import com.intellij.psi.util.MethodSignatureUtil;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.HashSet;
+import com.intellij.xml.util.XmlStringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -352,12 +353,12 @@ public class MethodParameterInfoHandler implements ParameterInfoHandlerWithTabAc
}
}
- public static void updateMethodPresentation(PsiMethod method, @Nullable PsiSubstitutor substitutor, ParameterInfoUIContext context) {
+ public static String updateMethodPresentation(PsiMethod method, @Nullable PsiSubstitutor substitutor, ParameterInfoUIContext context) {
CodeInsightSettings settings = CodeInsightSettings.getInstance();
if (!method.isValid() || substitutor != null && !substitutor.isValid()) {
context.setUIComponentEnabled(false);
- return;
+ return null;
}
StringBuilder buffer = new StringBuilder();
@@ -397,7 +398,7 @@ public class MethodParameterInfoHandler implements ParameterInfoHandlerWithTabAc
paramType = substitutor.substitute(paramType);
}
appendModifierList(buffer, param);
- buffer.append(StringUtil.escapeXml(paramType.getPresentableText()));
+ buffer.append(paramType.getPresentableText());
String name = param.getName();
if (name != null) {
buffer.append(" ");
@@ -405,7 +406,7 @@ public class MethodParameterInfoHandler implements ParameterInfoHandlerWithTabAc
}
}
- int endOffset = buffer.length();
+ int endOffset = XmlStringUtil.escapeString(buffer.toString()).length();
if (j < numParams - 1) {
buffer.append(", ");
@@ -426,7 +427,7 @@ public class MethodParameterInfoHandler implements ParameterInfoHandlerWithTabAc
buffer.append(")");
}
- context.setupUIComponentPresentation(
+ return context.setupUIComponentPresentation(
buffer.toString(),
highlightStartOffset,
highlightEndOffset,
diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/CreateClassDialog.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/CreateClassDialog.java
index 2f4ff88fddf0..13f6e2ec1fb5 100644
--- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/CreateClassDialog.java
+++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/CreateClassDialog.java
@@ -49,6 +49,7 @@ import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
@@ -207,7 +208,7 @@ public class CreateClassDialog extends DialogWrapper {
gbConstraints.insets.top = 4;
panel.add(myDestinationCB, gbConstraints);
- final boolean isMultipleSourceRoots = ProjectRootManager.getInstance(myProject).getContentSourceRoots().length > 1;
+ final boolean isMultipleSourceRoots = ProjectRootManager.getInstance(myProject).getModuleSourceRoots(JavaModuleSourceRootTypes.SOURCES).size() > 1;
myDestinationCB.setVisible(isMultipleSourceRoots);
label.setVisible(isMultipleSourceRoots);
label.setLabelFor(myDestinationCB);
diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/MoveFieldAssignmentToInitializerAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/MoveFieldAssignmentToInitializerAction.java
index c8a94ed44a08..0132b1651107 100644
--- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/MoveFieldAssignmentToInitializerAction.java
+++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/MoveFieldAssignmentToInitializerAction.java
@@ -133,13 +133,14 @@ public class MoveFieldAssignmentToInitializerAction extends BaseIntentionAction
if (!PsiUtil.isOnAssignmentLeftHand(reference)) return;
PsiElement resolved = reference.resolve();
if (resolved != field) return;
- PsiExpression rValue = ((PsiAssignmentExpression)reference.getParent()).getRExpression();
+ PsiAssignmentExpression assignmentExpression = PsiTreeUtil.getParentOfType(reference, PsiAssignmentExpression.class);
+ PsiExpression rValue = assignmentExpression.getRExpression();
if (currentInitializingBlock != null) {
// ignore usages other than initializing
if (rValue == null || !PsiEquivalenceUtil.areElementsEquivalent(rValue, expression)) {
result.set(Boolean.FALSE);
}
- initializingAssignments.add((PsiAssignmentExpression)reference.getParent());
+ initializingAssignments.add(assignmentExpression);
}
totalUsages.add(assignment);
}
@@ -153,7 +154,7 @@ public class MoveFieldAssignmentToInitializerAction extends BaseIntentionAction
}
private static PsiField getAssignedField(final PsiAssignmentExpression assignment) {
- PsiExpression lExpression = assignment.getLExpression();
+ PsiExpression lExpression = PsiUtil.skipParenthesizedExprDown(assignment.getLExpression());
if (!(lExpression instanceof PsiReferenceExpression)) return null;
PsiElement resolved = ((PsiReferenceExpression)lExpression).resolve();
if (!(resolved instanceof PsiField)) return null;
diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java
index 6ba29bbc1c45..dcbf1a7bd045 100644
--- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java
+++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java
@@ -15,6 +15,9 @@
*/
package com.intellij.codeInsight.intention.impl.config;
+import com.intellij.codeInsight.daemon.QuickFixActionRegistrar;
+import com.intellij.codeInsight.daemon.impl.analysis.IncreaseLanguageLevelFix;
+import com.intellij.codeInsight.daemon.impl.quickfix.ReplacePrimitiveWithBoxedTypeAction;
import com.intellij.codeInsight.daemon.impl.quickfix.*;
import com.intellij.codeInsight.daemon.quickFix.CreateClassOrPackageFix;
import com.intellij.codeInsight.daemon.quickFix.CreateFieldOrPropertyFix;
@@ -22,12 +25,16 @@ import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.QuickFixFactory;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.codeInspection.LocalQuickFixOnPsiElement;
+import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
+import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PropertyMemberType;
import com.intellij.psi.util.ClassKind;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import java.util.List;
+
/**
* @author cdr
*/
@@ -163,4 +170,400 @@ public class QuickFixFactoryImpl extends QuickFixFactory {
return new CreateFieldOrPropertyFix(aClass, name, type, targetMember, annotations);
}
+ @NotNull
+ @Override
+ public IntentionAction createSetupJDKFix() {
+ return SetupJDKFix.getInstance();
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createAddExceptionToCatchFix() {
+ return new AddExceptionToCatchFix();
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createAddExceptionToThrowsFix(@NotNull PsiElement element) {
+ return new AddExceptionToThrowsFix(element);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createSurroundWithTryCatchFix(@NotNull PsiElement element) {
+ return new SurroundWithTryCatchFix(element);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createGeneralizeCatchFix(@NotNull PsiElement element, @NotNull PsiClassType type) {
+ return new GeneralizeCatchFix(element, type);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createChangeToAppendFix(@NotNull IElementType sign,
+ @NotNull PsiType type,
+ @NotNull PsiAssignmentExpression assignment) {
+ return new ChangeToAppendFix(sign, type, assignment);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createAddTypeCastFix(@NotNull PsiType type, @NotNull PsiExpression expression) {
+ return new AddTypeCastFix(type, expression);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createWrapExpressionFix(@NotNull PsiType type, @NotNull PsiExpression expression) {
+ return new WrapExpressionFix(type, expression);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createReuseVariableDeclarationFix(@NotNull PsiLocalVariable variable) {
+ return new ReuseVariableDeclarationFix(variable);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createConvertToStringLiteralAction() {
+ return new ConvertToStringLiteralAction();
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createDeleteCatchFix(@NotNull PsiParameter parameter) {
+ return new DeleteCatchFix(parameter);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createDeleteMultiCatchFix(@NotNull PsiTypeElement element) {
+ return new DeleteMultiCatchFix(element);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createConvertSwitchToIfIntention(@NotNull PsiSwitchStatement statement) {
+ return new ConvertSwitchToIfIntention(statement);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createNegationBroadScopeFix(@NotNull PsiPrefixExpression expr) {
+ return new NegationBroadScopeFix(expr);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createCreateFieldFromUsageFix(@NotNull PsiReferenceExpression place) {
+ return new CreateFieldFromUsageFix(place);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createReplaceWithListAccessFix(@NotNull PsiArrayAccessExpression expression) {
+ return new ReplaceWithListAccessFix(expression);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createAddNewArrayExpressionFix(@NotNull PsiArrayInitializerExpression expression) {
+ return new AddNewArrayExpressionFix(expression);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createMoveCatchUpFix(@NotNull PsiCatchSection section, @NotNull PsiCatchSection section1) {
+ return new MoveCatchUpFix(section, section1);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createRenameWrongRefFix(@NotNull PsiReferenceExpression ref) {
+ return new RenameWrongRefFix(ref);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createRemoveQualifierFix(@NotNull PsiExpression qualifier,
+ @NotNull PsiReferenceExpression expression,
+ @NotNull PsiClass resolved) {
+ return new RemoveQualifierFix(qualifier, expression, resolved);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createRemoveParameterListFix(@NotNull PsiMethod parent) {
+ return new RemoveParameterListFix(parent);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createShowModulePropertiesFix(@NotNull PsiElement element) {
+ return new ShowModulePropertiesFix(element);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createIncreaseLanguageLevelFix(@NotNull LanguageLevel level) {
+ return new IncreaseLanguageLevelFix(level);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createChangeParameterClassFix(@NotNull PsiClass aClass, @NotNull PsiClassType type) {
+ return new ChangeParameterClassFix(aClass, type);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createReplaceInaccessibleFieldWithGetterSetterFix(@NotNull PsiElement element, @NotNull PsiMethod getter, boolean isSetter) {
+ return new ReplaceInaccessibleFieldWithGetterSetterFix(element, getter, isSetter);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createSurroundWithArrayFix(@Nullable PsiCall methodCall, @Nullable PsiExpression expression) {
+ return new SurroundWithArrayFix(methodCall, expression);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createImplementAbstractClassMethodsFix(@NotNull PsiElement elementToHighlight) {
+ return new ImplementAbstractClassMethodsFix(elementToHighlight);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createMoveClassToSeparateFileFix(@NotNull PsiClass aClass) {
+ return new MoveClassToSeparateFileFix(aClass);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createRenameFileFix(@NotNull String newName) {
+ return new RenameFileFix(newName);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createRenameElementFix(@NotNull PsiNamedElement element) {
+ return new RenameElementFix(element);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createRenameElementFix(@NotNull PsiNamedElement element, @NotNull String newName) {
+ return new RenameElementFix(element, newName);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createChangeExtendsToImplementsFix(@NotNull PsiClass aClass, @NotNull PsiClassType classToExtendFrom) {
+ return new ChangeExtendsToImplementsFix(aClass, classToExtendFrom);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createCreateConstructorMatchingSuperFix(@NotNull PsiClass aClass) {
+ return new CreateConstructorMatchingSuperFix(aClass);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createRemoveNewQualifierFix(@NotNull PsiNewExpression expression, PsiClass aClass) {
+ return new RemoveNewQualifierFix(expression, aClass);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createSuperMethodReturnFix(@NotNull PsiMethod superMethod, @NotNull PsiType superMethodType) {
+ return new SuperMethodReturnFix(superMethod, superMethodType);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createInsertNewFix(@NotNull PsiMethodCallExpression call, @NotNull PsiClass aClass) {
+ return new InsertNewFix(call, aClass);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createAddMethodBodyFix(@NotNull PsiMethod method) {
+ return new AddMethodBodyFix(method);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createDeleteMethodBodyFix(@NotNull PsiMethod method) {
+ return new DeleteMethodBodyFix(method);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createInsertSuperFix(@NotNull PsiMethod constructor) {
+ return new InsertSuperFix(constructor);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createChangeMethodSignatureFromUsageFix(@NotNull PsiMethod targetMethod,
+ @NotNull PsiExpression[] expressions,
+ @NotNull PsiSubstitutor substitutor,
+ @NotNull PsiElement context,
+ boolean changeAllUsages,
+ int minUsagesNumberToShowDialog) {
+ return new ChangeMethodSignatureFromUsageFix(targetMethod, expressions, substitutor, context, changeAllUsages, minUsagesNumberToShowDialog);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createChangeMethodSignatureFromUsageReverseOrderFix(@NotNull PsiMethod targetMethod,
+ @NotNull PsiExpression[] expressions,
+ @NotNull PsiSubstitutor substitutor,
+ @NotNull PsiElement context,
+ boolean changeAllUsages,
+ int minUsagesNumberToShowDialog) {
+ return new ChangeMethodSignatureFromUsageReverseOrderFix(targetMethod, expressions, substitutor, context, changeAllUsages, minUsagesNumberToShowDialog);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createCreateMethodFromUsageFix(@NotNull PsiMethodCallExpression call) {
+ return new CreateMethodFromUsageFix(call);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createCreateAbstractMethodFromUsageFix(@NotNull PsiMethodCallExpression call) {
+ return new CreateAbstractMethodFromUsageFix(call);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createCreatePropertyFromUsageFix(@NotNull PsiMethodCallExpression call) {
+ return new CreatePropertyFromUsageFix(call);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createCreateConstructorFromSuperFix(@NotNull PsiMethodCallExpression call) {
+ return new CreateConstructorFromSuperFix(call);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createCreateConstructorFromThisFix(@NotNull PsiMethodCallExpression call) {
+ return new CreateConstructorFromThisFix(call);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createCreateGetterSetterPropertyFromUsageFix(@NotNull PsiMethodCallExpression call) {
+ return new CreateGetterSetterPropertyFromUsageFix(call);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createStaticImportMethodFix(@NotNull PsiMethodCallExpression call) {
+ return new StaticImportMethodFix(call);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createReplaceAddAllArrayToCollectionFix(@NotNull PsiMethodCallExpression call) {
+ return new ReplaceAddAllArrayToCollectionFix(call);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createCreateConstructorFromCallFix(@NotNull PsiConstructorCall call) {
+ return new CreateConstructorFromCallFix(call);
+ }
+
+ @NotNull
+ @Override
+ public List<IntentionAction> getVariableTypeFromCallFixes(@NotNull PsiMethodCallExpression call, @NotNull PsiExpressionList list) {
+ return VariableTypeFromCallFix.getQuickFixActions(call, list);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createAddReturnFix(@NotNull PsiMethod method) {
+ return new AddReturnFix(method);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createAddVariableInitializerFix(@NotNull PsiVariable variable) {
+ return new AddVariableInitializerFix(variable);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createDeferFinalAssignmentFix(@NotNull PsiVariable variable, @NotNull PsiReferenceExpression expression) {
+ return new DeferFinalAssignmentFix(variable, expression);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createVariableAccessFromInnerClassFix(@NotNull PsiVariable variable, @NotNull PsiClass aClass) {
+ return new VariableAccessFromInnerClassFix(variable, aClass);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createCreateConstructorParameterFromFieldFix(@NotNull PsiField field) {
+ return new CreateConstructorParameterFromFieldFix(field);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createInitializeFinalFieldInConstructorFix(@NotNull PsiField field) {
+ return new InitializeFinalFieldInConstructorFix(field);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createRemoveTypeArgumentsFix(@NotNull PsiElement variable) {
+ return new RemoveTypeArgumentsFix(variable);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createChangeClassSignatureFromUsageFix(@NotNull PsiClass owner, @NotNull PsiReferenceParameterList parameterList) {
+ return new ChangeClassSignatureFromUsageFix(owner, parameterList);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createReplacePrimitiveWithBoxedTypeAction(@NotNull PsiTypeElement element, @NotNull String typeName, @NotNull String boxedTypeName) {
+ return new ReplacePrimitiveWithBoxedTypeAction(element, typeName, boxedTypeName);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createMakeVarargParameterLastFix(@NotNull PsiParameter parameter) {
+ return new MakeVarargParameterLastFix(parameter);
+ }
+
+ @NotNull
+ @Override
+ public IntentionAction createMoveBoundClassToFrontFix(@NotNull PsiClass aClass, @NotNull PsiClassType type) {
+ return new MoveBoundClassToFrontFix(aClass, type);
+ }
+
+ @Override
+ public void registerPullAsAbstractUpFixes(@NotNull PsiMethod method, @NotNull QuickFixActionRegistrar registrar) {
+ PullAsAbstractUpFix.registerQuickFix(method, registrar);
+ }
+
+ @Override
+ public IntentionAction createCreateAnnotationMethodFromUsageFix(PsiNameValuePair pair) {
+ return new CreateAnnotationMethodFromUsageFix(pair);
+ }
+
}
diff --git a/java/java-impl/src/com/intellij/codeInspection/SuppressManagerImpl.java b/java/java-impl/src/com/intellij/codeInspection/SuppressManagerImpl.java
index c67f925d6c09..ca3131d325eb 100644
--- a/java/java-impl/src/com/intellij/codeInspection/SuppressManagerImpl.java
+++ b/java/java-impl/src/com/intellij/codeInspection/SuppressManagerImpl.java
@@ -24,8 +24,6 @@ import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.psi.PsiDocCommentOwner;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiModifierListOwner;
-import com.intellij.util.Function;
-import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -36,17 +34,7 @@ public class SuppressManagerImpl extends SuppressManager {
@NotNull
public SuppressIntentionAction[] createSuppressActions(@NotNull final HighlightDisplayKey displayKey) {
SuppressQuickFix[] batchSuppressActions = createBatchSuppressActions(displayKey);
- return convertBatchToSuppressIntentionActions(batchSuppressActions);
- }
-
- @NotNull
- public static SuppressIntentionAction[] convertBatchToSuppressIntentionActions(@NotNull SuppressQuickFix[] actions) {
- return ContainerUtil.map2Array(actions, SuppressIntentionAction.class, new Function<SuppressQuickFix, SuppressIntentionAction>() {
- @Override
- public SuppressIntentionAction fun(SuppressQuickFix fix) {
- return SuppressIntentionActionFromFix.convertBatchToSuppressIntentionAction(fix);
- }
- });
+ return SuppressIntentionActionFromFix.convertBatchToSuppressIntentionActions(batchSuppressActions);
}
@Override
diff --git a/java/java-impl/src/com/intellij/ide/actions/JavaCreateTemplateInPackageAction.java b/java/java-impl/src/com/intellij/ide/actions/JavaCreateTemplateInPackageAction.java
index e80c05e0acf1..b1397f65526d 100644
--- a/java/java-impl/src/com/intellij/ide/actions/JavaCreateTemplateInPackageAction.java
+++ b/java/java-impl/src/com/intellij/ide/actions/JavaCreateTemplateInPackageAction.java
@@ -18,6 +18,7 @@ package com.intellij.ide.actions;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import javax.swing.*;
@@ -25,7 +26,7 @@ public abstract class JavaCreateTemplateInPackageAction<T extends PsiElement> ex
DumbAware {
protected JavaCreateTemplateInPackageAction(String text, String description, Icon icon, boolean inSourceOnly) {
- super(text, description, icon, inSourceOnly);
+ super(text, description, icon, inSourceOnly ? JavaModuleSourceRootTypes.SOURCES : null);
}
@Override
diff --git a/java/java-impl/src/com/intellij/ide/highlighter/JavaHighlightingColors.java b/java/java-impl/src/com/intellij/ide/highlighter/JavaHighlightingColors.java
deleted file mode 100644
index 324c0a1257cd..000000000000
--- a/java/java-impl/src/com/intellij/ide/highlighter/JavaHighlightingColors.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright 2000-2013 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.ide.highlighter;
-
-import com.intellij.openapi.editor.SyntaxHighlighterColors;
-import com.intellij.openapi.editor.colors.TextAttributesKey;
-
-/**
- * Highlighting text attributes for Java language.
- *
- * @author Rustam Vishnyakov
- */
-@SuppressWarnings("deprecation")
-public class JavaHighlightingColors {
- public static final TextAttributesKey LINE_COMMENT = SyntaxHighlighterColors.LINE_COMMENT;
- public static final TextAttributesKey JAVA_BLOCK_COMMENT = SyntaxHighlighterColors.JAVA_BLOCK_COMMENT;
- public static final TextAttributesKey DOC_COMMENT = SyntaxHighlighterColors.DOC_COMMENT;
- public static final TextAttributesKey KEYWORD = SyntaxHighlighterColors.KEYWORD;
- public static final TextAttributesKey NUMBER = SyntaxHighlighterColors.NUMBER;
- public static final TextAttributesKey STRING = SyntaxHighlighterColors.STRING;
- public static final TextAttributesKey OPERATION_SIGN = SyntaxHighlighterColors.OPERATION_SIGN;
- public static final TextAttributesKey PARENTHESES = SyntaxHighlighterColors.PARENTHS;
- public static final TextAttributesKey BRACKETS = SyntaxHighlighterColors.BRACKETS;
- public static final TextAttributesKey BRACES = SyntaxHighlighterColors.BRACES;
- public static final TextAttributesKey COMMA = SyntaxHighlighterColors.COMMA;
- public static final TextAttributesKey DOT = SyntaxHighlighterColors.DOT;
- public static final TextAttributesKey JAVA_SEMICOLON = SyntaxHighlighterColors.JAVA_SEMICOLON;
- public static final TextAttributesKey DOC_COMMENT_TAG = SyntaxHighlighterColors.DOC_COMMENT_TAG;
- public static final TextAttributesKey DOC_COMMENT_MARKUP = SyntaxHighlighterColors.DOC_COMMENT_MARKUP;
- public static final TextAttributesKey VALID_STRING_ESCAPE = SyntaxHighlighterColors.VALID_STRING_ESCAPE;
- public static final TextAttributesKey INVALID_STRING_ESCAPE = SyntaxHighlighterColors.INVALID_STRING_ESCAPE;
-
- /** @deprecated use {@link #PARENTHESES} (to remove in IDEA 14) */
- @SuppressWarnings("SpellCheckingInspection")
- public static final TextAttributesKey PARENTHS = PARENTHESES;
-}
diff --git a/java/java-impl/src/com/intellij/ide/navigationToolbar/JavaNavBarExtension.java b/java/java-impl/src/com/intellij/ide/navigationToolbar/JavaNavBarExtension.java
index cf9e0ad9f3b4..2fc52f481343 100644
--- a/java/java-impl/src/com/intellij/ide/navigationToolbar/JavaNavBarExtension.java
+++ b/java/java-impl/src/com/intellij/ide/navigationToolbar/JavaNavBarExtension.java
@@ -29,6 +29,7 @@ import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import java.util.Collection;
import java.util.Collections;
@@ -62,7 +63,7 @@ public class JavaNavBarExtension implements NavBarModelExtension{
final PsiFile containingFile = psiElement.getContainingFile();
if (containingFile != null) {
final VirtualFile file = containingFile.getVirtualFile();
- if (file != null && (index.isInSourceContent(file) || index.isInLibraryClasses(file) || index.isInLibrarySource(file))) {
+ if (file != null && (index.isUnderSourceRootOfType(file, JavaModuleSourceRootTypes.SOURCES) || index.isInLibraryClasses(file) || index.isInLibrarySource(file))) {
if (psiElement instanceof PsiJavaFile) {
final PsiJavaFile psiJavaFile = (PsiJavaFile)psiElement;
if (psiJavaFile.getViewProvider().getBaseLanguage() == StdLanguages.JAVA) {
diff --git a/java/java-impl/src/com/intellij/ide/projectView/impl/nodes/PackageViewProjectNode.java b/java/java-impl/src/com/intellij/ide/projectView/impl/nodes/PackageViewProjectNode.java
index f230d0baa577..76946c41e999 100644
--- a/java/java-impl/src/com/intellij/ide/projectView/impl/nodes/PackageViewProjectNode.java
+++ b/java/java-impl/src/com/intellij/ide/projectView/impl/nodes/PackageViewProjectNode.java
@@ -28,7 +28,6 @@ import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiPackage;
-import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.InvocationTargetException;
@@ -55,15 +54,12 @@ public class PackageViewProjectNode extends AbstractProjectNode {
return modulesAndGroups(allModules.toArray(new Module[allModules.size()]));
}
else {
- final List<VirtualFile> sourceRoots = new ArrayList<VirtualFile>();
final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(myProject);
- ContainerUtil.addAll(sourceRoots, projectRootManager.getContentSourceRoots());
-
final PsiManager psiManager = PsiManager.getInstance(myProject);
final List<AbstractTreeNode> children = new ArrayList<AbstractTreeNode>();
final Set<PsiPackage> topLevelPackages = new HashSet<PsiPackage>();
- for (final VirtualFile root : sourceRoots) {
+ for (final VirtualFile root : projectRootManager.getContentSourceRoots()) {
final PsiDirectory directory = psiManager.findDirectory(root);
if (directory == null) {
continue;
diff --git a/java/java-impl/src/com/intellij/ide/util/PackageChooserDialog.java b/java/java-impl/src/com/intellij/ide/util/PackageChooserDialog.java
index 2ad3edc9afa9..ac9ddb4f95f6 100644
--- a/java/java-impl/src/com/intellij/ide/util/PackageChooserDialog.java
+++ b/java/java-impl/src/com/intellij/ide/util/PackageChooserDialog.java
@@ -51,6 +51,7 @@ import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.tree.TreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
@@ -277,7 +278,7 @@ public class PackageChooserDialog extends PackageChooser {
fileIndex.iterateContent(
new ContentIterator() {
public boolean processFile(VirtualFile fileOrDir) {
- if (fileOrDir.isDirectory() && fileIndex.isInSourceContent(fileOrDir)){
+ if (fileOrDir.isDirectory() && fileIndex.isUnderSourceRootOfType(fileOrDir, JavaModuleSourceRootTypes.SOURCES)) {
final PsiDirectory psiDirectory = psiManager.findDirectory(fileOrDir);
LOG.assertTrue(psiDirectory != null);
PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
diff --git a/java/java-impl/src/com/intellij/ide/util/PackageUtil.java b/java/java-impl/src/com/intellij/ide/util/PackageUtil.java
index d0a0aa7f038c..59f09f594df4 100644
--- a/java/java-impl/src/com/intellij/ide/util/PackageUtil.java
+++ b/java/java-impl/src/com/intellij/ide/util/PackageUtil.java
@@ -40,6 +40,7 @@ import com.intellij.util.IncorrectOperationException;
import com.intellij.util.Query;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import java.io.File;
import java.io.IOException;
@@ -63,7 +64,7 @@ public class PackageUtil {
}
if (psiDirectory == null) {
if (checkSourceRootsConfigured(module)) {
- final VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
+ final List<VirtualFile> sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots(JavaModuleSourceRootTypes.SOURCES);
for (VirtualFile sourceRoot : sourceRoots) {
final PsiDirectory directory = PsiManager.getInstance(module.getProject()).findDirectory(sourceRoot);
if (directory != null) {
@@ -211,7 +212,7 @@ public class PackageUtil {
if (psiDirectory == null) {
if (!checkSourceRootsConfigured(module, askUserToCreate)) return null;
- final VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
+ final List<VirtualFile> sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots(JavaModuleSourceRootTypes.SOURCES);
List<PsiDirectory> directoryList = new ArrayList<PsiDirectory>();
for (VirtualFile sourceRoot : sourceRoots) {
final PsiDirectory directory = PsiManager.getInstance(project).findDirectory(sourceRoot);
@@ -359,8 +360,8 @@ public class PackageUtil {
}
public static boolean checkSourceRootsConfigured(final Module module, final boolean askUserToSetupSourceRoots) {
- VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
- if (sourceRoots.length == 0) {
+ List<VirtualFile> sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots(JavaModuleSourceRootTypes.SOURCES);
+ if (sourceRoots.isEmpty()) {
if (!askUserToSetupSourceRoots) {
return false;
}
@@ -372,8 +373,8 @@ public class PackageUtil {
ProjectSettingsService.getInstance(project).showModuleConfigurationDialog(module.getName(), CommonContentEntriesEditor.NAME);
- sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
- if (sourceRoots.length == 0) {
+ sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots(JavaModuleSourceRootTypes.SOURCES);
+ if (sourceRoots.isEmpty()) {
return false;
}
}
diff --git a/java/java-impl/src/com/intellij/ide/util/gotoByName/DefaultClassNavigationContributor.java b/java/java-impl/src/com/intellij/ide/util/gotoByName/DefaultClassNavigationContributor.java
index 0738da4cc1e3..ad21a92b2934 100644
--- a/java/java-impl/src/com/intellij/ide/util/gotoByName/DefaultClassNavigationContributor.java
+++ b/java/java-impl/src/com/intellij/ide/util/gotoByName/DefaultClassNavigationContributor.java
@@ -39,7 +39,7 @@ public class DefaultClassNavigationContributor implements EfficientChooseByNameC
if (FileBasedIndex.ourEnableTracingOfKeyHashToVirtualFileMapping) {
GlobalSearchScope scope = includeNonProjectItems ? GlobalSearchScope.allScope(project) : GlobalSearchScope.projectScope(project);
CommonProcessors.CollectProcessor<String> processor = new CommonProcessors.CollectProcessor<String>();
- processNames(processor, scope, DefaultFileNavigationContributor.getFilter(project, includeNonProjectItems));
+ processNames(processor, scope, IdFilter.getProjectIdFilter(project, includeNonProjectItems));
return ArrayUtil.toStringArray(processor.getResults());
}
diff --git a/java/java-impl/src/com/intellij/javadoc/JavadocConfiguration.java b/java/java-impl/src/com/intellij/javadoc/JavadocConfiguration.java
index 52c68a019a96..bf58e0f2253b 100644
--- a/java/java-impl/src/com/intellij/javadoc/JavadocConfiguration.java
+++ b/java/java-impl/src/com/intellij/javadoc/JavadocConfiguration.java
@@ -48,6 +48,7 @@ import com.intellij.util.containers.HashSet;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import javax.swing.*;
import java.io.File;
@@ -358,10 +359,8 @@ public class JavadocConfiguration implements ModuleRunProfile, JDOMExternalizabl
private static boolean containsPackagePrefix(Module module, String packageFQName) {
if (module == null) return false;
- final ContentEntry[] contentEntries = ModuleRootManager.getInstance(module).getContentEntries();
- for (ContentEntry contentEntry : contentEntries) {
- final SourceFolder[] sourceFolders = contentEntry.getSourceFolders();
- for (SourceFolder sourceFolder : sourceFolders) {
+ for (ContentEntry contentEntry : ModuleRootManager.getInstance(module).getContentEntries()) {
+ for (SourceFolder sourceFolder : contentEntry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES)) {
final String packagePrefix = sourceFolder.getPackagePrefix();
final int prefixLength = packagePrefix.length();
if (prefixLength > 0 && packageFQName.startsWith(packagePrefix)) {
diff --git a/java/java-impl/src/com/intellij/lang/java/JavaDocumentationProvider.java b/java/java-impl/src/com/intellij/lang/java/JavaDocumentationProvider.java
index fd616a551db6..683dcee76784 100644
--- a/java/java-impl/src/com/intellij/lang/java/JavaDocumentationProvider.java
+++ b/java/java-impl/src/com/intellij/lang/java/JavaDocumentationProvider.java
@@ -145,12 +145,7 @@ public class JavaDocumentationProvider implements CodeDocumentationProvider, Ext
return aPackage.getQualifiedName();
}
- @SuppressWarnings({"HardCodedStringLiteral"})
- public static String generateClassInfo(PsiClass aClass) {
- StringBuilder buffer = new StringBuilder();
-
- if (aClass instanceof PsiAnonymousClass) return LangBundle.message("java.terms.anonymous.class");
-
+ private static void generatePackageInfo(StringBuilder buffer, @NotNull PsiClass aClass) {
PsiFile file = aClass.getContainingFile();
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(aClass.getProject()).getFileIndex();
VirtualFile vFile = file.getVirtualFile();
@@ -175,7 +170,15 @@ public class JavaDocumentationProvider implements CodeDocumentationProvider, Ext
newLine(buffer);
}
}
+ }
+ @SuppressWarnings({"HardCodedStringLiteral"})
+ public static String generateClassInfo(PsiClass aClass) {
+ StringBuilder buffer = new StringBuilder();
+
+ if (aClass instanceof PsiAnonymousClass) return LangBundle.message("java.terms.anonymous.class");
+
+ generatePackageInfo(buffer, aClass);
generateModifiers(buffer, aClass);
final String classString = aClass.isAnnotationType() ? "java.terms.annotation.interface"
@@ -264,6 +267,10 @@ public class JavaDocumentationProvider implements CodeDocumentationProvider, Ext
PsiClass parentClass = method.getContainingClass();
if (parentClass != null) {
+ if (method.isConstructor() && !(parentClass instanceof PsiAnonymousClass)) {
+ generatePackageInfo(buffer, parentClass);
+ }
+
buffer.append(JavaDocUtil.getShortestClassName(parentClass, method));
newLine(buffer);
}
diff --git a/java/java-impl/src/com/intellij/openapi/module/JavaModuleType.java b/java/java-impl/src/com/intellij/openapi/module/JavaModuleType.java
index a86e3a6b7e7c..f81229bd5634 100644
--- a/java/java-impl/src/com/intellij/openapi/module/JavaModuleType.java
+++ b/java/java-impl/src/com/intellij/openapi/module/JavaModuleType.java
@@ -30,6 +30,7 @@ import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import javax.swing.*;
import java.util.ArrayList;
@@ -128,7 +129,7 @@ public class JavaModuleType extends ModuleType<JavaModuleBuilder> {
}
public static boolean isValidJavaSdk(@NotNull Module module) {
- if (ModuleRootManager.getInstance(module).getSourceRoots().length == 0) return true;
+ if (ModuleRootManager.getInstance(module).getSourceRoots(JavaModuleSourceRootTypes.SOURCES).isEmpty()) return true;
return JavaPsiFacade.getInstance(module.getProject()).findClass(CommonClassNames.JAVA_LANG_OBJECT,
module.getModuleWithLibrariesScope()) != null;
}
diff --git a/java/java-impl/src/com/intellij/psi/impl/JavaDirectoryIconProvider.java b/java/java-impl/src/com/intellij/psi/impl/JavaDirectoryIconProvider.java
index a5b1a2fc7f36..8efae1602e04 100644
--- a/java/java-impl/src/com/intellij/psi/impl/JavaDirectoryIconProvider.java
+++ b/java/java-impl/src/com/intellij/psi/impl/JavaDirectoryIconProvider.java
@@ -22,7 +22,8 @@ import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
-import com.intellij.openapi.roots.ui.configuration.IconSet;
+import com.intellij.openapi.roots.SourceFolder;
+import com.intellij.openapi.roots.ui.configuration.ModuleSourceRootEditHandler;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaDirectoryService;
@@ -47,8 +48,7 @@ public class JavaDirectoryIconProvider extends IconProvider implements DumbAware
final Project project = psiDirectory.getProject();
boolean isJarRoot = vFile.getParent() == null && vFile.getFileSystem() instanceof JarFileSystem;
boolean isContentRoot = ProjectRootsUtil.isModuleContentRoot(vFile, project);
- boolean inTestSource = ProjectRootsUtil.isInTestSource(vFile, project);
- boolean isSourceOrTestRoot = ProjectRootsUtil.isSourceOrTestRoot(vFile, project);
+ SourceFolder sourceFolder = ProjectRootsUtil.getModuleSourceRoot(vFile, project);
Icon symbolIcon;
if (isJarRoot) {
symbolIcon = PlatformIcons.JAR_ICON;
@@ -62,8 +62,8 @@ public class JavaDirectoryIconProvider extends IconProvider implements DumbAware
symbolIcon = PlatformIcons.CONTENT_ROOT_ICON_CLOSED;
}
}
- else if (isSourceOrTestRoot) {
- symbolIcon = IconSet.getSourceRootIcon(inTestSource);
+ else if (sourceFolder != null) {
+ symbolIcon = ModuleSourceRootEditHandler.getEditHandler(sourceFolder.getRootType()).getRootIcon();
}
else if (JavaDirectoryService.getInstance().getPackage(psiDirectory) != null) {
symbolIcon = PlatformIcons.PACKAGE_ICON;
diff --git a/java/java-impl/src/com/intellij/psi/impl/PackagePrefixIndex.java b/java/java-impl/src/com/intellij/psi/impl/PackagePrefixIndex.java
index 49483ecbe8ee..4629f8a883d3 100644
--- a/java/java-impl/src/com/intellij/psi/impl/PackagePrefixIndex.java
+++ b/java/java-impl/src/com/intellij/psi/impl/PackagePrefixIndex.java
@@ -25,6 +25,7 @@ import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.SmartList;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import java.util.Collection;
import java.util.List;
@@ -58,7 +59,7 @@ public class PackagePrefixIndex {
map = new MultiMap<String, Module>();
for (final Module module : ModuleManager.getInstance(myProject).getModules()) {
for (final ContentEntry entry : ModuleRootManager.getInstance(module).getContentEntries()) {
- for (final SourceFolder folder : entry.getSourceFolders()) {
+ for (final SourceFolder folder : entry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES)) {
final String prefix = folder.getPackagePrefix();
if (StringUtil.isNotEmpty(prefix)) {
map.putValue(prefix, module);
diff --git a/java/java-impl/src/com/intellij/psi/impl/file/PsiPackageImplementationHelperImpl.java b/java/java-impl/src/com/intellij/psi/impl/file/PsiPackageImplementationHelperImpl.java
index b37502c52ab8..658ee6561e68 100644
--- a/java/java-impl/src/com/intellij/psi/impl/file/PsiPackageImplementationHelperImpl.java
+++ b/java/java-impl/src/com/intellij/psi/impl/file/PsiPackageImplementationHelperImpl.java
@@ -37,6 +37,7 @@ import com.intellij.psi.*;
import com.intellij.psi.impl.PackagePrefixElementFinder;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiModificationTracker;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import java.util.ArrayList;
import java.util.List;
@@ -56,9 +57,8 @@ public class PsiPackageImplementationHelperImpl extends PsiPackageImplementation
final Module[] modules = ModuleManager.getInstance(psiPackage.getProject()).getModules();
for (final Module module : modules) {
- final ContentEntry[] contentEntries = ModuleRootManager.getInstance(module).getContentEntries();
- for (final ContentEntry contentEntry : contentEntries) {
- final SourceFolder[] sourceFolders = contentEntry.getSourceFolders();
+ for (final ContentEntry contentEntry : ModuleRootManager.getInstance(module).getContentEntries()) {
+ final List<SourceFolder> sourceFolders = contentEntry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES);
for (final SourceFolder sourceFolder : sourceFolders) {
final String packagePrefix = sourceFolder.getPackagePrefix();
if (packagePrefix.startsWith(psiPackage.getQualifiedName())) {
@@ -100,10 +100,8 @@ public class PsiPackageImplementationHelperImpl extends PsiPackageImplementation
for (final Module module : modules) {
boolean anyChange = false;
final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();
- final ContentEntry[] contentEntries = rootModel.getContentEntries();
- for (final ContentEntry contentEntry : contentEntries) {
- final SourceFolder[] sourceFolders = contentEntry.getSourceFolders();
- for (final SourceFolder sourceFolder : sourceFolders) {
+ for (final ContentEntry contentEntry : rootModel.getContentEntries()) {
+ for (final SourceFolder sourceFolder : contentEntry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES)) {
final String packagePrefix = sourceFolder.getPackagePrefix();
if (packagePrefix.startsWith(oldQualifiedName)) {
sourceFolder.setPackagePrefix(newQualifiedName + packagePrefix.substring(oldQualifiedName.length()));
diff --git a/java/java-impl/src/com/intellij/psi/scope/processor/VariablesNotProcessor.java b/java/java-impl/src/com/intellij/psi/scope/processor/VariablesNotProcessor.java
deleted file mode 100644
index d36a0c0f1792..000000000000
--- a/java/java-impl/src/com/intellij/psi/scope/processor/VariablesNotProcessor.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.psi.scope.processor;
-
-import com.intellij.psi.PsiVariable;
-import com.intellij.psi.ResolveState;
-import com.intellij.util.SmartList;
-
-import java.util.List;
-
-public class VariablesNotProcessor extends VariablesProcessor{
- private final PsiVariable myVariable;
-
- public VariablesNotProcessor(PsiVariable var, boolean staticSensitive, List<PsiVariable> list){
- super(staticSensitive, list);
- myVariable = var;
- }
-
- public VariablesNotProcessor(PsiVariable var, boolean staticSensitive){
- this(var, staticSensitive, new SmartList<PsiVariable>());
- }
-
- @Override
- protected boolean check(PsiVariable var, ResolveState state) {
- String name = var.getName();
- return name != null && name.equals(myVariable.getName()) && !var.equals(myVariable);
- }
-}
diff --git a/java/java-impl/src/com/intellij/psi/scope/processor/VariablesProcessor.java b/java/java-impl/src/com/intellij/psi/scope/processor/VariablesProcessor.java
deleted file mode 100644
index 08ecc5379f0b..000000000000
--- a/java/java-impl/src/com/intellij/psi/scope/processor/VariablesProcessor.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.intellij.psi.scope.processor;
-
-import com.intellij.openapi.util.Key;
-import com.intellij.psi.PsiElement;
-import com.intellij.psi.PsiModifier;
-import com.intellij.psi.PsiVariable;
-import com.intellij.psi.ResolveState;
-import com.intellij.psi.scope.BaseScopeProcessor;
-import com.intellij.psi.scope.ElementClassHint;
-import com.intellij.psi.scope.JavaScopeProcessorEvent;
-import com.intellij.util.SmartList;
-import org.jetbrains.annotations.NotNull;
-
-import java.util.List;
-
-public abstract class VariablesProcessor extends BaseScopeProcessor implements ElementClassHint {
- private boolean myStaticScopeFlag = false;
- private final boolean myStaticSensitiveFlag;
- private final List<PsiVariable> myResultList;
-
- /** Collecting _all_ variables in scope */
- public VariablesProcessor(boolean staticSensitive){
- this(staticSensitive, new SmartList<PsiVariable>());
- }
-
- /** Collecting _all_ variables in scope */
- public VariablesProcessor(boolean staticSensitive, List<PsiVariable> list){
- myStaticSensitiveFlag = staticSensitive;
- myResultList = list;
- }
-
- protected abstract boolean check(PsiVariable var, ResolveState state);
-
- @Override
- public boolean shouldProcess(DeclarationKind kind) {
- return kind == DeclarationKind.VARIABLE || kind == DeclarationKind.FIELD || kind == DeclarationKind.ENUM_CONST;
- }
-
- /** Always return true since we wanna get all vars in scope */
- @Override
- public boolean execute(@NotNull PsiElement pe, ResolveState state){
- if(pe instanceof PsiVariable){
- final PsiVariable pvar = (PsiVariable)pe;
- if(!myStaticSensitiveFlag || !myStaticScopeFlag || pvar.hasModifierProperty(PsiModifier.STATIC)){
- if(check(pvar, state)){
- myResultList.add(pvar);
- }
- }
- }
- return true;
- }
-
- @Override
- public final void handleEvent(Event event, Object associated){
- if(event == JavaScopeProcessorEvent.START_STATIC)
- myStaticScopeFlag = true;
- }
-
- public int size(){
- return myResultList.size();
- }
-
- public PsiVariable getResult(int i){
- return myResultList.get(i);
- }
-
- @Override
- public <T> T getHint(@NotNull Key<T> hintKey) {
- if (hintKey == ElementClassHint.KEY) {
- return (T)this;
- }
-
- return super.getHint(hintKey);
- }
-}
diff --git a/java/java-impl/src/com/intellij/psi/util/CreateClassUtil.java b/java/java-impl/src/com/intellij/psi/util/CreateClassUtil.java
index 9a6695953817..5d19b4d40c65 100644
--- a/java/java-impl/src/com/intellij/psi/util/CreateClassUtil.java
+++ b/java/java-impl/src/com/intellij/psi/util/CreateClassUtil.java
@@ -18,31 +18,22 @@ package com.intellij.psi.util;
import com.intellij.ide.fileTemplates.FileTemplate;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.ide.fileTemplates.JavaCreateFromTemplateHandler;
-import com.intellij.ide.util.DirectoryChooserUtil;
import com.intellij.ide.util.PackageUtil;
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
-import com.intellij.openapi.roots.ModuleRootManager;
-import com.intellij.openapi.roots.impl.DirectoryIndex;
-import com.intellij.openapi.roots.impl.DirectoryInfo;
import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.openapi.vfs.VirtualFile;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.application.ApplicationManager;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
-import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.text.StringTokenizer;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import java.io.File;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
import java.util.Map;
import java.util.Properties;
@@ -121,53 +112,6 @@ public class CreateClassUtil {
return directoryRoot;
}
- @Nullable
- public static PsiDirectory getRootDirectory(PsiClass aClass) {
- return getSourceRootDirectory(aClass.getContainingFile().getContainingDirectory());
- }
-
- @Nullable
- private static PsiDirectory getSourceRootDirectory(PsiDirectory directory) {
- PsiManager manager = directory.getManager();
- DirectoryIndex directoryIndex = DirectoryIndex.getInstance(manager.getProject());
- DirectoryInfo info = directoryIndex.getInfoForDirectory(directory.getVirtualFile());
- if (info == null || !info.hasSourceRoot()) return null;
- return manager.findDirectory(info.getSourceRoot());
- }
-
- @Nullable
- public static PsiDirectory obtainDirectoryRootForPackage(final Module module, final String packageName) {
- final Project project = module.getProject();
- GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
- final PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(packageName);
- if (aPackage != null) {
- PsiDirectory[] directories = aPackage.getDirectories(scope);
- if (directories.length == 1) return getSourceRootDirectory(directories[0]);
- }
-
- final VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
- List<PsiDirectory> directoryList = new ArrayList<PsiDirectory>();
- for (VirtualFile sourceRoot : sourceRoots) {
- final PsiDirectory directory = PsiManager.getInstance(project).findDirectory(sourceRoot);
- directoryList.add(directory);
- }
- PsiDirectory[] sourceDirectories = directoryList.toArray(new PsiDirectory[directoryList.size()]);
-
- return DirectoryChooserUtil.selectDirectory(project, sourceDirectories, null, File.separatorChar + packageName.replace('.', File.separatorChar));
- }
-
- @Nullable
- public static PsiDirectory getRoot(Module module, String className) {
- String aPackage = extractPackage(className);
- PsiManager psiManager = PsiManager.getInstance(module.getProject());
- PsiPackage psiPackage = JavaPsiFacade.getInstance(psiManager.getProject()).findPackage(aPackage);
- if (psiPackage == null) return null;
- PsiDirectory[] directories = psiPackage.getDirectories(GlobalSearchScope.moduleScope(module));
- if (directories.length == 0) return null;
-
- return directories[0];
- }
-
public static String extractClassName(String fqName) {
return StringUtil.getShortName(fqName);
}
diff --git a/java/java-impl/src/com/intellij/refactoring/copy/CopyClassDialog.java b/java/java-impl/src/com/intellij/refactoring/copy/CopyClassDialog.java
index d803d5daecff..5460ee36e1f3 100644
--- a/java/java-impl/src/com/intellij/refactoring/copy/CopyClassDialog.java
+++ b/java/java-impl/src/com/intellij/refactoring/copy/CopyClassDialog.java
@@ -38,6 +38,7 @@ import com.intellij.util.ui.FormBuilder;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import javax.swing.*;
import java.awt.*;
@@ -114,7 +115,7 @@ class CopyClassDialog extends DialogWrapper{
}
final JLabel label = new JLabel(RefactoringBundle.message("target.destination.folder"));
- final boolean isMultipleSourceRoots = ProjectRootManager.getInstance(myProject).getContentSourceRoots().length > 1;
+ final boolean isMultipleSourceRoots = ProjectRootManager.getInstance(myProject).getModuleSourceRoots(JavaModuleSourceRootTypes.SOURCES).size() > 1;
myDestinationCB.setVisible(!myDoClone && isMultipleSourceRoots);
label.setVisible(!myDoClone && isMultipleSourceRoots);
label.setLabelFor(myDestinationCB);
diff --git a/java/java-impl/src/com/intellij/refactoring/extractSuperclass/JavaExtractSuperBaseDialog.java b/java/java-impl/src/com/intellij/refactoring/extractSuperclass/JavaExtractSuperBaseDialog.java
index cceea3cf14b7..59349c39a2b0 100644
--- a/java/java-impl/src/com/intellij/refactoring/extractSuperclass/JavaExtractSuperBaseDialog.java
+++ b/java/java-impl/src/com/intellij/refactoring/extractSuperclass/JavaExtractSuperBaseDialog.java
@@ -36,6 +36,7 @@ import com.intellij.refactoring.util.classMembers.MemberInfo;
import com.intellij.ui.EditorComboBox;
import com.intellij.ui.components.JBLabel;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import javax.swing.*;
import java.awt.*;
@@ -71,8 +72,8 @@ public abstract class JavaExtractSuperBaseDialog extends ExtractSuperBaseDialog<
@Override
protected JPanel createDestinationRootPanel() {
- final VirtualFile[] sourceRoots = ProjectRootManager.getInstance(myProject).getContentSourceRoots();
- if (sourceRoots.length <= 1) return super.createDestinationRootPanel();
+ final List<VirtualFile> sourceRoots = ProjectRootManager.getInstance(myProject).getModuleSourceRoots(JavaModuleSourceRootTypes.SOURCES);
+ if (sourceRoots.size() <= 1) return super.createDestinationRootPanel();
final JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
final JBLabel label = new JBLabel(RefactoringBundle.message("target.destination.folder"));
diff --git a/java/java-impl/src/com/intellij/refactoring/extractclass/ExtractClassDialog.java b/java/java-impl/src/com/intellij/refactoring/extractclass/ExtractClassDialog.java
index e31476970235..24dd3cff4ee5 100644
--- a/java/java-impl/src/com/intellij/refactoring/extractclass/ExtractClassDialog.java
+++ b/java/java-impl/src/com/intellij/refactoring/extractclass/ExtractClassDialog.java
@@ -42,6 +42,7 @@ import com.intellij.util.ui.FormBuilder;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
@@ -60,7 +61,6 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
private final JTextField classNameField;
private final ReferenceEditorComboWithBrowseButton packageTextField;
private final DestinationFolderComboBox myDestinationFolderComboBox;
- private final JTextField sourceClassTextField = null;
private JCheckBox myGenerateAccessorsCb;
private final JavaVisibilityPanel myVisibilityPanel;
private final JCheckBox extractAsEnum;
@@ -252,7 +252,7 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
.addLabeledComponent(new JLabel(), extractAsEnum)
.addLabeledComponent(RefactorJBundle.message("package.for.new.class.label"), packageTextField);
- if (ProjectRootManager.getInstance(myProject).getContentSourceRoots().length > 1) {
+ if (ProjectRootManager.getInstance(myProject).getModuleSourceRoots(JavaModuleSourceRootTypes.SOURCES).size() > 1) {
builder.addLabeledComponent(RefactoringBundle.message("target.destination.folder"), myDestinationFolderComboBox);
}
diff --git a/java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpDialog.java b/java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpDialog.java
index 8d36bf6bef90..526262efaedc 100644
--- a/java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpDialog.java
+++ b/java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpDialog.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2011 JetBrains s.r.o.
+ * Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import com.intellij.psi.statistics.StatisticsInfo;
import com.intellij.psi.statistics.StatisticsManager;
import com.intellij.psi.util.MethodSignature;
import com.intellij.psi.util.MethodSignatureUtil;
+import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.HelpID;
import com.intellij.refactoring.JavaRefactoringSettings;
@@ -222,9 +223,12 @@ public class PullUpDialog extends RefactoringDialog {
boolean hasJavadoc = false;
for (MemberInfo info : myMemberInfos) {
final PsiMember member = info.getMember();
- if (myMemberInfoModel.isAbstractEnabled(info) && member instanceof PsiDocCommentOwner && ((PsiDocCommentOwner)member).getDocComment() != null) {
- hasJavadoc = true;
- break;
+ if (myMemberInfoModel.isAbstractEnabled(info) && member instanceof PsiDocCommentOwner) {
+ info.setToAbstract(myMemberInfoModel.isAbstractWhenDisabled(info));
+ if (((PsiDocCommentOwner)member).getDocComment() != null) {
+ hasJavadoc = true;
+ break;
+ }
}
}
UIUtil.setEnabled(myJavaDocPanel, hasJavadoc, true);
@@ -270,6 +274,9 @@ public class PullUpDialog extends RefactoringDialog {
public boolean isAbstractEnabled(MemberInfo member) {
PsiClass currentSuperClass = getSuperClass();
if (currentSuperClass == null || !currentSuperClass.isInterface()) return true;
+ if (PsiUtil.isLanguageLevel8OrHigher(currentSuperClass)) {
+ return true;
+ }
return false;
}
diff --git a/java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpHelper.java b/java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpHelper.java
index c7c8c7f194c5..7a11a69e2d82 100644
--- a/java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpHelper.java
+++ b/java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpHelper.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2012 JetBrains s.r.o.
+ * Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -232,10 +232,18 @@ public class PullUpHelper extends BaseRefactoringProcessor{
if (method.findSuperMethods(myTargetSuperClass).length == 0) {
deleteOverrideAnnotationIfFound(methodCopy);
}
- final boolean isOriginalMethodAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT) || method.hasModifierProperty(PsiModifier.DEFAULT);
+ boolean isOriginalMethodAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT) || method.hasModifierProperty(PsiModifier.DEFAULT);
if (myIsTargetInterface || info.isToAbstract()) {
ChangeContextUtil.clearContextInfo(method);
- RefactoringUtil.makeMethodAbstract(myTargetSuperClass, methodCopy);
+
+ if (!info.isToAbstract() && !method.hasModifierProperty(PsiModifier.ABSTRACT)) {
+ //pull as default
+ RefactoringUtil.makeMethodDefault(methodCopy);
+ isOriginalMethodAbstract = true;
+ } else {
+ RefactoringUtil.makeMethodAbstract(myTargetSuperClass, methodCopy);
+ }
+
RefactoringUtil.replaceMovedMemberTypeParameters(methodCopy, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
myJavaDocPolicy.processCopiedJavaDoc(methodCopy.getDocComment(), method.getDocComment(), isOriginalMethodAbstract);
diff --git a/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/DestinationFolderComboBox.java b/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/DestinationFolderComboBox.java
index 8ce4f9fd3e0c..bb9379add0b1 100644
--- a/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/DestinationFolderComboBox.java
+++ b/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/DestinationFolderComboBox.java
@@ -34,12 +34,14 @@ import com.intellij.refactoring.MoveDestination;
import com.intellij.refactoring.PackageWrapper;
import com.intellij.ui.*;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
+import java.util.List;
/**
* User: anna
@@ -49,7 +51,7 @@ public abstract class DestinationFolderComboBox extends ComboboxWithBrowseButton
private static final String LEAVE_IN_SAME_SOURCE_ROOT = "Leave in same source root";
private static final DirectoryChooser.ItemWrapper NULL_WRAPPER = new DirectoryChooser.ItemWrapper(null, null);
private PsiDirectory myInitialTargetDirectory;
- private VirtualFile[] mySourceRoots;
+ private List<VirtualFile> mySourceRoots;
public DestinationFolderComboBox() {
super(new ComboBoxWithWidePopup());
@@ -79,7 +81,7 @@ public abstract class DestinationFolderComboBox extends ComboboxWithBrowseButton
final PsiDirectory initialTargetDirectory,
final Pass<String> errorMessageUpdater, final EditorComboBox editorComboBox) {
myInitialTargetDirectory = initialTargetDirectory;
- mySourceRoots = ProjectRootManager.getInstance(project).getContentSourceRoots();
+ mySourceRoots = ProjectRootManager.getInstance(project).getModuleSourceRoots(JavaModuleSourceRootTypes.SOURCES);
new ComboboxSpeedSearch(getComboBox()) {
@Override
protected String getElementText(Object element) {
@@ -168,7 +170,7 @@ public abstract class DestinationFolderComboBox extends ComboboxWithBrowseButton
VirtualFile selectedDestination = selectedPsiDirectory.getVirtualFile();
if (showChooserWhenDefault &&
myInitialTargetDirectory != null && Comparing.equal(selectedDestination, myInitialTargetDirectory.getVirtualFile()) &&
- mySourceRoots.length > 1) {
+ mySourceRoots.size() > 1) {
selectedDestination = MoveClassesOrPackagesUtil.chooseSourceRoot(targetPackage, mySourceRoots, myInitialTargetDirectory);
}
if (selectedDestination == null) return null;
@@ -197,7 +199,7 @@ public abstract class DestinationFolderComboBox extends ComboboxWithBrowseButton
final VirtualFile initialTargetDirectorySourceRoot,
final VirtualFile oldSelection,
final ProjectFileIndex fileIndex,
- final VirtualFile[] sourceRoots,
+ final List<VirtualFile> sourceRoots,
final Project project,
final boolean forceIncludeAll,
final Pass<String> updateErrorMessage) {
diff --git a/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/JavaMoveClassesOrPackagesHandler.java b/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/JavaMoveClassesOrPackagesHandler.java
index 8322228ec52a..2bbeb9dff91a 100644
--- a/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/JavaMoveClassesOrPackagesHandler.java
+++ b/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/JavaMoveClassesOrPackagesHandler.java
@@ -44,6 +44,7 @@ import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.HashSet;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import javax.swing.*;
import java.awt.*;
@@ -300,7 +301,7 @@ public class JavaMoveClassesOrPackagesHandler extends MoveHandlerDelegate {
private static boolean canMoveOrRearrangePackages(PsiElement[] elements) {
if (elements.length == 0) return false;
final Project project = elements[0].getProject();
- if (ProjectRootManager.getInstance(project).getContentSourceRoots().length == 1) {
+ if (ProjectRootManager.getInstance(project).getModuleSourceRoots(JavaModuleSourceRootTypes.SOURCES).size() == 1) {
return false;
}
for (PsiElement element : elements) {
diff --git a/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesDialog.java b/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesDialog.java
index e94e32ee460e..424eae716012 100644
--- a/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesDialog.java
+++ b/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesDialog.java
@@ -33,7 +33,6 @@ import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.search.ProjectScope;
import com.intellij.psi.util.PsiTreeUtil;
-import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.refactoring.*;
import com.intellij.refactoring.move.MoveCallback;
@@ -55,11 +54,13 @@ import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
+import java.util.List;
import java.util.Set;
public class MoveClassesOrPackagesDialog extends RefactoringDialog {
@@ -141,7 +142,7 @@ public class MoveClassesOrPackagesDialog extends RefactoringDialog {
private void updateControlsEnabled() {
myClassPackageChooser.setEnabled(myToPackageRadioButton.isSelected());
myInnerClassChooser.setEnabled(myMakeInnerClassOfRadioButton.isSelected());
- UIUtil.setEnabled(myTargetPanel, isMoveToPackage() && getSourceRoots().length > 1 && !myTargetDirectoryFixed, true);
+ UIUtil.setEnabled(myTargetPanel, isMoveToPackage() && getSourceRoots().size() > 1 && !myTargetDirectoryFixed, true);
validateButtons();
}
@@ -162,8 +163,7 @@ public class MoveClassesOrPackagesDialog extends RefactoringDialog {
}
protected JComponent createCenterPanel() {
- final VirtualFile[] sourceRoots = getSourceRoots();
- boolean isDestinationVisible = sourceRoots.length > 1;
+ boolean isDestinationVisible = getSourceRoots().size() > 1;
myDestinationFolderCB.setVisible(isDestinationVisible);
myTargetDestinationLabel.setVisible(isDestinationVisible);
return null;
@@ -289,7 +289,7 @@ public class MoveClassesOrPackagesDialog extends RefactoringDialog {
setErrorText(s);
}
}, myHavePackages ? myWithBrowseButtonReference.getChildComponent() : myClassPackageChooser.getChildComponent());
- UIUtil.setEnabled(myTargetPanel, getSourceRoots().length > 0 && isMoveToPackage() && !isTargetDirectoryFixed, true);
+ UIUtil.setEnabled(myTargetPanel, !getSourceRoots().isEmpty() && isMoveToPackage() && !isTargetDirectoryFixed, true);
validateButtons();
myHelpID = helpID;
}
@@ -505,7 +505,7 @@ public class MoveClassesOrPackagesDialog extends RefactoringDialog {
return ((DestinationFolderComboBox)myDestinationFolderCB).selectDirectory(targetPackage, mySuggestToMoveToAnotherRoot);
}
- private VirtualFile[] getSourceRoots() {
- return ProjectRootManager.getInstance(myProject).getContentSourceRoots();
+ private List<VirtualFile> getSourceRoots() {
+ return ProjectRootManager.getInstance(myProject).getModuleSourceRoots(JavaModuleSourceRootTypes.SOURCES);
}
}
diff --git a/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesImpl.java b/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesImpl.java
index e55aba9953d2..2a5f6396a482 100644
--- a/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesImpl.java
+++ b/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesImpl.java
@@ -44,6 +44,7 @@ import com.intellij.usageView.UsageInfo;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import java.util.ArrayList;
import java.util.Arrays;
@@ -367,7 +368,7 @@ public class MoveClassesOrPackagesImpl {
}
private static List<PsiDirectory> buildRearrangeTargetsList(final Project project, final PsiDirectory[] directories) {
- final VirtualFile[] sourceRoots = ProjectRootManager.getInstance(project).getContentSourceRoots();
+ final List<VirtualFile> sourceRoots = ProjectRootManager.getInstance(project).getModuleSourceRoots(JavaModuleSourceRootTypes.SOURCES);
List<PsiDirectory> sourceRootDirectories = new ArrayList<PsiDirectory>();
sourceRoots:
for (final VirtualFile sourceRoot : sourceRoots) {
diff --git a/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesUtil.java b/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesUtil.java
index c2cd0afaf958..2f3e93382199 100644
--- a/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesUtil.java
+++ b/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/MoveClassesOrPackagesUtil.java
@@ -40,6 +40,7 @@ import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.HashMap;
import com.intellij.psi.util.FileTypeUtils;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import java.io.File;
import java.util.*;
@@ -283,12 +284,13 @@ public class MoveClassesOrPackagesUtil {
directory = directories[0];
}
else {
- final VirtualFile[] contentSourceRoots = ProjectRootManager.getInstance(project).getContentSourceRoots();
- if (contentSourceRoots.length == 1 && (baseDirVirtualFile == null || fileIndex.isInTestSourceContent(contentSourceRoots[0]) == isBaseDirInTestSources)) {
+ final List<VirtualFile> contentSourceRoots = ProjectRootManager.getInstance(project).getModuleSourceRoots(
+ JavaModuleSourceRootTypes.SOURCES);
+ if (contentSourceRoots.size() == 1 && (baseDirVirtualFile == null || fileIndex.isInTestSourceContent(contentSourceRoots.get(0)) == isBaseDirInTestSources)) {
directory = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
@Override
public PsiDirectory compute() {
- return RefactoringUtil.createPackageDirectoryInSourceRoot(packageWrapper, contentSourceRoots[0]);
+ return RefactoringUtil.createPackageDirectoryInSourceRoot(packageWrapper, contentSourceRoots.get(0));
}
});
}
@@ -307,7 +309,7 @@ public class MoveClassesOrPackagesUtil {
}
public static VirtualFile chooseSourceRoot(final PackageWrapper targetPackage,
- final VirtualFile[] contentSourceRoots,
+ final List<VirtualFile> contentSourceRoots,
final PsiDirectory initialDirectory) {
Project project = targetPackage.getManager().getProject();
//ensure that there would be no duplicates: e.g. when one content root is subfolder of another root (configured via excluded roots)
@@ -330,7 +332,7 @@ public class MoveClassesOrPackagesUtil {
}
public static void buildDirectoryList(PackageWrapper aPackage,
- VirtualFile[] contentSourceRoots,
+ List<VirtualFile> contentSourceRoots,
LinkedHashSet<PsiDirectory> targetDirectories,
Map<PsiDirectory, String> relativePathsToCreate) {
@@ -372,7 +374,7 @@ public class MoveClassesOrPackagesUtil {
}
}
}
- LOG.assertTrue(targetDirectories.size() <= contentSourceRoots.length);
- LOG.assertTrue(relativePathsToCreate.size() <= contentSourceRoots.length);
+ LOG.assertTrue(targetDirectories.size() <= contentSourceRoots.size());
+ LOG.assertTrue(relativePathsToCreate.size() <= contentSourceRoots.size());
}
}
diff --git a/java/java-impl/src/com/intellij/refactoring/move/moveInner/MoveInnerDialog.java b/java/java-impl/src/com/intellij/refactoring/move/moveInner/MoveInnerDialog.java
index 901cac555ffb..b10af008c89f 100644
--- a/java/java-impl/src/com/intellij/refactoring/move/moveInner/MoveInnerDialog.java
+++ b/java/java-impl/src/com/intellij/refactoring/move/moveInner/MoveInnerDialog.java
@@ -49,10 +49,12 @@ import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
+import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -196,10 +198,10 @@ public class MoveInnerDialog extends RefactoringDialog {
final String targetName = myPackageNameField.getText();
if (!Comparing.equal(name, targetName)) {
final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(myProject);
- final VirtualFile[] contentSourceRoots = projectRootManager.getContentSourceRoots();
+ final List<VirtualFile> contentSourceRoots = projectRootManager.getModuleSourceRoots(JavaModuleSourceRootTypes.SOURCES);
final PackageWrapper newPackage = new PackageWrapper(PsiManager.getInstance(myProject), targetName);
final VirtualFile targetSourceRoot;
- if (contentSourceRoots.length > 1) {
+ if (contentSourceRoots.size() > 1) {
PsiDirectory initialDir = null;
if (oldPackage != null) {
final PsiDirectory[] directories = oldPackage.getDirectories();
@@ -215,7 +217,7 @@ public class MoveInnerDialog extends RefactoringDialog {
targetSourceRoot = sourceRoot;
}
else {
- targetSourceRoot = contentSourceRoots [0];
+ targetSourceRoot = contentSourceRoots.get(0);
}
PsiDirectory dir = RefactoringUtil.findPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
if (dir == null) {
diff --git a/java/java-impl/src/com/intellij/refactoring/typeMigration/ui/MigrationPanel.java b/java/java-impl/src/com/intellij/refactoring/typeMigration/ui/MigrationPanel.java
index 52f67fb3e8fe..c571e76b5a21 100644
--- a/java/java-impl/src/com/intellij/refactoring/typeMigration/ui/MigrationPanel.java
+++ b/java/java-impl/src/com/intellij/refactoring/typeMigration/ui/MigrationPanel.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2011 JetBrains s.r.o.
+ * Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import com.intellij.CommonBundle;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.*;
+import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.diagnostic.Logger;
@@ -28,7 +29,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Splitter;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.ReadonlyStatusHandler;
-import com.intellij.openapi.vfs.VfsUtil;
+import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.presentation.java.SymbolPresentationUtil;
@@ -193,28 +194,35 @@ public class MigrationPanel extends JPanel implements Disposable {
if (userObject instanceof MigrationRootNode) {
ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
public void run() {
- new WriteCommandAction(myProject) {
- protected void run(Result result) throws Throwable {
- final Collection<? extends AbstractTreeNode> children = ((MigrationRootNode)userObject).getChildren();
- for (AbstractTreeNode child : children) {
- expandTree((MigrationNode)child);
+ final Collection<? extends AbstractTreeNode> children = ((MigrationRootNode)userObject).getChildren();
+ for (AbstractTreeNode child : children) {
+ expandTree((MigrationNode)child);
+ }
+ final TypeMigrationUsageInfo[] usages = myLabeler.getMigratedUsages();
+ final HashSet<VirtualFile> files = new HashSet<VirtualFile>();
+ for (TypeMigrationUsageInfo usage : usages) {
+ if (!usage.isExcluded()) {
+ final PsiElement element = usage.getElement();
+ if (element != null) {
+ files.add(element.getContainingFile().getVirtualFile());
}
- final TypeMigrationUsageInfo[] usages = myLabeler.getMigratedUsages();
- final HashSet<VirtualFile> files = new HashSet<VirtualFile>();
- for (TypeMigrationUsageInfo usage : usages) {
- if (!usage.isExcluded()) {
- final PsiElement element = usage.getElement();
- if (element != null) {
- files.add(element.getContainingFile().getVirtualFile());
- }
- }
- }
- if (ReadonlyStatusHandler.getInstance(myProject).
- ensureFilesWritable(VfsUtil.toVirtualFileArray(files)).hasReadonlyFiles()) return;
+ }
+ }
- TypeMigrationProcessor.change(myLabeler, usages);
+ ApplicationManager.getApplication().invokeLater(new Runnable() {
+ @Override
+ public void run() {
+ if (ReadonlyStatusHandler.getInstance(myProject).
+ ensureFilesWritable(VfsUtilCore.toVirtualFileArray(files)).hasReadonlyFiles()) {
+ return;
+ }
+ new WriteCommandAction(myProject) {
+ protected void run(Result result) throws Throwable {
+ TypeMigrationProcessor.change(myLabeler, usages);
+ }
+ }.execute();
}
- }.execute();
+ }, myProject.getDisposed());
}
}, "Type Migration", false, myProject);
}
diff --git a/java/java-impl/src/com/intellij/refactoring/util/RefactoringUtil.java b/java/java-impl/src/com/intellij/refactoring/util/RefactoringUtil.java
index abf333ce4f1e..acb22682ee05 100644
--- a/java/java-impl/src/com/intellij/refactoring/util/RefactoringUtil.java
+++ b/java/java-impl/src/com/intellij/refactoring/util/RefactoringUtil.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2012 JetBrains s.r.o.
+ * Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -763,14 +763,25 @@ public class RefactoringUtil {
PsiUtil.setModifierProperty(method, PsiModifier.ABSTRACT, true);
}
- PsiUtil.setModifierProperty(method, PsiModifier.FINAL, false);
- PsiUtil.setModifierProperty(method, PsiModifier.SYNCHRONIZED, false);
- PsiUtil.setModifierProperty(method, PsiModifier.NATIVE, false);
+ prepareForInterface(method);
if (!targetClass.isInterface()) {
PsiUtil.setModifierProperty(targetClass, PsiModifier.ABSTRACT, true);
}
+ }
+
+ public static void makeMethodDefault(@NotNull PsiMethod method) throws IncorrectOperationException {
+ PsiUtil.setModifierProperty(method, PsiModifier.DEFAULT, true);
+ PsiUtil.setModifierProperty(method, PsiModifier.ABSTRACT, false);
+
+ prepareForInterface(method);
+ }
+
+ private static void prepareForInterface(PsiMethod method) {
+ PsiUtil.setModifierProperty(method, PsiModifier.FINAL, false);
+ PsiUtil.setModifierProperty(method, PsiModifier.SYNCHRONIZED, false);
+ PsiUtil.setModifierProperty(method, PsiModifier.NATIVE, false);
removeFinalParameters(method);
}
diff --git a/java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestAction.java b/java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestAction.java
index daee6ffb5b8f..225247b9682e 100644
--- a/java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestAction.java
+++ b/java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestAction.java
@@ -25,10 +25,8 @@ import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.project.Project;
-import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ProjectRootManager;
-import com.intellij.openapi.roots.SourceFolder;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
@@ -38,6 +36,7 @@ import com.intellij.testIntegration.TestFramework;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaSourceRootType;
import java.util.HashSet;
import java.util.Set;
@@ -148,23 +147,12 @@ public class CreateTestAction extends PsiElementBaseIntentionAction {
final boolean isFirst = processed.isEmpty();
if (!processed.add(srcModule)) return;
- final ContentEntry[] entries = ModuleRootManager.getInstance(srcModule).getContentEntries();
- for (ContentEntry entry : entries) {
- for (SourceFolder sourceFolder : entry.getSourceFolders()) {
- if (sourceFolder.isTestSource()) {
- final VirtualFile sourceFolderFile = sourceFolder.getFile();
- if (sourceFolderFile != null) {
- testFolders.add(sourceFolderFile);
- }
- }
- }
- }
+ testFolders.addAll(ModuleRootManager.getInstance(srcModule).getSourceRoots(JavaSourceRootType.TEST_SOURCE));
if (isFirst && !testFolders.isEmpty()) return;
final HashSet<Module> modules = new HashSet<Module>();
ModuleUtilCore.collectModulesDependsOn(srcModule, modules);
for (Module module : modules) {
-
checkForTestRoots(module, testFolders, processed);
}
}
diff --git a/java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestDialog.java b/java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestDialog.java
index 400cb3ef3ccf..d34a3600d4dc 100644
--- a/java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestDialog.java
+++ b/java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestDialog.java
@@ -36,9 +36,7 @@ import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.help.HelpManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
-import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModuleRootManager;
-import com.intellij.openapi.roots.SourceFolder;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Pair;
@@ -53,10 +51,15 @@ import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.refactoring.util.classMembers.MemberInfo;
import com.intellij.testIntegration.TestFramework;
import com.intellij.testIntegration.TestIntegrationUtils;
-import com.intellij.ui.*;
+import com.intellij.ui.EditorTextField;
+import com.intellij.ui.RecentsManager;
+import com.intellij.ui.ReferenceEditorComboWithBrowseButton;
+import com.intellij.ui.ScrollPaneFactory;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
+import org.jetbrains.jps.model.java.JavaSourceRootType;
import javax.swing.*;
import java.awt.*;
@@ -449,16 +452,16 @@ public class CreateTestDialog extends DialogWrapper {
protected void run(Result<VirtualFile> result) throws Throwable {
final HashSet<VirtualFile> testFolders = new HashSet<VirtualFile>();
CreateTestAction.checkForTestRoots(myTargetModule, testFolders);
- VirtualFile[] roots;
+ List<VirtualFile> roots;
if (testFolders.isEmpty()) {
- roots = ModuleRootManager.getInstance(myTargetModule).getSourceRoots();
- if (roots.length == 0) return;
+ roots = ModuleRootManager.getInstance(myTargetModule).getSourceRoots(JavaModuleSourceRootTypes.SOURCES);
+ if (roots.isEmpty()) return;
} else {
- roots = testFolders.toArray(new VirtualFile[testFolders.size()]);
+ roots = new ArrayList<VirtualFile>(testFolders);
}
- if (roots.length == 1) {
- result.setResult(roots[0]);
+ if (roots.size() == 1) {
+ result.setResult(roots.get(0));
}
else {
PsiDirectory defaultDir = chooseDefaultDirectory(packageName);
@@ -479,15 +482,10 @@ public class CreateTestDialog extends DialogWrapper {
@Nullable
private PsiDirectory chooseDefaultDirectory(String packageName) {
List<PsiDirectory> dirs = new ArrayList<PsiDirectory>();
- for (ContentEntry e : ModuleRootManager.getInstance(myTargetModule).getContentEntries()) {
- for (SourceFolder f : e.getSourceFolders()) {
- final VirtualFile file = f.getFile();
- if (file != null && f.isTestSource()) {
- final PsiDirectory dir = PsiManager.getInstance(myProject).findDirectory(file);
- if (dir != null) {
- dirs.add(dir);
- }
- }
+ for (VirtualFile file : ModuleRootManager.getInstance(myTargetModule).getSourceRoots(JavaSourceRootType.TEST_SOURCE)) {
+ final PsiDirectory dir = PsiManager.getInstance(myProject).findDirectory(file);
+ if (dir != null) {
+ dirs.add(dir);
}
}
if (!dirs.isEmpty()) {