Commit 09fae6e7 authored by Roxane Kang Maréchal's avatar Roxane Kang Maréchal
Browse files

Added helpers to detect elements that violate the rules (defined in a generic maner)

parent 07df3972
......@@ -44,7 +44,7 @@
<mapEntry key="OUT" value="smm"/>
</mapAttribute>
<mapAttribute key="Path">
<mapEntry key="IN" value="/projet-2019/input/avoid-field-name-matching-method-name.xmi"/>
<mapEntry key="IN" value="/projet-2019/input/comment-required.xmi"/>
<mapEntry key="OUT" value="/projet-2019/output/test.xmi"/>
<mapEntry key="java" value="uri:http://www.eclipse.org/MoDisco/Java/0.2.incubation/java"/>
<mapEntry key="smm" value="uri:http://www.eclipse.org/MoDisco/SMM/1.0.Beta2/smm"/>
......
......@@ -368,7 +368,7 @@ rule MesureAvoidFieldNameMatchingMethodName(class : java!ClassDeclaration, metho
}
-- Creates a measure instance for each node that violates the rule : comment required
rule MeasureCommentRequired(element: java!ASTNode, violatedProperty: String) {
rule MeasureCommentRequired(element: java!NamedElement, violatedProperties: Set(String)) {
to
om: smm!ObservedMeasure (
measure <- commentRequired,
......@@ -379,10 +379,11 @@ rule MeasureCommentRequired(element: java!ASTNode, violatedProperty: String) {
shortDescription <- 'Denotes whether comments are required (or unwanted) for specific language elements.'
),
measurement: smm!DirectMeasurement (
error <- 'Violated the property "' + violatedProperty + '"=Required in Class ' + element.originalCompilationUnit.name
error <- 'Violated the properties -> ' + violatedProperties + ' in Class: ' + element.originalCompilationUnit.name + ' -> ' + element.oclType().name + ': ' + element.modifier.visibility + ' ' + element.name
)
do {
commentRequired;
}
}
\ No newline at end of file
}
library documentation;
-- Returs a set of measures for each element that violates the rule : comment required
helper def: commentRequired() : Set(smm!Measure) =
java!ASTNode.allInstances()
java!NamedElement.allInstances()
-> select (node | not node.oclIsTypeOf(java!Package) and node.proxy = false)
-> select (node | thisModule.commentRequiredPropertyCustomization().getKeys().contains(node.oclType().name))
-> select (node | thisModule.violatesCommentRequired(node))
-> collect (node | thisModule.MeasureCommentRequired(node))
-> collect (node | thisModule.MeasureCommentRequired(node, thisModule.violatedCommentRequiredProperty(node)))
;
helper def: violatesCommentRequired(node: java!ASTNode): Boolean =
false
-- Returns true if the given node violates any of the "comment required" rule properties
helper def: violatesCommentRequired(node: java!NamedElement): Boolean =
thisModule.commentRequiredPropertyCustomization().get(node.oclType().name)
->exists(propertyTuple | thisModule.violatesCommentRequiredPropertyRequirements(node, propertyTuple))
;
helper def: commentRequiredPropertyCustomization() : Set(Map(String, String)) = Set{
Map{('methodWithOverrideCommentRequirement','Ignored')},
Map{('accessorCommentRequirement', 'Ignored')},
Map{('headerCommentRequirement', 'Required')},
Map{('fieldCommentRequirement', 'Required')},
Map{('publicMethodCommentRequirement', 'Required')},
Map{('protectedMethodCommentRequirement', 'Required')},
Map{('enumCommentRequirement', 'Required')},
Map{('serialVersionUIDCommentRequired', 'Ignored')},
Map{('serialPersistentFieldsCommentRequired', 'Ignored')}
helper def: violatedCommentRequiredProperty(node: java!NamedElement): String =
thisModule.commentRequiredPropertyCustomization().get(node.oclType().name)
->select(propertyTuple | thisModule.violatesCommentRequiredPropertyRequirements(node, propertyTuple))
->iterate (tuple; propertyNames: String = '' |
propertyNames.concat(tuple.property+';')
)
;
-- Returns true if the given property tuple applies to the given node, false otherwise
helper def: violatesCommentRequiredPropertyRequirements(
node: java!NamedElement,
tuple: TupleType(property:String, required:Boolean, modifier: String, annotation: String)): Boolean =
-- Check whether the given property tuple applies to the given node
tuple.required
.and(not thisModule.hasComments(node))
.and(
if (tuple.modifier.isEmpty()) then true
else
if (tuple.modifier = node.modifier.visibility.name) then true else false endif
endif)
;
-- Returns true if the node has comments, false otherwise
helper def: hasComments(node: java!NamedElement): Boolean =
not node.comments.isEmpty()
;
-- Contains the default configuration for the "Comment required" rule
-- Can add, delete or modify properties here to customize them just like in PMD
helper def: commentRequiredPropertyCustomization() :
Map(String, Set(TupleType(property:String, required:Boolean, modifier: String, annotation: String))) = Map {
-- Configure comment requirement properties for method declarations
('MethodDeclaration',
Set {
Tuple{property='methodWithOverrideCommentRequirement', required=false, modifier='', annotation='override'},
Tuple{property='publicMethodCommentRequirement', required=true, modifier='public', annotation='override'},
Tuple{property='protectedMethodCommentRequirement', required=true, modifier='protected', annotation=''}
-- Tuple{property = 'accessorCommentRequirement', value='Ignored'},
-- Tuple{property = 'headerCommentRequirement', value='Required'},
}
),
-- Configure field requirement properties for field declarations
('FieldDeclaration',
Set{
Tuple{property='fieldCommentRequirement', required=true, modifier='', annotation=''}
}
)
-- TODO : find corresponding type for the following
-- (java!ASTNode, Map{('enumCommentRequirement', 'Required')}),
-- (java!ASTNode, Map{('serialVersionUIDCommentRequired', 'Ignored')}),
-- (java!ASTNode, Map{('serialPersistentFieldsCommentRequired', 'Ignored')})
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment