Example usage for org.eclipse.jdt.core JavaCore create

List of usage examples for org.eclipse.jdt.core JavaCore create

Introduction

In this page you can find the example usage for org.eclipse.jdt.core JavaCore create.

Prototype

public static IJavaElement create(IResource resource, IJavaProject project) 

Source Link

Document

Returns the Java element corresponding to the given file, its project being the given project.

Usage

From source file:apet.utils.SourceUtils.java

License:Open Source License

public static Class<?> ObtainCurrentlyEditingClass() throws ApetException {
    IEditorPart ieditorpart;// ww w.ja v  a  2s  .c o  m
    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 ApetException("Java file must be saved to analyze it."));
        }
    } catch (NullPointerException e) {
        throw (new ApetException("There is not any selected class."));

    }
    //obtain the file being modified
    IResource jresource = extractResource(ieditorpart);
    if (jresource == null)
        throw (new ApetException("Could not extract file from the current editor"));

    //get the project that owns the file
    IJavaProject jproject = obtainJavaProjectFromResource(jresource);
    if (jproject == null)
        throw (new ApetException("Cannot load the File: project must be a Java Project"));

    IJavaElement javaFile = JavaCore.create(jresource, jproject);
    if (javaFile == null)
        throw (new ApetException("Cannot load the File: file must be a Java File"));
    try {
        loader = ClasspathUtils.getProjectClassLoader(jproject);
    } catch (Exception e) {
        throw (new ApetException("Non valid project, failed to buid class loader"));
    }

    fileEvaluations(javaFile);
    return getClassFromResource((ICompilationUnit) javaFile, loader);
}