Refactor code to avoid duplication

This commit is contained in:
Alexey Andreev 2017-03-17 13:56:05 +03:00
parent 891b906a8c
commit 6c97f8afe3
5 changed files with 70 additions and 81 deletions

View File

@ -17,6 +17,7 @@ package org.teavm.idea.jps.model;
import java.util.List;
import org.teavm.tooling.TeaVMTargetType;
import org.teavm.vm.TeaVMProgressListener;
public interface TeaVMBuildStrategy {
void init();
@ -39,5 +40,7 @@ public interface TeaVMBuildStrategy {
void setSourceFilesCopied(boolean sourceFilesCopied);
void setProgressListener(TeaVMProgressListener progressListener);
TeaVMBuildResult build();
}

View File

@ -23,7 +23,6 @@ import java.util.Collection;
import java.util.List;
import org.jetbrains.jps.incremental.CompileContext;
import org.jetbrains.jps.incremental.messages.CompilerMessage;
import org.jetbrains.jps.incremental.messages.ProgressMessage;
import org.teavm.callgraph.CallGraph;
import org.teavm.diagnostics.ProblemProvider;
import org.teavm.idea.jps.model.TeaVMBuildResult;
@ -35,8 +34,6 @@ import org.teavm.tooling.TeaVMToolException;
import org.teavm.tooling.sources.DirectorySourceFileProvider;
import org.teavm.tooling.sources.JarSourceFileProvider;
import org.teavm.tooling.sources.SourceFileProvider;
import org.teavm.vm.TeaVMPhase;
import org.teavm.vm.TeaVMProgressFeedback;
import org.teavm.vm.TeaVMProgressListener;
public class InProcessBuildStrategy implements TeaVMBuildStrategy {
@ -49,6 +46,7 @@ public class InProcessBuildStrategy implements TeaVMBuildStrategy {
private boolean debugInformationGenerated;
private boolean sourceFilesCopied;
private final List<SourceFileProvider> sourceFileProviders = new ArrayList<>();
private TeaVMProgressListener progressListener;
public InProcessBuildStrategy(CompileContext context) {
this.context = context;
@ -105,10 +103,15 @@ public class InProcessBuildStrategy implements TeaVMBuildStrategy {
this.sourceFilesCopied = sourceFilesCopied;
}
@Override
public void setProgressListener(TeaVMProgressListener progressListener) {
this.progressListener = progressListener;
}
@Override
public TeaVMBuildResult build() {
TeaVMTool tool = new TeaVMTool();
tool.setProgressListener(createProgressListener(context));
tool.setProgressListener(progressListener);
tool.setLog(new EmptyTeaVMToolLog());
tool.setTargetType(targetType);
tool.setMainClass(mainClass);
@ -152,46 +155,6 @@ public class InProcessBuildStrategy implements TeaVMBuildStrategy {
return classLoader;
}
private TeaVMProgressListener createProgressListener(CompileContext context) {
return new TeaVMProgressListener() {
private TeaVMPhase currentPhase;
int expectedCount;
@Override
public TeaVMProgressFeedback phaseStarted(TeaVMPhase phase, int count) {
expectedCount = count;
context.processMessage(new ProgressMessage(phaseName(phase), 0));
currentPhase = phase;
return context.getCancelStatus().isCanceled() ? TeaVMProgressFeedback.CANCEL
: TeaVMProgressFeedback.CONTINUE;
}
@Override
public TeaVMProgressFeedback progressReached(int progress) {
context.processMessage(new ProgressMessage(phaseName(currentPhase), (float) progress / expectedCount));
return context.getCancelStatus().isCanceled() ? TeaVMProgressFeedback.CANCEL
: TeaVMProgressFeedback.CONTINUE;
}
};
}
private static String phaseName(TeaVMPhase phase) {
switch (phase) {
case DEPENDENCY_CHECKING:
return "Discovering classes to compile";
case LINKING:
return "Resolving method invocations";
case DECOMPILATION:
return "Compiling classes";
case OPTIMIZATION:
return "Optimizing code";
case RENDERING:
return "Building JS file";
default:
throw new AssertionError();
}
}
static class InProcessBuildResult implements TeaVMBuildResult {
private CallGraph callGraph;
private boolean errorOccurred;

View File

@ -19,8 +19,6 @@ import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Collection;
import java.util.List;
import org.jetbrains.jps.incremental.CompileContext;
import org.jetbrains.jps.incremental.messages.ProgressMessage;
import org.teavm.callgraph.CallGraph;
import org.teavm.diagnostics.Problem;
import org.teavm.diagnostics.ProblemProvider;
@ -33,14 +31,14 @@ import org.teavm.idea.jps.remote.TeaVMRemoteBuildService;
import org.teavm.tooling.TeaVMTargetType;
import org.teavm.vm.TeaVMPhase;
import org.teavm.vm.TeaVMProgressFeedback;
import org.teavm.vm.TeaVMProgressListener;
public class RemoteBuildStrategy implements TeaVMBuildStrategy {
private final CompileContext context;
private TeaVMRemoteBuildRequest request;
private TeaVMRemoteBuildService buildService;
private TeaVMProgressListener progressListener;
public RemoteBuildStrategy(CompileContext context, TeaVMRemoteBuildService buildService) {
this.context = context;
public RemoteBuildStrategy(TeaVMRemoteBuildService buildService) {
this.buildService = buildService;
}
@ -94,11 +92,16 @@ public class RemoteBuildStrategy implements TeaVMBuildStrategy {
request.sourceFilesCopied = sourceFilesCopied;
}
@Override
public void setProgressListener(TeaVMProgressListener progressListener) {
this.progressListener = progressListener;
}
@Override
public TeaVMBuildResult build() {
TeaVMRemoteBuildResponse response;
try {
response = buildService.build(request, new CallbackImpl(context));
response = buildService.build(request, new CallbackImpl(progressListener));
} catch (Throwable e) {
throw new RuntimeException(e);
}
@ -143,46 +146,21 @@ public class RemoteBuildStrategy implements TeaVMBuildStrategy {
}
static class CallbackImpl extends UnicastRemoteObject implements TeaVMRemoteBuildCallback {
private CompileContext context;
int expectedCount;
TeaVMPhase currentPhase;
private TeaVMProgressListener listener;
public CallbackImpl(CompileContext context) throws RemoteException {
public CallbackImpl(TeaVMProgressListener listener) throws RemoteException {
super();
this.context = context;
this.listener = listener;
}
@Override
public TeaVMProgressFeedback phaseStarted(TeaVMPhase phase, int count) throws RemoteException {
expectedCount = count;
context.processMessage(new ProgressMessage(phaseName(phase), 0));
currentPhase = phase;
return context.getCancelStatus().isCanceled() ? TeaVMProgressFeedback.CANCEL
: TeaVMProgressFeedback.CONTINUE;
return listener.phaseStarted(phase, count);
}
@Override
public TeaVMProgressFeedback progressReached(int progress) throws RemoteException {
context.processMessage(new ProgressMessage(phaseName(currentPhase), (float) progress / expectedCount));
return context.getCancelStatus().isCanceled() ? TeaVMProgressFeedback.CANCEL
: TeaVMProgressFeedback.CONTINUE;
}
}
private static String phaseName(TeaVMPhase phase) {
switch (phase) {
case DEPENDENCY_CHECKING:
return "Discovering classes to compile";
case LINKING:
return "Resolving method invocations";
case DECOMPILATION:
return "Compiling classes";
case OPTIMIZATION:
return "Optimizing code";
case RENDERING:
return "Building JS file";
default:
throw new AssertionError();
return listener.progressReached(progress);
}
}
}

View File

@ -38,6 +38,7 @@ import org.jetbrains.jps.incremental.CompileContext;
import org.jetbrains.jps.incremental.ModuleBuildTarget;
import org.jetbrains.jps.incremental.messages.BuildMessage;
import org.jetbrains.jps.incremental.messages.CompilerMessage;
import org.jetbrains.jps.incremental.messages.ProgressMessage;
import org.jetbrains.jps.model.JpsProject;
import org.jetbrains.jps.model.java.JpsJavaExtensionService;
import org.jetbrains.jps.model.library.JpsLibrary;
@ -65,6 +66,9 @@ import org.teavm.model.MethodReference;
import org.teavm.model.TextLocation;
import org.teavm.model.ValueType;
import org.teavm.tooling.TeaVMTargetType;
import org.teavm.vm.TeaVMPhase;
import org.teavm.vm.TeaVMProgressFeedback;
import org.teavm.vm.TeaVMProgressListener;
class TeaVMBuild {
private final CompileContext context;
@ -123,6 +127,7 @@ class TeaVMBuild {
buildStrategy.setMainClass(config.getMainClass());
buildStrategy.setTargetType(config.getTargetType());
buildStrategy.setTargetDirectory(config.getTargetDirectory());
buildStrategy.setProgressListener(createProgressListener(context));
TeaVMBuildResult buildResult = buildStrategy.build();
if (!buildResult.isErrorOccurred() && buildResult.getProblems().getSevereProblems().isEmpty()) {
@ -134,6 +139,46 @@ class TeaVMBuild {
return true;
}
private TeaVMProgressListener createProgressListener(CompileContext context) {
return new TeaVMProgressListener() {
private TeaVMPhase currentPhase;
int expectedCount;
@Override
public TeaVMProgressFeedback phaseStarted(TeaVMPhase phase, int count) {
expectedCount = count;
context.processMessage(new ProgressMessage(phaseName(phase), 0));
currentPhase = phase;
return context.getCancelStatus().isCanceled() ? TeaVMProgressFeedback.CANCEL
: TeaVMProgressFeedback.CONTINUE;
}
@Override
public TeaVMProgressFeedback progressReached(int progress) {
context.processMessage(new ProgressMessage(phaseName(currentPhase), (float) progress / expectedCount));
return context.getCancelStatus().isCanceled() ? TeaVMProgressFeedback.CANCEL
: TeaVMProgressFeedback.CONTINUE;
}
};
}
private static String phaseName(TeaVMPhase phase) {
switch (phase) {
case DEPENDENCY_CHECKING:
return "Discovering classes to compile";
case LINKING:
return "Resolving method invocations";
case DECOMPILATION:
return "Compiling classes";
case OPTIMIZATION:
return "Optimizing code";
case RENDERING:
return "Building JS file";
default:
throw new AssertionError();
}
}
private void reportProblems(ProblemProvider problemProvider, CallGraph callGraph) {
for (Problem problem : problemProvider.getProblems()) {
BuildMessage.Kind kind;

View File

@ -77,7 +77,7 @@ public class TeaVMBuilder extends ModuleLevelBuilder {
boolean doneSomething = false;
TeaVMBuildStrategy buildStrategy = buildService != null
? new RemoteBuildStrategy(context, buildService)
? new RemoteBuildStrategy(buildService)
: new InProcessBuildStrategy(context);
TeaVMBuild build = new TeaVMBuild(context, assistant, buildStrategy);
for (JpsModule module : chunk.getModules()) {