From 327a373297118df6c62ea7d8cb924bfa6117b78b Mon Sep 17 00:00:00 2001 From: Jacek Furmankiewicz Date: Mon, 6 Jul 2015 18:27:04 -0500 Subject: [PATCH] 0.5.0: bug fix to handle recursive/circular references --- README.md | 4 ++-- gradle.properties | 2 +- src/main/java/org/immutizer4j/Immutizer.java | 7 ++++++- src/test/java/org/immutizer4j/test/BaseTests.java | 14 ++++++++++++++ .../test/sample/RegexPatternReferencePojo.java | 14 ++++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/immutizer4j/test/sample/RegexPatternReferencePojo.java diff --git a/README.md b/README.md index 6c763f5..a9816e1 100644 --- a/README.md +++ b/README.md @@ -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 @@ -149,7 +149,7 @@ The artifacts for this library are published to the popular Bintray JCenter Mave org.immutizer4j immutizer4j - 0.4.0 + 0.5.0 diff --git a/gradle.properties b/gradle.properties index b8aa4e0..f73f294 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # speed up Gradle org.gradle.daemon=true -version = 0.4.0 +version = 0.5.0 group = org.immutizer4j diff --git a/src/main/java/org/immutizer4j/Immutizer.java b/src/main/java/org/immutizer4j/Immutizer.java index 1c30130..a52584b 100644 --- a/src/main/java/org/immutizer4j/Immutizer.java +++ b/src/main/java/org/immutizer4j/Immutizer.java @@ -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); } } @@ -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 diff --git a/src/test/java/org/immutizer4j/test/BaseTests.java b/src/test/java/org/immutizer4j/test/BaseTests.java index 072ddbc..2b4cfb9 100644 --- a/src/test/java/org/immutizer4j/test/BaseTests.java +++ b/src/test/java/org/immutizer4j/test/BaseTests.java @@ -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")); + } } diff --git a/src/test/java/org/immutizer4j/test/sample/RegexPatternReferencePojo.java b/src/test/java/org/immutizer4j/test/sample/RegexPatternReferencePojo.java new file mode 100644 index 0000000..707c3eb --- /dev/null +++ b/src/test/java/org/immutizer4j/test/sample/RegexPatternReferencePojo.java @@ -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])+"); +}