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 java.util.List;
import org.teavm.tooling.TeaVMTargetType; import org.teavm.tooling.TeaVMTargetType;
import org.teavm.vm.TeaVMProgressListener;
public interface TeaVMBuildStrategy { public interface TeaVMBuildStrategy {
void init(); void init();
@ -39,5 +40,7 @@ public interface TeaVMBuildStrategy {
void setSourceFilesCopied(boolean sourceFilesCopied); void setSourceFilesCopied(boolean sourceFilesCopied);
void setProgressListener(TeaVMProgressListener progressListener);
TeaVMBuildResult build(); TeaVMBuildResult build();
} }

View File

@ -23,7 +23,6 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import org.jetbrains.jps.incremental.CompileContext; import org.jetbrains.jps.incremental.CompileContext;
import org.jetbrains.jps.incremental.messages.CompilerMessage; import org.jetbrains.jps.incremental.messages.CompilerMessage;
import org.jetbrains.jps.incremental.messages.ProgressMessage;
import org.teavm.callgraph.CallGraph; import org.teavm.callgraph.CallGraph;
import org.teavm.diagnostics.ProblemProvider; import org.teavm.diagnostics.ProblemProvider;
import org.teavm.idea.jps.model.TeaVMBuildResult; 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.DirectorySourceFileProvider;
import org.teavm.tooling.sources.JarSourceFileProvider; import org.teavm.tooling.sources.JarSourceFileProvider;
import org.teavm.tooling.sources.SourceFileProvider; import org.teavm.tooling.sources.SourceFileProvider;
import org.teavm.vm.TeaVMPhase;
import org.teavm.vm.TeaVMProgressFeedback;
import org.teavm.vm.TeaVMProgressListener; import org.teavm.vm.TeaVMProgressListener;
public class InProcessBuildStrategy implements TeaVMBuildStrategy { public class InProcessBuildStrategy implements TeaVMBuildStrategy {
@ -49,6 +46,7 @@ public class InProcessBuildStrategy implements TeaVMBuildStrategy {
private boolean debugInformationGenerated; private boolean debugInformationGenerated;
private boolean sourceFilesCopied; private boolean sourceFilesCopied;
private final List<SourceFileProvider> sourceFileProviders = new ArrayList<>(); private final List<SourceFileProvider> sourceFileProviders = new ArrayList<>();
private TeaVMProgressListener progressListener;
public InProcessBuildStrategy(CompileContext context) { public InProcessBuildStrategy(CompileContext context) {
this.context = context; this.context = context;
@ -105,10 +103,15 @@ public class InProcessBuildStrategy implements TeaVMBuildStrategy {
this.sourceFilesCopied = sourceFilesCopied; this.sourceFilesCopied = sourceFilesCopied;
} }
@Override
public void setProgressListener(TeaVMProgressListener progressListener) {
this.progressListener = progressListener;
}
@Override @Override
public TeaVMBuildResult build() { public TeaVMBuildResult build() {
TeaVMTool tool = new TeaVMTool(); TeaVMTool tool = new TeaVMTool();
tool.setProgressListener(createProgressListener(context)); tool.setProgressListener(progressListener);
tool.setLog(new EmptyTeaVMToolLog()); tool.setLog(new EmptyTeaVMToolLog());
tool.setTargetType(targetType); tool.setTargetType(targetType);
tool.setMainClass(mainClass); tool.setMainClass(mainClass);
@ -152,46 +155,6 @@ public class InProcessBuildStrategy implements TeaVMBuildStrategy {
return classLoader; 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 { static class InProcessBuildResult implements TeaVMBuildResult {
private CallGraph callGraph; private CallGraph callGraph;
private boolean errorOccurred; private boolean errorOccurred;

View File

@ -19,8 +19,6 @@ import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject; import java.rmi.server.UnicastRemoteObject;
import java.util.Collection; import java.util.Collection;
import java.util.List; 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.callgraph.CallGraph;
import org.teavm.diagnostics.Problem; import org.teavm.diagnostics.Problem;
import org.teavm.diagnostics.ProblemProvider; import org.teavm.diagnostics.ProblemProvider;
@ -33,14 +31,14 @@ import org.teavm.idea.jps.remote.TeaVMRemoteBuildService;
import org.teavm.tooling.TeaVMTargetType; import org.teavm.tooling.TeaVMTargetType;
import org.teavm.vm.TeaVMPhase; import org.teavm.vm.TeaVMPhase;
import org.teavm.vm.TeaVMProgressFeedback; import org.teavm.vm.TeaVMProgressFeedback;
import org.teavm.vm.TeaVMProgressListener;
public class RemoteBuildStrategy implements TeaVMBuildStrategy { public class RemoteBuildStrategy implements TeaVMBuildStrategy {
private final CompileContext context;
private TeaVMRemoteBuildRequest request; private TeaVMRemoteBuildRequest request;
private TeaVMRemoteBuildService buildService; private TeaVMRemoteBuildService buildService;
private TeaVMProgressListener progressListener;
public RemoteBuildStrategy(CompileContext context, TeaVMRemoteBuildService buildService) { public RemoteBuildStrategy(TeaVMRemoteBuildService buildService) {
this.context = context;
this.buildService = buildService; this.buildService = buildService;
} }
@ -94,11 +92,16 @@ public class RemoteBuildStrategy implements TeaVMBuildStrategy {
request.sourceFilesCopied = sourceFilesCopied; request.sourceFilesCopied = sourceFilesCopied;
} }
@Override
public void setProgressListener(TeaVMProgressListener progressListener) {
this.progressListener = progressListener;
}
@Override @Override
public TeaVMBuildResult build() { public TeaVMBuildResult build() {
TeaVMRemoteBuildResponse response; TeaVMRemoteBuildResponse response;
try { try {
response = buildService.build(request, new CallbackImpl(context)); response = buildService.build(request, new CallbackImpl(progressListener));
} catch (Throwable e) { } catch (Throwable e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -143,46 +146,21 @@ public class RemoteBuildStrategy implements TeaVMBuildStrategy {
} }
static class CallbackImpl extends UnicastRemoteObject implements TeaVMRemoteBuildCallback { static class CallbackImpl extends UnicastRemoteObject implements TeaVMRemoteBuildCallback {
private CompileContext context; private TeaVMProgressListener listener;
int expectedCount;
TeaVMPhase currentPhase;
public CallbackImpl(CompileContext context) throws RemoteException { public CallbackImpl(TeaVMProgressListener listener) throws RemoteException {
super(); super();
this.context = context; this.listener = listener;
} }
@Override @Override
public TeaVMProgressFeedback phaseStarted(TeaVMPhase phase, int count) throws RemoteException { public TeaVMProgressFeedback phaseStarted(TeaVMPhase phase, int count) throws RemoteException {
expectedCount = count; return listener.phaseStarted(phase, count);
context.processMessage(new ProgressMessage(phaseName(phase), 0));
currentPhase = phase;
return context.getCancelStatus().isCanceled() ? TeaVMProgressFeedback.CANCEL
: TeaVMProgressFeedback.CONTINUE;
} }
@Override @Override
public TeaVMProgressFeedback progressReached(int progress) throws RemoteException { public TeaVMProgressFeedback progressReached(int progress) throws RemoteException {
context.processMessage(new ProgressMessage(phaseName(currentPhase), (float) progress / expectedCount)); return listener.progressReached(progress);
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();
} }
} }
} }

View File

@ -38,6 +38,7 @@ import org.jetbrains.jps.incremental.CompileContext;
import org.jetbrains.jps.incremental.ModuleBuildTarget; import org.jetbrains.jps.incremental.ModuleBuildTarget;
import org.jetbrains.jps.incremental.messages.BuildMessage; import org.jetbrains.jps.incremental.messages.BuildMessage;
import org.jetbrains.jps.incremental.messages.CompilerMessage; 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.JpsProject;
import org.jetbrains.jps.model.java.JpsJavaExtensionService; import org.jetbrains.jps.model.java.JpsJavaExtensionService;
import org.jetbrains.jps.model.library.JpsLibrary; 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.TextLocation;
import org.teavm.model.ValueType; import org.teavm.model.ValueType;
import org.teavm.tooling.TeaVMTargetType; import org.teavm.tooling.TeaVMTargetType;
import org.teavm.vm.TeaVMPhase;
import org.teavm.vm.TeaVMProgressFeedback;
import org.teavm.vm.TeaVMProgressListener;
class TeaVMBuild { class TeaVMBuild {
private final CompileContext context; private final CompileContext context;
@ -123,6 +127,7 @@ class TeaVMBuild {
buildStrategy.setMainClass(config.getMainClass()); buildStrategy.setMainClass(config.getMainClass());
buildStrategy.setTargetType(config.getTargetType()); buildStrategy.setTargetType(config.getTargetType());
buildStrategy.setTargetDirectory(config.getTargetDirectory()); buildStrategy.setTargetDirectory(config.getTargetDirectory());
buildStrategy.setProgressListener(createProgressListener(context));
TeaVMBuildResult buildResult = buildStrategy.build(); TeaVMBuildResult buildResult = buildStrategy.build();
if (!buildResult.isErrorOccurred() && buildResult.getProblems().getSevereProblems().isEmpty()) { if (!buildResult.isErrorOccurred() && buildResult.getProblems().getSevereProblems().isEmpty()) {
@ -134,6 +139,46 @@ class TeaVMBuild {
return true; 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) { private void reportProblems(ProblemProvider problemProvider, CallGraph callGraph) {
for (Problem problem : problemProvider.getProblems()) { for (Problem problem : problemProvider.getProblems()) {
BuildMessage.Kind kind; BuildMessage.Kind kind;

View File

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