Skip to content

Commit

Permalink
test: add tests for MethodType methods (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell committed Aug 4, 2023
1 parent 94ba427 commit f5eb675
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ class MethodHandleInspectionsTest : LightJavaCodeInsightFixtureTestCase() {
}

private fun doTypeCheckingTest() {
myFixture.enableInspections(MethodHandleTypeHelperInspection())
doTypeCheckingTest(false)
}

private fun doTypeCheckingTest(onlyAtCaret: Boolean) {
val methodHandleTypeHelperInspection =
if (onlyAtCaret) MethodHandleTypeHelperInspection { it == myFixture.elementAtCaret }
else MethodHandleTypeHelperInspection { true }
myFixture.enableInspections(methodHandleTypeHelperInspection)
myFixture.testHighlighting(false, true, false, getTestName(false) + ".java")
}

Expand All @@ -29,4 +36,28 @@ class MethodHandleInspectionsTest : LightJavaCodeInsightFixtureTestCase() {

fun testSimpleIdentity() = doTypeCheckingTest()

fun testMethodTypeAppendParameterTypes() = doTypeCheckingTest()

fun testMethodTypeChangeParameterType() = doTypeCheckingTest()

fun testMethodTypeChangeReturnType() = doTypeCheckingTest()

fun testMethodTypeCreateBasic() = doTypeCheckingTest()

fun testMethodTypeCreateWithParameters() = doTypeCheckingTest()

fun testMethodTypeDropParameterTypes() = doTypeCheckingTest()

fun testMethodTypeErase() = doTypeCheckingTest()

fun testMethodTypeGeneric() = doTypeCheckingTest()

fun testMethodTypeGenericMethodType() = doTypeCheckingTest()

fun testMethodTypeInsertParameterTypes() = doTypeCheckingTest()

fun testMethodTypeWrap() = doTypeCheckingTest()

fun testMethodTypeUnwrap() = doTypeCheckingTest()

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import de.sirywell.methodhandleplugin.TypeData
* This way, we can reuse existing test infrastructure that is meant to assert presence of inspection results.
* I'm aware this is a bit hacky, but it's just too simple to go a different route.
*/
class MethodHandleTypeHelperInspection : LocalInspectionTool() {
class MethodHandleTypeHelperInspection(private val elementFilter: (PsiElement) -> Boolean) : LocalInspectionTool() {

override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : PsiElementVisitor() {
override fun visitElement(element: PsiElement) {
if (!elementFilter(element)) return
val foundType = TypeData.forFile(element.containingFile)[element] ?: return
holder.registerProblem(element, foundType.toString(), ProblemHighlightType.INFORMATION)
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/testData/MethodTypeAppendParameterTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

class MethodTypeAppendParameterTypes {
<info descr="(boolean,String,double)int">private static final MethodType A = <info descr="(boolean,String,double)int"><info descr="(boolean)int">MethodType.methodType(int.class, boolean.class)</info>.appendParameterTypes(String.class, double.class)</info>;</info>
}
8 changes: 8 additions & 0 deletions src/test/testData/MethodTypeChangeParameterType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

class MethodTypeChangeParameterType {
<info descr="(double,int)void">private static final MethodType A = <info descr="(double,int)void"><info descr="(boolean,int)void">MethodType.methodType(void.class, boolean.class, int.class)</info>.changeParameterType(0, double.class)</info>;</info>
<info descr="(boolean,double)void">private static final MethodType B = <info descr="(boolean,double)void"><info descr="(boolean,int)void">MethodType.methodType(void.class, boolean.class, int.class)</info>.changeParameterType(1, double.class)</info>;</info>
}
8 changes: 8 additions & 0 deletions src/test/testData/MethodTypeChangeReturnType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

class MethodTypeChangeReturnType {
<info descr="(boolean,int)double">private static final MethodType A = <info descr="(boolean,int)double"><info descr="(boolean,int)void">MethodType.methodType(void.class, boolean.class, int.class)</info>.changeReturnType(double.class)</info>;</info>
<info descr="()double">private static final MethodType B = <info descr="()double"><info descr="()int">MethodType.methodType(int.class)</info>.changeReturnType(double.class)</info>;</info>
}
8 changes: 8 additions & 0 deletions src/test/testData/MethodTypeCreateBasic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

class MethodTypeWrap {
<info descr="()void">private static final MethodType A = <info descr="()void">MethodType.methodType(void.class)</info>;</info>
<info descr="(String)int">private static final MethodType B = <info descr="(String)int">MethodType.methodType(int.class, String.class)</info>;</info>
}
7 changes: 7 additions & 0 deletions src/test/testData/MethodTypeCreateWithParameters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

class MethodTypeWrap {
<info descr="(int,float)void">private static final MethodType A = <info descr="(int,float)void">MethodType.methodType(void.class, <info descr="(int,float)String">MethodType.methodType(String.class, int.class, float.class)</info>)</info>;</info>
}
7 changes: 7 additions & 0 deletions src/test/testData/MethodTypeDropParameterTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

class MethodTypeDropParameterTypes {
<info descr="(boolean,float,long)void">private static final MethodType A = <info descr="(boolean,float,long)void"><info descr="(boolean,int,double,float,long)void">MethodType.methodType(void.class, boolean.class, int.class, double.class, float.class, long.class)</info>.dropParameterTypes(1, 3)</info>;</info>
}
5 changes: 5 additions & 0 deletions src/test/testData/MethodTypeErase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java.lang.invoke.MethodType;

class MethodTypeErase {
<info descr="(Object,int,double,Object,Object)void">private static final MethodType A = <info descr="(Object,int,double,Object,Object)void"><info descr="(String,int,double,Object,Integer)void">MethodType.methodType(void.class, String.class, int.class, double.class, Object.class, Integer.class)</info>.erase()</info>;</info>
}
5 changes: 5 additions & 0 deletions src/test/testData/MethodTypeGeneric.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java.lang.invoke.MethodType;

class MethodTypeGeneric {
<info descr="(Object,Object,Object,Object,Object)Object">private static final MethodType A = <info descr="(Object,Object,Object,Object,Object)Object"><info descr="(String,int,double,Object,long)void">MethodType.methodType(void.class, String.class, int.class, double.class, Object.class, long.class)</info>.generic()</info>;</info>
}
7 changes: 7 additions & 0 deletions src/test/testData/MethodTypeGenericMethodType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.lang.invoke.MethodType;

class MethodTypeGenericMethodType {
<info descr="(Object,Object,Object)Object">private static final MethodType A = <info descr="(Object,Object,Object)Object">MethodType.genericMethodType(3)</info>;</info>
<info descr="(Object,Object,Object,Object,Object[])Object">private static final MethodType B = <info descr="(Object,Object,Object,Object,Object[])Object">MethodType.genericMethodType(4, true)</info>;</info>
<info descr="(Object,Object,Object,Object,Object)Object">private static final MethodType C = <info descr="(Object,Object,Object,Object,Object)Object">MethodType.genericMethodType(5, false)</info>;</info>
}
7 changes: 7 additions & 0 deletions src/test/testData/MethodTypeInsertParameterTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

class MethodTypeInsertParameterTypes {
<info descr="(boolean,int,double,float,long)void">private static final MethodType A = <info descr="(boolean,int,double,float,long)void"><info descr="(boolean,float,long)void">MethodType.methodType(void.class, boolean.class, float.class, long.class)</info>.insertParameterTypes(1, int.class, double.class)</info>;</info>
}
7 changes: 7 additions & 0 deletions src/test/testData/MethodTypeUnwrap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

class MethodTypeUnwrap {
<info descr="(boolean,String,double)int">private static final MethodType A = <info descr="(boolean,String,double)int"><info descr="(Boolean,String,Double)Integer">MethodType.methodType(Integer.class, Boolean.class, String.class, Double.class)</info>.unwrap()</info>;</info>
}
7 changes: 7 additions & 0 deletions src/test/testData/MethodTypeWrap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

class MethodTypeWrap {
<info descr="(Boolean,String,Double)Integer">private static final MethodType A = <info descr="(Boolean,String,Double)Integer"><info descr="(boolean,String,double)int">MethodType.methodType(int.class, boolean.class, String.class, double.class)</info>.wrap()</info>;</info>
}

0 comments on commit f5eb675

Please sign in to comment.