Skip to content

Commit

Permalink
0.5.0: bug fix to handle recursive/circular references
Browse files Browse the repository at this point in the history
  • Loading branch information
jacek99 committed Jul 6, 2015
1 parent db1670d commit 327a373
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ The artifacts for this library are published to the popular Bintray JCenter Mave
jcenter()
}

compile "org.immutizer4j:immutizer4j:0.4.0"
compile "org.immutizer4j:immutizer4j:0.5.0"


## Maven
Expand All @@ -149,7 +149,7 @@ The artifacts for this library are published to the popular Bintray JCenter Mave
<dependency>
<groupId>org.immutizer4j</groupId>
<artifactId>immutizer4j</artifactId>
<version>0.4.0</version>
<version>0.5.0</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# speed up Gradle
org.gradle.daemon=true

version = 0.4.0
version = 0.5.0
group = org.immutizer4j
7 changes: 6 additions & 1 deletion src/main/java/org/immutizer4j/Immutizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private ValidationResult validateField(Field field, ValidationResult result) {
Class<?> actualType = getActualType(field,result);
result = validateIfGenericsReference(field,actualType,result);

if (!isSafeType(actualType)) {
if (!isSafeType(actualType) && !isRecursive(field)) {
result = validateType(actualType, result);
}
}
Expand Down Expand Up @@ -223,6 +223,11 @@ private boolean isSafeType(Class<?> type) {
return false;
}

// checks if a field refers recursively to the containing/declaring class
private boolean isRecursive(Field field) {
return field.getType().equals(field.getDeclaringClass());
}

// standard handler for reporting errors, returns a new immutable ValidationResult instance
private ValidationResult addError(Field field, ViolationType violationType, ValidationResult result) {
// log it as long as it is not marked as @ImmutizerIgnore
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/immutizer4j/test/BaseTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,18 @@ public void testReferencesToAllFieldsInAnObjectHierarchy() {
assertTrue(result.toString(),
result.toString().contains("org.immutizer4j.test.sample.ParentPojo.paretMutableInt : NON_FINAL_FIELD"));
}

@Test
public void testRegexPatternReference() {
ValidationResult result = defaultImmutizer.getValidationResult(RegexPatternReferencePojo.class);

assertEquals(false,result.isValid());
// there are many errors in there, which is OK
assertEquals(20, result.getErrors().size());

// this is the one we care about, it used to cause StackOverFlow error due to circular reference
// now it should be reported only once
assertTrue(result.toString(),
result.toString().contains("java.util.regex.Pattern$Node.next : NON_FINAL_FIELD"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.immutizer4j.test.sample;

import lombok.Value;

import java.util.regex.Pattern;

/**
* A POJO with a regex matcher object, it caused a StackOverflowError first time
* Used for testing that it does not happen any more
*/
@Value
public class RegexPatternReferencePojo {
private Pattern pattern = Pattern.compile("([A-Z])+");
}

0 comments on commit 327a373

Please sign in to comment.