Search in sources :

Example 1 with JavaEditor

use of org.eclipse.jdt.internal.ui.javaeditor.JavaEditor in project jbosstools-hibernate by jbosstools.

the class OpenSourceAction method run.

/**
 * @param consoleConfig
 * @param selection
 * @param fullyQualifiedName
 * @throws JavaModelException
 * @throws PartInitException
 * @throws FileNotFoundException
 */
public static IEditorPart run(ConsoleConfiguration consoleConfig, Object selection, String fullyQualifiedName) throws JavaModelException, PartInitException, FileNotFoundException {
    if (fullyQualifiedName == null) {
        return null;
    }
    IJavaProject[] projs = ProjectUtils.findJavaProjects(consoleConfig);
    String remainder = null;
    IType type = null;
    IJavaProject proj = null;
    if (fullyQualifiedName.indexOf("$") > 0) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        remainder = fullyQualifiedName.substring(fullyQualifiedName.indexOf("$") + 1);
        // $NON-NLS-1$
        fullyQualifiedName = fullyQualifiedName.substring(0, fullyQualifiedName.indexOf("$"));
        for (int i = 0; i < projs.length && type == null; i++) {
            proj = projs[i];
            type = ProjectUtils.findType(proj, fullyQualifiedName);
        }
        while (remainder.indexOf("$") > 0) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            String subtype = remainder.substring(0, fullyQualifiedName.indexOf("$"));
            type = type.getType(subtype);
            // $NON-NLS-1$
            remainder = remainder.substring(fullyQualifiedName.indexOf("$") + 1);
        }
        type = type.getType(remainder);
    } else {
        for (int i = 0; i < projs.length && type == null; i++) {
            proj = projs[i];
            type = ProjectUtils.findType(proj, fullyQualifiedName);
        }
    }
    IJavaElement jElement = null;
    if (selection instanceof IProperty) {
        final String selectionName = ((IProperty) selection).getName();
        final IType typeSave = type;
        while (true) {
            jElement = type.getField(selectionName);
            if (jElement != null && jElement.exists()) {
                break;
            }
            String parentClassName = ProjectUtils.getParentTypename(proj, type.getFullyQualifiedName());
            if (parentClassName == null) {
                break;
            }
            type = ProjectUtils.findType(proj, parentClassName);
            for (int i = 0; i < projs.length && type == null; i++) {
                proj = projs[i];
                type = ProjectUtils.findType(proj, fullyQualifiedName);
            }
            if (type == null) {
                break;
            }
        }
        ;
        // do not find element - restore type
        if (jElement == null || !jElement.exists()) {
            type = typeSave;
        }
    }
    if (jElement == null) {
        jElement = type;
    }
    IEditorPart editorPart = JavaUI.openInEditor(type);
    if (editorPart instanceof JavaEditor) {
        JavaEditor jEditor = (JavaEditor) editorPart;
        selectionToEditor(jElement, jEditor);
    }
    if (editorPart == null) {
        String out = NLS.bind(HibernateConsoleMessages.OpenSourceAction_source_file_for_class_not_found, fullyQualifiedName);
        throw new FileNotFoundException(out);
    }
    return editorPart;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaProject(org.eclipse.jdt.core.IJavaProject) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) FileNotFoundException(java.io.FileNotFoundException) IEditorPart(org.eclipse.ui.IEditorPart) IType(org.eclipse.jdt.core.IType) JavaEditor(org.eclipse.jdt.internal.ui.javaeditor.JavaEditor)

Example 2 with JavaEditor

use of org.eclipse.jdt.internal.ui.javaeditor.JavaEditor in project evosuite by EvoSuite.

the class ExtendSuiteEditorAction method execute.

// @Override
// public void selectionChanged(IAction action, ISelection selection) {
// 
// }
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    ISelection selection = HandlerUtil.getActiveMenuSelection(event);
    String SUT = "";
    IResource target = null;
    System.out.println("Current selection of type " + selection.getClass().getName() + ": " + selection);
    if (selection instanceof TreeSelection) {
        TreeSelection treeSelection = (TreeSelection) selection;
        IAdaptable firstElement = (IAdaptable) treeSelection.getFirstElement();
        // Relies on an internal API, bad juju
        if (firstElement instanceof org.eclipse.jdt.internal.core.CompilationUnit) {
            try {
                org.eclipse.jdt.internal.core.CompilationUnit compilationUnit = (org.eclipse.jdt.internal.core.CompilationUnit) firstElement;
                String packageName = "";
                if (compilationUnit.getPackageDeclarations().length > 0) {
                    System.out.println("Package: " + compilationUnit.getPackageDeclarations()[0].getElementName());
                    packageName = compilationUnit.getPackageDeclarations()[0].getElementName();
                }
                String targetSuite = compilationUnit.getElementName().replace(".java", "");
                if (!packageName.isEmpty())
                    targetSuite = packageName + "." + targetSuite;
                System.out.println("Selected class: " + targetSuite);
                SUT = targetSuite;
                target = compilationUnit.getResource();
            } catch (JavaModelException e) {
            }
        }
    } else if (activeEditor instanceof JavaEditor) {
        ITypeRoot root = EditorUtility.getEditorInputJavaElement(activeEditor, false);
        ITextSelection sel = (ITextSelection) ((JavaEditor) activeEditor).getSelectionProvider().getSelection();
        int offset = sel.getOffset();
        IJavaElement element;
        try {
            element = root.getElementAt(offset);
            if (element.getElementType() == IJavaElement.METHOD) {
                IJavaElement pDeclaration = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                IPackageFragment pFragment = (IPackageFragment) pDeclaration;
                String packageName = "";
                if (pFragment.getCompilationUnits()[0].getPackageDeclarations().length > 0) {
                    System.out.println("Package: " + pFragment.getCompilationUnits()[0].getPackageDeclarations()[0].getElementName());
                    packageName = pFragment.getCompilationUnits()[0].getPackageDeclarations()[0].getElementName();
                }
                String targetSuite = element.getParent().getElementName();
                if (!packageName.isEmpty())
                    targetSuite = packageName + "." + targetSuite;
                System.out.println("Selected class: " + targetSuite);
                SUT = targetSuite;
            } else if (element.getElementType() == IJavaElement.TYPE) {
                IType type = ((IType) element);
                System.out.println("Selected class: " + type.getFullyQualifiedName());
                SUT = type.getFullyQualifiedName();
            }
            IWorkspaceRoot wroot = ResourcesPlugin.getWorkspace().getRoot();
            target = wroot.findMember(root.getPath());
        } catch (JavaModelException e) {
        }
    }
    if (!SUT.isEmpty() && target != null) {
        IProject proj = target.getProject();
        fixJUnitClassPath(JavaCore.create(proj));
        generateTests(target);
    }
    return null;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) IEditorPart(org.eclipse.ui.IEditorPart) ITextSelection(org.eclipse.jface.text.ITextSelection) IProject(org.eclipse.core.resources.IProject) JavaEditor(org.eclipse.jdt.internal.ui.javaeditor.JavaEditor) IType(org.eclipse.jdt.core.IType) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) TreeSelection(org.eclipse.jface.viewers.TreeSelection) ISelection(org.eclipse.jface.viewers.ISelection) IResource(org.eclipse.core.resources.IResource)

Example 3 with JavaEditor

use of org.eclipse.jdt.internal.ui.javaeditor.JavaEditor in project bndtools by bndtools.

the class JUnitShortcut method launch.

/*
     * This is called when the launch starts in the editor. If a method or type is selected in a Java Editor, we will
     * test that type/method.
     */
@Override
public void launch(IEditorPart editor, String mode) {
    try {
        if (editor instanceof JavaEditor) {
            IJavaElement element = getSelectedJavaElement((JavaEditor) editor);
            if (element == null) {
                IEditorInput input = editor.getEditorInput();
                element = (IJavaElement) input.getAdapter(IJavaElement.class);
            }
            if (element != null) {
                launchJavaElements(Collections.singletonList(element), mode);
                return;
            }
        }
    } catch (CoreException e) {
        e.printStackTrace();
    }
    // 
    // Did not succeed to get a JavaElement, we try the original way
    // 
    super.launch(editor, mode);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) CoreException(org.eclipse.core.runtime.CoreException) IEditorInput(org.eclipse.ui.IEditorInput) JavaEditor(org.eclipse.jdt.internal.ui.javaeditor.JavaEditor)

Example 4 with JavaEditor

use of org.eclipse.jdt.internal.ui.javaeditor.JavaEditor in project evosuite by EvoSuite.

the class GenerateTestsEditorAction method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    // ISelection selection = HandlerUtil.getCurrentSelection(event);
    ISelection selection = HandlerUtil.getActiveMenuSelection(event);
    String SUT = "";
    IResource target = null;
    System.out.println("Current selection of type " + selection.getClass().getName() + ": " + selection);
    if (selection instanceof TreeSelection) {
        TreeSelection treeSelection = (TreeSelection) selection;
        IAdaptable firstElement = (IAdaptable) treeSelection.getFirstElement();
        // Relies on an internal API, bad juju
        if (firstElement instanceof org.eclipse.jdt.internal.core.CompilationUnit) {
            try {
                org.eclipse.jdt.internal.core.CompilationUnit compilationUnit = (org.eclipse.jdt.internal.core.CompilationUnit) firstElement;
                String packageName = "";
                if (compilationUnit.getPackageDeclarations().length > 0) {
                    System.out.println("Package: " + compilationUnit.getPackageDeclarations()[0].getElementName());
                    packageName = compilationUnit.getPackageDeclarations()[0].getElementName();
                }
                String targetSuite = compilationUnit.getElementName().replace(".java", "");
                if (!packageName.isEmpty())
                    targetSuite = packageName + "." + targetSuite;
                System.out.println("Selected class: " + targetSuite);
                SUT = targetSuite;
                target = compilationUnit.getResource();
            } catch (JavaModelException e) {
            }
        }
    } else if (activeEditor instanceof JavaEditor) {
        ITypeRoot root = EditorUtility.getEditorInputJavaElement(activeEditor, false);
        ITextSelection sel = (ITextSelection) ((JavaEditor) activeEditor).getSelectionProvider().getSelection();
        int offset = sel.getOffset();
        IJavaElement element;
        try {
            element = root.getElementAt(offset);
            if (element == null) {
                ISelection sel2 = HandlerUtil.getCurrentSelection(event);
                System.out.println("Selected element of type " + sel2.getClass().getName() + ": " + sel2.toString());
            } else if (element.getElementType() == IJavaElement.METHOD) {
                IJavaElement pDeclaration = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                IPackageFragment pFragment = (IPackageFragment) pDeclaration;
                String packageName = "";
                if (pFragment.getCompilationUnits()[0].getPackageDeclarations().length > 0) {
                    System.out.println("Package: " + pFragment.getCompilationUnits()[0].getPackageDeclarations()[0].getElementName());
                    packageName = pFragment.getCompilationUnits()[0].getPackageDeclarations()[0].getElementName();
                }
                String targetSuite = element.getParent().getElementName();
                if (!packageName.isEmpty())
                    targetSuite = packageName + "." + targetSuite;
                System.out.println("Selected class: " + targetSuite);
                SUT = targetSuite;
            } else if (element.getElementType() == IJavaElement.TYPE) {
                IType type = ((IType) element);
                System.out.println("Selected class: " + type.getFullyQualifiedName());
                SUT = type.getFullyQualifiedName();
            }
            IWorkspaceRoot wroot = ResourcesPlugin.getWorkspace().getRoot();
            target = wroot.findMember(EditorUtility.getEditorInputJavaElement(activeEditor, false).getPath());
        } catch (JavaModelException e) {
        }
    }
    if (!SUT.isEmpty() && target != null) {
        IProject proj = target.getProject();
        fixJUnitClassPath(JavaCore.create(proj));
        generateTests(target);
    }
    return null;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) IEditorPart(org.eclipse.ui.IEditorPart) ITextSelection(org.eclipse.jface.text.ITextSelection) IProject(org.eclipse.core.resources.IProject) JavaEditor(org.eclipse.jdt.internal.ui.javaeditor.JavaEditor) IType(org.eclipse.jdt.core.IType) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) TreeSelection(org.eclipse.jface.viewers.TreeSelection) ISelection(org.eclipse.jface.viewers.ISelection) IResource(org.eclipse.core.resources.IResource)

Aggregations

IJavaElement (org.eclipse.jdt.core.IJavaElement)4 JavaEditor (org.eclipse.jdt.internal.ui.javaeditor.JavaEditor)4 IType (org.eclipse.jdt.core.IType)3 IEditorPart (org.eclipse.ui.IEditorPart)3 IProject (org.eclipse.core.resources.IProject)2 IResource (org.eclipse.core.resources.IResource)2 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)2 IAdaptable (org.eclipse.core.runtime.IAdaptable)2 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 ITypeRoot (org.eclipse.jdt.core.ITypeRoot)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 ITextSelection (org.eclipse.jface.text.ITextSelection)2 ISelection (org.eclipse.jface.viewers.ISelection)2 TreeSelection (org.eclipse.jface.viewers.TreeSelection)2 FileNotFoundException (java.io.FileNotFoundException)1 CoreException (org.eclipse.core.runtime.CoreException)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IEditorInput (org.eclipse.ui.IEditorInput)1 IProperty (org.jboss.tools.hibernate.runtime.spi.IProperty)1