| 1 | /* |
| 2 | * Copyright (C) Christian Schulte, 2005-206 |
| 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: ClassFileProcessorTask.java 4613 2012-09-22 10:07:08Z schulte $ |
| 29 | * |
| 30 | */ |
| 31 | package org.jomc.ant; |
| 32 | |
| 33 | import org.apache.tools.ant.BuildException; |
| 34 | import org.apache.tools.ant.Project; |
| 35 | import org.jomc.tools.ClassFileProcessor; |
| 36 | |
| 37 | /** |
| 38 | * Base class for executing class file processor based tasks. |
| 39 | * |
| 40 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
| 41 | * @version $JOMC: ClassFileProcessorTask.java 4613 2012-09-22 10:07:08Z schulte $ |
| 42 | * @see #processClassFiles() |
| 43 | */ |
| 44 | public class ClassFileProcessorTask extends JomcToolTask |
| 45 | { |
| 46 | |
| 47 | /** Controls processing of class files. */ |
| 48 | private boolean classProcessingEnabled = true; |
| 49 | |
| 50 | /** Class of the {@code ClassFileProcessor} backing the task. */ |
| 51 | private Class<? extends ClassFileProcessor> classFileProcessorClass; |
| 52 | |
| 53 | /** Creates a new {@code ClassFileProcessorTask} instance. */ |
| 54 | public ClassFileProcessorTask() |
| 55 | { |
| 56 | super(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Gets a flag indicating the processing of classes is enabled. |
| 61 | * |
| 62 | * @return {@code true}, if processing of classes is enabled; {@code false}, else. |
| 63 | * |
| 64 | * @see #setClassProcessingEnabled(boolean) |
| 65 | */ |
| 66 | public final boolean isClassProcessingEnabled() |
| 67 | { |
| 68 | return this.classProcessingEnabled; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Sets the flag indicating the processing of classes is enabled. |
| 73 | * |
| 74 | * @param value {@code true}, to enable processing of classes; {@code false}, to disable processing of classes. |
| 75 | * |
| 76 | * @see #isClassProcessingEnabled() |
| 77 | */ |
| 78 | public final void setClassProcessingEnabled( final boolean value ) |
| 79 | { |
| 80 | this.classProcessingEnabled = value; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Gets the class of the {@code ClassFileProcessor} backing the task. |
| 85 | * |
| 86 | * @return The class of the {@code ClassFileProcessor} backing the task. |
| 87 | * |
| 88 | * @see #setClassFileProcessorClass(java.lang.Class) |
| 89 | */ |
| 90 | public final Class<? extends ClassFileProcessor> getClassFileProcessorClass() |
| 91 | { |
| 92 | if ( this.classFileProcessorClass == null ) |
| 93 | { |
| 94 | this.classFileProcessorClass = ClassFileProcessor.class; |
| 95 | } |
| 96 | |
| 97 | return this.classFileProcessorClass; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Sets the class of the {@code ClassFileProcessor} backing the task. |
| 102 | * |
| 103 | * @param value The new class of the {@code ClassFileProcessor} backing the task or {@code null}. |
| 104 | * |
| 105 | * @see #getClassFileProcessorClass() |
| 106 | */ |
| 107 | public final void setClassFileProcessorClass( final Class<? extends ClassFileProcessor> value ) |
| 108 | { |
| 109 | this.classFileProcessorClass = value; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Creates a new {@code ClassFileProcessor} instance setup using the properties of the instance. |
| 114 | * |
| 115 | * @return A new {@code ClassFileProcessor} instance. |
| 116 | * |
| 117 | * @throws BuildException if creating a new {@code ClassFileProcessor} instance fails. |
| 118 | * |
| 119 | * @see #getClassFileProcessorClass() |
| 120 | * @see #configureClassFileProcessor(org.jomc.tools.ClassFileProcessor) |
| 121 | */ |
| 122 | public ClassFileProcessor newClassFileProcessor() throws BuildException |
| 123 | { |
| 124 | try |
| 125 | { |
| 126 | final ClassFileProcessor classFileProcessor = this.getClassFileProcessorClass().newInstance(); |
| 127 | this.configureClassFileProcessor( classFileProcessor ); |
| 128 | return classFileProcessor; |
| 129 | } |
| 130 | catch ( final InstantiationException e ) |
| 131 | { |
| 132 | throw new BuildException( Messages.getMessage( "failedCreatingObject", |
| 133 | this.getClassFileProcessorClass().getName() ), |
| 134 | e, this.getLocation() ); |
| 135 | |
| 136 | } |
| 137 | catch ( final IllegalAccessException e ) |
| 138 | { |
| 139 | throw new BuildException( Messages.getMessage( "failedCreatingObject", |
| 140 | this.getClassFileProcessorClass().getName() ), |
| 141 | e, this.getLocation() ); |
| 142 | |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Configures a given {@code ClassFileProcessor} instance using the properties of the instance. |
| 148 | * |
| 149 | * @param classFileProcessor The class file processor to configure. |
| 150 | * |
| 151 | * @throws NullPointerException if {@code classFileProcessor} is {@code null}. |
| 152 | * @throws BuildException if configuring {@code classFileProcessor} fails. |
| 153 | * |
| 154 | * @see #configureJomcTool(org.jomc.tools.JomcTool) |
| 155 | */ |
| 156 | public void configureClassFileProcessor( final ClassFileProcessor classFileProcessor ) throws BuildException |
| 157 | { |
| 158 | if ( classFileProcessor == null ) |
| 159 | { |
| 160 | throw new NullPointerException( "classFileProcessor" ); |
| 161 | } |
| 162 | |
| 163 | this.configureJomcTool( classFileProcessor ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Calls the {@code processClassFiles} method if class processing is enabled. |
| 168 | * |
| 169 | * @throws BuildException if processing class files fails. |
| 170 | * |
| 171 | * @see #processClassFiles() |
| 172 | */ |
| 173 | @Override |
| 174 | public final void executeTask() throws BuildException |
| 175 | { |
| 176 | if ( this.isClassProcessingEnabled() ) |
| 177 | { |
| 178 | this.processClassFiles(); |
| 179 | this.log( Messages.getMessage( "classProcessingSuccess" ) ); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | this.log( Messages.getMessage( "classProcessingDisabled" ) ); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Processes class files. |
| 189 | * |
| 190 | * @throws BuildException if processing class files fails. |
| 191 | * |
| 192 | * @see #executeTask() |
| 193 | */ |
| 194 | public void processClassFiles() throws BuildException |
| 195 | { |
| 196 | this.log( Messages.getMessage( "unimplementedTask", this.getClass().getName(), "processClassFiles" ), |
| 197 | Project.MSG_ERR ); |
| 198 | |
| 199 | } |
| 200 | |
| 201 | /** {@inheritDoc} */ |
| 202 | @Override |
| 203 | public ClassFileProcessorTask clone() |
| 204 | { |
| 205 | return (ClassFileProcessorTask) super.clone(); |
| 206 | } |
| 207 | |
| 208 | } |