/* * [StripRearranger.java] * * Summary: Strip all comments inserted by the IntelliJ Rearranger. * * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2009-04-25 initial version */ package com.mindprod.repair; import com.mindprod.commandline.CommandLine; import com.mindprod.filter.AllButSVNDirectoriesFilter; import com.mindprod.filter.ExtensionListFilter; import com.mindprod.hunkio.HunkIO; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.lang.System.*; /** * Strip all comments inserted by the IntelliJ Rearranger. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2009-04-25 initial version * @since 2009-04-25 */ public class StripRearranger { /** * pattern to recognise comments that the Rearranger inserted */ private static final Pattern REARRANGER_COMMENT_FINDER = Pattern.compile( "//\\s+[\\-]+\\s+(?:" + "|CANONICAL METHODS" + "|CONSTANTS" + "|CONSTRUCTORS" + "|FIELDS" + "|GETTER / SETTER METHODS" + "|INNER CLASSES" + "|Interface \\p{Alpha}+" + "|INTERFACE METHODS" + "|main\\(\\) method" + "|OTHER METHODS" + "|PUBLIC INSTANCE METHODS" + "|PUBLIC STATIC METHODS" + "|STATIC METHODS" + ")\\s+[\\-]+\\r\\n" ); /** * Remove Rearranger comments from Java source code * * @param args list of dirs to process, -s means subsequent dirs are recursed. * * @throws IOException if cannot read source code files. */ public static void main( String[] args ) throws IOException { // get files to process from command line. out.println( "Gathering Java files to strip comments inserted by the Rearranger..." ); CommandLine wantedFiles = new CommandLine( args, new AllButSVNDirectoriesFilter(), new ExtensionListFilter( "java" ) ); for ( File fileBeingProcessed : wantedFiles ) { try { final String big = HunkIO.readEntireFile( fileBeingProcessed ); // do not correct to StringBuilder final StringBuffer modified = new StringBuffer( big.length() ); final Matcher m = REARRANGER_COMMENT_FINDER.matcher( big ); while ( m.find() ) { // remove comment found m.appendReplacement( modified, "\r\n" ); // no need for Matcher.quoteReplacement } m.appendTail( modified ); final String modifiedBig = modified.toString(); if ( !modifiedBig.equals( big ) ) { final File tempFile = HunkIO.createTempFile( "temp_", ".tmp", fileBeingProcessed ); FileWriter emit = new FileWriter( tempFile ); emit.write( modifiedBig ); emit.close(); HunkIO.deleteAndRename( tempFile, fileBeingProcessed ); } } catch ( IOException e ) { e.printStackTrace( err ); err.println(); } } // end for to process each file out.println( "done" ); } }