Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/yegor256/jpeek into 139
- Loading branch information
Showing
8 changed files
with
320 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 < 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
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>TLCOM</title> | ||
<description> | ||
<xsl:text> | ||
"Transitive Lack of Cohesion in Methods" (TLCOM) expands upon | ||
the original definition for LCOM by extending the criteria for | ||
estabilishing relations between method pairs by also including cases | ||
when a m1 calls m2 and m2 happens to use attribute a2 of the class. This | ||
then means that m1 transitively uses a2 via m2. | ||
The chain of calls can have more than one hop: if m1 calls m2 which calls | ||
m3 which calls m4, and m4 uses attribute a4, then m1 is said to transitively | ||
use a4 as well. | ||
The final formula is the same as the original LCOM: | ||
TLCOM = |P| - |Q|, if |P| > |Q|, otherwise it's 0 (zero). 'P' is | ||
the set of method-pairs that are not connected according to the | ||
definition above, and 'Q' is the set of method-pairs that are connected. | ||
</xsl:text> | ||
</description> | ||
<xsl:apply-templates select="node()"/> | ||
</metric> | ||
</xsl:template> | ||
<xsl:template match="class"> | ||
<!-- | ||
@todo #65:30min TLCOM: we are including access to ALL fields here. After #156 is fixed, | ||
come back and refactor xpath for $Ms_that_use_attrs, $left_attrs, and $right_attrs | ||
in order to avoid including access to fields belonging to other classes. | ||
--> | ||
<xsl:variable name="class_fqn" select="replace(string-join(../@id | @id, '.'), '^\.', '')"/> | ||
<xsl:variable name="A" select="attributes/attribute"/> | ||
<xsl:variable name="M" select="methods/method"/> | ||
<xsl:variable name="Ms_that_use_attrs"> | ||
<xsl:for-each select="$M"> | ||
<xsl:if test="./ops/op[@code != 'call'][. = $A]"> | ||
<method> | ||
<xsl:value-of select="concat($class_fqn, '.', @name)"/> | ||
</method> | ||
</xsl:if> | ||
</xsl:for-each> | ||
</xsl:variable> | ||
<xsl:variable name="pairs"> | ||
<xsl:for-each select="$M"> | ||
<xsl:variable name="left" select="."/> | ||
<xsl:variable name="left_name" select="concat($class_fqn, '.', $left/@name)"/> | ||
<xsl:variable name="left_attrs" select="$left/ops/op[@code != 'call' and exists(. = $A)]"/> | ||
<xsl:variable name="left_calls" select="$left/ops/op[@code = 'call' and matches(., concat('^', $class_fqn, '\.[^\.]+$'))]"/> | ||
<xsl:for-each select="$left/following-sibling::method"> | ||
<xsl:variable name="right" select="."/> | ||
<xsl:variable name="right_name" select="concat($class_fqn, '.', $right/@name)"/> | ||
<xsl:variable name="right_attrs" select="$right/ops/op[@code != 'call' and exists(. = $A)]"/> | ||
<xsl:variable name="right_calls" select="$right/ops/op[@code = 'call' and matches(., concat('^', $class_fqn, '\.[^\.]+$'))]"/> | ||
<!-- | ||
@todo #65:30min TLCOM: need to establish transitive connections between methods with arbitrarily long | ||
chains of method calls between them. Right now, this code can establish transitive connections to a | ||
max depth of 3 levels, ie up to something like this: m1 calls m2 calls m3 which uses a3 therefore m1 is | ||
connected to m3. It will miss connections in longer chains. After fixing this, maybe we can add it to | ||
the standard metrics in App. | ||
--> | ||
<!-- | ||
@todo #65:15min TLCOM: add test for OneMethodCreatesLambda after #171 is fixed. The extra synthetic | ||
method introduced by the compiler distorts this metric's result. | ||
--> | ||
<xsl:choose> | ||
<xsl:when test="$left_attrs[. = $right_attrs]"> | ||
<Q/> | ||
</xsl:when> | ||
<xsl:when test="exists($left_calls[matches(., $right_name)]) and exists($Ms_that_use_attrs/method[. = $right_name])"> | ||
<Q/> | ||
</xsl:when> | ||
<xsl:when test="exists($right_calls[matches(., $left_name)]) and exists($Ms_that_use_attrs/method[. = $left_name])"> | ||
<Q/> | ||
</xsl:when> | ||
<xsl:when test="exists($left_calls[. = $right_calls]) and exists($right_calls[. = $Ms_that_use_attrs/method])"> | ||
<Q/> | ||
</xsl:when> | ||
<xsl:when test="exists($right_calls[. = $left_calls]) and exists($left_calls[. = $Ms_that_use_attrs/method])"> | ||
<Q/> | ||
</xsl:when> | ||
<xsl:otherwise> | ||
<P/> | ||
</xsl:otherwise> | ||
</xsl:choose> | ||
</xsl:for-each> | ||
</xsl:for-each> | ||
</xsl:variable> | ||
<xsl:variable name="P" select="count($pairs/P)"/> | ||
<xsl:variable name="Q" select="count($pairs/Q)"/> | ||
<xsl:copy> | ||
<xsl:attribute name="value"> | ||
<xsl:choose> | ||
<xsl:when test="$P > $Q"> | ||
<xsl:value-of select="$P - $Q"/> | ||
</xsl:when> | ||
<xsl:otherwise> | ||
<xsl:text>0</xsl:text> | ||
</xsl:otherwise> | ||
</xsl:choose> | ||
</xsl:attribute> | ||
<xsl:apply-templates select="@*"/> | ||
<vars> | ||
<var id="methods"> | ||
<xsl:value-of select="count($M)"/> | ||
</var> | ||
<var id="attributes"> | ||
<xsl:value-of select="count($A)"/> | ||
</var> | ||
<var id="|P|"> | ||
<xsl:value-of select="$P"/> | ||
</var> | ||
<var id="|Q|"> | ||
<xsl:value-of select="$Q"/> | ||
</var> | ||
</vars> | ||
</xsl:copy> | ||
</xsl:template> | ||
<xsl:template match="node()|@*"> | ||
<xsl:copy> | ||
<xsl:apply-templates select="node()|@*"/> | ||
</xsl:copy> | ||
</xsl:template> | ||
</xsl:stylesheet> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/test/resources/org/jpeek/samples/MethodMethodCalls.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |