Skip to content

Commit

Permalink
CLI implementation (#5)
Browse files Browse the repository at this point in the history
* Added CLI run
* Fixed odd manifest issue
* Added version and parenting command
* Prettified everything, ready for partial release
  • Loading branch information
terminalsin authored Jan 10, 2022
1 parent f871123 commit 63e9421
Show file tree
Hide file tree
Showing 10 changed files with 553 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.mapleir.app.service;

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.log4j.Logger;
import org.mapleir.app.service.ClassTree.InheritanceEdge;
import org.mapleir.asm.ClassHelper;
import org.mapleir.stdlib.collections.graph.FastDirectedGraph;
import org.mapleir.stdlib.collections.graph.FastGraphEdge;
import org.mapleir.stdlib.collections.graph.FastGraphEdgeImpl;
Expand All @@ -24,42 +27,42 @@
public class ClassTree extends FastDirectedGraph<ClassNode, InheritanceEdge> {
private static final Logger LOGGER = Logger.getLogger(ClassTree.class);
private static final boolean ALLOW_PHANTOM_CLASSES = true;

private final ApplicationClassSource source;
private final ClassNode rootNode;
private final boolean allowPhantomClasses;

public ClassTree(ApplicationClassSource source) {
this(source, ALLOW_PHANTOM_CLASSES);
}

public ClassTree(ApplicationClassSource source, boolean allowPhantomClasses) {
this.source = source;
this.allowPhantomClasses = allowPhantomClasses;
rootNode = findClass("java/lang/Object");
addVertex(rootNode);
}

protected void init() {
for (ClassNode node : source.iterateWithLibraries()) {
addVertex(node);
}
}

public ClassNode getRootNode() {
return rootNode;
}

public Iterable<ClassNode> iterateParents(ClassNode cn) {
// this avoids any stupid anonymous Iterable<ClassNode> and Iterator bullcrap
// and also avoids computing a temporary set, so it is performant
return () -> getEdges(cn).stream().map(e -> e.dst()).iterator();
}

public Iterable<ClassNode> iterateInterfaces(ClassNode cn) {
return () -> getEdges(cn).stream().filter(e -> e instanceof ImplementsEdge).map(e -> e.dst()).iterator();
}

public Iterable<ClassNode> iterateChildren(ClassNode cn) {
return () -> getReverseEdges(cn).stream().map(e -> e.src()).iterator();
}
Expand All @@ -79,15 +82,15 @@ public ClassNode next() {
}
};
}

public Collection<ClassNode> getParents(ClassNode cn) {
return __getnodes(getEdges(cn), true);
}

public Collection<ClassNode> getChildren(ClassNode cn) {
return __getnodes(getReverseEdges(cn), false);
}

private Collection<ClassNode> __getnodes(Collection<? extends FastGraphEdge<ClassNode>> edges, boolean dst) {
Set<ClassNode> set = new HashSet<>();
for(FastGraphEdge<ClassNode> e : edges) {
Expand All @@ -111,7 +114,7 @@ public List<ClassNode> getAllChildren(ClassNode cn) {
}
return SimpleDfs.postorder(this, cn, true);
}

/**
* @param cn classnode to search out from
* @return every class connected to the class in any way.
Expand All @@ -135,7 +138,7 @@ public Collection<ClassNode> getAllBranches(ClassNode cn) {
}
return results;
}

public ClassNode getSuper(ClassNode cn) {
if (cn == rootNode)
return null;
Expand All @@ -144,7 +147,7 @@ public ClassNode getSuper(ClassNode cn) {
return edge.dst();
throw new IllegalStateException("Couldn't find parent class?");
}

protected ClassNode findClass(String name) {
LocateableClassNode n = source.findClass(name);
if(n != null) {
Expand All @@ -158,25 +161,76 @@ protected ClassNode findClass(String name) {
}
}
}

private ClassNode requestClass0(String name, String from) {
try {
return findClass(name);
} catch(RuntimeException e) {
throw new RuntimeException("request from " + from, e);
}
}


public ClassNode getCommonSuperType(String type1, String type2) {
ClassNode ccn = source.findClassNode(type1);
ClassNode dcn = source.findClassNode(type2);

if(ccn == null) {
ClassNode c;
try {
c = ClassHelper.create(type1);
} catch (IOException e) {
e.printStackTrace();
return null;
}

this.addVertex(ccn = c);
}

if(dcn == null) {
ClassNode c;
try {
c = ClassHelper.create(type2);
} catch (IOException e) {
e.printStackTrace();
return null;
}
this.addVertex(dcn = c);
}

Collection<ClassNode> c = this.getAllParents(ccn);
Collection<ClassNode> d = this.getAllParents(dcn);

if(c.contains(dcn))
return dcn;

if(d.contains(ccn))
return ccn;

Stack<ClassNode> stack = new Stack<>();
List<ClassNode> cached = new ArrayList<>(c);
Collections.reverse(cached);
stack.addAll(cached);

while (!stack.isEmpty()) {
final ClassNode peek = stack.pop();

if (d.contains(peek))
return peek;
}

return null;
}

@Override
public boolean addVertex(ClassNode cn) {
if(cn == null) {
LOGGER.error("Received null to ClassTree.addVertex");
return false;
}

if (!super.addVertex(cn))
return false;

if(cn != rootNode) {
Set<InheritanceEdge> edges = new HashSet<>();
ClassNode sup = cn.node.superName != null ? requestClass0(cn.node.superName, cn.getName()) : rootNode;
Expand All @@ -196,12 +250,12 @@ public boolean addVertex(ClassNode cn) {
}
edges.add(new ImplementsEdge(cn, iface));
}

for(InheritanceEdge e : edges) {
super.addEdge(e);
}
}

return true;
}

Expand Down Expand Up @@ -232,7 +286,7 @@ public Set<InheritanceEdge> getReverseEdges(ClassNode cn) {
}
return super.getReverseEdges(cn);
}

@Override
public String toString() {
TabbedStringWriter sw = new TabbedStringWriter();
Expand All @@ -241,7 +295,7 @@ public String toString() {
}
return sw.toString();
}

public static void blockToString(TabbedStringWriter sw, ClassTree ct, ClassNode cn) {
sw.print(String.format("%s", cn.getDisplayName()));
sw.tab();
Expand All @@ -254,15 +308,15 @@ public static void blockToString(TabbedStringWriter sw, ClassTree ct, ClassNode
sw.untab();
sw.print("\n");
}

public interface InheritanceEdge extends FastGraphEdge<ClassNode> {
}

public static class ExtendsEdge extends FastGraphEdgeImpl<ClassNode> implements InheritanceEdge {
public ExtendsEdge(ClassNode child, ClassNode parent) {
super(child, parent);
}

@Override
public String toString() {
return String.format("#%s extends #%s", src.getDisplayName(), dst.getDisplayName());
Expand All @@ -273,7 +327,7 @@ public static class ImplementsEdge extends FastGraphEdgeImpl<ClassNode> implemen
public ImplementsEdge(ClassNode child, ClassNode parent) {
super(child, parent);
}

@Override
public String toString() {
return String.format("#%s implements #%s", src.getDisplayName(), dst.getDisplayName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ protected String getCommonSuperClass(String type1, String type2) {
try {
c = ClassHelper.create(type1);
} catch (IOException e) {
e.printStackTrace();
return "java/lang/Object";
}
if(c == null) {
return "java/lang/Object";
}
throw new UnsupportedOperationException(c.toString());

tree.addVertex(ccn = c);
// classTree.build(c);
// return getCommonSuperClass(type1, type2);
}
Expand All @@ -141,13 +143,15 @@ protected String getCommonSuperClass(String type1, String type2) {
try {
c = ClassHelper.create(type2);
} catch (IOException e) {
e.printStackTrace();
return "java/lang/Object";
}
if(c == null) {
return "java/lang/Object";
}
throw new UnsupportedOperationException(c.toString());
// classTree.build(c);

tree.addVertex(dcn = c);
// classTree.build(c)
// return getCommonSuperClass(type1, type2);
}

Expand Down
41 changes: 32 additions & 9 deletions org.mapleir.main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
<parent>
<groupId>org.mapleir</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../org.mapleir.parent</relativePath>
</parent>
<groupId>org.mapleir</groupId>
<artifactId>main</artifactId>
<name>MapleIR-main</name>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
Expand All @@ -19,25 +20,30 @@
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.6.2</version>
</dependency>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>modasm</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>ir</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>app-services</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>topdank-services</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mapleir</groupId>
Expand All @@ -52,7 +58,7 @@
<dependency>
<groupId>org.mapleir</groupId>
<artifactId>ir.printer</artifactId>
<version>0.0.1-ALPHA</version>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>

Expand All @@ -69,6 +75,11 @@
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.mapleir.Main</mainClass>
</transformer>
</transformers>
<artifactSet>
<includes>
<include>org.mapleir</include>
Expand All @@ -89,6 +100,15 @@
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.mapleir.Main</mainClass>
</manifest>
<manifestEntries>
<Main-Class>org.mapleir.Main</Main-Class>
<Built-By>maple-ir</Built-By>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
Expand All @@ -100,15 +120,18 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>org.mapleir.Main</mainClass>
</manifest>
<manifestEntries>
<Built-By>maple-ir</Built-By>
<Main-Class>org.mapleir.Main</Main-Class>
<Built-By>maple-ir</Built-By>
</manifestEntries>
</archive>
</configuration>
Expand Down
Loading

0 comments on commit 63e9421

Please sign in to comment.