package jetbrains.mps.execution.api.configurations;
/*Generated by MPS */
import com.intellij.execution.BeforeRunTaskProvider;
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import jetbrains.mps.util.annotation.ToRemove;
import com.intellij.execution.configurations.RunConfiguration;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import org.apache.log4j.Level;
import java.lang.reflect.Method;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
import java.lang.reflect.InvocationTargetException;
import com.intellij.execution.BeforeRunTask;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.project.Project;
public abstract class BaseMpsBeforeTaskProvider<T extends BaseMpsBeforeTaskProvider.BaseMpsBeforeRunTask> extends BeforeRunTaskProvider<T> {
private static final Logger LOG = LogManager.getLogger(BaseMpsBeforeTaskProvider.class);
private final String myName;
private final String myCaption;
/**
*
* @deprecated moved to language level. Use constructor with 2 parameters
*/
@Deprecated
@ToRemove(version = 3.5)
public BaseMpsBeforeTaskProvider(String name) {
this(name, name);
}
public BaseMpsBeforeTaskProvider(String name, String caption) {
myName = name;
myCaption = caption;
}
protected abstract T createTaskImpl();
@Override
public String getName() {
return myCaption;
}
@Override
public String getDescription(T task) {
return myCaption;
}
@Override
public T createTask(RunConfiguration runConfiguration) {
if (hasExecuteMethod(runConfiguration)) {
return createTaskImpl();
}
return null;
}
private boolean hasExecuteMethod(RunConfiguration runConfiguration) {
try {
if (getMethod(runConfiguration) != null) {
return true;
}
} catch (NoSuchMethodException e) {
}
return false;
}
@Override
public boolean executeTask(DataContext context, RunConfiguration configuration, ExecutionEnvironment env, T task) {
if (!(configure(configuration, task))) {
return false;
}
try {
return task.execute(PlatformDataKeys.PROJECT.getData(context), env);
} catch (Throwable t) {
if (LOG.isEnabledFor(Level.ERROR)) {
LOG.error("Error during executing provider " + (myCaption + "(" + this.getClass().getName() + ")"), t);
}
}
return false;
}
private boolean configure(RunConfiguration runConfiguration, T task) {
try {
Method method = getMethod(runConfiguration);
if (method == null) {
return false;
}
Object[] parameters = (Object[]) method.invoke(runConfiguration);
Method configureMethod = Sequence.fromIterable(Sequence.fromArray(task.getClass().getMethods())).findFirst(new IWhereFilter<Method>() {
public boolean accept(Method it) {
return eq_xh6sei_a0a0a0a0a0a3a0a11(it.getName(), getConfigureMethodName());
}
});
return (Boolean) configureMethod.invoke(task, parameters);
} catch (NoSuchMethodException e) {
} catch (InvocationTargetException e) {
if (LOG.isEnabledFor(Level.ERROR)) {
LOG.error("", e);
}
} catch (IllegalAccessException e) {
if (LOG.isEnabledFor(Level.ERROR)) {
LOG.error("", e);
}
} catch (ClassCastException e) {
if (LOG.isEnabledFor(Level.ERROR)) {
LOG.error("", e);
}
}
return false;
}
private Method getMethod(RunConfiguration configuration) throws NoSuchMethodException {
return configuration.getClass().getMethod(getCreateMethodName(myName));
}
@Override
public boolean isConfigurable() {
return false;
}
@Override
public boolean configureTask(RunConfiguration runConfiguration, T task) {
return hasExecuteMethod(runConfiguration);
}
@Override
public boolean canExecuteTask(RunConfiguration configuration, T task) {
return true;
}
public static String getCreateMethodName(String name) {
return "create" + name.replace(" ", "") + "Task";
}
public static String getConfigureMethodName() {
return "configure";
}
public static abstract class BaseMpsBeforeRunTask<T extends BaseMpsBeforeTaskProvider.BaseMpsBeforeRunTask> extends BeforeRunTask<T> {
public BaseMpsBeforeRunTask(@NotNull Key<T> providerId) {
super(providerId);
setEnabled(true);
}
public abstract boolean execute(Project project, ExecutionEnvironment environment);
}
private static boolean eq_xh6sei_a0a0a0a0a0a3a0a11(Object a, Object b) {
return (a != null ? a.equals(b) : a == b);
}
}