Skip to content

Commit

Permalink
HV-1180 added check of interface inheritance in group sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta authored and gsmet committed Dec 12, 2016
1 parent edb25d6 commit 5e5da72
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
Expand Up @@ -37,7 +37,7 @@
*/
public class GroupSequenceCheck extends AnnotationParametersAbstractCheck {

private Types typeUtils;
private final Types typeUtils;
private final ConstraintHelper constraintHelper;

public GroupSequenceCheck(AnnotationApiHelper annotationApiHelper, Types typeUtils, ConstraintHelper constraintHelper) {
Expand Down Expand Up @@ -121,6 +121,11 @@ else if ( !annotationApiHelper.isInterface( typeMirror ) ) {
*/
private ConstraintCheckIssue checkForCyclicDefinition(Set<TypeMirror> processedTypes, TypeMirror currentTypeMirror,
TypeElement originalElement, AnnotationMirror annotation) {
// if passed currentTypeMirror is not a class/interface than - we will not do anything about it, as
// such error was already processed by another part of code and if do not ignore them then Types#asElement will fail
if ( !TypeKind.DECLARED.equals( currentTypeMirror.getKind() ) ) {
return null;
}
if ( processedTypes.contains( currentTypeMirror ) ) {
if ( !redefinesDefaultGroupSequence( originalElement, currentTypeMirror ) ) {
return ConstraintCheckIssue.error( originalElement, annotation, "INVALID_GROUP_SEQUENCE_VALUE_CYCLIC_DEFINITION" );
Expand All @@ -131,9 +136,8 @@ private ConstraintCheckIssue checkForCyclicDefinition(Set<TypeMirror> processedT
}
else {
processedTypes.add( currentTypeMirror );

AnnotationMirror groupSequenceMirror = getGroupSequence( currentTypeMirror );
List<? extends AnnotationValue> annotationValue = annotationApiHelper.getAnnotationArrayValue( groupSequenceMirror, "value" );
// check if there is a @GroupSequence annotation on a currentTypeMirror and check it values if present
List<? extends AnnotationValue> annotationValue = annotationApiHelper.getAnnotationArrayValue( getGroupSequence( currentTypeMirror ), "value" );
if ( annotationValue != null ) {
for ( AnnotationValue value : annotationValue ) {
TypeMirror groupTypeMirror = (TypeMirror) value.getValue();
Expand All @@ -143,6 +147,13 @@ private ConstraintCheckIssue checkForCyclicDefinition(Set<TypeMirror> processedT
}
}
}
// check if currentTypeMirror extends any other interfaces and if so - check them if they are not in the sequence already
for ( TypeMirror extendedInterfaceTypeMirror : ( (TypeElement) typeUtils.asElement( currentTypeMirror ) ).getInterfaces() ) {
ConstraintCheckIssue issue = checkForCyclicDefinition( processedTypes, extendedInterfaceTypeMirror, originalElement, annotation );
if ( issue != null ) {
return issue;
}
}
}
return null;
}
Expand Down
Expand Up @@ -61,7 +61,9 @@ public void testInvalidGroupSequenceParameter() {
new DiagnosticExpectation( Kind.ERROR, 113 ),
new DiagnosticExpectation( Kind.ERROR, 128 ),
new DiagnosticExpectation( Kind.ERROR, 132 ),
new DiagnosticExpectation( Kind.ERROR, 136 )
new DiagnosticExpectation( Kind.ERROR, 136 ),
new DiagnosticExpectation( Kind.ERROR, 151 ),
new DiagnosticExpectation( Kind.ERROR, 174 )
);
}

Expand Down
Expand Up @@ -140,8 +140,6 @@ public interface GroupSequence3 {

/**
* Case 8: Cyclic definition due to group inheritance - incorrect
*
* XXX: currently, it does not throw an error
*/
public static class Case8 {
public interface Group1 extends Group2 {
Expand All @@ -153,6 +151,29 @@ public interface Group2 {
@GroupSequence(value = { Group1.class, Group2.class })
public interface GroupSequence1 {
}
}

/**
* Case 9: Cyclic definition due to group inheritance and group sequence inheritance - incorrect
*/
public static class Case9 {
public interface Group1 extends Group2 {
}

public interface Group2 {
}

@GroupSequence(value = Group1.class)
public interface GroupSequence1 {
}

@GroupSequence(value = GroupSequence1.class)
public interface GroupSequence2 {
}

@GroupSequence(value = { Group2.class, GroupSequence2.class })
public interface GroupSequence3 {
}

}
}

0 comments on commit 5e5da72

Please sign in to comment.