Skip to content

Commit

Permalink
(cqfn#215) LCOM4 static fields are now called by FQN (formatting)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyakharlamov committed Jan 24, 2019
1 parent 2c579e9 commit fdbadf6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 59 deletions.
40 changes: 6 additions & 34 deletions src/main/java/org/jpeek/skeleton/OpsOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@
package org.jpeek.skeleton;

import org.cactoos.Text;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Joined;
import org.cactoos.text.JoinedText;
import org.cactoos.text.SplitText;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.xembly.Directives;
Expand Down Expand Up @@ -70,21 +65,21 @@ public void visitFieldInsn(final int opcode, final String owner,
final String attr, final String dsc) {
super.visitFieldInsn(opcode, owner, attr, dsc);
this.target.addIf("ops").add("op");
final String name;
final Text name;
if (opcode == Opcodes.GETFIELD) {
this.target.attr("code", "get");
name = attr;
name = new TextOf(attr);
} else if (opcode == Opcodes.PUTFIELD) {
this.target.attr("code", "put");
name = attr;
name = new TextOf(attr);
} else if (opcode == Opcodes.GETSTATIC) {
this.target.attr("code", "get_static");
name = OpsOf.getQualifiedName(owner, attr);
name = new QualifiedName(owner, attr);
} else if (opcode == Opcodes.PUTSTATIC) {
this.target.attr("code", "put_static");
name = OpsOf.getQualifiedName(owner, attr);
name = new QualifiedName(owner, attr);
} else {
name = attr;
name = new TextOf(attr);
}
this.target.set(
name
Expand All @@ -102,27 +97,4 @@ public void visitMethodInsn(final int opcode,
.set(owner.replace("/", ".").concat(".").concat(mtd))
.up().up();
}

/**
* Returns fully qualified name, an unambiguous name
* that specifies field without regard
* to the context of the call.
* @param owner The class the attribute belongs to
* @param attr The name of the field
* @return Fully qualified name of the field
*/
private static String getQualifiedName(final String owner,
final String attr) {
return new UncheckedText(
new JoinedText(
new TextOf("."),
new Joined<Text>(
new SplitText(owner, "/"),
new IterableOf<>(
new TextOf(attr)
)
)
)
).asString();
}
}
64 changes: 64 additions & 0 deletions src/main/java/org/jpeek/skeleton/QualifiedName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2019 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.
*/
package org.jpeek.skeleton;

import org.cactoos.Text;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Joined;
import org.cactoos.text.JoinedText;
import org.cactoos.text.SplitText;
import org.cactoos.text.TextEnvelope;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;

/**
* A fully qualified name of a field, an unambiguous name
* that specifies field without regard
* to the context of the call.
* @author Ilya Kharlamov (ilya.kharlamov@gmail.com)
* @version $Id$
* @since 0.29
*/
public final class QualifiedName extends TextEnvelope {
/**
* Ctor.
* @param owner The class the attribute belongs to
* @param attr The name of the field
*/
public QualifiedName(final String owner, final String attr) {
super(
new UncheckedText(
new JoinedText(
new TextOf("."),
new Joined<Text>(
new SplitText(owner, "/"),
new IterableOf<>(
new TextOf(attr)
)
)
)
)
);
}
}
36 changes: 18 additions & 18 deletions src/test/java/org/jpeek/metrics/Lcom4Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
package org.jpeek.metrics;

import org.cactoos.list.ListOf;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

Expand Down Expand Up @@ -59,16 +58,9 @@ public final class Lcom4Test {
public static final String METHODS = "methods";

/**
* Test helper.
* Path to XSL.
*/
private MetricBase base;

@Before
public void setUpMetric() throws Exception {
this.base = new MetricBase(
"org/jpeek/metrics/LCOM4.xsl"
);
}
private static final String XSL = "org/jpeek/metrics/LCOM4.xsl";

/**
* Tests the deep dependencies methodFour() -> methodTwo() -> methodOne().
Expand All @@ -89,8 +81,10 @@ public void setUpMetric() throws Exception {
*/
@Test
@Ignore
public void methodMethodCalls() {
final MetricBase.Report report = this.base.transform(
public void methodMethodCalls() throws Exception {
final MetricBase.Report report = new MetricBase(
Lcom4Test.XSL
).transform(
"MethodMethodCalls"
);
report.assertVariable(
Expand All @@ -115,8 +109,10 @@ public void methodMethodCalls() {
* This is an ideal LCOM4 value = 1
*/
@Test
public void oneCommonAttribute() {
final MetricBase.Report report = this.base.transform(
public void oneCommonAttribute() throws Exception {
final MetricBase.Report report = new MetricBase(
Lcom4Test.XSL
).transform(
"OneCommonAttribute"
);
report.assertVariable(
Expand All @@ -140,8 +136,10 @@ public void oneCommonAttribute() {
* So the value is 2.0
*/
@Test
public void notCommonAttributes() {
final MetricBase.Report report = this.base.transform(
public void notCommonAttributes() throws Exception {
final MetricBase.Report report = new MetricBase(
Lcom4Test.XSL
).transform(
"NotCommonAttributes"
);
report.assertVariable(Lcom4Test.ATTRIBUTES, 2);
Expand All @@ -154,8 +152,10 @@ public void notCommonAttributes() {
* since constructors are not methods and should be ignored
*/
@Test
public void notCommonAttributesWithAllArgsConstructor() {
final MetricBase.Report report = this.base.transform(
public void notCommonAttributesWithAllArgsConstructor() throws Exception {
final MetricBase.Report report = new MetricBase(
Lcom4Test.XSL
).transform(
"NotCommonAttributesWithAllArgsConstructor"
);
report.assertVariable(Lcom4Test.ATTRIBUTES, 2);
Expand Down
18 changes: 11 additions & 7 deletions src/test/java/org/jpeek/metrics/MetricBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.jcabi.xml.XML;
import com.jcabi.xml.XSLDocument;
import org.cactoos.io.ResourceOf;
import org.cactoos.text.FormattedText;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.hamcrest.number.IsCloseTo;
Expand Down Expand Up @@ -101,14 +102,16 @@ public Report(final String name, final XML xml) {
* Asserts the variable produced.
* @param variable Variable name
* @param expected Expected value
* @throws Exception String format exception
*/
public void assertVariable(final String variable, final int expected) {
public void assertVariable(final String variable,
final int expected) throws Exception {
MatcherAssert.assertThat(
String.format(
new FormattedText(
"Variable '%s' is not calculated correctly for class '%s'",
variable,
this.name
),
).asString(),
this.xml.xpath(
String.format(
"//class[@id='%s']/vars/var[@id='%s']/text()",
Expand All @@ -127,22 +130,23 @@ public void assertVariable(final String variable, final int expected) {
/**
* Asserts the main metric value.
* @param value Expected value of the metric
* @throws Exception String format exception
*/
public void assertValue(final double value) {
public void assertValue(final double value) throws Exception {
MatcherAssert.assertThat(
"The metric value is not calculated properly",
Double.parseDouble(
this.xml.xpath(
String.format(
new FormattedText(
"//class[@id='%s']/@value",
this.name
)
).asString()
).get(0)
),
new IsCloseTo(
value,
// @checkstyle MagicNumberCheck (1 line)
0.001D
0.001d
)
);
}
Expand Down

0 comments on commit fdbadf6

Please sign in to comment.