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

patterntesting.plugin.aspectj.AjcErrorHandler Maven / Gradle / Ivy

Go to download

PatternTesting is a testing framework that allows to automatically verify that Architecture/Design recommendations are implemented correctly in the code. It uses AOP and AspectJ to perform this feat.

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.plugin.aspectj;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;
import org.aspectj.bridge.*;

import patterntesting.io.FileHelper;

/**
 * Error handler to pass to the Ajc compiler in order to get
 * structured error information.
 *
 * @author Vincent Massol
 * @author Matt Smith
 *
 * @version $Id: AjcErrorHandler.java,v 1.7 2008/09/26 17:42:34 oboehm Exp $
 */
public class AjcErrorHandler extends MessageHandler
{
	private static Log log = LogFactoryImpl.getLog(AjcErrorHandler.class);
	protected static int loggedWarnings = 0;
	
    /**
     * Lists of errors received from the Ajc compiler. Array of
     * {@link AjcFileResult} objects.
     */
    private Hashtable errors = new Hashtable();
    
    /**
     * @return the list of errors received from the Ajc compiler as
     *          a List of AjcResult objects
     */
    public Enumeration getResults()    
    {
        return this.errors.elements();
    }
    
    /**
     * @return the map of errors received from the Ajc compiler as
     *          a map of AjcResult objects
     */
    public Map getErrorResults()
    {
       return errors;
    }
    
    /**
     * using now "/tmp/patterntesting/patterntesting.xml" as output file
     * (20-May-07, [email protected])
     */
    private void write() {
		XMLFormatter formatter = new XMLFormatter();
		try {
			OutputStream resultStream = new FileOutputStream(FileHelper
					.getTmpdir("/patterntesting/patterntesting.xml"));
			resultStream.write(formatter.format(this.getResults()).getBytes());
		} catch (IOException e) {
			log.warn("can't write result", e);
			loggedWarnings++;
		}
	}

    /*
	 * (non-Javadoc)
	 * 
	 * @see org.aspectj.bridge.IMessageHandler#handleMessage(org.aspectj.bridge.IMessage)
	 */
    @SuppressWarnings("unchecked")
	public boolean handleMessage(IMessage message)
    {
        ISourceLocation location = message.getSourceLocation();
        if(location != null)
        {
            String path = location.getSourceFile().getPath();
            AjcFileResult fileResult = 
                (AjcFileResult)this.errors.get(path);
            if(fileResult == null)
            {
                fileResult = new AjcFileResult(path);
                this.errors.put(path, fileResult);
            }
            if(message.getKind() == IMessage.ERROR)
            {
               fileResult.addError(new AjcResult(location.getLine(), message.getMessage()));   
            }
            else
            {
               fileResult.addWarning(new AjcResult(location.getLine(), message.getMessage()));
            }
            //this.write();
        }
        return true;
    }
    
    public String toString() {
    	if (errors.size() > 0) {
    		return "AjcErrorHandler: with error/warning(s)";
    	} else {
    		return super.toString();
    	}
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy