发现没有@Transaction的方法?

时间:2015-09-30 15:17:32

标签: java jqassistant

我正在努力实现我想实施的验证。我想验证以update开头的服务中的每个方法都必须具有@Transactional注释。到目前为止,我已经创建了一个概念,它提供了我的服务类中以update(例如updateInvoice)开头的方法。但我不知道如何构建一个约束来选择没有@Transaction注释的方法。

2 个答案:

答案 0 :(得分:3)

我建议定义一些代表你的关键元素的概念来定义它们的约束,即

您的服务:

<concept id="service:ServiceClass">
  <description>Adds a label "Service" to every class annotated by "@com.mycompany.services.Service"</description>
  <cypher><![CDATA[
  MATCH
    (service:Type:Class)-[:ANNOTATED_BY]->()-[:OF_TYPE]->(serviceAnnotationType)
  SET
    service:Service
  WHERE
    serviceAnnotationType.fqn = "com.mycompany.services.Service"
  RETURN
    service   
  ]]>
  </cypher>
</concept>

您的交易方法:

<concept id="service:TransactMethod">
  <description>Adds a label "Transact" to every method annotated by "@com.mycompany.services.Transact"</description>
  <cypher><![CDATA[
  MATCH
    (method:Method)-[:ANNOTATED_BY]->()-[:OF_TYPE]->(transactAnnotationType)
  SET
    method:Transact
  WHERE
    transactAnnotationType.fqn = "com.mycompany.services.Transact"
  RETURN
    method   
  ]]>
  </cypher>
</concept>

你的约束:

<constraint id="service:AllUpdateMethodsMustBeTransacted">
  <requiresConcept refId="service:ServiceClass" />
  <requiresConcept refId="service:TransactMethod" />
  <description>All update methods must be transacted</description>
  <cypher><![CDATA[
  MATCH
    (service:Service:Class)-[:DECLARES]->(updateMethod:Method)
  WHERE
    updateMethod.name =~ "update.*" // even this could be extracted to a concept
    and not updateMethod:Transact
  RETURN
    updateMethod
  ]]>
  </cypher>
</constraint >

这种方法有几个优点:

  • 您现在获得了更多规则,但每个规则的可读性都更高(特别是constaint),因为您使用的是为设计定义的术语
  • 您很可能需要这些概念&#34;服务&#34;和#34; Transact&#34;对于其他约束 - 现在只使用标签
  • 如果您正在创建Maven网站,则会收到有关您设计中所有概念的报告(即目前存在的服务实施)

答案 1 :(得分:1)

以下似乎有效:

match
   (aType:Type:Class)-[:ANNOTATED_BY]->()-[:OF_TYPE]->(anAnnotationType:Type),
   (aType:Type)-[:DECLARES]->(aMethod:Method)
optional match
   (aMethod)-[:ANNOTATED_BY]->()-[:OF_TYPE]->(tType:Type)
with anAnnotationType, aMethod, tType
where
    anAnnotationType.fqn = "com.mycompany.services.Service" 
    and aMethod.name =~ "update.*"
    and ((tType is null) or not (tType.fqn = "com.mycompany.services.Transact"))
return
    aMethod.name, tType