Skip to content

Add a plugin for unpick v3 #52

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,27 @@ To create the executable jar with your custom transformer, you should shadow the
Note that this tool is not intended to be run by users directly. Rather it is integrated into
the [NeoGradle](https://github.com/neoforged/NeoGradle) build process.

It can be invoked as a standalone executable Jar-File. Java 17 is required.
It can be invoked as a standalone executable Jar-File. Java 21 is required.

```
Usage: jst [-hV] [--in-format=<inputFormat>] [--libraries-list=<librariesList>]
Usage: jst [-hV] [--debug] [--in-format=<inputFormat>] [--libraries-list=<librariesList>]
[--max-queue-depth=<maxQueueDepth>] [--out-format=<outputFormat>]
[--classpath=<addToClasspath>]... [--ignore-prefix=<ignoredPrefixes>]...
[--enable-parchment --parchment-mappings=<mappingsPath> [--[no-]parchment-javadoc]
[--problems-report=<problemsReport>] [--classpath=<addToClasspath>]...
[--ignore-prefix=<ignoredPrefixes>]... [--enable-parchment
--parchment-mappings=<mappingsPath> [--[no-]parchment-javadoc]
[--parchment-conflict-prefix=<conflictPrefix>]] [--enable-accesstransformers
--access-transformer=<atFiles> [--access-transformer=<atFiles>]...
[--access-transformer-validation=<validation>]] [--enable-interface-injection
[--interface-injection-stubs=<stubOut>]
[--interface-injection-marker=<annotationMarker>]
[--interface-injection-data=<paths>]...] INPUT OUTPUT
[--interface-injection-data=<paths>]...] [--enable-unpick [--unpick-data=<paths>]...]
INPUT OUTPUT
INPUT Path to a single Java-file, a source-archive or a folder containing the
source to transform.
OUTPUT Path to where the resulting source should be placed.
--classpath=<addToClasspath>
Additional classpath entries to use. Is combined with --libraries-list.
--debug Print additional debugging information
-h, --help Show this help message and exit.
--ignore-prefix=<ignoredPrefixes>
Do not apply transformations to paths that start with any of these
Expand All @@ -89,6 +92,8 @@ Usage: jst [-hV] [--in-format=<inputFormat>] [--libraries-list=<librariesList>]
--out-format=<outputFormat>
Specify the format of OUTPUT explicitly. Allows the same options as
--in-format.
--problems-report=<problemsReport>
Write problems to this report file.
-V, --version Print version information and exit.
Plugin - parchment
--enable-parchment Enable parchment
Expand Down Expand Up @@ -116,6 +121,10 @@ Plugin - interface-injection
injected interfaces
--interface-injection-stubs=<stubOut>
The path to a zip to save interface stubs in
Plugin - unpick
--enable-unpick Enable unpick
--unpick-data=<paths>
The paths to read unpick definition files from
```

## Licenses
Expand Down
13 changes: 13 additions & 0 deletions api/src/main/java/net/neoforged/jst/api/PsiHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.intellij.lang.jvm.types.JvmPrimitiveTypeKind;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiParameterListOwner;
import com.intellij.psi.PsiPrimitiveType;
import com.intellij.psi.PsiReferenceExpression;
import com.intellij.psi.PsiTypeParameter;
import com.intellij.psi.PsiTypes;
import com.intellij.psi.PsiWhiteSpace;
Expand All @@ -17,6 +20,7 @@
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.containers.ObjectIntHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -242,4 +246,13 @@ public static int getLastLineLength(PsiWhiteSpace psiWhiteSpace) {
return psiWhiteSpace.getTextLength();
}
}

@Nullable
public static PsiElement resolve(PsiReferenceExpression expression) {
try {
return expression.resolve();
} catch (Exception ignored) {
return null;
}
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ subprojects {

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
languageVersion = JavaLanguageVersion.of(21)
}
}

Expand Down
1 change: 1 addition & 0 deletions cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
include project(":parchment")
include project(":accesstransformers")
include project(':interfaceinjection')
include project(':unpick')

testImplementation platform("org.junit:junit-bom:$junit_version")
testImplementation 'org.junit.jupiter:junit-jupiter'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public void putDirectory(String relativePath) throws IOException {
@Override
public void putFile(String relativePath, FileTime lastModified, byte[] content) throws IOException {
var targetPath = path.resolve(relativePath);
if (targetPath.getParent() != null) {
Files.createDirectories(targetPath.getParent());
}
Files.write(targetPath, content);
Files.setLastModifiedTime(targetPath, lastModified);
}
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ include 'parchment'
include 'tests'
include 'accesstransformers'
include 'interfaceinjection'
include 'unpick'
16 changes: 16 additions & 0 deletions tests/data/unpick/const/def.unpick
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
unpick v3

group String
com.example.Constants.VERSION

group float
@strict
java.lang.Math.PI
java.lang.Math.PI / 3

group float
com.example.Constants.FLOAT_CT

group long
com.example.Constants.LONG_VAL
(com.example.Constants.LONG_VAL + 1) * 2
9 changes: 9 additions & 0 deletions tests/data/unpick/const/expected/com/example/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example;

public class Constants {
public static final String VERSION = "1.21.4";

public static final float FLOAT_CT = 2.5;

public static final long LONG_VAL = 34L;
}
21 changes: 21 additions & 0 deletions tests/data/unpick/const/expected/com/stuff/Uses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.stuff;

import com.example.Constants;

public class Uses {
public String fld = Constants.VERSION;

void run() {
String s = Constants.VERSION + "2";

float f = Math.PI;

f = Math.PI / 3;

double d = 3.141592653589793d; // PI unpick is strict float so this should not be replaced

d = Constants.FLOAT_CT; // but the other float unpick isn't so this double literal should be replaced

System.out.println(Long.toHexString((Constants.LONG_VAL + 1) * 2));
}
}
9 changes: 9 additions & 0 deletions tests/data/unpick/const/source/com/example/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example;

public class Constants {
public static final String VERSION = "1.21.4";

public static final float FLOAT_CT = 2.5;

public static final long LONG_VAL = 34L;
}
19 changes: 19 additions & 0 deletions tests/data/unpick/const/source/com/stuff/Uses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.stuff;

public class Uses {
public String fld = "1.21.4";

void run() {
String s = "1.21.4" + "2";

float f = 3.141592653589793f;

f = 1.0471975511965976f;

double d = 3.141592653589793d; // PI unpick is strict float so this should not be replaced

d = 2.5d; // but the other float unpick isn't so this double literal should be replaced

System.out.println(Long.toHexString(70L));
}
}
11 changes: 11 additions & 0 deletions tests/data/unpick/flags/def.unpick
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
unpick v3

group int Flags
@flags
com.example.Example.FLAG_1
com.example.Example.FLAG_2
com.example.Example.FLAG_3
com.example.Example.FLAG_4

target_method com.example.Example applyFlags(I)V
param 0 Flags
18 changes: 18 additions & 0 deletions tests/data/unpick/flags/expected/com/example/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example;

public class Example {
public static final int
FLAG_1 = 2,
FLAG_2 = 4,
FLAG_3 = 8,
FLAG_4 = 16;

public static void main(String[] args) {
applyFlags(Example.FLAG_1);
applyFlags(Example.FLAG_1 | Example.FLAG_2);
applyFlags(Example.FLAG_3 | Example.FLAG_4 | Example.FLAG_1 | 129);
applyFlags(-1);
}

public static void applyFlags(int flags) {}
}
18 changes: 18 additions & 0 deletions tests/data/unpick/flags/source/com/example/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example;

public class Example {
public static final int
FLAG_1 = 2,
FLAG_2 = 4,
FLAG_3 = 8,
FLAG_4 = 16;

public static void main(String[] args) {
applyFlags(2);
applyFlags(6);
applyFlags(155);
applyFlags(-1);
}

public static void applyFlags(int flags) {}
}
21 changes: 21 additions & 0 deletions tests/data/unpick/formats/def.unpick
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
unpick v3

group int HEXInt
@format hex
target_method com.example.Example acceptHex(I)V
param 0 HEXInt

group int BINInt
@format binary
target_method com.example.Example acceptBin(I)V
param 0 BINInt

group int OCTInt
@format octal
target_method com.example.Example acceptOct(I)V
param 0 OCTInt

group int CharInt
@format char
target_method com.example.Example acceptChar(C)V
param 0 CharInt
17 changes: 17 additions & 0 deletions tests/data/unpick/formats/expected/com/example/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example;

public class Example {

void execute() {
acceptHex(0xA505);
acceptBin(0b1010100111010110000);
acceptOct(017350);
acceptChar('d');
}

void acceptHex(int hex) {}
void acceptBin(int b) {}
void acceptOct(int oct) {}

void acceptChar(char c) {}
}
17 changes: 17 additions & 0 deletions tests/data/unpick/formats/source/com/example/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example;

public class Example {

void execute() {
acceptHex(42245);
acceptBin(347824);
acceptOct(7912);
acceptChar(100);
}

void acceptHex(int hex) {}
void acceptBin(int b) {}
void acceptOct(int oct) {}

void acceptChar(char c) {}
}
10 changes: 10 additions & 0 deletions tests/data/unpick/local_variables/def.unpick
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
unpick v3

group int Color
@format hex
com.example.Example.RED
com.example.Example.PURPLE
com.example.Example.PINK

target_method com.example.Example setColor(I)V
param 0 Color
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example;

public class Example {
public static final int
RED = 0xFF0000,
PURPLE = 0x800080,
PINK = 0xFFC0CB;

public static void acceptColor(int in) {
int color = 0xD7837F;
if (in < 0) {
color = Example.PURPLE;
} else {
color = in == 0x0 ? Example.RED : Example.PINK;
}

setColor(color);
}

public static void setColor(int color) {}
}
21 changes: 21 additions & 0 deletions tests/data/unpick/local_variables/source/com/example/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example;

public class Example {
public static final int
RED = 0xFF0000,
PURPLE = 0x800080,
PINK = 0xFFC0CB;

public static void acceptColor(int in) {
int color = 14123903;
if (in < 0) {
color = 8388736;
} else {
color = in == 0 ? 16711680 : 16761035;
}

setColor(color);
}

public static void setColor(int color) {}
}
9 changes: 9 additions & 0 deletions tests/data/unpick/returns/def.unpick
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
unpick v3

group int Constant
com.example.Example.ONE
com.example.Example.TWO
com.example.Example.FOUR

target_method com.example.Example getNumber(ZZ)I
return Constant
21 changes: 21 additions & 0 deletions tests/data/unpick/returns/expected/com/example/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example;

public class Example {
public static final int
ONE = 1,
TWO = 2,
FOUR = 4;

public static int getNumber(boolean odd, boolean b) {
int value = 0;
if (odd) {
value = Example.ONE;
} else if (b) {
return Example.FOUR;
} else {
value = Example.TWO;
}

return value;
}
}
Loading
Loading