Commit b399a8a9 authored by Henri BOUVET's avatar Henri BOUVET Committed by Gerson SUNYE
Browse files

Fix #908 UseArraysAsList

parent 3db4ddec
......@@ -94,7 +94,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
-- thisModule.startsWith(),
thisModule.stringToString(),
thisModule.tooFewBranchesForASwitchStatement(),
-- #FIXME: thisModule.useArraysAsList(),
thisModule.useArraysAsList(),
thisModule.useIndexOfChar(),
thisModule.uselessStringValueOf(),
......@@ -1523,7 +1523,7 @@ rule MeasureUnnecessaryReturn(state : java!ReturnStatement) {
}
-- ----------------------------------------- UseArraysAsList ------------------------------------------------------------------------------------
-- #TODO: Add comment
--- A measure triggered when a For loop is used to fill a List with values of an array[] instead of using List = Arrays.asList(array[]).
rule MeasureUseArraysAsList(w: java!CompilationUnit) {
to
om: smm!ObservedMeasure (
......
......@@ -188,17 +188,35 @@ helper def: tooFewBranchesForASwitchStatement(): Set(smm!Measure) =
--- UseArraysAsList
helper def: useArraysAsList() : Set(smm!Measure) =
java!ForStatement.allInstances()
->select(st | st.body.oclIsTypeOf(java!Block))
->select(st | st.body.statements.size() = 1)
->select(st | st.body.statements.get(0).oclIsTypeOf(java!ExpressionStatement))
->select(st | st.body.statements.get(0).expression.oclIsTypeOf(java!MethodInvocation))
->select(st | st.body.statements.get(0).expression.method.name = 'add')
->select(st | not st.body.statements.get(0).expression.method.abstractTypeDeclaration.oclIsUndefined())
->select(st | st.body.statements.get(0).expression.method.abstractTypeDeclaration.name = 'Collection'
or st.body.statements.get(0).expression.method.abstractTypeDeclaration.name = 'List'
or st.body.statements.get(0).expression.method.abstractTypeDeclaration.name = 'ArrayList')
->select(st | st.body.statements.get(0).expression.expression.variable.initializer.type.type.type.type.name = 'ArrayList')
->select(st | not st.body.statements.get(0).expression.expression.oclIsUndefined())
->select(st | st.body.statements.get(0).expression.expression.oclIsTypeOf(java!SingleVariableAccess))
->select(st | not st.body.statements.get(0).expression.expression.variable.initializer.oclIsUndefined())
->select(st | st.body.statements.get(0).expression.expression.variable.initializer.oclIsTypeOf(java!ClassInstanceCreation))
->select(st | st.body.statements.get(0).expression.expression.variable.initializer.type.type.getTypeName() = 'ArrayList')
->select(st | st.body.statements.get(0).expression.arguments.size() = 1
and st.body.statements.get(0).expression.arguments.get(0).oclIsTypeOf(java!ArrayAccess))
->select(st | st.body.statements.get(0).expression.arguments.get(0).array.oclIsTypeOf(java!SingleVariableAccess))
->select(st | st.body.statements.get(0).expression.arguments.get(0).array.variable.oclIsTypeOf(java!SingleVariableDeclaration))
->select(st | st.body.statements.get(0).expression.arguments.get(0).array.variable.type.type.oclIsTypeOf(java!ArrayType))
->collect(st | thisModule.MeasureUseArraysAsList(st.originalCompilationUnit))
;
->collect(st | thisModule.MeasureUseArraysAsList(st.originalCompilationUnit));
helper context java!ParameterizedType def: getTypeName() : String =
self.type.type.name;
helper context java!TypeAccess def: getTypeName() : String =
self.type.name;
helper context java!ClassDeclaration def: getTypeName() : String =
self.name;
--- Use ArrayList Instead Of Vector
helper def: UseArrayListInsteadOfVector() : Set(smm!Measure) =
......@@ -231,4 +249,4 @@ helper def: uselessStringValueOf() : Set(smm!Measure) =
-> select (m | m.method.name='valueOf' )
-> select (m | m.refImmediateComposite().oclIsTypeOf(java!InfixExpression))
-> collect (m | thisModule.MeasureUselessStringValueOf(m))
;
\ No newline at end of file
;
Markdown is supported
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