Skip to content

Commit

Permalink
add print progress
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux committed Jun 15, 2018
1 parent 6fdeeca commit 66266fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 37 deletions.
43 changes: 11 additions & 32 deletions src/main/java/spoon/support/JavaOutputProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public void init() {
*/
public void createJavaFile(CtType<?> element) {
Path typePath = getElementPath(element);
getEnvironment().debugMessage("printing " + element.getQualifiedName() + " to " + typePath);

// we only create a file for top-level classes
if (!element.isTopLevel()) {
Expand All @@ -133,29 +132,26 @@ public void createJavaFile(CtType<?> element) {

printer.calculate(cu, toBePrinted);

PrintStream stream = null;

// print type
try {
File file = typePath.toFile();
file.createNewFile();
if (!printedFiles.contains(file)) {
printedFiles.add(file);
}
stream = new PrintStream(file);
stream.print(printer.getResult());
for (CtType<?> t : toBePrinted) {
lineNumberMappings.put(t.getQualifiedName(), printer.getLineNumberMapping());
// print type
try (PrintStream stream = new PrintStream(file)) {
stream.print(printer.getResult());
for (CtType<?> t : toBePrinted) {
lineNumberMappings.put(t.getQualifiedName(), printer.getLineNumberMapping());
}
}
stream.close();
} catch (IOException e) {
Launcher.LOGGER.error(e.getMessage(), e);
} finally {
if (stream != null) {
stream.close();
}
}

if (getEnvironment().getSpoonProgress() != null) {
getEnvironment().getSpoonProgress().step(SpoonProgress.Process.PRINT, element.getQualifiedName());
}
}

@Override
Expand All @@ -175,9 +171,6 @@ public void process(CtNamedElement nameElement) {
} else if (nameElement instanceof CtModule) {
createModuleFile((CtModule) nameElement);
}
if (getEnvironment().getSpoonProgress() != null) {
getEnvironment().getSpoonProgress().step(SpoonProgress.Process.PRINT, nameElement.getSimpleName());
}
}

private void createPackageFile(CtPackage pack) {
Expand All @@ -186,17 +179,10 @@ private void createPackageFile(CtPackage pack) {
if (!printedFiles.contains(packageAnnot)) {
printedFiles.add(packageAnnot);
}
PrintStream stream = null;
try {
stream = new PrintStream(packageAnnot);
try (PrintStream stream = new PrintStream(packageAnnot)) {
stream.println(printer.printPackageInfo(pack));
stream.close();
} catch (FileNotFoundException e) {
Launcher.LOGGER.error(e.getMessage(), e);
} finally {
if (stream != null) {
stream.close();
}
}
}

Expand All @@ -206,17 +192,10 @@ private void createModuleFile(CtModule module) {
if (!printedFiles.contains(moduleFile)) {
printedFiles.add(moduleFile);
}
PrintStream stream = null;
try {
stream = new PrintStream(moduleFile);
try (PrintStream stream = new PrintStream(moduleFile)) {
stream.println(printer.printModuleInfo(module));
stream.close();
} catch (FileNotFoundException e) {
Launcher.LOGGER.error(e.getMessage(), e);
} finally {
if (stream != null) {
stream.close();
}
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/spoon/support/compiler/ProgressLogger.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
package spoon.support.compiler;

import spoon.support.compiler.jdt.JDTTreeBuilder;
import spoon.support.StandardEnvironment;

import java.util.GregorianCalendar;

public class ProgressLogger implements SpoonProgress {
private long stepTimer;
private long timer;
private StandardEnvironment environment;

public ProgressLogger(StandardEnvironment environment) {
this.environment = environment;
}

@Override
public void start(Process process) {
System.out.println("Start " + process);
environment.debugMessage("Start " + process);
timer = getCurrentTimeInMillis();
stepTimer = timer;
}

@Override
public void step(Process process, String task, int taskId, int nbTask) {
JDTTreeBuilder.getLogger().trace("Step " + process + " " + taskId + "/" + nbTask + " " + task + " in " + (getCurrentTimeInMillis() - timer) + " ms");
environment.debugMessage("Step " + process + " " + taskId + "/" + nbTask + " " + task + " in " + (getCurrentTimeInMillis() - timer) + " ms");
timer = getCurrentTimeInMillis();
}

@Override
public void step(Process process, String task) {
JDTTreeBuilder.getLogger().trace("Step " + process + " " + task + " in " + (getCurrentTimeInMillis() - timer) + " ms");
environment.debugMessage("Step " + process + " " + task + " in " + (getCurrentTimeInMillis() - timer) + " ms");
timer = getCurrentTimeInMillis();
}

@Override
public void end(Process process) {
JDTTreeBuilder.getLogger().trace("End " + process + " in " + (getCurrentTimeInMillis() - stepTimer) + " ms");
environment.debugMessage("End " + process + " in " + (getCurrentTimeInMillis() - stepTimer) + " ms");
}

private long getCurrentTimeInMillis() {
Expand Down

0 comments on commit 66266fb

Please sign in to comment.