From d78652bda7ce92a3db05311c4460ecdaf17b40c2 Mon Sep 17 00:00:00 2001 From: "Matteo Franci a.k.a. Fugerit" Date: Wed, 13 Sep 2023 11:28:54 +0200 Subject: [PATCH] fix to custom code substitution #13 --- .../base/gen/util/ExtractCustomCode.java | 58 +++++++++++++++++++ .../daogen/util/TestExtractCustomCode.java | 33 ++++++++++- .../base/tool/handler/CompareHandler.java | 4 +- 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/util/ExtractCustomCode.java b/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/util/ExtractCustomCode.java index 15aa5ca8..0c57be2e 100644 --- a/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/util/ExtractCustomCode.java +++ b/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/util/ExtractCustomCode.java @@ -5,10 +5,14 @@ import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; +import java.util.List; +import java.util.function.Function; +import java.util.stream.Collectors; import org.fugerit.java.core.function.SafeFunction; import org.fugerit.java.core.function.SimpleValue; import org.fugerit.java.core.io.FileIO; +import org.fugerit.java.core.lang.helpers.StringUtils; public class ExtractCustomCode { @@ -59,4 +63,58 @@ public static String addCustomContent( CharSequence text, String startTag, Strin } ); } + private static String addWithCondition( CharSequence text, String customContent, Function condition ) { + return SafeFunction.get( () -> { + try ( BufferedReader reader = new BufferedReader( new StringReader( text.toString() ) ); + StringWriter buffer = new StringWriter(); + PrintWriter writer = new PrintWriter( buffer, true ) ) { + List lines = reader.lines().collect( Collectors.toList() ); + LineCursor cursor = new LineCursor(); + cursor.lines = lines; + for ( cursor.index = 0; cursor.index { + boolean ok = false; + String previousLine = c.getPreviousLine(); + if ( StringUtils.isNotEmpty( previousLine ) ) { + ok = previousLine.trim().startsWith( "package" ); + } + return ok; + } ); + } + + public static String addBeforeClassEnd( CharSequence text, String customContent ) { + return addWithCondition(text, customContent, c -> c.isLast() && c.getCurrentLine().trim().equals( "}" ) ); + } + +} + +class LineCursor { + + public List lines; + + public int index; + + public boolean isLast() { + return this.lines.size()-1 == this.index; + } + + public String getPreviousLine() { + return this.index==0 ? null : this.lines.get( this.index-1 ); + } + + public String getCurrentLine() { + return this.lines.get( this.index ); + } } diff --git a/fj-daogen-base/src/test/java/test/org/fugerit/java/daogen/util/TestExtractCustomCode.java b/fj-daogen-base/src/test/java/test/org/fugerit/java/daogen/util/TestExtractCustomCode.java index db4533c0..8625336c 100644 --- a/fj-daogen-base/src/test/java/test/org/fugerit/java/daogen/util/TestExtractCustomCode.java +++ b/fj-daogen-base/src/test/java/test/org/fugerit/java/daogen/util/TestExtractCustomCode.java @@ -26,12 +26,43 @@ public void test001() { String result = ExtractCustomCode.extractCustom( content , SimpleJavaGenerator.CUSTOM_CODE_START, SimpleJavaGenerator.CUSTOM_CODE_END ); Assert.assertNotNull( result ); log.info( "result : {}", result ); + // try to correct log.info( "try correct : {}", fileReal.getCanonicalPath() ); String realContent = FileIO.readString( fileReal ); + // test real mode 1 String resultReal = ExtractCustomCode.addCustomContent( realContent , SimpleJavaGenerator.CUSTOM_CODE_START, SimpleJavaGenerator.CUSTOM_CODE_END, result ); Assert.assertNotNull( resultReal ); + // test real mode 2 + log.info( "try correct alt : {}", fileReal.getCanonicalPath() ); + String resultRealAlt = ExtractCustomCode.addBeforeClassEnd( realContent , result ); + log.info( "real result alt {}", resultRealAlt ); + Assert.assertNotNull( resultRealAlt ); + } ); + } + + @Test + public void test002() { + SafeFunction.apply( () -> { + String path = "../fj-daogen-tool/src/test/resources/compare_handler_test/daogen_original/org/fugerit/java/daogen/sample/impl/facade/data/DataEntityAddressFacadeHelper.java"; + String pathReal = "../fj-daogen-tool/src/test/resources/compare_handler_test/daogen_original/org/fugerit/java/daogen/sample/impl/facade/data/DataEntityAddressFacade.java"; + File file = new File( path ); + File fileReal = new File( pathReal ); + log.info( "try source : {}", file.getCanonicalPath() ); + String result = ExtractCustomCode.extractCustom( file , SimpleJavaGenerator.CUSTOM_IMPORT_START, SimpleJavaGenerator.CUSTOM_IMPORT_END ); + Assert.assertNotNull( result ); + log.info( "result : {}", result ); + // try to correct + log.info( "try correct : {}", fileReal.getCanonicalPath() ); + String realContent = FileIO.readString( fileReal ); + // test real mode 1 + String resultReal = ExtractCustomCode.addCustomContent( realContent , SimpleJavaGenerator.CUSTOM_IMPORT_START, SimpleJavaGenerator.CUSTOM_IMPORT_END, result ); + Assert.assertNotNull( resultReal ); + // test real mode 2 + log.info( "try correct alt : {}", fileReal.getCanonicalPath() ); + String resultRealAlt = ExtractCustomCode.addAfterPackageClassEnd( realContent , result ); + log.info( "real result alt {}", resultRealAlt ); + Assert.assertNotNull( resultRealAlt ); } ); - } } diff --git a/fj-daogen-tool/src/main/java/org/fugerit/java/daogen/base/tool/handler/CompareHandler.java b/fj-daogen-tool/src/main/java/org/fugerit/java/daogen/base/tool/handler/CompareHandler.java index 902b79f7..9c1ba651 100644 --- a/fj-daogen-tool/src/main/java/org/fugerit/java/daogen/base/tool/handler/CompareHandler.java +++ b/fj-daogen-tool/src/main/java/org/fugerit/java/daogen/base/tool/handler/CompareHandler.java @@ -74,10 +74,10 @@ private void handleRealFileCorrect( File file2, Properties params, PrintWriter r if ( StringUtils.isNotEmpty( contentCode ) || StringUtils.isNotEmpty( contentImport ) ) { String realFileContent = FileIO.readString( realFile ); if ( StringUtils.isNotEmpty( contentCode ) ) { - realFileContent = ExtractCustomCode.addCustomContent( realFileContent , customCodeStart, customCodeEnd, contentCode ); + realFileContent = ExtractCustomCode.addBeforeClassEnd( realFileContent, contentCode ); } if ( StringUtils.isNotEmpty( contentImport ) ) { - realFileContent = ExtractCustomCode.addCustomContent( realFileContent , customImportStart, customImportEnd, contentImport ); + realFileContent = ExtractCustomCode.addAfterPackageClassEnd( realFileContent, contentImport ); } FileIO.writeString( realFileContent , realFile ); report.print( " real file customized! "+realFileContent );