11package io .github .linyimin .plugin .service ;
22
33import com .alibaba .fastjson .JSON ;
4- import com .google .common .collect .ConcurrentHashMultiset ;
54import com .google .common .collect .Lists ;
65import com .intellij .openapi .project .Project ;
76import com .intellij .openapi .ui .Messages ;
87import com .intellij .psi .*;
98import com .intellij .psi .impl .source .PsiClassReferenceType ;
109import com .intellij .psi .util .PsiUtil ;
10+ import com .intellij .psi .xml .XmlElement ;
1111import com .intellij .psi .xml .XmlTag ;
1212import com .intellij .psi .xml .XmlToken ;
1313import com .siyeh .ig .psiutils .CollectionUtils ;
1616import io .github .linyimin .plugin .dom .model .MybatisConfiguration ;
1717import io .github .linyimin .plugin .provider .MapperXmlProcessor ;
1818import io .github .linyimin .plugin .service .model .MybatisSqlConfiguration ;
19+ import io .github .linyimin .plugin .utils .JavaUtils ;
1920import io .github .linyimin .plugin .utils .MapperDomUtils ;
2021import io .github .linyimin .plugin .utils .MybatisSqlUtils ;
2122import org .apache .commons .collections .MapUtils ;
@@ -34,7 +35,42 @@ public void generate(PsiElement psiElement) {
3435
3536 public String generateSql (Project project , String methodQualifiedName , String params ) {
3637
37- MybatisConfiguration mybatisConfiguration = MapperDomUtils .findConfiguration (project );
38+ List <PsiMethod > psiMethods = JavaUtils .findMethod (project , methodQualifiedName );
39+
40+ if (org .apache .commons .collections .CollectionUtils .isEmpty (psiMethods )) {
41+ Messages .showInfoMessage (String .format ("method %s is not exist." , methodQualifiedName ), Constant .APPLICATION_NAME );
42+ return StringUtils .EMPTY ;
43+ }
44+
45+ String mybatisConfig = getMybatisConfiguration (project , psiMethods .get (0 ));
46+
47+ boolean isNeedCompile = checkNeedCompile (mybatisConfig , methodQualifiedName , params );
48+
49+ if (isNeedCompile ) {
50+ MybatisPojoCompile .compile (project );
51+ }
52+
53+ return MybatisSqlUtils .getSql (mybatisConfig , methodQualifiedName , params );
54+
55+ }
56+
57+ private boolean checkNeedCompile (String mybatisConfig , String methodQualifiedName , String params ) {
58+ if (Objects .isNull (MybatisPojoCompile .classLoader )) {
59+ return true ;
60+ }
61+
62+ try {
63+ MybatisSqlUtils .getSql (mybatisConfig , methodQualifiedName , params );
64+ } catch (Exception e ) {
65+ return true ;
66+ }
67+
68+ return false ;
69+ }
70+
71+ private String getMybatisConfiguration (Project project , PsiMethod psiMethod ) {
72+
73+ MybatisConfiguration mybatisConfiguration = MapperDomUtils .findConfiguration (project , psiMethod );
3874 if (Objects .isNull (mybatisConfiguration )) {
3975 Messages .showInfoMessage ("Mybatis配置文件不存在" , Constant .APPLICATION_NAME );
4076 return StringUtils .EMPTY ;
@@ -47,10 +83,18 @@ public String generateSql(Project project, String methodQualifiedName, String pa
4783
4884 String mybatisConfig = mybatisConfiguration .getParent ().getXmlElement ().getText ();
4985
50- MybatisPojoCompile .compile (project );
86+ // 不处理plugins
87+ XmlElement xmlElement = mybatisConfiguration .getXmlElement ();
5188
52- return MybatisSqlUtils .getSql (mybatisConfig , methodQualifiedName , params );
89+ if (Objects .nonNull (xmlElement ) && xmlElement instanceof XmlTag ) {
90+ XmlTag [] tags = ((XmlTag ) xmlElement ).findSubTags ("plugins" );
91+ if (tags .length > 0 ) {
92+ String plugins = tags [0 ].getText ();
93+ mybatisConfig = mybatisConfig .replace (plugins , StringUtils .EMPTY );
94+ }
95+ }
5396
97+ return mybatisConfig ;
5498 }
5599
56100
@@ -112,10 +156,25 @@ private String parseParamNameTypeList(List<ParamNameType> paramNameTypes) {
112156
113157 Map <String , Object > param = new HashMap <>();
114158
115- if (isNormalType (psiClass .getQualifiedName ())) {
116- param .put (name , getPrimitiveDefaultValue (name , psiClass .getQualifiedName ()));
159+ if (Objects .isNull (psiClass ) || isNormalType (psiClass .getQualifiedName ())) {
160+ String paramType ;
161+ if (Objects .isNull (psiClass )) {
162+ if (type instanceof PsiArrayType ) {
163+ paramType = ((PsiArrayType ) type ).getComponentType ().getCanonicalText ();
164+ } else {
165+ paramType = type .getCanonicalText ();
166+ }
167+ } else {
168+ paramType = psiClass .getQualifiedName ();
169+ }
170+ param .put (name , getPrimitiveDefaultValue (name , paramType ));
117171 } else {
118- param = getFieldFromClass (paramNameType .psiClass );
172+ Map <String , Object > classParam = getFieldFromClass (psiClass );
173+ if (paramNameType .isParamAnnotation ) {
174+ param .putAll (classParam );
175+ } else {
176+ param .put (name , classParam );
177+ }
119178 }
120179
121180 // 数组或者列表
@@ -157,9 +216,11 @@ private Map<String, Object> getFieldFromClass(PsiClass psiClass) {
157216 list .add (value );
158217 } else {
159218 PsiClass psiClassInArray = PsiUtil .resolveClassInType (deepType );
160- Map <String , Object > temp = getFieldFromClass (psiClassInArray );
161- if (MapUtils .isNotEmpty (temp )) {
162- list .add (temp );
219+ if (Objects .nonNull (psiClassInArray ) && !StringUtils .equals (psiClassInArray .getQualifiedName (), psiClass .getQualifiedName ())) {
220+ Map <String , Object > temp = getFieldFromClass (psiClassInArray );
221+ if (MapUtils .isNotEmpty (temp )) {
222+ list .add (temp );
223+ }
163224 }
164225 }
165226
@@ -184,9 +245,11 @@ private Map<String, Object> getFieldFromClass(PsiClass psiClass) {
184245 list .add (value );
185246 } else {
186247 PsiClass iterableClass = PsiUtil .resolveClassInClassTypeOnly (iterableType );
187- Map <String , Object > temp = getFieldFromClass (iterableClass );
188- if (MapUtils .isNotEmpty (temp )) {
189- list .add (temp );
248+ if (Objects .nonNull (iterableClass ) && !StringUtils .equals (iterableClass .getQualifiedName (), psiClass .getQualifiedName ())) {
249+ Map <String , Object > temp = getFieldFromClass (iterableClass );
250+ if (MapUtils .isNotEmpty (temp )) {
251+ list .add (temp );
252+ }
190253 }
191254 }
192255
@@ -196,11 +259,17 @@ private Map<String, Object> getFieldFromClass(PsiClass psiClass) {
196259 }
197260
198261 // class
199- Map <String , Object > temp = getFieldFromClass (PsiUtil .resolveClassInType (type ));
200- if (MapUtils .isNotEmpty (temp )) {
201- param .put (fieldName , temp );
262+ PsiClass typePsiClass = PsiUtil .resolveClassInType (type );
263+ if (Objects .nonNull (typePsiClass ) && StringUtils .equals (typePsiClass .getQualifiedName (), psiClass .getQualifiedName ())) {
264+ param .put (fieldName , new HashMap <>());
265+ } else {
266+ Map <String , Object > temp = getFieldFromClass (typePsiClass );
267+ if (MapUtils .isNotEmpty (temp )) {
268+ param .put (fieldName , temp );
269+ }
202270 }
203271
272+
204273 }
205274
206275 return param ;
@@ -235,7 +304,7 @@ public List<ParamNameType> getMethodBodyParamList(PsiMethod psiMethod) {
235304 String paramAnnotationValue = getParamAnnotationValue (param );
236305 String name = StringUtils .isBlank (paramAnnotationValue ) ? param .getName () : paramAnnotationValue ;
237306
238- ParamNameType paramNameType = new ParamNameType (name , psiClass , param .getType ());
307+ ParamNameType paramNameType = new ParamNameType (name , psiClass , param .getType (), StringUtils . isNotEmpty ( paramAnnotationValue ) );
239308 result .add (paramNameType );
240309 }
241310 return result ;
@@ -290,11 +359,13 @@ static class ParamNameType {
290359 private final String name ;
291360 private final PsiClass psiClass ;
292361 private final PsiType psiType ;
362+ private final boolean isParamAnnotation ;
293363
294- public ParamNameType (String name , PsiClass psiClass , PsiType psiType ) {
364+ public ParamNameType (String name , PsiClass psiClass , PsiType psiType , boolean isParamAnnotation ) {
295365 this .name = name ;
296366 this .psiClass = psiClass ;
297367 this .psiType = psiType ;
368+ this .isParamAnnotation = isParamAnnotation ;
298369 }
299370 }
300371
0 commit comments