pl.wroc.pwr.jbehaveplugin.editor.JavaAnalyzer.java Source code

Java tutorial

Introduction

Here is the source code for pl.wroc.pwr.jbehaveplugin.editor.JavaAnalyzer.java

Source

/*******************************************************************************
 * Copyright (c) 2012 M. Cenkar, P. Nurkowski, A. Pawelec, M. Piatek
 * 
 * Permission is hereby granted,  free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal 
 * in the Software without  restriction, including without limitation the rights 
 * to use, copy,  modify,  merge,  publish,  distribute, sublicense, and/or sell 
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The  above  copyright  notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF  OR  IN CONNECTION  WITH  THE  SOFTWARE  OR  THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE.
 ******************************************************************************/
package pl.wroc.pwr.jbehaveplugin.editor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.Annotation;
import org.eclipse.jdt.internal.core.JavaElement;

public class JavaAnalyzer {
    public static boolean isPackageFragmentRootOfSource(IJavaElement elem) throws JavaModelException {
        if (elem instanceof IPackageFragmentRoot) {
            return ((IPackageFragmentRoot) elem).getKind() == IPackageFragmentRoot.K_SOURCE;
        }
        return false;
    }

    private List<IType> collectedTypes = new ArrayList<IType>();

    public List<IType> getTypes() {
        return collectedTypes;
    }

    public void collectTypes(IProject project) throws JavaModelException {
        IJavaProject javaProject = (IJavaProject) JavaCore.create(project);
        for (IJavaElement jElem : javaProject.getChildren()) {
            if (isPackageFragmentRootOfSource(jElem)) {
                collectTypes((IPackageFragmentRoot) jElem);
            }
        }
    }

    public void collectTypes(IPackageFragmentRoot packageFragmentRoot) throws JavaModelException {
        for (IJavaElement jElem : packageFragmentRoot.getChildren()) {
            if (jElem instanceof IPackageFragment) {
                collectTypes((IPackageFragment) jElem);
            }
        }
    }

    public void collectTypes(IPackageFragment packageFragment) throws JavaModelException {
        for (IJavaElement jElem : packageFragment.getChildren()) {
            if (jElem instanceof ICompilationUnit) {
                collectTypes((ICompilationUnit) jElem);
            }
        }
    }

    public void collectTypes(ICompilationUnit compilationUnit) throws JavaModelException {
        for (IJavaElement jElem : compilationUnit.getChildren()) {
            if (jElem instanceof IType) {
                collectedTypes.add((IType) jElem);
            }
        }
    }

    public List<IMethod> getMethods() throws JavaModelException {
        return getMethods(getTypes());
    }

    @SuppressWarnings("restriction")
    public List<IMethod> getMethodsWithJBehaveAnnotations() throws JavaModelException {
        List<IMethod> methods = getMethods();
        List<IMethod> returnList = new ArrayList<IMethod>();
        for (IMethod method : methods) {
            String collectedAnnotationName = "";
            IAnnotation[] annotations = method.getAnnotations();
            for (IAnnotation annotation : annotations) {
                String annotationName = annotation.getElementName();
                if ("Given".equals(annotationName) || "When".equals(annotationName)
                        || "Then".equals(annotationName)) {
                    returnList.add(method);
                    collectedAnnotationName = annotationName;
                }
                if ("Alias".equals(annotationName)) {
                    @SuppressWarnings("unused")
                    Annotation newFromAlias = new Annotation((JavaElement) annotation.getParent(),
                            collectedAnnotationName);
                }
            }
        }
        return returnList;
    }

    @SuppressWarnings("restriction")
    public List<IAnnotation> getJBehaveAnnotations() throws JavaModelException {
        List<IMethod> methodsWithJBehaveAnnotations = getMethodsWithJBehaveAnnotations();
        List<IAnnotation> result = new ArrayList<IAnnotation>();

        for (IMethod method : methodsWithJBehaveAnnotations) {
            String collectedAnnotationName = "";
            for (IAnnotation annotation : method.getAnnotations()) {
                String annotationName = annotation.getElementName();
                if ("Given".equals(annotationName) || "When".equals(annotationName)
                        || "Then".equals(annotationName)) {
                    result.add(annotation);
                    collectedAnnotationName = annotationName;
                }
                if ("Alias".equals(annotationName)) {
                    // Annotation newFromAlias = new
                    // Annotation((JavaElement)annotation,
                    // collectedAnnotationName);
                    // result.add(newFromAlias);
                    AliasAnnotation aliasAnn = new AliasAnnotation(collectedAnnotationName,
                            annotation.getMemberValuePairs());
                    result.add(aliasAnn);
                }
            }
        }

        return result;
    }

    public static List<IMethod> getMethods(List<IType> types) throws JavaModelException {
        List<IMethod> methods = new ArrayList<IMethod>();
        for (IType type : types) {
            for (IJavaElement jElem : type.getChildren()) {
                if (jElem instanceof IMethod)
                    methods.add((IMethod) jElem);
            }
        }
        return methods;
    }

    public static List<IMethod> getMethods(IType type) throws JavaModelException {
        List<IMethod> methods = new ArrayList<IMethod>();
        for (IJavaElement jElem : type.getChildren()) {
            if (jElem instanceof IMethod)
                methods.add((IMethod) jElem);
        }
        return methods;
    }
}