1 /*
2 * Copyright (C) Christian Schulte <cs@schulte.it>, 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 5043 2015-05-27 07:03:39Z 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 5043 2015-05-27 07:03:39Z schulte $
42 * @see #processClassFiles()
43 */
44 public class ClassFileProcessorTask extends JomcToolTask
45 {
46
47 /**
48 * Controls processing of class files.
49 */
50 private boolean classProcessingEnabled = true;
51
52 /**
53 * Class of the {@code ClassFileProcessor} backing the task.
54 */
55 private Class<? extends ClassFileProcessor> classFileProcessorClass;
56
57 /**
58 * Creates a new {@code ClassFileProcessorTask} instance.
59 */
60 public ClassFileProcessorTask()
61 {
62 super();
63 }
64
65 /**
66 * Gets a flag indicating the processing of classes is enabled.
67 *
68 * @return {@code true}, if processing of classes is enabled; {@code false}, else.
69 *
70 * @see #setClassProcessingEnabled(boolean)
71 */
72 public final boolean isClassProcessingEnabled()
73 {
74 return this.classProcessingEnabled;
75 }
76
77 /**
78 * Sets the flag indicating the processing of classes is enabled.
79 *
80 * @param value {@code true}, to enable processing of classes; {@code false}, to disable processing of classes.
81 *
82 * @see #isClassProcessingEnabled()
83 */
84 public final void setClassProcessingEnabled( final boolean value )
85 {
86 this.classProcessingEnabled = value;
87 }
88
89 /**
90 * Gets the class of the {@code ClassFileProcessor} backing the task.
91 *
92 * @return The class of the {@code ClassFileProcessor} backing the task.
93 *
94 * @see #setClassFileProcessorClass(java.lang.Class)
95 */
96 public final Class<? extends ClassFileProcessor> getClassFileProcessorClass()
97 {
98 if ( this.classFileProcessorClass == null )
99 {
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 }