diff --git a/src/main/java/gr/uom/java/xmi/LocationInfo.java b/src/main/java/gr/uom/java/xmi/LocationInfo.java index ea9e597d7..a1718879b 100644 --- a/src/main/java/gr/uom/java/xmi/LocationInfo.java +++ b/src/main/java/gr/uom/java/xmi/LocationInfo.java @@ -18,6 +18,7 @@ public class LocationInfo { private int startColumn; private int endLine; private int endColumn; + private int compilationUnitLength; private CodeElementType codeElementType; public LocationInfo(CompilationUnit cu, String sourceFolder, String filePath, ASTNode node, CodeElementType codeElementType) { @@ -48,6 +49,12 @@ public LocationInfo(CompilationUnit cu, String sourceFolder, String filePath, AS if(this.endColumn > 0) { this.endColumn += 1; } + + int cuEndOffset = cu.getStartPosition() + cu.getLength(); + this.compilationUnitLength = cu.getLineNumber(cuEndOffset); + if(this.compilationUnitLength == -1) { + this.compilationUnitLength = cu.getLineNumber(cuEndOffset-1); + } } public String getSourceFolder() { @@ -86,6 +93,10 @@ public int getEndColumn() { return endColumn; } + public int getCompilationUnitLength() { + return compilationUnitLength; + } + public CodeElementType getCodeElementType() { return codeElementType; } diff --git a/src/main/java/gr/uom/java/xmi/diff/ClassMoveComparator.java b/src/main/java/gr/uom/java/xmi/diff/ClassMoveComparator.java index db20b35ab..11cf70049 100644 --- a/src/main/java/gr/uom/java/xmi/diff/ClassMoveComparator.java +++ b/src/main/java/gr/uom/java/xmi/diff/ClassMoveComparator.java @@ -15,8 +15,26 @@ public int compare(UMLClassMoveDiff o1, UMLClassMoveDiff o2) { else { Set set1 = o1.commonPackagesInQualifiedName(); Set set2 = o2.commonPackagesInQualifiedName(); - if(set1.size() != set2.size()) { - return -Integer.compare(set1.size(), set2.size()); + boolean sameNumberOfLines1 = o1.getOriginalClass().getLocationInfo().getCompilationUnitLength() == o1.getNextClass().getLocationInfo().getCompilationUnitLength(); + boolean sameNumberOfLines2 = o2.getOriginalClass().getLocationInfo().getCompilationUnitLength() == o2.getNextClass().getLocationInfo().getCompilationUnitLength(); + int lineNumberDifference1 = Math.abs(o1.getOriginalClass().getLocationInfo().getCompilationUnitLength() - o1.getNextClass().getLocationInfo().getCompilationUnitLength()); + int lineNumberDifference2 = Math.abs(o2.getOriginalClass().getLocationInfo().getCompilationUnitLength() - o2.getNextClass().getLocationInfo().getCompilationUnitLength()); + boolean isEmpty1 = o1.getOriginalClass().isEmpty() && o1.getNextClass().isEmpty(); + boolean isEmpty2 = o2.getOriginalClass().isEmpty() && o2.getNextClass().isEmpty(); + if(set1.size() != set2.size() && !isEmpty1 && !isEmpty2) { + if(lineNumberDifference1 == lineNumberDifference2) + return -Integer.compare(set1.size(), set2.size()); + else { + if(sameNumberOfLines1 && !sameNumberOfLines2) { + return -1; + } + else if(!sameNumberOfLines1 && sameNumberOfLines2) { + return 1; + } + else { + return Integer.compare(lineNumberDifference1, lineNumberDifference2); + } + } } double sourceFolderDistance1 = o1.getMovedClass().normalizedSourceFolderDistance(o1.getOriginalClass()); double sourceFolderDistance2 = o2.getMovedClass().normalizedSourceFolderDistance(o2.getOriginalClass());