/** * Returns true if this message or one of its sub-messages refers back to this message. */ @Requires(Resolved.class) public boolean isCyclic() { if (isCyclic != null) { return isCyclic; } return isCyclic = checkCyclic(Sets.<MessageType>newHashSet(), this); }
/** * Returns true if this message or one of its sub-messages refers back to this message. */ @Requires(Resolved.class) public boolean isCyclic() { if (isCyclic != null) { return isCyclic; } return isCyclic = checkCyclic(Sets.<MessageType>newHashSet(), this); }
private boolean checkCyclic(Set<MessageType> visited, MessageType message) { if (!visited.add(this)) { return false; } for (Field field : fields) { TypeRef type = field.getType(); if (type.getKind() == FieldDescriptorProto.Type.TYPE_MESSAGE) { if (message == type.getMessageType()) { return true; } if (type.getMessageType().checkCyclic(visited, message)) { // If there is a cycle to message via this, then this is also cyclic. That's the nature // of a cycle. return true; } } } return false; }
private boolean checkCyclic(Set<MessageType> visited, MessageType message) { if (!visited.add(this)) { return false; } for (Field field : fields) { TypeRef type = field.getType(); if (type.getKind() == FieldDescriptorProto.Type.TYPE_MESSAGE) { if (message == type.getMessageType()) { return true; } if (type.getMessageType().checkCyclic(visited, message)) { // If there is a cycle to message via this, then this is also cyclic. That's the nature // of a cycle. return true; } } } return false; }