Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Feb 26, 2018
2 parents cc1a59b + 0db2cd3 commit 26b20a9
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ Cohesion and Reuse in an Object-Oriented System,<br/>
Department of Computer Science, Colorado State University, 1995,
[PDF](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.53.2683).

[`hitz95`]
Lack of Cohesion in Methods 4 (**LCOM4**).<br/>
Martin Hitz et al.,<br/>
Measuring Coupling and Cohesion In Object-Oriented Systems,<br/>
Institute of Applied Computer Science and Systems Analysis, University of Vienna, 1995,
[PDF](http://www.isys.uni-klu.ac.at/PDF/1995-0043-MHBM.pdf).

## How it works?

First, `Skeleton` parses Java bytecode using Javaassit and ASM, in order to produce
Expand Down
Binary file added papers/hitz95.pdf
Binary file not shown.
110 changes: 110 additions & 0 deletions src/main/resources/org/jpeek/metrics/LCOM4.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License (MIT)
Copyright (c) 2017-2018 Yegor Bugayenko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="skeleton">
<metric>
<xsl:apply-templates select="@*"/>
<title>LCOM4</title>
<description>
<xsl:text>'LCOM4' is a 1995 revision by Martin Hitz and Behzad Montazeri,
that further improves upon the revised versions LCOM3 proposed by
W. Li and S. Henry in "Maintenance Metrics for the Object Oriented Paradigm".
LCOM4 establishes connections between methods not just on shared attributes
of the class, but also on whether one method calls the other. That is,
methods A and B are said to be related if either both use at least one
instance variable in common, OR A calls B, OR B calls A.
LCOM4 tweaks the formula for method connections in order to eliminate
the distorting effects that 'getters' may have on earlier LCOM variants,
and also methods that purely rely on the reuse of other methods without
accessing attributes directly (think method overloads where the ones
with the least args reuse the ones with the most args).
</xsl:text>
</description>
<xsl:apply-templates select="node()"/>
</metric>
</xsl:template>
<xsl:template match="class">
<xsl:variable name="class_fqn" select="replace(string-join(../@id | @id, '.'), '^\.', '')"/>
<xsl:variable name="A" select="attributes/attribute[@static = 'false']/text()"/>
<xsl:variable name="a" select="count($A)"/>
<xsl:variable name="M" select="methods/method"/>
<xsl:variable name="m" select="count($M)"/>
<!--
@todo #8:30min LCOM4: #156 needs to be fixed in order to avoid confusing access to
fields belonging to other classes as if they actually belong to this class.
Once #156 is fixed, refactor xpaths 'this_attrs' and 'other_attrs' accordingly.
-->
<!--
@todo #8:30min LCOM4: need to finish implementing the rest of the test cases. Only test case
"MethodMethodCalls" was implemented in this 30min ticket.
-->
<xsl:variable name="E">
<xsl:for-each select="$M">
<xsl:variable name="this" select="."/>
<xsl:variable name="this_fullname" select="concat($class_fqn, '.', $this/@name)"/>
<xsl:variable name="this_attrs" select="$A[. = $this/ops/op[starts-with(@code, 'put') or starts-with(@code, 'get')]/text()]"/>
<xsl:variable name="this_methods" select="$this/ops/op[@code = 'call' and matches(replace(., $class_fqn, ''), '^\.[^.]+$')]"/>
<xsl:for-each select="$this/following-sibling::method">
<xsl:variable name="other" select="."/>
<xsl:variable name="other_fullname" select="concat($class_fqn, '.', $other/@name)"/>
<xsl:variable name="other_attrs" select="$A[. = $other/ops/op[starts-with(@code, 'put') or starts-with(@code, 'get')]/text()]"/>
<xsl:variable name="other_methods" select="$other/ops/op[@code = 'call' and matches(replace(., $class_fqn, ''), '^\.[^.]+$')]"/>
<xsl:if test="exists($this_attrs[.= $other_attrs]) or exists($this_methods[.= $other_fullname]) or exists($other_methods[.= $this_fullname])">
<pair/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:copy>
<xsl:attribute name="value">
<xsl:choose>
<xsl:when test="$m &lt; 2 or $a = 0">
<xsl:text>0</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number((((count($E/pair) div $a) - $m) div (1 - $m)), '0.####')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:apply-templates select="@*"/>
<vars>
<var id="methods">
<xsl:value-of select="$m"/>
</var>
<var id="attributes">
<xsl:value-of select="$a"/>
</var>
<var id="pairs">
<xsl:value-of select="count($E/pair)"/>
</var>
</vars>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
3 changes: 2 additions & 1 deletion src/test/java/org/jpeek/MetricsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ public static Collection<Object[]> targets() {
new Object[] {"MethodsWithDiffParamTypes", "PCC", 0.3333d},
new Object[] {"Foo", "OCC", 0.5d},
new Object[] {"Foo", "TCC", 1.0d},
new Object[] {"MethodsWithDiffParamTypes", "TCC", 0.2d}
new Object[] {"MethodsWithDiffParamTypes", "TCC", 0.2d},
new Object[] {"MethodMethodCalls", "LCOM4", 0.6d}
);
}

Expand Down
23 changes: 23 additions & 0 deletions src/test/resources/org/jpeek/samples/MethodMethodCalls.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public final class MethodMethodCalls {
private int num;

public int methodOne() {
return num++;
}

public int methodTwo() {
return this.methodOne();
}

public int methodThree() {
return num--;
}

public int methodFour() {
return this.methodTwo();
}

public int methodFive() {
throw new UnsupportedOperationException();
}
}

2 comments on commit 26b20a9

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 26b20a9 Feb 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 8-429ff082 discovered in src/main/resources/org/jpeek/metrics/LCOM4.xsl and submitted as #215. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 26b20a9 Feb 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 8-435f2b33 discovered in src/main/resources/org/jpeek/metrics/LCOM4.xsl and submitted as #216. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.