Example usage for org.apache.commons.javaflow.utils RewritingUtils rewriteClassFile

List of usage examples for org.apache.commons.javaflow.utils RewritingUtils rewriteClassFile

Introduction

In this page you can find the example usage for org.apache.commons.javaflow.utils RewritingUtils rewriteClassFile.

Prototype

public static void rewriteClassFile(final File pInput, final ResourceTransformer transformer,
            final File pOutput) throws IOException 

Source Link

Usage

From source file:meme.singularsyntax.mojo.JavaflowEnhanceMojo.java

private void enhanceClassFiles(String outputDir, File backupDir, List<String> classFileNames)
        throws MojoExecutionException {

    Log log = getLog();/*from   w w  w . ja v  a  2 s  .  c  o  m*/
    ResourceTransformer transformer = new AsmClassTransformer();

    for (String classFileName : classFileNames) {
        try {
            File source = new File(outputDir, classFileName);
            File destination = new File(String.format(CLASSFILE_REWRITE_TEMPLATE, source.getAbsolutePath()));
            File backupClassFile = new File(backupDir, classFileName);

            if (backupClassFile.exists() && (source.lastModified() <= backupClassFile.lastModified())) {
                log.info(source + " is up to date");
                continue;
            }

            log.info(String.format("Enhancing class file bytecode for Javaflow: %s", source));
            RewritingUtils.rewriteClassFile(source, transformer, destination);

            if (backupClassFile.exists()) {
                log.debug(String.format("Backup for original class file %s already exists - removing it",
                        backupClassFile));
                backupClassFile.delete();
            }

            log.debug(String.format("Renaming original class file from %s to %s", source, backupClassFile));
            FileUtils.moveFile(source, backupClassFile);

            log.debug(String.format("Renaming rewritten class file from %s to %s", destination, source));
            FileUtils.moveFile(destination, source);

            backupClassFile.setLastModified(source.lastModified());

        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage());
        }
    }
}