Skip to content

Instantly share code, notes, and snippets.

@aparnachaudhary
Last active August 29, 2015 13:57
Show Gist options
  • Save aparnachaudhary/9770673 to your computer and use it in GitHub Desktop.
Save aparnachaudhary/9770673 to your computer and use it in GitHub Desktop.
jqassistant JavaEE constraints

Constraints

JPA2

FieldAccess

Verifies that entities prefer field access over property access.

MATCH
  (entity:Jpa:Entity)-[:DECLARES]->(m:Method)-[:ANNOTATED_BY]-()-[:OF_TYPE]->(a:Type)
WHERE
  a.fqn IN ["javax.persistence.Id", "javax.persistence.EmbeddedId"]
RETURN
  entity.name AS InvalidEntity

PROS:

  • Only interesting fields are exposed to the outside world.

  • The state is well encapsulated.

  • You can declare getters and setters in interfaces, abstract classes or mapped superclasses, and override them in the concrete subclasses.

  • It is easier to define transient contract; cleaner to mark fields transient than the getters.

CONS:

  • No debugging possibility

EntityEquality

Verifies that entities override equals() and hashcode() methods.

MATCH
  (entity:Jpa:Entity)-[:DECLARES]->(m:Method)
WITH
  collect(m.signature) as methodSignatures, entity
RETURN
  DISTINCT entity.name AS Entity,
  CASE WHEN "boolean equals(java.lang.Object)" IN methodSignatures THEN true ELSE false END AS OverridesEquals,
  CASE WHEN "int hashCode()" IN methodSignatures THEN true ELSE false END AS OverridesHashCode

Constraint:

MATCH
  (entity:Jpa:Entity)-[:DECLARES]->(m:Method)
WITH
  DISTINCT entity.name AS InvalidEntity,
  CASE WHEN "boolean equals(java.lang.Object)" IN collect(m.signature) THEN true ELSE false END AS OverridesEquals,
  CASE WHEN "int hashCode()" IN collect(m.signature) THEN true ELSE false END AS OverridesHashCode
WHERE OverridesEquals = false or OverridesHashCode = false
RETURN InvalidEntity, OverridesEquals, OverridesHashCode
MATCH
  (entity:Jpa:Entity)-[:DECLARES]->(m:Method)
WITH
  entity, collect(m.signature) AS signatures
WHERE NOT "boolean equals(java.lang.Object)" IN signatures
OR NOT "int hashCode()" IN signatures
RETURN DISTINCT entity.name AS InvalidEntity
ORDER BY entity.name

PROS:

CONS:

JAX-RS

GetMustNotBeVoid

Verifies that REST GET method does not return void.

MATCH (m:Rest:GetResourceMethod)
WHERE m.SIGNATURE =~ "void.*"
RETURN DISTINCT m.NAME AS invalidGetResource

MultipleRequestMethodDesignators

Verifies that REST resource method does not have multiple request method designators.

MATCH (m:Rest:ResourceMethod)-[:ANNOTATED_BY]-()-[:OF_TYPE]->(a:Rest:RequestMethodDesignator)
WITH m, count(a.FQN) as verbCount, collect(a.FQN) as verbs
WHERE verbCount > 1
RETURN m.NAME AS method, verbs

EJB

TransactionalMethods

Avoid invoking @Transactional method from non-transactional one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment