void () { options.setToReformatAccordingToStyle(true); String s1 = "class A { public void b() {} }"; String s2 = "class 'a { '_Other* }"; String s3 = "class $a$New { Logger LOG; $Other$ }"; String expectedResult = """ class ANew { Logger LOG; public void b() { } }"""; assertEquals("Basic class replacement", expectedResult, replace(s1, s2, s3, true)); String s4 = "class A { class C {} public void b() {} int f; }"; String s5 = "class 'a { '_Other* }"; String s6 = "class $a$ { Logger LOG; $Other$ }"; String expectedResult2 = """ class A { Logger LOG; class C { } public void b() { } int f; }"""; assertEquals("Order of members in class replacement", expectedResult2, replace(s4, s5, s6, true)); String s7 = "class A extends B { int c; void b() {} { a = 1; } }"; String s8 = "class 'A extends B { '_Other* }"; String s9 = "class $A$ extends B2 { $Other$ }"; String expectedResult3 = """ class A extends B2 { int c; void b() { } { a = 1; } }"""; assertEquals("Unsupported pattern exception", expectedResult3, replace(s7, s8, s9, true)); String s10 = """ /** @example */ class A { class C {} public void b() {} int f; }"""; String s11 = "class 'a { '_Other* }"; String s12 = """ public class $a$ { $Other$ }"""; String expectedResult4 = """ /** * @example */ public class A { class C { } public void b() { } int f; }"""; options.setToReformatAccordingToStyle(true); assertEquals("Make class public", expectedResult4, replace(s10, s11, s12, true)); options.setToReformatAccordingToStyle(false); String s13 = """ class CustomThread extends Thread { public CustomThread(InputStream in, OutputStream out, boolean closeOutOnExit) { super(CustomThreadGroup.getThreadGroup(), "CustomThread"); setDaemon(true); if (in instanceof BufferedInputStream) { bis = (BufferedInputStream)in; } else { bis = new BufferedInputStream(in); } this.out = out; this.closeOutOnExit = closeOutOnExit; } }"""; String s14 = """ class '_Class extends Thread { '_Class('_ParameterType '_ParameterName*) { \t super (CustomThreadGroup.getThreadGroup(), '_superarg* ); '_Statement*; } }"""; String s15 = """ class $Class$ extends CustomThread { $Class$($ParameterType$ $ParameterName$) { \t super($superarg$); $Statement$; } }"""; String expectedResult5 = """ class CustomThread extends CustomThread { CustomThread(InputStream in, OutputStream out, boolean closeOutOnExit) { super("CustomThread"); setDaemon(true); if (in instanceof BufferedInputStream) { bis = (BufferedInputStream) in; } else { bis = new BufferedInputStream(in); } this.out = out; this.closeOutOnExit = closeOutOnExit; } }"""; options.setToReformatAccordingToStyle(true); assertEquals("Constructor replacement", expectedResult5, replace(s13, s14, s15, true)); options.setToReformatAccordingToStyle(false); String s16 = "public class A {}\n" + "final class B {}"; String s17 = "class 'A { '_Other* }"; String s17_2 = "class 'A { private Log log = LogFactory.createLog(); '_Other* }"; String s18 = "class $A$ { private Log log = LogFactory.createLog(); $Other$ }"; String s18_2 = "class $A$ { $Other$ }"; String expectedResult6 = "public class A { private Log log = LogFactory.createLog(); }\n" + "final class B { private Log log = LogFactory.createLog(); }"; assertEquals("Modifier list for class", expectedResult6, replace(s16, s17, s18)); String expectedResult7 = "public class A { }\n" + "final class B { }"; assertEquals("Removing field", expectedResult7, replace(expectedResult6, s17_2, s18_2)); String s19 = "public class A extends Object implements Cloneable {}\n"; String s20 = "class 'A { '_Other* }"; String s21 = "class $A$ { private Log log = LogFactory.createLog(); $Other$ }"; String expectedResult8 = "public class A extends Object implements Cloneable { private Log log = LogFactory.createLog(); }\n"; assertEquals("Extends / implements list for class", expectedResult8, replace(s19, s20, s21, true)); String s22 = "public class A<T> { int Afield; }\n"; String s23 = "class 'A { '_Other* }"; String s24 = "class $A$ { private Log log = LogFactory.createLog(); $Other$ }"; String expectedResult9 = "public class A<T> { private Log log = LogFactory.createLog(); int Afield; }\n"; assertEquals("Type parameters for the class", expectedResult9, replace(s22, s23, s24)); String s25 = """ class A { // comment before protected short a; // comment after }"""; String s26 = "short a;"; String s27 = "Object a;"; String expectedResult10 = """ class A { // comment before protected Object a; // comment after }"""; assertEquals("Replacing dcl with saving access modifiers", expectedResult10, replace(s25, s26, s27)); String s28 = "aaa"; String s29 = """ class 'Class { 'Class('_ParameterType '_ParameterName) { 'Class('_ParameterName); } }"""; String s30 = """ class $Class$ { $Class$($ParameterType$ $ParameterName$) { this($ParameterName$); } }"""; String expectedResult11 = "aaa"; assertEquals("Complex class replacement", expectedResult11, replace(s28, s29, s30)); String s31 = """ class A { int a; // comment char b; int c; // comment2 }"""; String s32 = "'_Type 'Variable = '_Value?; //'_Comment"; String s33 = "/**$Comment$*/\n" + "$Type$ $Variable$ = $Value$;"; String expectedResult12 = """ class A { /** * comment */ int a; char b; /** * comment2 */ int c; }"""; options.setToReformatAccordingToStyle(true); assertEquals("Replacing comments with javadoc for fields", expectedResult12, replace(s31, s32, s33, true)); options.setToReformatAccordingToStyle(false); String s34 = """ /** * This interface stores XXX * <p/> */ public interface X { public static final String HEADER = Headers.HEADER; }"""; String s35 = """ public interface '_MessageInterface { public static final String '_X = '_VALUE; '_blah*}"""; String s36 = """ public interface $MessageInterface$ { public static final String HEADER = $VALUE$; $blah$ }"""; String expectedResult13 = """ /** * This interface stores XXX * <p/> */ public interface X { public static final String HEADER = Headers.HEADER; \s }"""; assertEquals("Replacing interface with interface, saving comments properly", expectedResult13, replace(s34, s35, s36, true)); }