Search in sources :

Example 1 with CostabsException

use of costabs.exceptions.CostabsException in project abstools by abstools.

the class SourceUtils method obtainCurrProject.

public static IProject obtainCurrProject() throws CostabsException {
    IEditorPart ieditorpart;
    ClassLoader loader;
    // we obtain the current window that is being edited
    try {
        ieditorpart = SourceUtils.obtainActiveEditor();
    } catch (NullPointerException e) {
        throw (new CostabsException("There is not any selected class."));
    }
    IResource jresource = extractResource(ieditorpart);
    if (jresource == null)
        throw (new CostabsException("Could not extract file from the current editor"));
    return jresource.getProject();
}
Also used : CostabsException(costabs.exceptions.CostabsException) IEditorPart(org.eclipse.ui.IEditorPart) IResource(org.eclipse.core.resources.IResource)

Example 2 with CostabsException

use of costabs.exceptions.CostabsException in project abstools by abstools.

the class SourceUtils method ObtainCurrentlyEditingClass.

public static Class ObtainCurrentlyEditingClass() throws CostabsException {
    IEditorPart ieditorpart;
    ClassLoader loader;
    // we obtain the current window that is being edited
    try {
        ieditorpart = SourceUtils.obtainActiveEditor();
        // If the file is not safe we fail and say it
        if (ieditorpart.isDirty()) {
            throw (new CostabsException("Java file must be saved to analyze it."));
        }
    } catch (NullPointerException e) {
        throw (new CostabsException("There is not any selected class."));
    }
    // obtain the file being modified
    IResource jresource = extractResource(ieditorpart);
    if (jresource == null)
        throw (new CostabsException("Could not extract file from the current editor"));
    // get the project that owns the file
    IJavaProject jproject = obtainJavaProjectFromResource(jresource);
    if (jproject == null)
        throw (new CostabsException("Cannot load the File: project must be a Java Project"));
    IJavaElement javaFile = JavaCore.create(jresource, jproject);
    if (javaFile == null)
        throw (new CostabsException("Cannot load the File: file must be a Java File"));
    try {
        loader = ClasspathUtils.getProjectClassLoader(jproject);
    } catch (Exception e) {
        throw (new CostabsException("Non valid project, failed to buid class loader"));
    }
    fileEvaluations(javaFile);
    return getClassFromResource((ICompilationUnit) javaFile, loader);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaProject(org.eclipse.jdt.core.IJavaProject) CostabsException(costabs.exceptions.CostabsException) IEditorPart(org.eclipse.ui.IEditorPart) IResource(org.eclipse.core.resources.IResource) CostabsException(costabs.exceptions.CostabsException) JavaModelException(org.eclipse.jdt.core.JavaModelException)

Example 3 with CostabsException

use of costabs.exceptions.CostabsException in project abstools by abstools.

the class SourceUtils method getClassFromResource.

private static Class getClassFromResource(ICompilationUnit javaFile, ClassLoader loader) throws CostabsException {
    try {
        String className = javaFile.getElementName();
        className = className.substring(0, className.length() - 5);
        if (!"".equals(javaFile.getParent().getElementName()))
            className = javaFile.getParent().getElementName() + "." + className;
        Class clazz = loader.loadClass(className);
        return clazz;
    } catch (ClassNotFoundException t) {
        // t.printStackTrace();
        throw new CostabsException("The class loading failed... Classpath may be wrong");
    }
}
Also used : CostabsException(costabs.exceptions.CostabsException)

Aggregations

CostabsException (costabs.exceptions.CostabsException)3 IResource (org.eclipse.core.resources.IResource)2 IEditorPart (org.eclipse.ui.IEditorPart)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1