15
15
*/
16
16
package org .openrewrite .java ;
17
17
18
+ import lombok .AllArgsConstructor ;
18
19
import lombok .EqualsAndHashCode ;
19
20
import org .jspecify .annotations .Nullable ;
20
21
import org .openrewrite .Cursor ;
@@ -172,6 +173,12 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String
172
173
173
174
List <JRightPadded <J .Import >> newImports = layoutStyle .addImport (cu .getPadding ().getImports (), importToAdd , cu .getPackageDeclaration (), classpath );
174
175
176
+ if (member != null && typeReference .isPresent ()) {
177
+ cu = (JavaSourceFile ) new ShortenFullyQualifiedMemberReferences (typeReference .get ()).visit (cu , p );
178
+ } else if (member == null && typeReference .isPresent ()) {
179
+ cu = (JavaSourceFile ) new ShortenFullyQualifiedTypeReferences ((JavaType .FullyQualified ) typeReference .get ()).visit (cu , p );
180
+ }
181
+
175
182
// ImportLayoutStyle::addImport adds always `\n` as newlines. Checking if we need to fix them
176
183
newImports = checkCRLF (cu , newImports );
177
184
@@ -202,7 +209,7 @@ private ImportStatus checkImportsForType(List<J.Import> imports) {
202
209
return ImportStatus .IMPLICITLY_IMPORTED ;
203
210
}
204
211
}
205
- if (ending .equals (member )) {
212
+ if (! "*" . equals ( ending ) && ending .equals (member )) {
206
213
return ImportStatus .IMPORT_AMBIGUITY ;
207
214
}
208
215
} else {
@@ -213,7 +220,7 @@ private ImportStatus checkImportsForType(List<J.Import> imports) {
213
220
return ImportStatus .IMPLICITLY_IMPORTED ;
214
221
}
215
222
}
216
- if (ending .equals (typeName )) {
223
+ if (! "*" . equals ( ending ) && ending .equals (typeName )) {
217
224
return ImportStatus .IMPORT_AMBIGUITY ;
218
225
}
219
226
}
@@ -319,4 +326,91 @@ public J.Identifier visitIdentifier(J.Identifier identifier, AtomicReference<Jav
319
326
return identifier ;
320
327
}
321
328
}
329
+
330
+ @ AllArgsConstructor
331
+ private class ShortenFullyQualifiedTypeReferences extends ShortenFullyQualifiedReference {
332
+ private final JavaType .FullyQualified typeToShorten ;
333
+
334
+ @ Override
335
+ public J visitFieldAccess (J .FieldAccess fieldAccess , P p ) {
336
+ if (fieldAccess .isFullyQualifiedClassReference (typeToShorten .getFullyQualifiedName ())) {
337
+ return fieldAccess .getName ().withPrefix (fieldAccess .getPrefix ());
338
+ }
339
+ return super .visitFieldAccess (fieldAccess , p );
340
+ }
341
+
342
+ @ Override
343
+ public J visitIdentifier (J .Identifier identifier , P p ) {
344
+ if (isFullyQualifiedClassReference (identifier , typeToShorten .getFullyQualifiedName ())) {
345
+ return identifier .withSimpleName (typeToShorten .getClassName ());
346
+ }
347
+ return super .visitIdentifier (identifier , p );
348
+ }
349
+ }
350
+
351
+ @ AllArgsConstructor
352
+ private class ShortenFullyQualifiedMemberReferences extends ShortenFullyQualifiedReference {
353
+ private final JavaType memberToShorten ;
354
+
355
+ @ Override
356
+ public J visitMethodInvocation (J .MethodInvocation methodInvocation , P p ) {
357
+ J .MethodInvocation mi = (J .MethodInvocation ) super .visitMethodInvocation (methodInvocation , p );
358
+ if (!(memberToShorten instanceof JavaType .Method )) {
359
+ return mi ;
360
+ }
361
+ JavaType .Method m = (JavaType .Method ) memberToShorten ;
362
+ JavaType .FullyQualified targetType = m .getDeclaringType ();
363
+
364
+ if (isFullyQualifiedClassReference (mi .getSelect (), targetType .getFullyQualifiedName ()) && mi .getSimpleName ().equals (m .getName ())) {
365
+ return methodInvocation .withSelect (null );
366
+ }
367
+ return mi ;
368
+ }
369
+
370
+ @ Override
371
+ public J visitFieldAccess (J .FieldAccess fieldAccess , P p ) {
372
+ if (!(memberToShorten instanceof JavaType .Variable )) {
373
+ return super .visitFieldAccess (fieldAccess , p );
374
+ }
375
+ JavaType .Variable var = (JavaType .Variable ) memberToShorten ;
376
+ JavaType .FullyQualified targetType = (JavaType .FullyQualified ) var .getOwner ();
377
+ if (targetType != null ) {
378
+ if (fieldAccess .getTarget () instanceof J .FieldAccess ) {
379
+ J .FieldAccess target = (J .FieldAccess ) fieldAccess .getTarget ();
380
+ if (target .isFullyQualifiedClassReference (targetType .getFullyQualifiedName ())) {
381
+ return fieldAccess .getName ().withPrefix (fieldAccess .getPrefix ());
382
+ }
383
+ }
384
+ }
385
+ return super .visitFieldAccess (fieldAccess , p );
386
+ }
387
+ }
388
+
389
+ private abstract class ShortenFullyQualifiedReference extends JavaVisitor <P > {
390
+ @ Override
391
+ public J visitImport (J .Import _import , P p ) {
392
+ return _import ;
393
+ }
394
+
395
+ @ Override
396
+ protected JavadocVisitor <P > getJavadocVisitor () {
397
+ return new JavadocVisitor <P >(new JavaVisitor <>()) {
398
+ @ Override
399
+ public Javadoc visitReference (Javadoc .Reference reference , P p ) {
400
+ return reference ;
401
+ }
402
+ };
403
+ }
404
+
405
+ protected boolean isFullyQualifiedClassReference (@ Nullable Expression expr , String className ) {
406
+ if (expr instanceof J .FieldAccess ) {
407
+ return ((J .FieldAccess ) expr ).isFullyQualifiedClassReference (className );
408
+ }
409
+ if (expr instanceof J .Identifier ) {
410
+ J .Identifier id = (J .Identifier ) expr ;
411
+ return id .getFieldType () == null && id .getSimpleName ().equals (className );
412
+ }
413
+ return false ;
414
+ }
415
+ }
322
416
}
0 commit comments