void () { final String in = "class X {{ Math.abs(-1); }}"; final String what = "Math.abs('_a)"; final String by = "Math.abs($a$)"; options.setToUseStaticImport(true); final String expected = """ import static java.lang.Math.abs; class X {{ abs(-1); }}"""; assertEquals("Replacing with static import", expected, replace(in, what, by, true, true)); final String in2 = "class X { void m(java.util.Random r) { Math.abs(r.nextInt()); }}"; final String expected2 = """ import static java.lang.Math.abs; class X { void m(java.util.Random r) { abs(r.nextInt()); }}"""; assertEquals("don't add broken static imports", expected2, replace(in2, what, by, true, true)); final String by2 = "new java.util.Map.Entry() {}"; final String expected3 = """ import static java.util.Map.Entry; class X {{ new Entry() {}; }}"""; assertEquals("", expected3, replace(in, what, by2, true, true)); final String in3 = """ import java.util.Collections; class X { void m() { System.out.println(Collections.<String>emptyList()); } }"""; final String what3 = "'_q.'_method:[regex( println )]('_a)"; final String by3 = "$q$.$method$($a$)"; final String expected4 = """ import java.util.Collections; import static java.lang.System.out; class X { void m() { out.println(Collections.<String>emptyList()); } }"""; assertEquals("don't break references with type parameters", expected4, replace(in3, what3, by3, true, true)); final String in4 = """ import java.util.Collections; public class X { void some() { System.out.println(1); boolean b = Collections.eq(null, null); } }"""; final String what4 = "System.out.println(1);"; final String by4 = "System.out.println(2);"; final String expected5 = """ import java.util.Collections; import static java.lang.System.out; public class X { void some() { out.println(2); boolean b = Collections.eq(null, null); } }"""; assertEquals("don't add static import to inaccessible members", expected5, replace(in4, what4, by4, true, true)); final String in5 = """ package cz.ahoj.sample.annotations; /** * @author Ales Holy * @since 18. 7. 2017. */ @OuterAnnotation({ @InnerAnnotation(classes = {Integer.class}), @InnerAnnotation(classes = {String.class}), @InnerAnnotation(classes = {ReplacementTest.ReplacementTestConfig.class}) }) public class ReplacementTest { static class ReplacementTestConfig { } } @interface InnerAnnotation { Class<?>[] classes() default {}; } @interface OuterAnnotation { InnerAnnotation[] value(); }"""; final String what5 = "@'_a:[regex( InnerAnnotation )](classes = { String.class })"; final String by5 = "@$a$(classes = { Integer.class })\n" + "@$a$(classes = { String.class })"; assertEquals("add import when reference is just outside the class", """ package cz.ahoj.sample.annotations; import static cz.ahoj.sample.annotations.ReplacementTest.ReplacementTestConfig; /** * @author Ales Holy * @since 18. 7. 2017. */ @OuterAnnotation({ @InnerAnnotation(classes = {Integer.class}), @InnerAnnotation(classes = { Integer.class }), @InnerAnnotation(classes = { String.class }), @InnerAnnotation(classes = {ReplacementTestConfig.class}) }) public class ReplacementTest { static class ReplacementTestConfig { } } @interface InnerAnnotation { Class<?>[] classes() default {}; } @interface OuterAnnotation { InnerAnnotation[] value(); }""", replace(in5, what5, by5, true, true)); final String in6 = """ class X {{ Predicate<String> p = Integer::valueOf; }} interface Predicate<T> { boolean test(T t); }"""; final String what6 = "Integer::valueOf"; final String by6 = "Boolean::valueOf"; assertEquals(""" class X {{ Predicate<String> p = Boolean::valueOf; }} interface Predicate<T> { boolean test(T t); }""", replace(in6, what6, by6, true)); }