1 /* 2 * Copyright (C) 2009 Christian Schulte <cs@schulte.it> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * o Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * o Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 19 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $JOMC: AbstractClassFileProcessorCommand.java 5215 2016-04-24 06:54:04Z schulte $ 29 * 30 */ 31 package org.jomc.cli.commands; 32 33 import java.util.logging.Level; 34 import org.apache.commons.cli.CommandLine; 35 import org.jomc.tools.ClassFileProcessor; 36 37 /** 38 * {@code ClassFileProcessor} based command implementation. 39 * 40 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 41 */ 42 public abstract class AbstractClassFileProcessorCommand extends AbstractJomcToolCommand 43 { 44 45 /** 46 * Creates a new {@code AbstractClassFileProcessorCommand} instance. 47 */ 48 public AbstractClassFileProcessorCommand() 49 { 50 super(); 51 } 52 53 @Override 54 public org.apache.commons.cli.Options getOptions() 55 { 56 final org.apache.commons.cli.Options options = super.getOptions(); 57 options.addOption( Options.CLASS_FILE_PROCESSOR_CLASSNAME_OPTION ); 58 options.addOption( Options.NO_CLASS_PROCESSING_OPTION ); 59 return options; 60 } 61 62 /** 63 * Creates a new {@code ClassFileProcessor} instance taking a command line. 64 * 65 * @param commandLine The command line to process. 66 * 67 * @return A new {@code ClassFileProcessor} instance as specified by the given command line. 68 * 69 * @throws NullPointerException if {@code commandLine} is {@code null}. 70 * @throws CommandExecutionException if creating a new instance fails. 71 */ 72 protected ClassFileProcessor createClassFileProcessor( final CommandLine commandLine ) 73 throws CommandExecutionException 74 { 75 if ( commandLine == null ) 76 { 77 throw new NullPointerException( "commandLine" ); 78 } 79 80 final String className = 81 commandLine.hasOption( Options.CLASS_FILE_PROCESSOR_CLASSNAME_OPTION.getOpt() ) 82 ? commandLine.getOptionValue( Options.CLASS_FILE_PROCESSOR_CLASSNAME_OPTION.getOpt() ) 83 : ClassFileProcessor.class.getName(); 84 85 final ClassFileProcessor tool = this.createJomcTool( className, ClassFileProcessor.class, commandLine ); 86 return tool; 87 } 88 89 /** 90 * {@inheritDoc} 91 */ 92 protected final void executeCommand( final CommandLine commandLine ) throws CommandExecutionException 93 { 94 if ( commandLine == null ) 95 { 96 throw new NullPointerException( "commandLine" ); 97 } 98 99 if ( commandLine.hasOption( Options.NO_CLASS_PROCESSING_OPTION.getOpt() ) ) 100 { 101 this.log( Level.INFO, Messages.getMessage( "classProcessingDisabled" ), null ); 102 } 103 else 104 { 105 this.processClassFiles( commandLine ); 106 } 107 } 108 109 /** 110 * Processes class files. 111 * 112 * @param commandLine The command line to execute. 113 * 114 * @throws CommandExecutionException if processing class files fails. 115 */ 116 protected abstract void processClassFiles( final CommandLine commandLine ) throws CommandExecutionException; 117 118 }