11package io .github .linyimin .plugin .compile ;
22
33import com .intellij .compiler .impl .ProjectCompileScope ;
4- import com .intellij .ide .plugins .cl .PluginClassLoader ;
54import com .intellij .openapi .compiler .CompilerManager ;
65import com .intellij .openapi .project .Project ;
76import com .intellij .openapi .roots .OrderEnumerator ;
87import com .intellij .openapi .roots .ProjectRootManager ;
98import com .intellij .openapi .ui .Messages ;
109import com .intellij .openapi .util .io .FileUtil ;
1110import com .intellij .openapi .vfs .VirtualFile ;
12- import com .intellij .util .lang .UrlClassLoader ;
13- import io .github .linyimin .plugin .dom .Constant ;
1411import io .github .linyimin .plugin .utils .JavaUtils ;
1512import org .apache .commons .collections .CollectionUtils ;
13+ import org .apache .commons .lang3 .StringUtils ;
1614
1715import java .io .File ;
18- import java .lang . reflect . Field ;
16+ import java .io . IOException ;
1917import java .net .MalformedURLException ;
2018import java .net .URL ;
2119import java .util .*;
@@ -31,7 +29,7 @@ public class MybatisPojoCompile {
3129
3230 // TODO: 需要优化成只编译Mybatis需要的类
3331
34- public static UrlClassLoader classLoader ;
32+ public static ProjectLoader classLoader ;
3533 public static List <String > preDependencies ;
3634
3735 public static void compile (Project project ) {
@@ -47,15 +45,7 @@ public static void setClassLoader(Project project) {
4745
4846 List <String > dependencies = getProjectDependencies (project );
4947
50- List <URL > urls = new ArrayList <>();
51- for (String path : dependencies ) {
52- try {
53- urls .add (new File (FileUtil .toSystemIndependentName (path )).toURI ().toURL ());
54- }
55- catch (MalformedURLException e ) {
56- Messages .showErrorDialog (e .getMessage (), "MalformedURLException" );
57- }
58- }
48+ List <URL > urls = pathToURL (dependencies );
5949
6050 if (Objects .nonNull (classLoader )) {
6151 changeLoaderUrls (preDependencies , dependencies );
@@ -65,6 +55,30 @@ public static void setClassLoader(Project project) {
6555 preDependencies = dependencies ;
6656 }
6757
58+ private static List <URL > pathToURL (List <String > paths ) {
59+ List <URL > urls = new ArrayList <>();
60+
61+ try {
62+ for (String path : paths ) {
63+ urls .add (new File (FileUtil .toSystemIndependentName (path )).toURI ().toURL ());
64+ }
65+
66+ // 添加本地获取消息类
67+ URL url = MybatisPojoCompile .class .getClassLoader ().getResource ("io/github/linyimin/plugin/utils/MybatisSqlUtils.class" );
68+ if (Objects .nonNull (url )) {
69+ String fileName = MybatisPojoCompile .class .getClassLoader ().getResource ("io/github/linyimin/plugin/utils/MybatisSqlUtils.class" ).getFile ();
70+
71+ fileName = StringUtils .substring (fileName , 0 , fileName .indexOf ("jar!" ) + 3 );
72+
73+ urls .add (new URL (fileName ));
74+ }
75+
76+ } catch (MalformedURLException e ) {
77+ Messages .showErrorDialog (e .getMessage (), "MalformedURLException" );
78+ }
79+
80+ return urls ;
81+ }
6882 private static List <String > getProjectDependencies (Project project ) {
6983
7084 Set <String > mapperDependencies = JavaUtils .getAllDependenciesRecursive (project );
@@ -75,13 +89,7 @@ private static List<String> getProjectDependencies(Project project) {
7589 .runtimeOnly ()
7690 .withoutSdk ()
7791 .getPathsList ()
78- .getPathList ()
79- .stream ()
80- .filter (path -> path .contains (Constant .MYBATIS_LOGGING_LOG4J )
81- || path .contains (Constant .MYBATIS_LOGGING_SLF4J )
82- || isProjectModule (project , path ))
83- .collect (Collectors .toList ());
84-
92+ .getPathList ();
8593
8694 list .addAll (mapperDependencies );
8795
@@ -99,49 +107,10 @@ private static boolean isProjectModule(Project project, String path) {
99107 }
100108
101109 private static void createProjectLoader (List <URL > urls ) {
102- classLoader = UrlClassLoader .build ().urls (urls ).parent (ClassLoader .getSystemClassLoader ()).get ();
103- attachPluginParentLoader (classLoader );
104- }
105110
106- private static void attachPluginParentLoader (UrlClassLoader classLoader ) {
107- PluginClassLoader pluginClassLoader = (PluginClassLoader ) MybatisPojoCompile .class .getClassLoader ();
108-
109- Class <? extends PluginClassLoader > pluginClassLoaderClass = pluginClassLoader .getClass ();
110-
111- Field field = null ;
112-
113- try {
114- field = pluginClassLoaderClass .getDeclaredField (Constant .PLUGIN_CLASS_LOADER_PARENTS );
115- } catch (NoSuchFieldException ignored ) {
116- }
117-
118- try {
119- field = pluginClassLoaderClass .getDeclaredField (Constant .PLUGIN_CLASS_LOADER_MY_PARENTS );
120- } catch (NoSuchFieldException ignored ) {
121- }
122-
123- if (Objects .isNull (field )) {
124- Messages .showInfoMessage ("Unsupported this version" , Constant .APPLICATION_NAME );
125- return ;
126- }
127-
128- try {
129- field .setAccessible (true );
130-
131- ClassLoader [] parents = (ClassLoader []) field .get (pluginClassLoader );
132- if (Objects .isNull (parents )) {
133- parents = new ClassLoader [] {classLoader };
134- field .set (pluginClassLoader , parents );
135- } else {
136- ClassLoader [] newParents = new ClassLoader [parents .length + 1 ];
137- newParents [parents .length ] = classLoader ;
138- System .arraycopy (parents , 0 , newParents , 0 , parents .length );
139- field .set (pluginClassLoader , newParents );
140- }
141- } catch (IllegalAccessException e ) {
142- e .printStackTrace ();
143- }
111+ URL [] urlArr = urls .toArray (new URL [0 ]);
144112
113+ classLoader = new ProjectLoader (urlArr , MybatisPojoCompile .class .getClassLoader ());
145114 }
146115
147116 private static void changeLoaderUrls (List <String > preDependencies , List <String > dependencies ) {
@@ -156,15 +125,15 @@ private static void changeLoaderUrls(List<String> preDependencies, List<String>
156125 return ;
157126 }
158127
159- for (String path : list ) {
160- try {
161- URL url = new File (FileUtil .toSystemIndependentName (path )).toURI ().toURL ();
162- classLoader .addURL (url );
163- }
164- catch (MalformedURLException e ) {
165- Messages .showErrorDialog (e .getMessage (), "MalformedURLException" );
166- }
128+ try {
129+ classLoader .close ();
130+ } catch (IOException e ) {
131+ throw new RuntimeException (e );
167132 }
133+
134+ List <URL > urls = pathToURL (dependencies );
135+ classLoader = new ProjectLoader (urls .toArray (new URL [0 ]), MybatisPojoCompile .class .getClassLoader ());
136+
168137 }
169138
170139}
0 commit comments