Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a plugin for ATs #9

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions accesstransformers/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
id "java-library"
}

dependencies {
implementation project(":api")
implementation 'net.neoforged.accesstransformers:at-parser:10.0.15'

testImplementation platform("org.junit:junit-bom:$junit_version")
testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.neoforged.jst.accesstransformers;

import net.neoforged.jst.api.SourceTransformer;
import net.neoforged.jst.api.SourceTransformerPlugin;

Matyrobbrt marked this conversation as resolved.
Show resolved Hide resolved
public class ATsPlugin implements SourceTransformerPlugin {
@Override
public String getName() {
return "accesstransformers";
}

@Override
public SourceTransformer createTransformer() {
return new ATsTransformer();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.neoforged.jst.accesstransformers;

import com.intellij.psi.PsiFile;
import net.neoforged.accesstransformer.parser.AccessTransformerFiles;
import net.neoforged.jst.api.Replacements;
import net.neoforged.jst.api.SourceTransformer;
import net.neoforged.jst.api.TransformContext;
import picocli.CommandLine;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Path;
import java.util.List;

public class ATsTransformer implements SourceTransformer {
Matyrobbrt marked this conversation as resolved.
Show resolved Hide resolved

@CommandLine.Option(names = "--access-transformer", required = true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about required=true here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea how it works with lists actually, I need to test it.

public List<Path> atFiles;

private AccessTransformerFiles ats;
@Override
public void beforeRun(TransformContext context) {
ats = new AccessTransformerFiles();
try {
for (Path path : atFiles) {
ats.loadFromPath(path);
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}

@Override
public void visitFile(PsiFile psiFile, Replacements replacements) {
new ApplyATsVisitor(ats, replacements).visitFile(psiFile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package net.neoforged.jst.accesstransformers;

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiKeyword;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiModifierList;
import com.intellij.psi.PsiRecursiveElementVisitor;
import com.intellij.psi.util.ClassUtil;
import net.neoforged.accesstransformer.parser.AccessTransformerFiles;
import net.neoforged.accesstransformer.parser.Target;
import net.neoforged.accesstransformer.parser.Transformation;
import net.neoforged.jst.api.Replacements;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

class ApplyATsVisitor extends PsiRecursiveElementVisitor {
private static final List<String> ACCESS_MODIFIERS = List.of(PsiModifier.PUBLIC, PsiModifier.PRIVATE, PsiModifier.PROTECTED);
public static final EnumMap<Transformation.Modifier, String> MODIFIER_TO_STRING = new EnumMap<>(
Map.of(Transformation.Modifier.PRIVATE, PsiModifier.PRIVATE, Transformation.Modifier.PUBLIC, PsiModifier.PUBLIC, Transformation.Modifier.PROTECTED, PsiModifier.PROTECTED)
);

private final AccessTransformerFiles ats;
private final Replacements replacements;

public ApplyATsVisitor(AccessTransformerFiles ats, Replacements replacements) {
this.ats = ats;
this.replacements = replacements;
}

@Override
public void visitElement(@NotNull PsiElement element) {
shartte marked this conversation as resolved.
Show resolved Hide resolved
if (element instanceof PsiClass psiClass) {
if (psiClass.getQualifiedName() != null) {
String className = ClassUtil.getJVMClassName(psiClass);
if (!ats.containsClassTarget(className)) return; // Skip this class and all its children
Matyrobbrt marked this conversation as resolved.
Show resolved Hide resolved

apply(ats.getAccessTransformers().get(new Target.ClassTarget(className)), psiClass.getModifierList());
var fieldWildcard = ats.getAccessTransformers().get(new Target.WildcardFieldTarget(className));
if (fieldWildcard != null) {
for (PsiField field : psiClass.getFields()) {
apply(fieldWildcard, field.getModifierList());
}
}

var methodWildcard = ats.getAccessTransformers().get(new Target.WildcardMethodTarget(className));
if (methodWildcard != null) {
for (PsiMethod method : psiClass.getMethods()) {
apply(methodWildcard, method.getModifierList());
}
}
}
} else if (element instanceof PsiField field) {
final var cls = field.getContainingClass();
if (cls != null && cls.getQualifiedName() != null) {
String className = ClassUtil.getJVMClassName(cls);
apply(ats.getAccessTransformers().get(new Target.FieldTarget(className, field.getName())), field.getModifierList());
}
} else if (element instanceof PsiMethod method) {
final var cls = method.getContainingClass();
if (cls != null && cls.getQualifiedName() != null) {
String className = ClassUtil.getJVMClassName(cls);
apply(ats.getAccessTransformers().get(new Target.MethodTarget(className, method.getName(), ClassUtil.getAsmMethodSignature(method))), method.getModifierList());
}
}

element.acceptChildren(this);
}

private void apply(@Nullable Transformation at, PsiModifierList modifiers) {
if (at == null || !at.isValid()) return;

var targetAcc = at.modifier();
if (targetAcc == Transformation.Modifier.DEFAULT || !modifiers.hasModifierProperty(MODIFIER_TO_STRING.get(targetAcc))) {
final var existingModifier = Arrays.stream(modifiers.getChildren())
.filter(el -> el instanceof PsiKeyword)
.map(el -> (PsiKeyword) el)
.filter(kw -> ACCESS_MODIFIERS.contains(kw.getText()))
.findFirst();

if (targetAcc == Transformation.Modifier.DEFAULT) {
existingModifier.ifPresent(replacements::remove);
} else {
if (existingModifier.isPresent()) {
replacements.replace(existingModifier.get(), MODIFIER_TO_STRING.get(targetAcc));
} else {
if (modifiers.getChildren().length == 0) {
// Empty modifiers are blank so we basically replace them
replacements.insertAfter(modifiers, MODIFIER_TO_STRING.get(targetAcc) + " ");
} else {
replacements.insertBefore(modifiers, MODIFIER_TO_STRING.get(targetAcc) + " ");
}
}
}
}

var finalState = at.finalState();
if (finalState == Transformation.FinalState.REMOVEFINAL && modifiers.hasModifierProperty(PsiModifier.FINAL)) {
Arrays.stream(modifiers.getChildren())
.filter(el -> el instanceof PsiKeyword)
.map(el -> (PsiKeyword) el)
.filter(kw -> kw.getText().equals(PsiModifier.FINAL))
.findFirst()
.ifPresent(replacements::remove);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
net.neoforged.jst.accesstransformers.ATsPlugin
12 changes: 12 additions & 0 deletions api/src/main/java/net/neoforged/jst/api/Replacements.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ public void replace(PsiElement element, String newText) {
add(new Replacement(element.getTextRange(), newText));
}

public void remove(PsiElement element) {
final int pos = element.getTextRange().getEndOffset();
if (element.getContainingFile().getText().charAt(pos) == ' ') {
add(new Replacement(new TextRange(
element.getTextRange().getStartOffset(),
pos + 1
), ""));
} else {
replace(element, "");
}
}

public void insertBefore(PsiElement element, String newText) {
var startOffset = element.getTextRange().getStartOffset();
add(new Replacement(new TextRange(
Expand Down
5 changes: 5 additions & 0 deletions cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
implementation project(":api")
implementation "info.picocli:picocli:$picocli_version"
implementation project(":parchment")
implementation project(":accesstransformers")

testImplementation platform("org.junit:junit-bom:$junit_version")
testImplementation 'org.junit.jupiter:junit-jupiter'
Expand All @@ -29,6 +30,10 @@ test {
useJUnitPlatform()
}

shadowJar {
mergeServiceFiles()
}

assemble.configure {
dependsOn shadowJar
}
Expand Down
11 changes: 11 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ dependencyResolutionManagement {
maven {
url "https://maven.neoforged.net/releases/"
}
maven {
name 'Maven for PR #6' // https://github.com/neoforged/AccessTransformers/pull/6
Matyrobbrt marked this conversation as resolved.
Show resolved Hide resolved
url 'https://prmaven.neoforged.net/AccessTransformers/pr6'
content {
includeModule('net.neoforged', 'accesstransformers')
includeModule('net.neoforged.accesstransformers', 'at-parser')
includeModule('net.neoforged.accesstransformers', 'at-cli')
includeModule('net.neoforged.accesstransformers', 'at-modlauncher')
}
}
}
}

Expand All @@ -36,3 +46,4 @@ include 'api'
include 'cli'
include 'parchment'
include 'tests'
include 'accesstransformers'
2 changes: 2 additions & 0 deletions tests/data/accesstransformer/classes/accesstransformer.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public C1
default-f C2f
3 changes: 3 additions & 0 deletions tests/data/accesstransformer/classes/expected/C1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class C1 {

}
3 changes: 3 additions & 0 deletions tests/data/accesstransformer/classes/expected/C2f.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class C2f {

}
3 changes: 3 additions & 0 deletions tests/data/accesstransformer/classes/source/C1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class C1 {

}
3 changes: 3 additions & 0 deletions tests/data/accesstransformer/classes/source/C2f.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
final class C2f {

}
3 changes: 3 additions & 0 deletions tests/data/accesstransformer/fields/accesstransformer.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
protected ABC s1
public-f ABC s2
public ABC b1
5 changes: 5 additions & 0 deletions tests/data/accesstransformer/fields/expected/ABC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ABC {
protected String s1;
public String s2 = "";
public static boolean b1;
}
5 changes: 5 additions & 0 deletions tests/data/accesstransformer/fields/source/ABC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ABC {
private String s1;
public final String s2 = "";
protected static boolean b1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public com.example.RootClass$InnerClass1
protected-f com.example.RootClass$InnerClass1$InnerClassNested
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example;

class RootClass {
public class InnerClass1 {
protected static class InnerClassNested {

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example;

class RootClass {
class InnerClass1 {
static final class InnerClassNested {

}
}
}
3 changes: 3 additions & 0 deletions tests/data/accesstransformer/methods/accesstransformer.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public-f ABC doThing()V
protected ABC ret(Z)I
default ABC m2(Ljava/lang/String;)V
13 changes: 13 additions & 0 deletions tests/data/accesstransformer/methods/expected/ABC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ABC {
public void doThing() {

}

protected int ret(boolean b) {
return 0;
}

static void m2(String str) {

}
}
13 changes: 13 additions & 0 deletions tests/data/accesstransformer/methods/source/ABC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ABC {
public final void doThing() {

}

private int ret(boolean b) {
return 0;
}

private static void m2(String str) {

}
}
2 changes: 2 additions & 0 deletions tests/data/accesstransformer/wildcard/accesstransformer.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
default TestClass *
public TestClass *()
12 changes: 12 additions & 0 deletions tests/data/accesstransformer/wildcard/expected/TestClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class TestClass {
String f1;
int f2;

public void m1(int a) {

}

public static String m2(boolean ok) {
return "abc";
}
}
12 changes: 12 additions & 0 deletions tests/data/accesstransformer/wildcard/source/TestClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class TestClass {
private String f1;
private int f2;

private void m1(int a) {

}

private static String m2(boolean ok) {
return "abc";
}
}
Loading
Loading