ClassFileProcessorTask.java

  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. import org.apache.tools.ant.BuildException;
  33. import org.apache.tools.ant.Project;
  34. import org.jomc.tools.ClassFileProcessor;

  35. /**
  36.  * Base class for executing class file processor based tasks.
  37.  *
  38.  * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
  39.  * @version $JOMC: ClassFileProcessorTask.java 5043 2015-05-27 07:03:39Z schulte $
  40.  * @see #processClassFiles()
  41.  */
  42. public class ClassFileProcessorTask extends JomcToolTask
  43. {

  44.     /**
  45.      * Controls processing of class files.
  46.      */
  47.     private boolean classProcessingEnabled = true;

  48.     /**
  49.      * Class of the {@code ClassFileProcessor} backing the task.
  50.      */
  51.     private Class<? extends ClassFileProcessor> classFileProcessorClass;

  52.     /**
  53.      * Creates a new {@code ClassFileProcessorTask} instance.
  54.      */
  55.     public ClassFileProcessorTask()
  56.     {
  57.         super();
  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.      * Sets the flag indicating the processing of classes is enabled.
  72.      *
  73.      * @param value {@code true}, to enable processing of classes; {@code false}, to disable processing of classes.
  74.      *
  75.      * @see #isClassProcessingEnabled()
  76.      */
  77.     public final void setClassProcessingEnabled( final boolean value )
  78.     {
  79.         this.classProcessingEnabled = value;
  80.     }

  81.     /**
  82.      * Gets the class of the {@code ClassFileProcessor} backing the task.
  83.      *
  84.      * @return The class of the {@code ClassFileProcessor} backing the task.
  85.      *
  86.      * @see #setClassFileProcessorClass(java.lang.Class)
  87.      */
  88.     public final Class<? extends ClassFileProcessor> getClassFileProcessorClass()
  89.     {
  90.         if ( this.classFileProcessorClass == null )
  91.         {
  92.             this.classFileProcessorClass = ClassFileProcessor.class;
  93.         }

  94.         return this.classFileProcessorClass;
  95.     }

  96.     /**
  97.      * Sets the class of the {@code ClassFileProcessor} backing the task.
  98.      *
  99.      * @param value The new class of the {@code ClassFileProcessor} backing the task or {@code null}.
  100.      *
  101.      * @see #getClassFileProcessorClass()
  102.      */
  103.     public final void setClassFileProcessorClass( final Class<? extends ClassFileProcessor> value )
  104.     {
  105.         this.classFileProcessorClass = value;
  106.     }

  107.     /**
  108.      * Creates a new {@code ClassFileProcessor} instance setup using the properties of the instance.
  109.      *
  110.      * @return A new {@code ClassFileProcessor} instance.
  111.      *
  112.      * @throws BuildException if creating a new {@code ClassFileProcessor} instance fails.
  113.      *
  114.      * @see #getClassFileProcessorClass()
  115.      * @see #configureClassFileProcessor(org.jomc.tools.ClassFileProcessor)
  116.      */
  117.     public ClassFileProcessor newClassFileProcessor() throws BuildException
  118.     {
  119.         try
  120.         {
  121.             final ClassFileProcessor classFileProcessor = this.getClassFileProcessorClass().newInstance();
  122.             this.configureClassFileProcessor( classFileProcessor );
  123.             return classFileProcessor;
  124.         }
  125.         catch ( final InstantiationException e )
  126.         {
  127.             throw new BuildException( Messages.getMessage( "failedCreatingObject",
  128.                                                            this.getClassFileProcessorClass().getName() ),
  129.                                       e, this.getLocation() );

  130.         }
  131.         catch ( final IllegalAccessException e )
  132.         {
  133.             throw new BuildException( Messages.getMessage( "failedCreatingObject",
  134.                                                            this.getClassFileProcessorClass().getName() ),
  135.                                       e, this.getLocation() );

  136.         }
  137.     }

  138.     /**
  139.      * Configures a given {@code ClassFileProcessor} instance using the properties of the instance.
  140.      *
  141.      * @param classFileProcessor The class file processor to configure.
  142.      *
  143.      * @throws NullPointerException if {@code classFileProcessor} is {@code null}.
  144.      * @throws BuildException if configuring {@code classFileProcessor} fails.
  145.      *
  146.      * @see #configureJomcTool(org.jomc.tools.JomcTool)
  147.      */
  148.     public void configureClassFileProcessor( final ClassFileProcessor classFileProcessor ) throws BuildException
  149.     {
  150.         if ( classFileProcessor == null )
  151.         {
  152.             throw new NullPointerException( "classFileProcessor" );
  153.         }

  154.         this.configureJomcTool( classFileProcessor );
  155.     }

  156.     /**
  157.      * Calls the {@code processClassFiles} method if class processing is enabled.
  158.      *
  159.      * @throws BuildException if processing class files fails.
  160.      *
  161.      * @see #processClassFiles()
  162.      */
  163.     @Override
  164.     public final void executeTask() throws BuildException
  165.     {
  166.         if ( this.isClassProcessingEnabled() )
  167.         {
  168.             this.processClassFiles();
  169.             this.log( Messages.getMessage( "classProcessingSuccess" ) );
  170.         }
  171.         else
  172.         {
  173.             this.log( Messages.getMessage( "classProcessingDisabled" ) );
  174.         }
  175.     }

  176.     /**
  177.      * Processes class files.
  178.      *
  179.      * @throws BuildException if processing class files fails.
  180.      *
  181.      * @see #executeTask()
  182.      */
  183.     public void processClassFiles() throws BuildException
  184.     {
  185.         this.log( Messages.getMessage( "unimplementedTask", this.getClass().getName(), "processClassFiles" ),
  186.                   Project.MSG_ERR );

  187.     }

  188.     /**
  189.      * {@inheritDoc}
  190.      */
  191.     @Override
  192.     public ClassFileProcessorTask clone()
  193.     {
  194.         return (ClassFileProcessorTask) super.clone();
  195.     }

  196. }