Skip to content

Commit 636a0d6

Browse files
marko-bekhtagsmet
authored andcommitted
HV-1377 Adding a check if the type was already processed
1 parent 657cb07 commit 636a0d6

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

annotation-processor/src/main/java/org/hibernate/validator/ap/ClassVisitor.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.annotation.processing.ProcessingEnvironment;
1212
import javax.lang.model.element.Element;
1313
import javax.lang.model.element.ExecutableElement;
14+
import javax.lang.model.element.Name;
1415
import javax.lang.model.element.TypeElement;
1516
import javax.lang.model.util.Elements;
1617
import javax.tools.Diagnostic;
@@ -33,6 +34,8 @@
3334
*/
3435
public class ClassVisitor extends AbstractElementVisitor<Void, Void> {
3536

37+
private final Set<Name> processedTypes;
38+
3639
private final ClassCheckFactory factory;
3740

3841
private final Elements elementUtils;
@@ -54,6 +57,8 @@ public ClassVisitor(
5457
)
5558
)
5659
);
60+
61+
this.processedTypes = CollectionHelper.newHashSet();
5762
}
5863

5964
/**
@@ -101,8 +106,12 @@ public Void visitExecutableAsMethod(ExecutableElement element, Void aVoid) {
101106
* @param typeElement inner elements of which you want to visit
102107
*/
103108
private void visitAllMyElements(TypeElement typeElement) {
104-
for ( Element element : elementUtils.getAllMembers( typeElement ) ) {
105-
visit( element );
109+
Name qualifiedName = typeElement.getQualifiedName();
110+
if ( !processedTypes.contains( qualifiedName ) ) {
111+
processedTypes.add( qualifiedName );
112+
for ( Element element : elementUtils.getAllMembers( typeElement ) ) {
113+
visit( element );
114+
}
106115
}
107116
}
108117

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.ap;
8+
9+
import static org.testng.Assert.assertTrue;
10+
11+
import org.hibernate.validator.ap.testmodel.circular.CircularProperty;
12+
import org.hibernate.validator.ap.testmodel.circular.CircularPropertyImpl;
13+
import org.testng.annotations.Test;
14+
15+
/**
16+
* Tests that in case of circular nested types there's no infinite loop during analysis.
17+
*
18+
* @author Marko Bekhta
19+
*/
20+
public class CircularNestedTypesTest extends ConstraintValidationProcessorTestBase {
21+
22+
@Test
23+
public void testNoInfiniteLoop() {
24+
boolean compilationResult =
25+
compilerHelper.compile( new ConstraintValidationProcessor(), diagnostics,
26+
compilerHelper.getSourceFile( CircularPropertyImpl.class ),
27+
compilerHelper.getSourceFile( CircularProperty.class )
28+
);
29+
30+
assertTrue( compilationResult );
31+
}
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.ap.testmodel.circular;
8+
9+
/**
10+
* @author Marko Bekhta
11+
*/
12+
public interface CircularProperty {
13+
14+
void doSomething();
15+
16+
interface ImprovedCircularProperty extends CircularProperty {
17+
18+
void maybeDoSomething();
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.ap.testmodel.circular;
8+
9+
/**
10+
* @author Marko Bekhta
11+
*/
12+
public class CircularPropertyImpl implements CircularProperty {
13+
14+
@Override
15+
public void doSomething() {
16+
//nothing here
17+
}
18+
}

0 commit comments

Comments
 (0)