// ĠCopyright Ġ 2 0 0 0 - 2 0 2 0 ĠJetBrains Ġs . r . o . ĠUse Ġof Ġthis Ġsource Ġcode Ġis Ġgoverned Ġby Ġthe ĠApache Ġ 2 . 0 Ġlicense Ġthat Ġcan Ġbe Ġfound Ġin Ġthe ĠLICENSE Ġfile . Ċ @ file : Jvm Name (" Gradle Jvm Resolution Util ") Ċ Ċ package Ġorg . jetbrains . plugins . gradle . util Ċ Ċ import Ġcom . intellij . ide . impl . Project View Select In Target Ċ import Ġcom . intellij . ide . project View . impl . Project View Pane Ċ import Ġcom . intellij . ide . util . Editor Helper Ċ import Ġcom . intellij . notification . Notification Ċ import Ġcom . intellij . notification . Notification Listener Ċ import Ġcom . intellij . notification . Notification Type . WARNING Ċ import Ġcom . intellij . openapi . external System . service . execution . External System J dk Util Ċ import Ġcom . intellij . openapi . external System . util . External System Api Util Ċ import Ġcom . intellij . openapi . project . Project Ċ import Ġcom . intellij . openapi . project Roots . Project J dk Table Ċ import Ġcom . intellij . openapi . project Roots . Sdk Ċ import Ġcom . intellij . openapi . roots . Project Root Manager Ċ import Ġcom . intellij . openapi . util . io . File Util Ċ import Ġcom . intellij . openapi . vfs . Local FileSystem Ċ import Ġcom . intellij . psi . Psi Manager Ċ import Ġcom . intellij . util . lang . Java Version Ċ import Ġorg . gradle . util . Gradle Version Ċ import Ġorg . jetbrains . plugins . gradle . service . project . Gradle Notification . NOTIFICATION _ GROUP Ċ import Ġorg . jetbrains . plugins . gradle . settings . Gradle Project Settings Ċ import Ġorg . jetbrains . plugins . gradle . settings . Gradle Settings Ċ import Ġorg . jetbrains . plugins . gradle . util . Gradle Properties . Gradle Property Ċ import Ġjavax . swing . event . Hyper link Event Ċ Ċ const Ġval ĠJAVA _ HOME Ġ= Ġ" JAVA _ HOME " Ċ Ċ fun Ġsuggest Gradle Jvm ( project : ĠProject , Ġproject Sdk : ĠS dk ?, Ġexternal Project Path : ĠString , Ġgradle Version : ĠGradle Version ): ĠString ? Ġ{ ĊĠ Ġwith ( Gradle Jvm Resolution Context ( project , Ġproject Sdk , Ġexternal Project Path , Ġgradle Version )) Ġ{ ĊĠĠĠ Ġval Ġsuggested Gradle Jvm Ġ= ĊĠĠĠĠĠ Ġget Or Add Gradle Java Home J dk Reference () ĊĠĠĠĠĠ Ġ?: Ġget Or Add Env Java Home J dk Reference () ĊĠĠĠĠĠ Ġ?: Ġget Gradle J dk Reference () ĊĠĠĠĠĠ Ġ?: Ġget Project J dk Reference () ĊĠĠĠĠĠ Ġ?: Ġget Most Recent J dk Reference () ĊĠĠĠĠĠ Ġ?: Ġget And Add External J dk Reference () ĊĠĠĠĠĠ Ġ?: Ġreturn Ġnull ĊĠĠĠ Ġreturn Ġresolve Reference ( suggest ed Gradle Jvm ) ĊĠ Ġ} Ċ } Ċ Ċ fun Ġupdate Gradle Jvm ( project : ĠProject , Ġexternal Project Path : ĠString ) Ġ{ ĊĠ Ġval Ġsettings Ġ= ĠGradle Settings . getInstance ( project ) ĊĠ Ġval Ġproject Settings Ġ= Ġsettings . get Linked Project Settings ( external Project Path ) Ġ?: Ġreturn ĊĠ Ġval Ġgradle Jvm Ġ= Ġproject Settings . gradle Jvm Ġ?: Ġreturn ĊĠ Ġval Ġproject Root Manager Ġ= ĠProject Root Manager . getInstance ( project ) ĊĠ Ġval Ġproject Sdk Ġ= Ġproject Root Manager . project Sdk Ġ?: Ġreturn ĊĠ Ġval Ġgradle Version Ġ= Ġproject Settings . resolve Gradle Version () ĊĠ Ġwith ( Gradle Jvm Resolution Context ( project , Ġproject Sdk , Ġexternal Project Path , Ġgradle Version )) Ġ{ ĊĠĠĠ Ġif Ġ( project Sdk . name Ġ!= Ġgradle Jvm ) Ġreturn ĊĠĠĠ Ġif Ġ(! isValid And Supported ( project Sdk )) Ġreturn ĊĠĠĠ Ġproject Settings . gradle Jvm Ġ= ĠExternal System J dk Util . USE _ PROJECT _ JDK ĊĠ Ġ} Ċ } Ċ Ċ private Ġclass ĠGradle Jvm Resolution Context ( ĊĠ Ġval Ġproject : ĠProject , ĊĠ Ġproject Sdk : ĠS dk ?, ĊĠ Ġval Ġexternal Project Path : ĠString , ĊĠ Ġval Ġgradle Version : ĠGradle Version Ċ ) Ġ{ ĊĠ Ġval Ġpossible Gradle Jv ms : ĠList < Sdk > Ġby Ġlazy Ġ{ Ġresolve Possible Gradle Jv ms () Ġ} ĊĠ Ġval Ġproject Sdk : ĠS dk ? Ġby Ġlazy Ġ{ Ġproject Sdk Ġ?: ĠProject Root Manager . getInstance ( project ). project Sdk Ġ} Ċ } Ċ Ċ sealed Ġclass ĠFailure Reason Ġ{ ĊĠ Ġobject ĠInvalid Ġ: ĠFailure Reason () ĊĠ Ġdata Ġclass ĠUnsupported ( val Ġjava Version : ĠJava Version , Ġval Ġgradle Version : ĠGradle Version ) Ġ: ĠFailure Reason () Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . resolve Reference ( sdk : ĠS dk ?): ĠString ? Ġ{ ĊĠ Ġreturn Ġwhen Ġ( sdk ) Ġ{ ĊĠĠĠ Ġnull Ġ-> Ġnull ĊĠĠĠ Ġproject Sdk Ġ-> ĠExternal System J dk Util . USE _ PROJECT _ JDK ĊĠĠĠ Ġelse Ġ-> Ġsdk . name ĊĠ Ġ} Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . check Gradle Jvm ( java Home : ĠString ): ĠFailure Reason ? Ġ{ ĊĠ Ġif Ġ(! External System J dk Util . isValid J dk ( java Home )) Ġreturn ĠFailure Reason . Invalid ĊĠ Ġval Ġjava Sdk Type Ġ= ĠExternal System J dk Util . get Java Sdk Type () ĊĠ Ġval Ġversion String Ġ= Ġjava Sdk Type . getVersion String ( java Home ) Ġ?: Ġreturn ĠFailure Reason . Invalid ĊĠ Ġval Ġjava Version Ġ= ĠJava Version . try Parse ( version String ) Ġ?: Ġreturn ĠFailure Reason . Invalid ĊĠ Ġif Ġ(! is Supported ( version String )) Ġreturn ĠFailure Reason . Unsupported ( java Version , Ġgradle Version ) ĊĠ Ġreturn Ġnull Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . isValid And Supported ( home Path : ĠString ): ĠBoolean Ġ{ ĊĠ Ġif Ġ(! External System J dk Util . isValid J dk ( home Path )) Ġreturn Ġfalse ĊĠ Ġval Ġjava Sdk Type Ġ= ĠExternal System J dk Util . get Java Sdk Type () ĊĠ Ġval Ġversion String Ġ= Ġjava Sdk Type . getVersion String ( home Path ) Ġ?: Ġreturn Ġfalse ĊĠ Ġreturn Ġis Supported ( version String ) Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . isValid And Supported ( sdk : ĠS dk ): ĠBoolean Ġ{ ĊĠ Ġif Ġ(! External System J dk Util . isValid J dk ( sdk )) Ġreturn Ġfalse ĊĠ Ġval Ġversion String Ġ= Ġsdk . version String Ġ?: Ġreturn Ġfalse ĊĠ Ġreturn Ġis Supported ( version String ) Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . is Supported ( version String : ĠString ): ĠBoolean Ġ{ ĊĠ Ġval Ġversion Ġ= ĠJava Version . try Parse ( version String ) Ġ?: Ġreturn Ġfalse ĊĠ Ġreturn Ġwhen Ġ{ ĊĠĠĠ Ġgradle Version Ġ>= ĠGradle Version . version (" 6 . 0 ") Ġ-> Ġversion . feature Ġin Ġ 8 .. 1 3 ĊĠĠĠ Ġgradle Version Ġ>= ĠGradle Version . version (" 5 . 4 . 1 ") Ġ-> Ġversion . feature Ġin Ġ 8 .. 1 2 ĊĠĠĠ Ġgradle Version Ġ>= ĠGradle Version . version (" 5 . 0 ") Ġ-> Ġversion . feature Ġin Ġ 8 .. 1 1 ĊĠĠĠ Ġgradle Version Ġ>= ĠGradle Version . version (" 4 . 1 ") Ġ-> Ġversion . feature Ġin Ġ 7 .. 9 ĊĠĠĠ Ġgradle Version Ġ>= ĠGradle Version . version (" 4 . 0 ") Ġ-> Ġversion . feature Ġin Ġ 7 .. 8 ĊĠĠĠ Ġelse Ġ-> Ġversion . feature Ġin Ġ 6 .. 8 ĊĠ Ġ} Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . resolve Possible Gradle Jv ms (): ĠList < Sdk > Ġ{ ĊĠ Ġval Ġproject J dk Table Ġ= ĠProject J dk Table . getInstance () ĊĠ Ġval Ġjava Sdk Type Ġ= ĠExternal System J dk Util . get Java Sdk Type () ĊĠ Ġreturn Ġproject J dk Table . get Sdk s OfType ( java Sdk Type ) ĊĠĠĠ Ġ. filter Ġ{ ĠisValid And Supported ( it ) Ġ} Ċ } Ċ Ċ private Ġfun Ġfind Or Add J dk ( home Path : ĠString ): ĠS dk ? Ġ{ ĊĠ Ġval Ġproject J dk Table Ġ= ĠProject J dk Table . getInstance () ĊĠ Ġval Ġjava Sdk Type Ġ= ĠExternal System J dk Util . get Java Sdk Type () ĊĠ Ġval Ġcanonical Home Path Ġ= ĠFile Util . to Canonical Path ( home Path ) ĊĠ Ġval Ġfound J dk Ġ= Ġproject J dk Table . get Sdk s OfType ( java Sdk Type ) ĊĠĠĠ Ġ. find Ġ{ ĠFile Util . to Canonical Path ( it . home Path ) Ġ== Ġcanonical Home Path Ġ} ĊĠ Ġif Ġ( found J dk Ġ!= Ġnull ) Ġreturn Ġfound J dk ĊĠ Ġreturn ĠExternal System J dk Util . add J dk ( canonical Home Path ) Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . find Or Add Gradle J dk ( home Path : ĠString ): ĠS dk ? Ġ{ ĊĠ Ġval Ġcanonical Home Path Ġ= ĠFile Util . to Canonical Path ( home Path ) ĊĠ Ġval Ġfound J dk Ġ= Ġpossible Gradle Jv ms . find Ġ{ ĠFile Util . to Canonical Path ( it . home Path ) Ġ== Ġcanonical Home Path Ġ} ĊĠ Ġif Ġ( found J dk Ġ!= Ġnull ) Ġreturn Ġfound J dk ĊĠ Ġreturn ĠExternal System J dk Util . add J dk ( canonical Home Path ) Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . get Gradle J dk Reference (): ĠS dk ? Ġ{ ĊĠ Ġval Ġsettings Ġ= ĠExternal System Api Util . get Settings ( project , ĠGradle Constants . SYSTEM _ ID ) ĊĠ Ġreturn Ġsettings . get Linked Projects Settings () ĊĠĠĠ Ġ. as Sequence () ĊĠĠĠ Ġ. filter Is Instance < Gradle Project Settings >() ĊĠĠĠ Ġ. map NotNull Ġ{ Ġps Ġ-> Ġpossible Gradle Jv ms . find Ġ{ Ġit . name Ġ== Ġps . gradle Jvm Ġ} Ġ} ĊĠĠĠ Ġ. first OrNull () Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . get Or Add Gradle Java Home J dk Reference (): ĠS dk ? Ġ{ ĊĠ Ġval Ġproperties Ġ= Ġget Gradle Properties ( external Project Path ) ĊĠ Ġval Ġjava Home Property Ġ= Ġproperties . java Home Property Ġ?: Ġreturn Ġnull ĊĠ Ġval Ġjava Home Ġ= Ġjava Home Property . value ĊĠ Ġval Ġfailure Reason Ġ= Ġcheck Gradle Jvm ( java Home ) ĊĠ Ġif Ġ( failure Reason Ġ!= Ġnull ) Ġ{ ĊĠĠĠ Ġnotify Invalid Gradle Java Home Warning ( java Home Property , Ġfailure Reason ) ĊĠ Ġ} ĊĠ Ġreturn Ġfind Or Add J dk ( java Home ) Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . get Or Add Env Java Home J dk Reference (): ĠS dk ? Ġ{ ĊĠ Ġval Ġjava Home Ġ= ĠEnvironment . get Env Variable ( JAVA _ HOME ) ĊĠ Ġif Ġ( java Home Ġ== Ġnull ) Ġ{ ĊĠĠĠ Ġnotify Undefined Java Home Warning () ĊĠĠĠ Ġreturn Ġnull ĊĠ Ġ} ĊĠ Ġval Ġfailure Reason Ġ= Ġcheck Gradle Jvm ( java Home ) ĊĠ Ġif Ġ( failure Reason Ġ!= Ġnull ) Ġ{ ĊĠĠĠ Ġnotify Invalid Java Home Warning ( failure Reason ) ĊĠĠĠ Ġreturn Ġnull ĊĠ Ġ} ĊĠ Ġreturn Ġfind Or Add Gradle J dk ( java Home ) Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . getProject J dk Reference (): ĠS dk ? Ġ{ ĊĠ Ġval Ġproject Sdk Ġ= Ġproject Sdk Ġ?: Ġreturn Ġnull ĊĠ Ġval Ġresolved Project Sdk Ġ= ĠExternal System J dk Util . resolve Dependent J dk ( project Sdk ) ĊĠ Ġif Ġ(! isValid And Supported ( resolved Project Sdk )) Ġreturn Ġnull ĊĠ Ġreturn Ġresolved Project Sdk Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . get Most Recent J dk Reference (): ĠS dk ? Ġ{ ĊĠ Ġval Ġjava Sdk Type Ġ= ĠExternal System J dk Util . get Java Sdk Type () ĊĠ Ġreturn Ġpossible Gradle Jv ms . max With ( java Sdk Type . version Comparator ()) Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . get And Add External J dk Reference (): ĠS dk ? Ġ{ ĊĠ Ġval Ġjava Sdk Type Ġ= ĠExternal System J dk Util . get Java Sdk Type () ĊĠ Ġval Ġversion Comparator Ġ= Ġjava Sdk Type . version String Comparator () ĊĠ Ġreturn ĠExternal System J dk Util . suggest J dk Home Paths () ĊĠĠĠ Ġ. filter Ġ{ ĠisValid And Supported ( it ) Ġ} ĊĠĠĠ Ġ. map Ġ{ Ġit Ġto Ġjava Sdk Type . getVersion String ( it ) Ġ} ĊĠĠĠ Ġ. max With ( Comparator Ġ{ Ġ(_, Ġv 1 ), Ġ(_, Ġv 2 ) Ġ-> Ġversion Comparator . compare ( v 1 , Ġv 2 ) Ġ}) ĊĠĠĠ Ġ? . let Ġ{ Ġ( home Path , Ġ_) Ġ-> Ġfind Or Add Gradle J dk ( home Path ) Ġ} Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . notify Invalid Gradle Java Home Warning ( java Home Property : ĠGradle Property < String >, Ġreason : ĠFailure Reason ) Ġ{ ĊĠ Ġval Ġproperty Location Ġ= Ġcreate Link ToFile ( java Home Property . location ) ĊĠ Ġval Ġnotification Content Ġ= ĠGradle Bundle . message (" gradle . notifications . java . home . property . content ", Ġproperty Location ) ĊĠ Ġnotify Invalid Gradle Jvm Warning ( notification Content , Ġreason ) Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . notify Invalid Java Home Warning ( reason : ĠFailure Reason ) Ġ{ ĊĠ Ġval Ġnotification Content Ġ= ĠGradle Bundle . message (" gradle . notifications . java . home . variable . content ") ĊĠ Ġnotify Invalid Gradle Jvm Warning ( notification Content , Ġreason ) Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . create Link ToFile ( path : ĠString ): ĠString Ġ{ ĊĠ Ġval Ġpresent able Path Ġ= Ġwhen Ġ{ ĊĠĠĠ ĠFile Util . is Ancestor ( external Project Path , Ġpath , Ġtrue ) Ġ-> ĠFile Util . get RelativePath ( external Project Path , Ġpath , Ġ'/') ĊĠĠĠ Ġelse Ġ-> ĠFile Util . getLocation Relative To User Home ( path ) ĊĠ Ġ} ĊĠ Ġreturn Ġ"< a Ġhref ='$ path '> $ present able Path </ a >" Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . notify Invalid Gradle Jvm Warning ( notification Hint : ĠString , Ġreason : ĠFailure Reason ) Ġ{ ĊĠ Ġval Ġnotification Title Ġ= ĠGradle Bundle . message (" gradle . notifications . java . home . invalid . title ") ĊĠ Ġvar Ġnotification Content Ġ= Ġnotification Hint ĊĠ Ġif Ġ( reason Ġis ĠFailure Reason . Unsupported ) Ġ{ ĊĠĠĠ Ġval Ġjava Version Ġ= Ġreason . java Version . toString () ĊĠĠĠ Ġval Ġgradle Version Ġ= Ġreason . gradle Version . version ĊĠĠĠ Ġval Ġadditional Failure Hint Ġ= ĠGradle Bundle . message (" gradle . notifications . java . home . unsupported . content ", Ġjava Version , Ġgradle Version ) ĊĠĠĠ Ġnotification Content Ġ= Ġ"$ additional Failure Hint Ġ$ notification Content " ĊĠ Ġ} ĊĠ Ġval Ġhyper Link Processor Ġ= Ġobject Ġ: ĠNotification Listener . Adapter () Ġ{ ĊĠĠĠ Ġoverride Ġfun Ġhyper link Activated ( notification : ĠNotification , Ġe : ĠHyper link Event ) Ġ{ ĊĠĠĠĠĠ Ġval Ġfile Ġ= ĠLocal FileSystem . getInstance (). find File By Path ( e . description ) Ġ?: Ġreturn ĊĠĠĠĠĠ ĠProject View Select In Target . select ( project , Ġfile , ĠProject View Pane . ID , Ġnull , Ġfile , Ġtrue ) ĊĠĠĠĠĠ Ġval Ġpsi File Ġ= ĠPsi Manager . getInstance ( project ). find File ( file ) Ġ?: Ġreturn ĊĠĠĠĠĠ ĠEditor Helper . open In Editor ( psi File ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĠ Ġval Ġnotification Ġ= ĠNOT IFICATION _ GROUP . create Notification ( notification Title , Ġnotification Content , ĠWARNING , Ġhyper Link Processor ) ĊĠ Ġnotification . notify ( project ) Ċ } Ċ Ċ private Ġfun ĠGradle Jvm Resolution Context . notify Undefined Java Home Warning () Ġ{ ĊĠ Ġval Ġnotification Title Ġ= ĠGradle Bundle . message (" gradle . notifications . java . home . undefined . title ") ĊĠ Ġval Ġnotification Content Ġ= ĠGradle Bundle . message (" gradle . notifications . java . home . undefined . content ") ĊĠ Ġval Ġnotification Ġ= ĠNOT IFICATION _ GROUP . create Notification ( notification Title , Ġnotification Content , ĠWARNING ) ĊĠ Ġnotification . notify ( project ) Ċ }