Skip to content

Commit

Permalink
try to make SFUImpl work like JavaFileParser
Browse files Browse the repository at this point in the history
  • Loading branch information
ramidzkh committed Nov 21, 2023
1 parent ea16821 commit ee40ec1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/main/java/io/github/astrarre/sfu/SourceFixerUpper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.astrarre.sfu;

import io.github.astrarre.sfu.impl.SFUImpl;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import net.fabricmc.mappingio.tree.MappingTreeView;

Expand All @@ -10,6 +12,8 @@ static SourceFixerUpper create() {
return new SFUImpl();
}

SourceFixerUpper charset(Charset charset);

SourceFixerUpper mappings(MappingTreeView tree, int srcNamespace, int dstNamespace);

SourceFixerUpper input(Path root);
Expand All @@ -20,5 +24,5 @@ static SourceFixerUpper create() {

SourceFixerUpper output(Path output);

void process();
void process() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public void parseJavaSourceFile(Path filePath) throws IOException {
codeTree.accept(jsv, null);
MemoryMappingTree tree = new MemoryMappingTree();
Tiny1Reader.read(Files.newBufferedReader(Path.of("test.tiny")), tree);
Remapper remapper = new Remapper(builder, jsv.members, jsv.types, tree, "from", "to");
Remapper remapper = new Remapper(builder, jsv.members, jsv.types, tree, tree.getNamespaceId("from"),
tree.getNamespaceId("to"));
remapper.apply();
}

Expand Down
20 changes: 9 additions & 11 deletions src/main/java/io/github/astrarre/sfu/impl/Remapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,20 @@ public class Remapper {
public final List<RangeCollectingVisitor.MemberRange> members;
public final List<RangeCollectingVisitor.TypeRange> types;
public final MappingTreeView view;
public final String from, to;
public final int fromIndex, toIndex;
public final int srcNamespace, dstNamespace;

public Remapper(StringBuilder builder,
List<RangeCollectingVisitor.MemberRange> members,
List<RangeCollectingVisitor.TypeRange> types,
MappingTreeView view,
String from,
String to) {
this.fromIndex = view.getNamespaceId(from);
this.toIndex = view.getNamespaceId(to);
int srcNamespace,
int dstNamespace) {
this.srcNamespace = srcNamespace;
this.dstNamespace = dstNamespace;
this.builder = builder;
this.members = new ArrayList<>(members);
this.types = new ArrayList<>(types);
this.view = view;
this.from = from;
this.to = to;
// todo merge into one list before sorting
// todo resolve from index for proper sorting
this.members.sort(Comparator.comparingInt(RangeCollectingVisitor.MemberRange::from).reversed());
Expand All @@ -36,11 +33,12 @@ public Remapper(StringBuilder builder,
public void apply() {
for (RangeCollectingVisitor.MemberRange member : this.members) {
String name = member.name();
var mapping = member.isMethod() ? this.view.getMethod(member.owner(), name, member.desc(), this.fromIndex)
: this.view.getField(member.owner(), name, member.desc(), this.fromIndex);
var mapping = member.isMethod()
? this.view.getMethod(member.owner(), name, member.desc(), this.srcNamespace)
: this.view.getField(member.owner(), name, member.desc(), this.srcNamespace);

if (mapping != null) {
String dst = mapping.getDstName(this.toIndex);
String dst = mapping.getDstName(this.dstNamespace);
int from = member.from(), to = member.to();
if (member.to() == -1) {
from = this.findStart(from, name, false);
Expand Down
50 changes: 46 additions & 4 deletions src/main/java/io/github/astrarre/sfu/impl/SFUImpl.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
package io.github.astrarre.sfu.impl;

import io.github.astrarre.sfu.SourceFixerUpper;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import javax.tools.JavaFileObject;
import net.fabricmc.mappingio.tree.MappingTreeView;
import sfu_rpkg.com.sun.source.tree.CompilationUnitTree;
import sfu_rpkg.com.sun.source.util.JavacTask;
import sfu_rpkg.com.sun.source.util.Trees;
import sfu_rpkg.com.sun.tools.javac.api.JavacTool;
import sfu_rpkg.com.sun.tools.javac.file.JavacFileManager;
import sfu_rpkg.com.sun.tools.javac.util.Context;

public class SFUImpl implements SourceFixerUpper {

private Charset charset = Charset.defaultCharset();
private MappingTreeView tree;
private int srcNamespace, dstNamespace;
private Path output;

private final List<Path> inputs, sourcepath, classpath;

public SFUImpl() {
inputs = new ArrayList<>();
sourcepath = new ArrayList<>();
classpath = new ArrayList<>();
this.inputs = new ArrayList<>();
this.sourcepath = new ArrayList<>();
this.classpath = new ArrayList<>();
}

@Override
public SourceFixerUpper charset(Charset charset) {
this.charset = charset;
return this;
}

@Override
Expand Down Expand Up @@ -53,7 +70,32 @@ public SourceFixerUpper output(Path output) {
}

@Override
public void process() {
public void process() throws IOException {
Context context = new Context();
JavacFileManager jcFileManager = new JavacFileManager(context, true, charset);
JavacTool javac = JavacTool.create();

Path filePath = Files.list(inputs.get(0)).filter(Files::isRegularFile).findAny().get();

Iterable<? extends JavaFileObject> javaFiles = jcFileManager.getJavaFileObjects(filePath);
JavacTask jcTask = javac.getTask(null,
jcFileManager,
null,
List.of(),
null,
javaFiles);
Trees trees = Trees.instance(jcTask);

Iterable<? extends CompilationUnitTree> codeResult = jcTask.parse();
jcTask.analyze();

for (CompilationUnitTree codeTree : codeResult) {
var jsv = new RangeCollectingVisitor(trees);
codeTree.accept(jsv, null);
StringBuilder builder = new StringBuilder(Files.readString(filePath));
Remapper remapper = new Remapper(builder, jsv.members, jsv.types, tree, srcNamespace, dstNamespace);
remapper.apply();
System.out.println(builder);
}
}
}

0 comments on commit ee40ec1

Please sign in to comment.