Skip to content

Commit

Permalink
Merge branch 'master' into task_136-Get_rid_of_Guava
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Aug 26, 2022
2 parents 1f497c6 + 548e657 commit 39393d8
Show file tree
Hide file tree
Showing 19 changed files with 617 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@

package org.pitest.pitclipse.launch.ui;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.ui.IEditorInput;
import static org.eclipse.jdt.ui.JavaUI.getEditorInputTypeRoot;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;

import static org.eclipse.jdt.ui.JavaUI.getEditorInputTypeRoot;
import org.eclipse.core.resources.IResource;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.ui.IEditorInput;
import org.pitest.pitclipse.launch.ui.utils.PitclipseLaunchUiUtils;

final class LaunchShortcut {

Expand All @@ -40,25 +39,16 @@ static <T> Optional<T> forEditorInputDo(IEditorInput i, Function<ITypeRoot, Opti
}

static Function<ITypeRoot, Optional<IResource>> getCorrespondingResource() {
return t -> {
try {
return Optional.ofNullable(t.getCorrespondingResource());
} catch (JavaModelException e) {
return Optional.empty();
}
};
return t ->
Optional.ofNullable(PitclipseLaunchUiUtils.executeSafelyOrElse(
t::getCorrespondingResource, null));
}

static Optional<IJavaElement> asJavaElement(Object o) {
if (o instanceof IJavaElement) {
IJavaElement element = (IJavaElement) o;
return Optional.of(element);
} else if (o instanceof IAdaptable) {
Object adapted = ((IAdaptable) o).getAdapter(IJavaElement.class);
return Optional.ofNullable((IJavaElement) adapted);
} else {
return Optional.empty();
return Optional.of((IJavaElement) o);
}
return Optional.ofNullable(PitclipseLaunchUiUtils.tryToAdapt(o, IJavaElement.class));
}

static List<ILaunchConfiguration> emptyLaunchConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,6 @@

package org.pitest.pitclipse.launch.ui;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.ILaunchShortcut2;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.pitest.pitclipse.core.PitCoreActivator;
import org.pitest.pitclipse.runner.config.PitConfiguration;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import static org.eclipse.jdt.core.IJavaElement.CLASS_FILE;
import static org.eclipse.jdt.core.IJavaElement.COMPILATION_UNIT;
import static org.eclipse.jdt.core.IJavaElement.JAVA_PROJECT;
Expand Down Expand Up @@ -76,6 +43,39 @@
import static org.pitest.pitclipse.launch.ui.PitLaunchUiActivator.getActiveWorkbenchShell;
import static org.pitest.pitclipse.launch.ui.PitMigrationDelegate.mapResources;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.ILaunchShortcut2;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.pitest.pitclipse.core.PitCoreActivator;
import org.pitest.pitclipse.launch.ui.utils.PitclipseLaunchUiUtils;
import org.pitest.pitclipse.runner.config.PitConfiguration;

/**
* Allows to launch a PIT analyze from a contextual menu.
*/
Expand All @@ -98,32 +98,23 @@ public void launch(IEditorPart editor, String mode) {

@Override
public void launch(ISelection selection, String mode) {
if (selection instanceof IStructuredSelection) {
launch(((IStructuredSelection) selection).toArray(), mode);
} else {
showNoTestsFoundDialog();
}
launch(((IStructuredSelection) selection).toArray(), mode);
}

private void launch(Object[] elements, String mode) {
try {
PitclipseLaunchUiUtils.executeSafely(() -> {
if (elements.length == 1) {
Optional<IJavaElement> selected = asJavaElement(elements[0]);
Optional<IJavaElement> launchElement =
selected.flatMap(this::getLaunchElementFor);

if (launchElement.isPresent()) {
performLaunch(launchElement.get(), mode);
return;
}
}
else {
showNoTestsFoundDialog();
}
} catch (InterruptedException e) { // NOSONAR
// OK, silently move on
} catch (CoreException e) { // NOSONAR
// OK, silently move on
}
showNoTestsFoundDialog();
});
}

private void showNoTestsFoundDialog() {
Expand Down Expand Up @@ -194,14 +185,6 @@ protected ILaunchConfigurationWorkingCopy createLaunchConfiguration(IJavaElement
final String containerHandleId;

switch (element.getElementType()) {
case JAVA_PROJECT:
case PACKAGE_FRAGMENT_ROOT:
case PACKAGE_FRAGMENT:
String name = getTextLabel(element, ALL_FULLY_QUALIFIED);
containerHandleId = element.getHandleIdentifier();
mainTypeQualifiedName = EMPTY_STRING;
testName = name.substring(name.lastIndexOf(IPath.SEPARATOR) + 1);
break;
case TYPE:
containerHandleId = EMPTY_STRING;
// don't replace, fix for binary inner types
Expand All @@ -214,8 +197,11 @@ protected ILaunchConfigurationWorkingCopy createLaunchConfiguration(IJavaElement
mainTypeQualifiedName = method.getDeclaringType().getFullyQualifiedName('.');
testName = method.getDeclaringType().getElementName() + '.' + method.getElementName();
break;
default:
throw new IllegalArgumentException("Invalid element type to create a launch configuration: " + element.getClass().getName()); //$NON-NLS-1$
default: // JAVA_PROJECT, PACKAGE_FRAGMENT_ROOT, PACKAGE_FRAGMENT
String name = getTextLabel(element, ALL_FULLY_QUALIFIED);
containerHandleId = element.getHandleIdentifier();
mainTypeQualifiedName = EMPTY_STRING;
testName = name.substring(name.lastIndexOf(IPath.SEPARATOR) + 1);
}

ILaunchConfigurationType configType = getLaunchManager().getLaunchConfigurationType(getLaunchConfigurationTypeId());
Expand Down Expand Up @@ -247,7 +233,7 @@ protected String[] getAttributeNamesToCompare() {
}

private static boolean hasSameAttributes(ILaunchConfiguration config1, ILaunchConfiguration config2, String[] attributeToCompare) {
try {
return PitclipseLaunchUiUtils.executeSafely(() -> {
for (String element : attributeToCompare) {
String val1 = config1.getAttribute(element, EMPTY_STRING);
String val2 = config2.getAttribute(element, EMPTY_STRING);
Expand All @@ -256,10 +242,7 @@ private static boolean hasSameAttributes(ILaunchConfiguration config1, ILaunchCo
}
}
return true;
} catch (CoreException e) {
// ignore access problems here, return false
}
return false;
});
}

private Optional<ILaunchConfiguration> findExistingLaunchConfiguration(ILaunchConfigurationWorkingCopy temporary) throws InterruptedException,
Expand All @@ -275,17 +258,13 @@ private Optional<ILaunchConfiguration> findExistingLaunchConfiguration(ILaunchCo
return Optional.empty();
} else if (candidateCount == 1) {
return Optional.ofNullable(candidateConfigs.get(0));
} else {
// Prompt the user to choose a config. A null result means the user
// cancelled the dialog, in which case this method returns null,
// since cancelling the dialog should also cancel launching
// anything.
ILaunchConfiguration config = chooseConfiguration(candidateConfigs);
if (config != null) {
return Optional.ofNullable(config);
}
}
return Optional.empty();

// Prompt the user to choose a config.
// if the user does not presses OK we have already interrupted
// the launch with an InterruptedException
ILaunchConfiguration config = chooseConfiguration(candidateConfigs);
return Optional.ofNullable(config);
}

private List<ILaunchConfiguration> findExistingLaunchConfigurations(ILaunchConfigurationWorkingCopy temporary) throws CoreException {
Expand All @@ -307,20 +286,18 @@ private List<ILaunchConfiguration> findExistingLaunchConfigurations(Object candi
Optional<IJavaElement> element = asJavaElement(candidate);
return element.map(findLaunchConfigurations()).orElse(emptyLaunchConfiguration());
}

/**
* {@inheritDoc}
*
* @since 3.4
*/
@Override
public ILaunchConfiguration[] getLaunchConfigurations(ISelection selection) {
if (selection instanceof IStructuredSelection) {
IStructuredSelection ss = (IStructuredSelection) selection;
if (ss.size() == 1) {
List<ILaunchConfiguration> configs = findExistingLaunchConfigurations(ss.getFirstElement());
return toArrayOfILaunchConfiguration(configs);
}
IStructuredSelection ss = (IStructuredSelection) selection;
if (ss.size() == 1) {
List<ILaunchConfiguration> configs = findExistingLaunchConfigurations(ss.getFirstElement());
return toArrayOfILaunchConfiguration(configs);
}
return null; // NOSONAR Eclipse code relies on returned null array
}
Expand All @@ -343,17 +320,10 @@ public ILaunchConfiguration[] getLaunchConfigurations(final IEditorPart editor)
*/
@Override
public IResource getLaunchableResource(ISelection selection) {
if (selection instanceof IStructuredSelection) {
IStructuredSelection ss = (IStructuredSelection) selection;
if (ss.size() == 1) {
Object selected = ss.getFirstElement();
if (!(selected instanceof IJavaElement) && selected instanceof IAdaptable) {
selected = ((IAdaptable) selected).getAdapter(IJavaElement.class);
}
if (selected instanceof IJavaElement) {
return ((IJavaElement) selected).getResource();
}
}
IStructuredSelection ss = (IStructuredSelection) selection;
if (ss.size() == 1) {
Object selected = ss.getFirstElement();
return asJavaElement(selected).map(IJavaElement::getResource).orElse(null);
}
return null;
}
Expand All @@ -365,26 +335,23 @@ public IResource getLaunchableResource(ISelection selection) {
*/
@Override
public IResource getLaunchableResource(IEditorPart editor) {
return forEditorInputDo(editor.getEditorInput(), getCorrespondingResource(), Optional::empty).orElse(null);
return forEditorInputDo(
editor.getEditorInput(),
getCorrespondingResource(),
Optional::empty)
.orElse(null);
}

private Function<IJavaElement, List<ILaunchConfiguration>> findLaunchConfigurations() {
return new Function<IJavaElement, List<ILaunchConfiguration>>() {
public List<ILaunchConfiguration> apply(IJavaElement element) {
Optional<IJavaElement> launchElement = getLaunchElementFor(element);
return launchElement.map(locateLaunchConfigurations()).orElse(emptyLaunchConfiguration());
}

private Function<IJavaElement, List<ILaunchConfiguration>> locateLaunchConfigurations() {
return javaElement -> {
try {
ILaunchConfigurationWorkingCopy workingCopy = createLaunchConfiguration(javaElement);
return findExistingLaunchConfigurations(workingCopy);
} catch (CoreException e1) {
return emptyLaunchConfiguration();
}
};
}
return element -> {
Optional<IJavaElement> launchElement = getLaunchElementFor(element);
return launchElement
.map(javaElement ->
PitclipseLaunchUiUtils.executeSafelyOrElse(
() -> findExistingLaunchConfigurations
(createLaunchConfiguration(javaElement)),
emptyLaunchConfiguration()))
.orElse(emptyLaunchConfiguration());
};
}

Expand All @@ -404,7 +371,7 @@ private Optional<IJavaElement> getLaunchElementFor(IJavaElement element) {
return Optional.empty();
}
}

private Function<ITypeRoot, ILaunchConfiguration[]> toLaunchConfigurations() {
return typeRoot -> {
List<ILaunchConfiguration> configs = findExistingLaunchConfigurations(typeRoot);
Expand Down
Loading

0 comments on commit 39393d8

Please sign in to comment.