Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
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));
}
}
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) {}
}
14 changes: 14 additions & 0 deletions tests/data/unpick/scoped/def.unpick
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
unpick v3

group int
@scope class com.example.Example
com.example.Example.V1
com.example.Example.V2

group int
@scope method com.Outsider anotherExecute ()V
com.Outsider.DIFFERENT_CONST

group int
@scope package com.example
com.example.Example.FOUR
17 changes: 17 additions & 0 deletions tests/data/unpick/scoped/expected/com/Outsider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com;

public class Outsider {
private static final int DIFFERENT_CONST = 12;

public void execute() {
int i = 472; // This should NOT be unpicked to Example.V1

int j = 12; // This should NOT be unpicked to DIFFERENT_CONST

int k = 4; // This should NOT be unpicked to Example.FOUR since it's outside the package
}

public void anotherExecute() {
int i = Outsider.DIFFERENT_CONST; // This should be replaced with DIFFERENT_CONST
}
}
12 changes: 12 additions & 0 deletions tests/data/unpick/scoped/expected/com/example/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example;

public class Example {
private static final int V1 = 472, V2 = 84;
static final int FOUR = 4;

void execute() {
System.out.println(Example.V1);

System.out.println(Example.V2);
}
}
5 changes: 5 additions & 0 deletions tests/data/unpick/scoped/expected/com/example/Example2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example;

public class Example2 {
private final int fld = Example.FOUR;
}
17 changes: 17 additions & 0 deletions tests/data/unpick/scoped/source/com/Outsider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com;

public class Outsider {
private static final int DIFFERENT_CONST = 12;

public void execute() {
int i = 472; // This should NOT be unpicked to Example.V1

int j = 12; // This should NOT be unpicked to DIFFERENT_CONST

int k = 4; // This should NOT be unpicked to Example.FOUR since it's outside the package
}

public void anotherExecute() {
int i = 12; // This should be replaced with DIFFERENT_CONST
}
}
12 changes: 12 additions & 0 deletions tests/data/unpick/scoped/source/com/example/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example;

public class Example {
private static final int V1 = 472, V2 = 84;
static final int FOUR = 4;

void execute() {
System.out.println(472);

System.out.println(84);
}
}
5 changes: 5 additions & 0 deletions tests/data/unpick/scoped/source/com/example/Example2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example;

public class Example2 {
private final int fld = 4;
}
29 changes: 29 additions & 0 deletions tests/src/test/java/net/neoforged/jst/tests/EmbeddedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,24 @@ void testGenerics() throws Exception {
}
}

@Nested
class Unpick {
@Test
void testConst() throws Exception {
runUnpickTest("const");
}

@Test
void testFormats() throws Exception {
runUnpickTest("formats");
}

@Test
void testScoped() throws Exception {
runUnpickTest("scoped");
}
}

protected final void runInterfaceInjectionTest(String testDirName, Path tempDir, String... additionalArgs) throws Exception {
var stub = tempDir.resolve("jst-" + testDirName + "-stub.jar");
testDirName = "interfaceinjection/" + testDirName;
Expand All @@ -368,6 +386,17 @@ protected final void runInterfaceInjectionTest(String testDirName, Path tempDir,
}
}

protected final void runUnpickTest(String testDirName, String... additionalArgs) throws Exception {
testDirName = "unpick/" + testDirName;
var testDir = testDataRoot.resolve(testDirName);
var inputPath = testDir.resolve("def.unpick");

var args = new ArrayList<>(Arrays.asList("--enable-unpick", "--unpick-data", inputPath.toString()));
args.addAll(Arrays.asList(additionalArgs));

runTest(testDirName, UnaryOperator.identity(), args.toArray(String[]::new));
}

protected final void runATTest(String testDirName, final String... extraArgs) throws Exception {
testDirName = "accesstransformer/" + testDirName;
var atPath = testDataRoot.resolve(testDirName).resolve("accesstransformer.cfg");
Expand Down
8 changes: 8 additions & 0 deletions unpick/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
id 'java-library'
}

dependencies {
implementation project(':api')
implementation 'net.fabricmc.unpick:unpick-format-utils:3.0.0-beta.8'
}
Loading
Loading