Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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));
}
}
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;
}
}
21 changes: 21 additions & 0 deletions tests/data/unpick/returns/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
ONE = 1,
TWO = 2,
FOUR = 4;

public static int getNumber(boolean odd, boolean b) {
int value = 0;
if (odd) {
value = 1;
} else if (b) {
return 4;
} else {
value = 2;
}

return value;
}
}
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
}
}
Loading
Loading