Skip to content

Commit

Permalink
Fix: use a separate ContractExpressionTransformer for each kind of co…
Browse files Browse the repository at this point in the history
…ntract

This should fix a bug where previously Ensures and ThrowEnsures
annotations would share a same old ID counter, leading to crashes at
instrumentation time if old() was used in both kinds of contracts.
  • Loading branch information
nhatminhle committed Feb 1, 2016
1 parent 4737bbb commit c2070fe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ public ContractExpressionTransformer(DiagnosticManager diagnosticManager,
oldId = 0;
}

public void setAcceptOld(boolean acceptOld) {
this.acceptOld = acceptOld;
}

@Requires({
"currentBuffer != null",
"tokenizer != null",
Expand Down
24 changes: 16 additions & 8 deletions src/com/google/java/contract/core/apt/MethodContractCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
*/
@Invariant({
"diagnosticManager != null",
"transformer != null"
"preTransformer != null",
"postTransformer != null",
"postSignalTransformer != null"
})
public class MethodContractCreator extends ElementScanner {
/**
Expand All @@ -67,7 +69,6 @@ public PreMethodCreationTrait(
@Override
public boolean transform(List<String> code, List<Long> lineNumbers,
Object sourceInfo) {
transformer.setAcceptOld(false);
return super.transform(code, lineNumbers, sourceInfo);
}

Expand All @@ -93,7 +94,6 @@ public CommonPostMethodCreationTrait(
public boolean transform(List<String> code, List<Long> lineNumbers,
Object sourceInfo) {
int id = transformer.getNextOldId();
transformer.setAcceptOld(true);
boolean success = super.transform(code, lineNumbers, sourceInfo);

if (success) {
Expand Down Expand Up @@ -242,7 +242,9 @@ public List<String> getSourceExpressions() {
protected ContractMethodModel postMethod;
protected ContractMethodModel postSignalMethod;

protected ContractExpressionTransformer transformer;
protected ContractExpressionTransformer preTransformer;
protected ContractExpressionTransformer postTransformer;
protected ContractExpressionTransformer postSignalTransformer;

/**
* Constructs a new MethodContractCreator.
Expand All @@ -254,7 +256,12 @@ public MethodContractCreator(DiagnosticManager diagnosticManager) {
preMethod = null;
postMethod = null;
postSignalMethod = null;
transformer = new ContractExpressionTransformer(diagnosticManager, true);
preTransformer =
new ContractExpressionTransformer(diagnosticManager, false);
postTransformer =
new ContractExpressionTransformer(diagnosticManager, true);
postSignalTransformer =
new ContractExpressionTransformer(diagnosticManager, true);
}

@Override
Expand All @@ -271,14 +278,15 @@ public void visitContractAnnotation(ContractAnnotationModel annotation) {
List<String> code = annotation.getValues();

if (annotation.getKind().equals(ElementKind.REQUIRES)) {
PreMethodCreationTrait trait = new PreMethodCreationTrait(transformer);
PreMethodCreationTrait trait = new PreMethodCreationTrait(preTransformer);
preMethod = createContractMethods(trait, preMethod, annotation);
} else if (annotation.getKind().equals(ElementKind.ENSURES)) {
PostMethodCreationTrait trait = new PostMethodCreationTrait(transformer);
PostMethodCreationTrait trait =
new PostMethodCreationTrait(postTransformer);
postMethod = createContractMethods(trait, postMethod, annotation);
} else if (annotation.getKind().equals(ElementKind.THROW_ENSURES)) {
PostSignalMethodCreationTrait trait =
new PostSignalMethodCreationTrait(transformer);
new PostSignalMethodCreationTrait(postSignalTransformer);
postSignalMethod = createContractMethods(trait, postSignalMethod,
annotation);
} else {
Expand Down

0 comments on commit c2070fe

Please sign in to comment.