Skip to content

Commit

Permalink
Merge pull request #2 from michaelwelly/520
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwelly authored Jun 24, 2024
2 parents 987df24 + 514ce04 commit 78bb7bd
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 104 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ SOFTWARE.
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
64 changes: 25 additions & 39 deletions src/main/java/org/jpeek/graph/XmlGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
package org.jpeek.graph;

import com.jcabi.xml.XML;
import java.util.List;
import java.util.Map;

import java.util.*;

import org.cactoos.list.ListOf;
import org.cactoos.map.MapOf;
import org.cactoos.scalar.Sticky;
Expand Down Expand Up @@ -74,47 +75,32 @@ public List<Node> nodes() {
* @param cname Class in the skeleton this graph is for
* @return List of nodes
*/
private static List<Node> build(final Skeleton skeleton, final String pname,
final String cname) {
final Map<XML, Node> byxml = new MapOf<>(
method -> method,
method -> new Node.Simple(
new XmlMethodSignature(
skeleton.xml()
.nodes(
new FormattedText(
"//package[@id='%s']", pname
).toString()
).get(0)
.nodes(
new FormattedText(
"//class[@id='%s']", cname
).toString()
).get(0),
method
).asString()
),
skeleton.xml().nodes(
"//methods/method[@ctor='false' and @abstract='false']"
)
);
final Map<String, Node> byname = new MapOf<>(
Node::name,
node -> node,
byxml.values()
);
for (final Map.Entry<XML, Node> entry : byxml.entrySet()) {
final List<XML> calls = entry.getKey().nodes("ops/op[@code='call']");
final Node caller = entry.getValue();
for (final XML call : calls) {
final String name = new XmlMethodCall(call).toString();
if (byname.containsKey(name)) {
final Node callee = byname.get(name);
private static List<Node> build(final Skeleton skeleton, final String pname, final String cname) throws Exception {
final Map<XML, Node> byxml = new HashMap<>();
final Set<String> visitedNames = new HashSet<>();
for (XML methodXml : skeleton.xml().nodes("//methods/method[@ctor='false' and @abstract='false']")) {
String methodName = new XmlMethodSignature(skeleton.xml().nodes(new FormattedText("//package[@id='%s']", pname).toString())
.get(0)
.nodes(new FormattedText("//class[@id='%s']", cname).toString())
.get(0), methodXml).asString();
if (!visitedNames.contains(methodName)) {
Node.Simple node = new Node.Simple(methodName);
byxml.put(methodXml, node);
visitedNames.add(methodName);
}
}
for (Map.Entry<XML, Node> entry : byxml.entrySet()) {
XML methodXml = entry.getKey();
Node caller = entry.getValue();
for (XML call : methodXml.nodes("ops/op[@code='call']")) {
String calleeName = new XmlMethodCall(call).toString();
if (visitedNames.contains(calleeName)) {
Node callee = byxml.get(call); // assuming they exist in byxml
caller.connections().add(callee);
callee.connections().add(caller);
}
}
}
return new ListOf<>(byxml.values());
return new ArrayList<>(byxml.values());
}
}
125 changes: 60 additions & 65 deletions src/main/java/org/jpeek/skeleton/XmlClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

import javassist.CannotCompileException;
import javassist.CtClass;
import org.cactoos.iterable.Joined;
import org.cactoos.iterable.Mapped;
import org.jetbrains.annotations.NotNull;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
Expand All @@ -44,9 +46,9 @@
*
* <p>There is no thread-safety guarantee.</p>
*
* @checkstyle ParameterNumberCheck (500 lines)
* @see <a href="http://www.pitt.edu/~ckemerer/CK%20research%20papers/MetricForOOD_ChidamberKemerer94.pdf">A packages suite for object oriented design</a>
* @since 0.27
* @checkstyle ParameterNumberCheck (500 lines)
*/
final class XmlClass extends ClassVisitor implements Iterable<Directive> {

Expand All @@ -67,6 +69,7 @@ final class XmlClass extends ClassVisitor implements Iterable<Directive> {

/**
* Ctor.
*
* @param src The source
*/
XmlClass(final CtClass src) {
Expand All @@ -77,6 +80,7 @@ final class XmlClass extends ClassVisitor implements Iterable<Directive> {
}

@Override
@NotNull
public Iterator<Directive> iterator() {
final ClassReader reader;
try {
Expand All @@ -87,42 +91,42 @@ public Iterator<Directive> iterator() {
this.attrs.add("attributes");
reader.accept(this, 0);
return new Directives()
.append(this.attrs)
.up()
.add("methods")
.append(
new Joined<>(
new Mapped<>(
dirs -> new Directives().append(dirs).up(),
this.methods
)
.append(this.attrs)
.up()
.add("methods")
.append(
new Joined<>(
new Mapped<>(
dirs -> new Directives().append(dirs).up(),
this.methods
)
)
)
)
.up()
.iterator();
.up()
.iterator();
}

@Override
public FieldVisitor visitField(final int access,
final String name, final String desc,
final String signature, final Object value) {
final String name, final String desc,
final String signature, final Object value) {
this.attrs
.add("attribute")
.set(name)
.attr("type", desc.replaceAll(";$", ""))
.attr(
"public",
(access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC
)
.attr(
"final",
(access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL
)
.attr(
"static",
(access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC
)
.up();
.add("attribute")
.set(name)
.attr("type", desc.replaceAll(";$", ""))
.attr(
"public",
(access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC
)
.attr(
"final",
(access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL
)
.attr(
"static",
(access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC
)
.up();
return super.visitField(access, name, desc, signature, value);
}

Expand All @@ -140,53 +144,44 @@ public FieldVisitor visitField(final int access,
// - the `visitMethodInsn` arguments.
@Override
@SuppressWarnings(
{
"PMD.UseVarargs",
"PMD.UseObjectForClearerAPI"
}
{
"PMD.UseVarargs",
"PMD.UseObjectForClearerAPI"
}
)
public MethodVisitor visitMethod(final int access,
final String mtd, final String desc,
final String signature, final String[] exceptions) {
final String mtd, final String desc,
final String signature, final String[] exceptions) {
final Directives dirs = new Directives();
if ((access & Opcodes.ACC_SYNTHETIC) != Opcodes.ACC_SYNTHETIC) {
String visibility = "default";
if ((access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC) {
visibility = "public";
} else if (
(access & Opcodes.ACC_PROTECTED) == Opcodes.ACC_PROTECTED) {
} else if ((access & Opcodes.ACC_PROTECTED) == Opcodes.ACC_PROTECTED) {
visibility = "protected";
} else if ((access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE) {
visibility = "private";
}

// Извлечение имени переменной из описания метода
String[] parts = desc.split("\\)");
String[] methodParts = parts[0].split("\\(");
String methodName = methodParts[0];
String[] variables = methodParts[1].split(",");
String variableName = variables[0]; // Предполагаем, что первая переменная - это та, к которой применен вызов метода

dirs.add("method")
.attr("name", mtd)
.attr("desc", desc)
.attr(
"ctor",
"<init>".equals(mtd) || "<clinit>".equals(mtd)
)
.attr(
"static",
(access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC
)
.attr(
"abstract",
(access & Opcodes.ACC_ABSTRACT) == Opcodes.ACC_ABSTRACT
)
.attr(
"visibility",
visibility
)
.attr(
"bridge",
(access & Opcodes.ACC_BRIDGE) == Opcodes.ACC_BRIDGE
)
.append(new TypesOf(desc));
.attr("name", mtd)
.attr("variableName", variableName)
.attr("desc", desc)
.attr("ctor", "<init>".equals(mtd) || "<clinit>".equals(mtd))
.attr("static", (access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC)
.attr("abstract", (access & Opcodes.ACC_ABSTRACT) == Opcodes.ACC_ABSTRACT)
.attr("visibility", visibility)
.attr("bridge", (access & Opcodes.ACC_BRIDGE) == Opcodes.ACC_BRIDGE)
.append(new TypesOf(desc));
this.methods.add(dirs);
}
return new OpsOf(
dirs, super.visitMethod(access, mtd, desc, signature, exceptions)
);
return new OpsOf(dirs, super.visitMethod(access, mtd, desc, signature, exceptions));
}
}

0 comments on commit 78bb7bd

Please sign in to comment.