Skip to content

Commit

Permalink
feat(checks): Disable AST checks. (closes #649)
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot authored and monperrus committed Jun 3, 2016
1 parent 5be4205 commit ffba095
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 3 deletions.
4 changes: 4 additions & 0 deletions doc/command_line.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,8 @@ Options :
[(-f|--generate-files) <generate-files>]
Only generate the given fully qualified java classes (separated by ':'
if multiple are given).

[-a|--disable-model-self-checks]
Disables checks made on the AST (hashcode violation, method's signature
violation and parent violation). Default: false.
```
9 changes: 9 additions & 0 deletions src/main/java/spoon/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ protected static JSAP defineArgs() {
opt2.setRequired(false);
jsap.registerParameter(opt2);

// Disable checks.
sw1 = new Switch("disable-model-self-checks");
sw1.setShortFlag('a');
sw1.setLongFlag("disable-model-self-checks");
sw1.setHelp("Disables checks made on the AST (hashcode violation, method's signature violation and parent violation). Default: false.");
sw1.setDefault("false");
jsap.registerParameter(sw1);

return jsap;
} catch (JSAPException e) {
throw new SpoonException(e.getMessage(), e);
Expand Down Expand Up @@ -430,6 +438,7 @@ protected void processArguments() {
environment.setCommentEnabled(jsapActualArgs.getBoolean("enable-comments"));

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

if (getArguments().getString("generate-files") != null) {
setOutputFilter(getArguments().getString("generate-files").split(":"));
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/spoon/compiler/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import spoon.processing.Processor;
import spoon.processing.ProcessorProperties;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.factory.Factory;

import java.io.File;
Expand Down Expand Up @@ -334,4 +335,18 @@ void report(Processor<?> processor, Level level,
* Sets the compile argument.
*/
void setShouldCompile(boolean shouldCompile);

/**
* Checks if {@link spoon.reflect.visitor.AstParentConsistencyChecker},
* hashcode violation declared in CtElement#equals(CtElement) and
* method violation declared in {@link spoon.reflect.declaration.CtType#addMethod(CtMethod)}
* are active or not.
*/
boolean checksAreSkipped();

/**
* Enable or not checks on the AST. See {@link #checksAreSkipped()} to know all checks enabled.
* true means that no self checks are made.
*/
void setSelfChecks(boolean skip);
}
12 changes: 12 additions & 0 deletions src/main/java/spoon/support/StandardEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public class StandardEnvironment implements Serializable, Environment {

private boolean shouldCompile;

private boolean skipSelfChecks;

/**
* Creates a new environment with a <code>null</code> default file
* generator.
Expand Down Expand Up @@ -152,6 +154,16 @@ public void setShouldCompile(boolean shouldCompile) {
this.shouldCompile = shouldCompile;
}

@Override
public boolean checksAreSkipped() {
return skipSelfChecks;
}

@Override
public void setSelfChecks(boolean skip) {
skipSelfChecks = skip;
}

private Level toLevel(String level) {
if (level == null || level.isEmpty()) {
throw new SpoonException("Wrong level given at Spoon.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ public boolean build(JDTBuilder builder) {
}

private void checkModel() {
factory.getModel().getRootPackage().accept(new AstParentConsistencyChecker());
if (!factory.getEnvironment().checksAreSkipped()) {
factory.getModel().getRootPackage().accept(new AstParentConsistencyChecker());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public boolean equals(Object o) {
}
boolean ret = EqualsVisitor.equals(this, (CtElement) o);
// neat online testing of core Java contract
if (ret && this.hashCode() != o.hashCode()) {
if (ret && !factory.getEnvironment().checksAreSkipped() && this.hashCode() != o.hashCode()) {
throw new IllegalStateException("violation of equal/hashcode contract between \n" + getDeepRepresentation(this) + "\nand\n" + getDeepRepresentation((CtElement) o) + "\n");
}
return ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ public <M, C extends CtType<T>> C addMethod(CtMethod<M> method) {
methods.remove(m);
} else {
// checking contract signature implies equal
if (m.equals(method)) {
if (!factory.getEnvironment().checksAreSkipped() && m.equals(method)) {
throw new AssertionError("violation of core contract! different signature but same equal");
}
}
Expand Down

0 comments on commit ffba095

Please sign in to comment.