IDEA-172425 Add tests to check that generated code is correct

This commit is contained in:
Vitaliy.Bibaev
2018-03-27 12:11:27 +03:00
parent 3317d14e8d
commit de907c6cd3
10 changed files with 465 additions and 0 deletions
@@ -0,0 +1,28 @@
public class WithReflectionAccess {
private int field = 10;
private WithReflectionAccess(int value) {
field = value;
}
public static void main(String[] args) {
WithReflectionAccess instance = new WithReflectionAccess(2000);
<caret>int a = 42;
}
private static void method() {
}
private int method(int arg) {
return 0;
}
private Object method(WithReflectionAccess arg) {
return arg;
}
public static void apply(Runnable runnable) {
runnable.run();
}
}
@@ -0,0 +1,38 @@
call text: WithReflectionAccess result = Test.invoke();
class:
static class Test {
static WithReflectionAccess invoke() {
return newWithReflectionAccess1(50);
}
public static WithReflectionAccess newWithReflectionAccess1(int value) {
try {
Class<?> klass = Class.forName("WithReflectionAccess");
java.lang.reflect.Constructor<?> member = null;
int interfaceNumber = -1;
Class<?>[] interfaces = null;
while (member == null) {
try {
member = klass.getDeclaredConstructor(int.class);
} catch (ReflectiveOperationException e) {
if (interfaceNumber == -1) {
interfaces = klass.getInterfaces();
interfaceNumber = 0;
}
if (interfaceNumber < interfaces.length) {
klass = interfaces[interfaceNumber];
interfaceNumber += 1;
} else {
klass = klass.getSuperclass();
if (klass == null) throw e;
interfaceNumber = -1;
}
}
}
member.setAccessible(true);
return (WithReflectionAccess) member.newInstance(value);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,44 @@
call text: new Test(instance).invoke();
class:
static class Test {
private WithReflectionAccess instance;
public Test(WithReflectionAccess instance) {
this.instance = instance;
}
public static int accessToFieldField1(Object object, Object value) {
try {
Class<?> klass = Class.forName("WithReflectionAccess");
java.lang.reflect.Field member = null;
int interfaceNumber = -1;
Class<?>[] interfaces = null;
while (member == null) {
try {
member = klass.getDeclaredField("field");
} catch (ReflectiveOperationException e) {
if (interfaceNumber == -1) {
interfaces = klass.getInterfaces();
interfaceNumber = 0;
}
if (interfaceNumber < interfaces.length) {
klass = interfaces[interfaceNumber];
interfaceNumber += 1;
} else {
klass = klass.getSuperclass();
if (klass == null) throw e;
interfaceNumber = -1;
}
}
}
member.setAccessible(true);
return (int) member.get(object);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public void invoke() {
System.out.println(accessToFieldField1(instance, null));
}
}
@@ -0,0 +1,38 @@
call text: Test.invoke();
class:
static class Test {
static void invoke() {
callMethod1(null);
}
public static void callMethod1(Object object) {
try {
Class<?> klass = Class.forName("WithReflectionAccess");
java.lang.reflect.Method member = null;
int interfaceNumber = -1;
Class<?>[] interfaces = null;
while (member == null) {
try {
member = klass.getDeclaredMethod("method");
} catch (ReflectiveOperationException e) {
if (interfaceNumber == -1) {
interfaces = klass.getInterfaces();
interfaceNumber = 0;
}
if (interfaceNumber < interfaces.length) {
klass = interfaces[interfaceNumber];
interfaceNumber += 1;
} else {
klass = klass.getSuperclass();
if (klass == null) throw e;
interfaceNumber = -1;
}
}
}
member.setAccessible(true);
member.invoke(object);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,38 @@
call text: Test.invoke();
class:
static class Test {
static void invoke() {
apply(() -> callMethod1(null));
}
public static void callMethod1(Object object) {
try {
Class<?> klass = Class.forName("WithReflectionAccess");
java.lang.reflect.Method member = null;
int interfaceNumber = -1;
Class<?>[] interfaces = null;
while (member == null) {
try {
member = klass.getDeclaredMethod("method");
} catch (ReflectiveOperationException e) {
if (interfaceNumber == -1) {
interfaces = klass.getInterfaces();
interfaceNumber = 0;
}
if (interfaceNumber < interfaces.length) {
klass = interfaces[interfaceNumber];
interfaceNumber += 1;
} else {
klass = klass.getSuperclass();
if (klass == null) throw e;
interfaceNumber = -1;
}
}
}
member.setAccessible(true);
member.invoke(object);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,44 @@
call text: Object result = new Test(instance).invoke();
class:
static class Test {
private WithReflectionAccess instance;
public Test(WithReflectionAccess instance) {
this.instance = instance;
}
public static Object callMethod1(Object object, WithReflectionAccess arg) {
try {
Class<?> klass = Class.forName("WithReflectionAccess");
java.lang.reflect.Method member = null;
int interfaceNumber = -1;
Class<?>[] interfaces = null;
while (member == null) {
try {
member = klass.getDeclaredMethod("method", Class.forName("WithReflectionAccess"));
} catch (ReflectiveOperationException e) {
if (interfaceNumber == -1) {
interfaces = klass.getInterfaces();
interfaceNumber = 0;
}
if (interfaceNumber < interfaces.length) {
klass = interfaces[interfaceNumber];
interfaceNumber += 1;
} else {
klass = klass.getSuperclass();
if (klass == null) throw e;
interfaceNumber = -1;
}
}
}
member.setAccessible(true);
return (Object) member.invoke(object, arg);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public Object invoke() {
return callMethod1(instance, instance);
}
}
@@ -0,0 +1,44 @@
call text: int result = new Test(instance).invoke();
class:
static class Test {
private WithReflectionAccess instance;
public Test(WithReflectionAccess instance) {
this.instance = instance;
}
public static int callMethod1(Object object, int arg) {
try {
Class<?> klass = Class.forName("WithReflectionAccess");
java.lang.reflect.Method member = null;
int interfaceNumber = -1;
Class<?>[] interfaces = null;
while (member == null) {
try {
member = klass.getDeclaredMethod("method", int.class);
} catch (ReflectiveOperationException e) {
if (interfaceNumber == -1) {
interfaces = klass.getInterfaces();
interfaceNumber = 0;
}
if (interfaceNumber < interfaces.length) {
klass = interfaces[interfaceNumber];
interfaceNumber += 1;
} else {
klass = klass.getSuperclass();
if (klass == null) throw e;
interfaceNumber = -1;
}
}
}
member.setAccessible(true);
return (int) member.invoke(object, arg);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public int invoke() {
return callMethod1(instance, 42);
}
}
@@ -0,0 +1,75 @@
call text: int result = new Test(instance).invoke();
class:
static class Test {
private WithReflectionAccess instance;
public Test(WithReflectionAccess instance) {
this.instance = instance;
}
public static int accessToFieldField1(Object object, Object value) {
try {
Class<?> klass = Class.forName("WithReflectionAccess");
java.lang.reflect.Field member = null;
int interfaceNumber = -1;
Class<?>[] interfaces = null;
while (member == null) {
try {
member = klass.getDeclaredField("field");
} catch (ReflectiveOperationException e) {
if (interfaceNumber == -1) {
interfaces = klass.getInterfaces();
interfaceNumber = 0;
}
if (interfaceNumber < interfaces.length) {
klass = interfaces[interfaceNumber];
interfaceNumber += 1;
} else {
klass = klass.getSuperclass();
if (klass == null) throw e;
interfaceNumber = -1;
}
}
}
member.setAccessible(true);
return (int) member.get(object);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public static int accessToFieldField2(Object object, Object value) {
try {
Class<?> klass = Class.forName("WithReflectionAccess");
java.lang.reflect.Field member = null;
int interfaceNumber = -1;
Class<?>[] interfaces = null;
while (member == null) {
try {
member = klass.getDeclaredField("field");
} catch (ReflectiveOperationException e) {
if (interfaceNumber == -1) {
interfaces = klass.getInterfaces();
interfaceNumber = 0;
}
if (interfaceNumber < interfaces.length) {
klass = interfaces[interfaceNumber];
interfaceNumber += 1;
} else {
klass = klass.getSuperclass();
if (klass == null) throw e;
interfaceNumber = -1;
}
}
}
member.setAccessible(true);
return (int) member.get(object);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public int invoke() {
return accessToFieldField1(instance, null) + accessToFieldField2(instance, null);
}
}
@@ -0,0 +1,44 @@
call text: new Test(instance).invoke();
class:
static class Test {
private WithReflectionAccess instance;
public Test(WithReflectionAccess instance) {
this.instance = instance;
}
public static void accessToFieldField1(Object object, Object value) {
try {
Class<?> klass = Class.forName("WithReflectionAccess");
java.lang.reflect.Field member = null;
int interfaceNumber = -1;
Class<?>[] interfaces = null;
while (member == null) {
try {
member = klass.getDeclaredField("field");
} catch (ReflectiveOperationException e) {
if (interfaceNumber == -1) {
interfaces = klass.getInterfaces();
interfaceNumber = 0;
}
if (interfaceNumber < interfaces.length) {
klass = interfaces[interfaceNumber];
interfaceNumber += 1;
} else {
klass = klass.getSuperclass();
if (klass == null) throw e;
interfaceNumber = -1;
}
}
}
member.setAccessible(true);
member.set(object, value);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public void invoke() {
accessToFieldField1(instance, 50);
}
}
@@ -0,0 +1,72 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.refactoring;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.psi.JavaCodeFragment;
import com.intellij.psi.JavaCodeFragmentFactory;
import com.intellij.psi.PsiElement;
import com.intellij.refactoring.extractMethod.PrepareFailedException;
import com.intellij.refactoring.extractMethodObject.ExtractLightMethodObjectHandler;
import com.intellij.testFramework.UsefulTestCase;
import org.jetbrains.annotations.NotNull;
/**
* @author Vitaliy.Bibaev
*/
public class ExtractMethodObject4DebuggerReflectionTest extends LightRefactoringTestCase {
public void testAccessField() throws PrepareFailedException {
doTest("System.out.println(instance.field)");
}
public void testUpdateField() throws PrepareFailedException {
doTest("instance.field = 50");
}
public void testAccessConstructor() throws PrepareFailedException {
doTest("new WithReflectionAccess(50)");
}
public void testAccessMethod() throws PrepareFailedException {
doTest("method()");
}
public void testAccessMethodReference() throws PrepareFailedException {
doTest("apply(WithReflectionAccess::method)");
}
public void testTwiceAccessToTheSameField() throws PrepareFailedException {
doTest("instance.field + instance.field");
}
public void testMethodWithParameter() throws PrepareFailedException {
doTest("instance.method(instance)");
}
public void testMethodWithPrimitiveParameter() throws PrepareFailedException {
doTest("instance.method(42)");
}
@NotNull
@Override
protected String getTestDataPath() {
return super.getTestDataPath() + "/refactoring/extractMethodObject4Debugger";
}
private void doTest(String evaluatedText) throws PrepareFailedException {
String testName = getTestName(true);
configureByFile("/WithReflectionAccess.java");
final int offset = getEditor().getCaretModel().getOffset();
final PsiElement context = getFile().findElementAt(offset);
final JavaCodeFragmentFactory fragmentFactory = JavaCodeFragmentFactory.getInstance(getProject());
final JavaCodeFragment fragment = fragmentFactory.createExpressionCodeFragment(evaluatedText, context, null, false);
final ExtractLightMethodObjectHandler.ExtractedData extractedData =
ExtractLightMethodObjectHandler.extractLightMethodObject(getProject(), context, fragment, "test", JavaSdkVersion.JDK_1_9);
assertNotNull(extractedData);
assertFalse(extractedData.useMagicAccessor());
String actualText = "call text: " + extractedData.getGeneratedCallText() + "\n" +
"class: " + "\n" +
extractedData.getGeneratedInnerClass().getText();
UsefulTestCase
.assertSameLinesWithFile(getTestDataPath() + "/outs/" + testName + ".out", actualText, true);
}
}