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;
}
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;
}
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);
}
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;
}
Aggregations