001/* 002 * Copyright (C) 2005 Christian Schulte <cs@schulte.it> 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions 007 * are met: 008 * 009 * o Redistributions of source code must retain the above copyright 010 * notice, this list of conditions and the following disclaimer. 011 * 012 * o Redistributions in binary form must reproduce the above copyright 013 * notice, this list of conditions and the following disclaimer in 014 * the documentation and/or other materials provided with the 015 * distribution. 016 * 017 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 018 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 019 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 020 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, 021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 027 * 028 * $JOMC: ClassFileProcessorTask.java 5135 2016-04-08 13:53:07Z schulte $ 029 * 030 */ 031package org.jomc.ant; 032 033import org.apache.tools.ant.BuildException; 034import org.apache.tools.ant.Project; 035import org.jomc.tools.ClassFileProcessor; 036 037/** 038 * Base class for executing class file processor based tasks. 039 * 040 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 041 * @version $JOMC: ClassFileProcessorTask.java 5135 2016-04-08 13:53:07Z schulte $ 042 * @see #processClassFiles() 043 */ 044public class ClassFileProcessorTask extends JomcToolTask 045{ 046 047 /** 048 * Controls processing of class files. 049 */ 050 private boolean classProcessingEnabled = true; 051 052 /** 053 * Class of the {@code ClassFileProcessor} backing the task. 054 */ 055 private Class<? extends ClassFileProcessor> classFileProcessorClass; 056 057 /** 058 * Creates a new {@code ClassFileProcessorTask} instance. 059 */ 060 public ClassFileProcessorTask() 061 { 062 super(); 063 } 064 065 /** 066 * Gets a flag indicating the processing of classes is enabled. 067 * 068 * @return {@code true}, if processing of classes is enabled; {@code false}, else. 069 * 070 * @see #setClassProcessingEnabled(boolean) 071 */ 072 public final boolean isClassProcessingEnabled() 073 { 074 return this.classProcessingEnabled; 075 } 076 077 /** 078 * Sets the flag indicating the processing of classes is enabled. 079 * 080 * @param value {@code true}, to enable processing of classes; {@code false}, to disable processing of classes. 081 * 082 * @see #isClassProcessingEnabled() 083 */ 084 public final void setClassProcessingEnabled( final boolean value ) 085 { 086 this.classProcessingEnabled = value; 087 } 088 089 /** 090 * Gets the class of the {@code ClassFileProcessor} backing the task. 091 * 092 * @return The class of the {@code ClassFileProcessor} backing the task. 093 * 094 * @see #setClassFileProcessorClass(java.lang.Class) 095 */ 096 public final Class<? extends ClassFileProcessor> getClassFileProcessorClass() 097 { 098 if ( this.classFileProcessorClass == null ) 099 { 100 this.classFileProcessorClass = ClassFileProcessor.class; 101 } 102 103 return this.classFileProcessorClass; 104 } 105 106 /** 107 * Sets the class of the {@code ClassFileProcessor} backing the task. 108 * 109 * @param value The new class of the {@code ClassFileProcessor} backing the task or {@code null}. 110 * 111 * @see #getClassFileProcessorClass() 112 */ 113 public final void setClassFileProcessorClass( final Class<? extends ClassFileProcessor> value ) 114 { 115 this.classFileProcessorClass = value; 116 } 117 118 /** 119 * Creates a new {@code ClassFileProcessor} instance setup using the properties of the instance. 120 * 121 * @return A new {@code ClassFileProcessor} instance. 122 * 123 * @throws BuildException if creating a new {@code ClassFileProcessor} instance fails. 124 * 125 * @see #getClassFileProcessorClass() 126 * @see #configureClassFileProcessor(org.jomc.tools.ClassFileProcessor) 127 */ 128 public ClassFileProcessor newClassFileProcessor() throws BuildException 129 { 130 try 131 { 132 final ClassFileProcessor classFileProcessor = this.getClassFileProcessorClass().newInstance(); 133 this.configureClassFileProcessor( classFileProcessor ); 134 return classFileProcessor; 135 } 136 catch ( final InstantiationException e ) 137 { 138 throw new BuildException( Messages.getMessage( "failedCreatingObject", 139 this.getClassFileProcessorClass().getName() ), 140 e, this.getLocation() ); 141 142 } 143 catch ( final IllegalAccessException e ) 144 { 145 throw new BuildException( Messages.getMessage( "failedCreatingObject", 146 this.getClassFileProcessorClass().getName() ), 147 e, this.getLocation() ); 148 149 } 150 } 151 152 /** 153 * Configures a given {@code ClassFileProcessor} instance using the properties of the instance. 154 * 155 * @param classFileProcessor The class file processor to configure. 156 * 157 * @throws NullPointerException if {@code classFileProcessor} is {@code null}. 158 * @throws BuildException if configuring {@code classFileProcessor} fails. 159 * 160 * @see #configureJomcTool(org.jomc.tools.JomcTool) 161 */ 162 public void configureClassFileProcessor( final ClassFileProcessor classFileProcessor ) throws BuildException 163 { 164 if ( classFileProcessor == null ) 165 { 166 throw new NullPointerException( "classFileProcessor" ); 167 } 168 169 this.configureJomcTool( classFileProcessor ); 170 } 171 172 /** 173 * Calls the {@code processClassFiles} method if class processing is enabled. 174 * 175 * @throws BuildException if processing class files fails. 176 * 177 * @see #processClassFiles() 178 */ 179 @Override 180 public final void executeTask() throws BuildException 181 { 182 if ( this.isClassProcessingEnabled() ) 183 { 184 this.processClassFiles(); 185 this.log( Messages.getMessage( "classProcessingSuccess" ) ); 186 } 187 else 188 { 189 this.log( Messages.getMessage( "classProcessingDisabled" ) ); 190 } 191 } 192 193 /** 194 * Processes class files. 195 * 196 * @throws BuildException if processing class files fails. 197 * 198 * @see #executeTask() 199 */ 200 public void processClassFiles() throws BuildException 201 { 202 this.log( Messages.getMessage( "unimplementedTask", this.getClass().getName(), "processClassFiles" ), 203 Project.MSG_ERR ); 204 205 } 206 207 /** 208 * {@inheritDoc} 209 */ 210 @Override 211 public ClassFileProcessorTask clone() 212 { 213 return (ClassFileProcessorTask) super.clone(); 214 } 215 216}