SpoonLabs/astor

Bug - modification point is pointing to a spoon element that has a NoSourcePosition

MaximoOliveira opened this issue · 4 comments

For context see bug 1) from #298

This can be solved by adding a condition to check that a ctElement's position is not an instance of NoSourcePosition:

Replace this loop

for (CtElement suspiciousElement : extractedElements) {

By :

for (CtElement suspiciousElement : extractedElements) {

				if (!(suspiciousElement.getPosition() instanceof NoSourcePosition)) {
					List<CtVariable> contextOfGen = VariableResolver.searchVariablesInScope(suspiciousElement);

					SuspiciousModificationPoint point = new SuspiciousModificationPoint();
					point.setSuspicious(new SuspiciousCode(ctclasspointed.getQualifiedName(), "",
							suspiciousElement.getPosition().getLine(), 0d, null));
					point.setCtClass(ctclasspointed);
					point.setCodeElement(suspiciousElement);
					point.setContextOfModificationPoint(contextOfGen);
					suspGen.add(point);
					log.info("--ModificationPoint:" + suspiciousElement.getClass().getSimpleName() + ", suspValue "
							+ point.getSuspicious().getSuspiciousValue() + ", line "
							+ suspiciousElement.getPosition().getLine() + ", file "
							+ suspiciousElement.getPosition().getFile().getName());

					if (suspGen.size() > maxModPoints) {
						log.info("Reducing Total ModPoint created to: " + maxModPoints);
						return suspGen;
					}
				}
			}

And this loop:

By:

for (CtElement ctElement : filteredTypeByLine) {
            if (!(ctElement.getPosition() instanceof NoSourcePosition)) {
                SuspiciousModificationPoint modifPoint = new SuspiciousModificationPoint();
                modifPoint.setSuspicious(suspiciousCode);
                modifPoint.setCtClass(ctclasspointed);
                modifPoint.setCodeElement(ctElement);
                modifPoint.setContextOfModificationPoint(contextOfPoint);
                suspiciousModificationPoints.add(modifPoint);
                log.debug("--ModifPoint:" + ctElement.getClass().getSimpleName() + ", suspValue "
                        + suspiciousCode.getSuspiciousValue() + ", line " + ctElement.getPosition().getLine() + ", file "
                        + ((ctElement.getPosition().getFile() == null) ? "-null-file-"
                        : ctElement.getPosition().getFile().getName()));
            }

        }

What do you think @martinezmatias ?

Hi @MaximoOliveira

Thanks for the proposed changes. So, If I well understood the changes, those add a guard that check the filtered element is has a SourcePosition, right?

Thanks!
Regards
Matias

Hi @MaximoOliveira

It would be nice if you create a PR with those two changes and a test case that exposes the problem and assert the new behaviour i.e., to assert that no modification point targets a NoSourcePosition element.

Thanks!
Regards
Matias

Hi @martinezmatias

Thanks for the proposed changes. So, If I well understood the changes, those add a guard that check the filtered element is has a SourcePosition, right?

Correct!

It would be nice if you create a PR with those two changes and a test case that exposes the problem and assert the new behaviour i.e., to assert that no modification point targets a NoSourcePosition element.

I can open a pr with that today or tomorrow!

Thank you!

Great thanks!