Skip to content

Commit

Permalink
feat: Enable comments by default (#2065)
Browse files Browse the repository at this point in the history
  • Loading branch information
surli authored and monperrus committed Jun 19, 2018
1 parent b4c4649 commit fb9baae
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 9 deletions.
23 changes: 21 additions & 2 deletions src/main/java/spoon/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,14 @@ protected static JSAP defineArgs() {
sw1 = new Switch("enable-comments");
sw1.setShortFlag('c');
sw1.setLongFlag("enable-comments");
sw1.setHelp("Adds all code comments in the Spoon AST (Javadoc, line-based comments), rewrites them when pretty-printing.");
sw1.setHelp("[DEPRECATED] Adds all code comments in the Spoon AST (Javadoc, line-based comments), rewrites them when pretty-printing. (deprecated: by default, the comments are enabled.)");
sw1.setDefault("false");
jsap.registerParameter(sw1);

// Disable generation of javadoc.
sw1 = new Switch("disable-comments");
sw1.setLongFlag("disable-comments");
sw1.setHelp("Disable the parsing of comments in Spoon.");
sw1.setDefault("false");
jsap.registerParameter(sw1);

Expand Down Expand Up @@ -425,7 +432,19 @@ protected void processArguments() {
environment.setTabulationSize(jsapActualArgs.getInt("tabsize"));
environment.useTabulations(jsapActualArgs.getBoolean("tabs"));
environment.setCopyResources(!jsapActualArgs.getBoolean("no-copy-resources"));
environment.setCommentEnabled(jsapActualArgs.getBoolean("enable-comments"));

if (jsapActualArgs.getBoolean("enable-comments")) {
Launcher.LOGGER.warn("The option --enable-comments (-c) is deprecated as it is now the default behaviour in Spoon.");
} else {
Launcher.LOGGER.warn("Spoon now parse by default the comments. Consider using the option --disable-comments if you want the old behaviour.");
}

if (jsapActualArgs.getBoolean("disable-comments")) {
environment.setCommentEnabled(false);
} else {
environment.setCommentEnabled(true);
}

environment.setShouldCompile(jsapActualArgs.getBoolean("compile"));
environment.setSelfChecks(jsapActualArgs.getBoolean("disable-model-self-checks"));

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/support/StandardEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class StandardEnvironment implements Serializable, Environment {

private boolean copyResources = true;

private boolean enableComments = false;
private boolean enableComments = true;

private Logger logger = Launcher.LOGGER;

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/spoon/testing/utils/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public static Factory createFactory() {

/** Utility method for testing: creates the model of `packageName` from src/test/java and returns the CtType corresponding to `className` */
public static <T extends CtType<?>> T build(String packageName, String className) throws Exception {
SpoonModelBuilder comp = new Launcher().createCompiler();
Launcher launcher = new Launcher();
launcher.getEnvironment().setCommentEnabled(false); // we don't want to parse the comments for equals
SpoonModelBuilder comp = launcher.createCompiler();
comp.addInputSources(SpoonResourceHelper.resources("./src/test/java/" + packageName.replace('.', '/') + "/" + className + ".java"));
comp.build();
return comp.getFactory().Package().get(packageName).getType(className);
Expand All @@ -61,7 +63,9 @@ public Factory createFactory() {

/** Utility method for testing: creates the model of the given `classesToBuild` from src/test/java and returns the factory */
public static Factory build(Class<?>... classesToBuild) throws Exception {
SpoonModelBuilder comp = new Launcher().createCompiler();
Launcher launcher = new Launcher();
launcher.getEnvironment().setCommentEnabled(false);
SpoonModelBuilder comp = launcher.createCompiler();
for (Class<?> classToBuild : classesToBuild) {
comp.addInputSources(SpoonResourceHelper.resources("./src/test/java/" + classToBuild.getName().replace('.', '/') + ".java"));
}
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/spoon/reflect/visitor/CtScannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,10 @@ public void exit(CtElement o) {
assertEquals(0, counter.nObject);
// this is a coarse-grain check to see if the scanner changes
// no more exec ref in paramref
assertEquals(3616, counter.nElement);
assertEquals(2396, counter.nEnter);
assertEquals(2396, counter.nExit);
// also takes into account the comments
assertEquals(3655, counter.nElement);
assertEquals(2435, counter.nEnter);
assertEquals(2435, counter.nExit);

// contract: all AST nodes which are part of Collection or Map are visited first by method "scan(Collection|Map)" and then by method "scan(CtElement)"
Counter counter2 = new Counter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public void testAnnotationPrintAnnotation() throws Exception {
Launcher launcher = new Launcher();
launcher.addInputResource("src/test/resources/printer-test/spoon/test/AnnotationSpecTest.java");
launcher.getEnvironment().setNoClasspath(true);
launcher.getEnvironment().setCommentEnabled(false); // avoid getting the comment for the equals
launcher.buildModel();

assertEquals(strCtClassOracle,
Expand Down
1 change: 1 addition & 0 deletions src/test/java/spoon/test/api/NoClasspathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void test() throws Exception {
Launcher spoon = new Launcher();
spoon.getEnvironment().setNoClasspath(true);
spoon.getEnvironment().setLevel("OFF");
spoon.getEnvironment().setCommentEnabled(false); // avoid getting the comments for the equals
spoon.addInputResource("./src/test/resources/spoon/test/noclasspath/fields");
spoon.getEnvironment().setSourceOutputDirectory(new File("target/spooned/apitest"));
spoon.run();
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/spoon/test/javadoc/JavaDocTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.Test;
import spoon.Launcher;
import spoon.OutputType;
import spoon.SpoonAPI;
import spoon.reflect.code.CtComment;
import spoon.reflect.declaration.CtClass;
Expand Down Expand Up @@ -61,7 +62,7 @@ public void testJavadocNotPresentInAST() throws Exception {
Launcher launcher = new Launcher();
launcher.getEnvironment().setCommentEnabled(false);
launcher.getEnvironment().setNoClasspath(true);
launcher.setArgs(new String[] {"--output-type", "nooutput" });
launcher.getEnvironment().setOutputType(OutputType.NO_OUTPUT);
launcher.addInputResource("./src/test/java/spoon/test/javadoc/testclasses/");
launcher.run();

Expand Down
1 change: 1 addition & 0 deletions src/test/java/spoon/test/main/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ public void testTest() throws Exception {
"-i", "src/test/java",
"-o", "target/spooned",
"--noclasspath",
"--disable-comments",
"--compliance", "8",
"--level", "OFF"
});
Expand Down
1 change: 1 addition & 0 deletions src/test/java/spoon/test/prettyprinter/PrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public void testPrintingOfOrphanFieldReference() throws Exception {
public void testPrinterTokenListener() throws Exception {
Launcher spoon = new Launcher();
Factory factory = spoon.createFactory();
factory.getEnvironment().setCommentEnabled(false);
spoon.createCompiler(
factory,
SpoonResourceHelper
Expand Down

0 comments on commit fb9baae

Please sign in to comment.