All Downloads are FREE. Search and download functionalities are using the official Maven repository.

patterntesting.tool.aspectj.AjcXmlTask Maven / Gradle / Ivy

Go to download

PatternTesting Tools (patterntesting-tools) is the container for tools around PatternTesting like the Ant extensions and Maven plugin.

The newest version!
/* 
 *========================================================================
 * 
 * Copyright 2001-2004 Vincent Massol & Matt Smith.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 *========================================================================
 */
package patterntesting.tool.aspectj;

import java.io.*;
import java.util.*;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;
import org.apache.tools.ant.BuildException;
import org.aspectj.tools.ant.taskdefs.AjcTask;
import org.aspectj.tools.ant.taskdefs.compilers.Ajc;

/**
 * Ant task that is a wrapper around the {@link Ajc} task. It configures it
 * to use the {@link AjcErrorHandler} error handler in order to provide
 * structured results from Ajc. 
 *
 * @author Vincent Massol
 * @author Matt Smith
 *
 * @version $Id: AjcXmlTask.java,v 1.6 2009/12/30 13:33:04 oboehm Exp $
 */
public class AjcXmlTask extends AjcTask {

	private static Log log = LogFactoryImpl.getLog(AjcXmlTask.class);
   
   /**
	 * Lists of errors received from the Ajc compiler. Array of
	 * {@link AjcFileResult} objects.
	 */
	private Map errors = new Hashtable();

	/**
	 * Output stream to write to.
	 */
	private OutputStream resultStream;

	/**
	 * Formatter
	 */
	private ResultFormatter formatter = new XMLFormatter();

	/**
	 * As a precondition for a successful compilation the aspectjrt.jar must
	 * be in the java.class.path - otherwise the AjcTask doesn't seem to work
	 * (got by experimental programming, also knows als trial & error).
	 * 
	 * @since 24-May-2007
	 * @author oliver ([email protected])
	 */
	public AjcXmlTask() {
		checkClasspath();
	}

	private void checkClasspath() {
		String classpath = System.getProperty("java.class.path");
		if (!classpath.matches(".*/aspectj\\w*rt.jar.*")) {
			log.debug("aspectj.rt not found in " + classpath);
			File aspectjrt = getAspectjPath();
			String s = File.pathSeparatorChar + aspectjrt.toString();
			System.setProperty("java.class.path", classpath + s);
			log.info(s + " added to java.class.path");
		}
	}

	/**
	 * It use the environment variable ASPECTJ_HOME to determine the path of
	 * aspectjrt.jar. If ASPECTJ_HOME is not set it uses
	 * "/usr/java/aspectj/lib/aspectjrt.jar" as default which is valid on the
	 * build machine (bbb.i).
	 * 
	 * @return path of aspectjrt.jar, e.g. /usr/java/aspectj/lib/aspectjrt.jar
	 */
	private File getAspectjPath() {
		String aspectjHome = System.getenv("ASPECTJ_HOME");
		if (StringUtils.isEmpty(aspectjHome)) {
			aspectjHome = "/usr/java/aspectj";
		}
		log.debug("using ASPECTJ_HOME=" + aspectjHome);
		File aspectjrt = new File(aspectjHome, "lib/aspectjrt.jar");
		if (!aspectjrt.exists()) {
			String msg = "no aspectjrt.jar in classpath, " + aspectjrt
					+ " not found";
			log.fatal(msg);
			throw new RuntimeException(msg);
		}
		return aspectjrt;
	}

	/**
	 * @return map with the errors
	 */
    public Map getErrors()
    {
       return this.errors;
    }

    /**
     * Sets the XML result file.
     * 
     * @param resultFile the result file
     * @throws BuildException in case of build problems
     */
    public void setResultFile(File resultFile) throws BuildException
    {
        try 
        {
            setOutputStream(new FileOutputStream(resultFile));
        } 
        catch (IOException e)
        {
            throw new BuildException("Failed to create output file [" 
                + resultFile + "]");
        }
    }

    /**
     * Sets the output stream. Useful if you wish to call this Ant task
     * from a java application and not from an Ant script.
     * 
     * @param outputStream the output stream onto which the compiler
     *         will output the result in XML
     */
    public void setOutputStream(OutputStream outputStream)
    {
        this.resultStream = outputStream;
    }

    /**
     * Sets the formatter to "plain" or "xml".
     * 
     * @param formatter e.g. "plain"
     */
    public void setFormatter(String formatter)
    {
       if("plain".equals(formatter))
       {
          this.formatter = new PlainFormatter();
       }
       else
       {
          this.formatter = new XMLFormatter();
       }
    }

    /**
	 * @see Ajc#execute()
	 */
	public void execute() throws BuildException {
		try {
			if (this.resultStream == null) {
				throw new BuildException("Must specify result file");
			}
			AjcErrorHandler handler = new AjcErrorHandler();
			this.setMessageHolder(handler);
			super.execute();
			log.debug("compiler executed - " + handler);
			this.resultStream.write(formatter.format(handler.getResults())
					.getBytes());
			this.errors = handler.getErrorResults();
		} catch (Exception e) {
			log.error("Could not execute compiler!", e);
			throw new BuildException("Could not execute compiler!");
		}
	}

    /**
	 * @see Ajc#spoon()
	 */
  /*
	 * protected int spoon() throws BuildException { if (version) {
	 * version(null); }
	 * 
	 * try { log("Running in-process " + AJC_CLASSNAME + " using " +
	 * Ajc.render(cmd.getCommandline()), Project.MSG_VERBOSE);
	 * 
	 * Class mainClass = Class.forName(AJC_CLASSNAME); Constructor
	 * mainConstructor = mainClass.getConstructor( new Class[] {
	 * ErrorHandler.class });
	 * 
	 * AjcErrorHandler handler = new AjcErrorHandler();
	 * 
	 * Object main = mainConstructor.newInstance( new Object[] { handler });
	 * 
	 * int result = ((Integer) main.getClass().getMethod("compile", new Class[] {
	 * String[].class }).invoke(main, new Object[] { cmd.getCommandline()
	 * })).intValue();
	 *  // Create the XML result file
	 * this.resultStream.write(XMLFormatter.format(
	 * handler.getResults()).getBytes());
	 * 
	 * return result; } catch (Throwable t) { t.printStackTrace(); throw new
	 * BuildException("Couldn't create compiler!", t, location); } }
	 */
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy