Skip to content

Commit

Permalink
feat: (Java) Import classes are filtered, sorted and grouped.
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Mar 5, 2023
1 parent c29fdeb commit 1627e9a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
27 changes: 26 additions & 1 deletion src/main/java/reincarnation/coder/java/Imports.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@
*/
package reincarnation.coder.java;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import kiss.I;
import kiss.model.Model;

class Imports {

/** The core package. */
private static final Package Core = Object.class.getPackage();

/** The imported class. */
final Set<Class> imported = new HashSet();
private final Set<Class> imported = new TreeSet<>(Comparator.comparing(Class::getName));

/** The imported simple class name. */
private final Set<String> importedName = new HashSet();
Expand Down Expand Up @@ -104,4 +112,21 @@ String name(Class clazz) {
return raw.getSimpleName().concat("[]".repeat(depth));
}
}

/**
* Export classes.
*
* @return
*/
Map<String, List<Class>> export() {
return I.signal(imported).skip(x -> x.getPackage() == Core).toGroup(x -> {
String name = x.getPackage().getName();
int index = name.indexOf(".");
if (index == -1) {
return name;
} else {
return name.substring(0, index);
}
});
}
}
10 changes: 6 additions & 4 deletions src/main/java/reincarnation/coder/java/JavaCoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ private void writeHierarchy(Hierarchy hierarchy) {
* Write import part.
*/
private void writeImport() {
line();
for (Class clazz : imports.imported) {
line("import ", clazz.getCanonicalName(), ";");
}
imports.export().values().forEach(classes -> {
line();
for (Class clazz : classes) {
line("import ", clazz.getCanonicalName(), ";");
}
});
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/reincarnation/decompiler/lambda/LambdaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.Test;

import reincarnation.CodeVerifier;
import reincarnation.Debuggable;
import reincarnation.TestCode;

class LambdaTest extends CodeVerifier {
Expand Down Expand Up @@ -76,6 +77,7 @@ private int lambda(IntBinaryOperator supplier) {
}

@Test
@Debuggable
void inlineWithObject() {
verify(new TestCode.Text() {

Expand Down

0 comments on commit 1627e9a

Please sign in to comment.