Example usage for org.eclipse.jdt.internal.core JavaModelManager createClassFileFrom

List of usage examples for org.eclipse.jdt.internal.core JavaModelManager createClassFileFrom

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core JavaModelManager createClassFileFrom.

Prototype

public static IClassFile createClassFileFrom(IFile file, IJavaProject project) 

Source Link

Document

Creates and returns a class file element for the given .class file, its project being the given project.

Usage

From source file:com.drgarbage.bytecodevisualizer.editors.BytecodeDocumentProvider.java

License:Apache License

protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {
    if (element instanceof IEditorInput) {
        IEditorInput input = (IEditorInput) element;

        if (input instanceof IFileEditorInput) {

            /* a local class file */
            IFileEditorInput fileEditorInput = (IFileEditorInput) input;
            IFile file = fileEditorInput.getFile();

            String fqTypeName = BytecodeUtils.toFullyQualifiedTypeName(file);

            if (fqTypeName != null) {
                /* FIX for BUG#214:
                 *  NullPointerException when opening a Class which is 
                 *  not in build path from Navigator */

                IProject project = file.getProject();

                /* create java project */
                IJavaProject javaProject = JavaCore.create(project);

                try {
                    IType t = javaProject.findType(fqTypeName);
                    if (t != null) {
                        IResource res = t.getCompilationUnit().getResource();

                        BytecodeMarkerAnnotationModel model = new BytecodeMarkerAnnotationModel(res,
                                classFileEditor);

                        IClassFile classFile = t.getClassFile();
                        if (classFile == null) {
                            classFile = JavaModelManager.createClassFileFrom(file, javaProject);
                        }/*from   w w  w. j  a  v  a 2s .co m*/

                        model.setClassFile(classFile);
                        model.setClassFileDocument(classFileDocument);

                        return model;
                    }
                } catch (JavaModelException e) {
                    throw new CoreException(
                            new Status(IStatus.ERROR, CoreConstants.BYTECODE_VISUALIZER_PLUGIN_ID, IStatus.OK,
                                    BytecodeVisualizerMessages.Error_not_load_file, e));
                }
            }
        }
    }

    if (element instanceof IClassFileEditorInput) {
        return createClassFileAnnotationModel((IClassFileEditorInput) element);
    }

    return null;
}