88import java .io .IOException ;
99import java .io .InputStreamReader ;
1010import java .io .PrintStream ;
11+ import java .nio .charset .Charset ;
1112import java .nio .file .Files ;
1213import java .nio .file .Paths ;
1314import java .util .ArrayDeque ;
1718import java .util .List ;
1819import java .util .Map ;
1920import java .util .Queue ;
21+ import java .util .Scanner ;
2022import java .util .Set ;
2123import java .util .jar .JarInputStream ;
2224import java .util .zip .ZipEntry ;
@@ -33,6 +35,15 @@ public class Generator {
3335
3436 private static final String DEFAULT_PACKAGE_NAME = "com.tns.gen" ;
3537
38+ private static final String LINE_SEPARATOR = System .getProperty ("line.separator" );
39+
40+ private static final String AUTO_GENERATED_FILE_PROLOGUE =
41+ "/* AUTO-GENERATED FILE. DO NOT MODIFY." + LINE_SEPARATOR +
42+ " * This class was automatically generated by the" + LINE_SEPARATOR +
43+ " * static binding generator from the resources it found." + LINE_SEPARATOR +
44+ " * Please do not modify by hand." + LINE_SEPARATOR +
45+ " */" + LINE_SEPARATOR ;
46+
3647 private final File outputDir ;
3748 private final List <DataRow > libs ;
3849 private final Map <String , JavaClass > classes ;
@@ -48,11 +59,13 @@ public Generator(File outputDir, List<DataRow> libs, boolean throwOnError) throw
4859 }
4960
5061 public void writeBindings (String filename ) throws IOException , ClassNotFoundException {
62+ this .cleanPreviouslyAutoGeneratedFiles (this .outputDir );
5163 Binding [] bindings = generateBindings (filename );
5264 Set <File > writtenFiles = new HashSet <File >();
5365 for (Binding b : bindings ) {
5466 if (writtenFiles .add (b .getFile ())) {
5567 try (PrintStream ps = new PrintStream (b .getFile ())) {
68+ ps .append (AUTO_GENERATED_FILE_PROLOGUE );
5669 ps .append (b .getContent ());
5770 }
5871 // A file with that name has already been written
@@ -747,4 +760,55 @@ public List<Method> getList() {
747760 return this .methods ;
748761 }
749762 }
763+
764+ private void cleanPreviouslyAutoGeneratedFiles (File folder ) {
765+ // Recursively traverse all files in the output folder and
766+ // remove them if they were auto-generated from a previous run
767+ // of the static binding generator. This will ensure that if
768+ // some javascript files containing native extends are removed,
769+ // their corresponding java classes will also be removed.
770+ File [] files = folder .listFiles ();
771+ for (File file : files ) {
772+ if (file .isDirectory ()) {
773+ this .cleanPreviouslyAutoGeneratedFiles (file );
774+ } else if ("java" .equalsIgnoreCase (this .getFileExtension (file .toString ()))) {
775+ String fileContents = this .readFile (file );
776+ if (fileContents .startsWith (AUTO_GENERATED_FILE_PROLOGUE )) {
777+ file .delete ();
778+ }
779+ }
780+ }
781+ }
782+
783+ private String getFileExtension (String filename ) {
784+ String extension = "" ;
785+
786+ int i = filename .lastIndexOf ('.' );
787+ if (i > 0 ) {
788+ extension = filename .substring (i + 1 );
789+ }
790+
791+ return extension ;
792+ }
793+
794+ private String readFile (File file ) {
795+ Scanner scanner = null ;
796+ try {
797+ StringBuilder fileContents = new StringBuilder ((int ) file .length ());
798+ scanner = new Scanner (file );
799+
800+ while (scanner .hasNextLine ()) {
801+ fileContents .append (scanner .nextLine () + LINE_SEPARATOR );
802+ }
803+
804+ return fileContents .toString ();
805+ } catch (IOException e ) {
806+ e .printStackTrace ();
807+ return "" ;
808+ } finally {
809+ if (scanner != null ) {
810+ scanner .close ();
811+ }
812+ }
813+ }
750814}
0 commit comments