Skip to content

Commit

Permalink
[lang] Add warning for unnecessary use of a capacity that is implemen…
Browse files Browse the repository at this point in the history
…ted by the current skill.

close #733

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Sep 2, 2017
1 parent 9a29336 commit e7b551b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
Expand Up @@ -55,6 +55,7 @@ private Messages() {
public static String SARLValidator_2;
public static String SARLValidator_20;
public static String SARLValidator_21;
public static String SARLValidator_22;
public static String SARLValidator_25;
public static String SARLValidator_28;
public static String SARLValidator_29;
Expand Down
Expand Up @@ -1975,19 +1975,31 @@ public void checkUnusedCapacities(SarlCapacityUses uses) {
}
}

final boolean isSkill = container instanceof SarlSkill;
int index = 0;
for (final JvmTypeReference capacity : uses.getCapacities()) {
final String fieldName = Utils.createNameForHiddenCapacityImplementationAttribute(capacity.getIdentifier());
final String operationName = Utils.createNameForHiddenCapacityImplementationCallingMethodFromFieldName(fieldName);
final JvmOperation operation = importedFeatures.get(operationName);
if (operation != null && !isLocallyUsed(operation, container)) {
final LightweightTypeReference lreference = toLightweightTypeReference(capacity);
if (isSkill && lreference.isAssignableFrom(jvmContainer)) {
addIssue(MessageFormat.format(
Messages.SARLValidator_78,
Messages.SARLValidator_22,
capacity.getSimpleName()),
uses,
SARL_CAPACITY_USES__CAPACITIES,
index, UNUSED_AGENT_CAPACITY,
capacity.getSimpleName());
} else {
final String fieldName = Utils.createNameForHiddenCapacityImplementationAttribute(capacity.getIdentifier());
final String operationName = Utils.createNameForHiddenCapacityImplementationCallingMethodFromFieldName(fieldName);
final JvmOperation operation = importedFeatures.get(operationName);
if (operation != null && !isLocallyUsed(operation, container)) {
addIssue(MessageFormat.format(
Messages.SARLValidator_78,
capacity.getSimpleName()),
uses,
SARL_CAPACITY_USES__CAPACITIES,
index, UNUSED_AGENT_CAPACITY,
capacity.getSimpleName());
}
}
++index;
}
Expand Down
Expand Up @@ -13,6 +13,7 @@ SARLValidator_19=Forbidden reference to not final field {0} from a default value
SARLValidator_2=Invalid use of the read-only occurrence. You cannot use occurrence in the left-side of an assignment operator.
SARLValidator_20=Undefined type for the formal parameter {0}
SARLValidator_21=Undefined type for the default value of the formal parameter {0}
SARLValidator_22=Unnecessary use of the capacity ''{0}'' because it is implemented by the current skill.
SARLValidator_25=Nested classes must be static
SARLValidator_28=Nested agents are not allowed inside {0}.
SARLValidator_29=Nested behaviors are not allowed inside {0}.
Expand Down
Expand Up @@ -2406,6 +2406,62 @@ public void agentUnsuedCapacity_2() throws Exception {
assertParameterNames(action.getParameters());
}

@Test
public void agentUnsuedCapacity_3() throws Exception {
SarlScript mas = file(multilineString(
"capacity C1 {",
" def myfct",
"}",
"capacity C2 extends C1 {",
" def myfct2",
"}",
"skill S1 implements C2 {",
" uses C2",
" def myfct {}",
" def myfct2 {}",
" def myaction {",
" myfct",
" }",
"}"
));
List<Issue> issues = issues(mas);
assertWarning(
issues,
mas,
SarlPackage.eINSTANCE.getSarlCapacityUses(),
IssueCodes.UNUSED_AGENT_CAPACITY,
"Unnecessary use of the capacity 'C2'");
assertNoMoreIssues(issues, mas);
}

@Test
public void agentUnsuedCapacity_4() throws Exception {
SarlScript mas = file(multilineString(
"capacity C1 {",
" def myfct",
"}",
"capacity C2 extends C1 {",
" def myfct2",
"}",
"skill S1 implements C2 {",
" uses C1",
" def myfct {}",
" def myfct2 {}",
" def myaction {",
" myfct",
" }",
"}"
));
List<Issue> issues = issues(mas);
assertWarning(
issues,
mas,
SarlPackage.eINSTANCE.getSarlCapacityUses(),
IssueCodes.UNUSED_AGENT_CAPACITY,
"Unnecessary use of the capacity 'C1'");
assertNoMoreIssues(issues, mas);
}

@Test
public void multipleCapacityUses_0() throws Exception {
SarlScript mas = file(multilineString(
Expand Down

0 comments on commit e7b551b

Please sign in to comment.