import com.intellij.openapi.compiler.ClassObject;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
-import com.intellij.openapi.util.registry.Registry;
import com.intellij.psi.PsiElement;
import com.intellij.refactoring.extractMethodObject.ExtractLightMethodObjectHandler;
import com.sun.jdi.ClassLoaderReference;
JavaSdkVersion version = JavaSdkVersion.fromVersionString(((VirtualMachineProxyImpl)process.getVirtualMachineProxy()).version());
Collection<ClassObject> classes = compile(version);
- defineClasses(version, classes, autoLoadContext, process, classLoader);
+ defineClasses(classes, autoLoadContext, process, classLoader);
try {
// invoke base evaluator on call code
}
}
- private void defineClasses(JavaSdkVersion version,
- Collection<ClassObject> classes,
+ private void defineClasses(Collection<ClassObject> classes,
EvaluationContext context,
DebugProcess process,
ClassLoaderReference classLoader) throws EvaluateException {
- boolean useMagicAccessorImpl = version != null && !version.isAtLeast(JavaSdkVersion.JDK_1_9) &&
- Registry.is("debugger.compiling.evaluator.magic.accessor");
+ boolean useMagicAccessorImpl = myData.useMagicAccessor();
for (ClassObject cls : classes) {
if (cls.getPath().contains(GEN_CLASS_NAME)) {
import com.intellij.compiler.CompilerConfiguration;
import com.intellij.compiler.server.BuildManager;
+import com.intellij.debugger.engine.DebugProcessImpl;
import com.intellij.debugger.engine.SuspendContextImpl;
import com.intellij.debugger.engine.evaluation.EvaluateException;
import com.intellij.debugger.engine.evaluation.expression.ExpressionEvaluator;
if (Registry.is("debugger.compiling.evaluator") && psiContext != null) {
return ApplicationManager.getApplication().runReadAction((ThrowableComputable<ExpressionEvaluator, EvaluateException>)() -> {
try {
- boolean useReflection = Registry.is("debugger.compiling.evaluator.reflection.access.with.java8");
XDebugSession currentSession = XDebuggerManager.getInstance(project).getCurrentSession();
- if (!useReflection && currentSession != null) {
- XSuspendContext suspendContext = currentSession.getSuspendContext();
- if (suspendContext instanceof SuspendContextImpl) {
- JavaSdkVersion version =
- JavaSdkVersion.fromVersionString(((SuspendContextImpl)suspendContext).getDebugProcess().getVirtualMachineProxy().version());
- useReflection = version != null && version.isAtLeast(JavaSdkVersion.JDK_1_9);
- }
- }
-
+ JavaSdkVersion javaVersion = getJavaVersion(currentSession);
ExtractLightMethodObjectHandler.ExtractedData data = ExtractLightMethodObjectHandler.extractLightMethodObject(
project,
findPhysicalContext(psiContext),
fragmentFactory.apply(psiContext),
getGeneratedClassName(),
- useReflection);
+ javaVersion);
if (data != null) {
return new CompilingEvaluatorImpl(project, psiContext, data);
}
}
return element;
}
+
+ @Nullable
+ public static JavaSdkVersion getJavaVersion(@Nullable XDebugSession session) {
+ if (session != null) {
+ XSuspendContext suspendContext = session.getSuspendContext();
+ if (suspendContext instanceof SuspendContextImpl) {
+ DebugProcessImpl debugProcess = ((SuspendContextImpl)suspendContext).getDebugProcess();
+ return JavaSdkVersion.fromVersionString(debugProcess.getVirtualMachineProxy().version());
+ }
+ }
+
+ return null;
+ }
}
import com.intellij.codeInsight.CodeInsightUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
+import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
private final String myGeneratedCallText;
private final PsiClass myGeneratedInnerClass;
private final PsiElement myAnchor;
+ private final boolean myUseMagicAccessor;
- public ExtractedData(String generatedCallText, PsiClass generatedInnerClass, PsiElement anchor) {
+ public ExtractedData(String generatedCallText, PsiClass generatedInnerClass, PsiElement anchor, boolean useMagicAccessor) {
myGeneratedCallText = generatedCallText;
myGeneratedInnerClass = generatedInnerClass;
myAnchor = anchor;
+ myUseMagicAccessor = useMagicAccessor;
}
public PsiElement getAnchor() {
public PsiClass getGeneratedInnerClass() {
return myGeneratedInnerClass;
}
+
+ public boolean useMagicAccessor() {
+ return myUseMagicAccessor;
+ }
}
@Nullable
@Nullable PsiElement originalContext,
@NotNull final PsiCodeFragment fragment,
final String methodName,
- boolean useReflection) throws PrepareFailedException {
+ @Nullable JavaSdkVersion javaVersion) throws PrepareFailedException {
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
PsiElement[] elements = completeToStatementArray(fragment, elementFactory);
if (elements == null) {
PsiStatement outStatement = elementFactory.createStatementFromText("System.out.println(" + outputVariables + ");", anchor);
outStatement = (PsiStatement)container.addAfter(outStatement, elementsCopy[elementsCopy.length - 1]);
- if (Registry.is("debugger.compiling.evaluator.magic.accessor") && !useReflection) {
+ boolean useMagicAccessor = Registry.is("debugger.compiling.evaluator.magic.accessor") &&
+ javaVersion != null && !javaVersion.isAtLeast(JavaSdkVersion.JDK_1_9);
+ if (useMagicAccessor) {
copy.accept(new JavaRecursiveElementWalkingVisitor() {
private void makePublic(PsiMember method) {
if (method.hasModifierProperty(PsiModifier.PRIVATE)) {
final PsiClass inner = extractMethodObjectProcessor.getInnerClass();
final PsiMethod[] methods = inner.findMethodsByName("invoke", false);
+ boolean useReflection = javaVersion == null || javaVersion.isAtLeast(JavaSdkVersion.JDK_1_9) ||
+ Registry.is("debugger.compiling.evaluator.reflection.access.with.java8");
if (useReflection && methods.length == 1) {
final PsiMethod method = methods[0];
CompositeReflectionAccessor.createAccessorToEverything(inner, elementFactory)
final String generatedCall = copy.getText().substring(startOffset, outStatement.getTextOffset());
return new ExtractedData(generatedCall,
(PsiClass)CodeStyleManager.getInstance(project).reformat(extractMethodObjectProcessor.getInnerClass()),
- originalAnchor);
+ originalAnchor, useMagicAccessor);
}
@Nullable
package com.intellij.java.refactoring;
import com.intellij.JavaTestUtil;
+import com.intellij.openapi.projectRoots.JavaSdkVersion;
+import com.intellij.openapi.util.registry.Registry;
import com.intellij.psi.JavaCodeFragment;
import com.intellij.psi.JavaCodeFragmentFactory;
import com.intellij.psi.PsiClass;
final JavaCodeFragmentFactory fragmentFactory = JavaCodeFragmentFactory.getInstance(getProject());
final JavaCodeFragment fragment = codeBlock ? fragmentFactory.createCodeBlockCodeFragment(evaluatedText, context, false) : fragmentFactory.createExpressionCodeFragment(evaluatedText, context, null, false);
final ExtractLightMethodObjectHandler.ExtractedData extractedData =
- ExtractLightMethodObjectHandler.extractLightMethodObject(getProject(), context, fragment, "test", false);
+ ExtractLightMethodObjectHandler.extractLightMethodObject(getProject(), context, fragment, "test", JavaSdkVersion.JDK_1_8);
assertNotNull(extractedData);
assertEquals(expectedCallSite, extractedData.getGeneratedCallText());
final PsiClass innerClass = extractedData.getGeneratedInnerClass();
assertEquals(expectedClass, innerClass.getText());
}
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ Registry.get("debugger.compiling.evaluator.magic.accessor").setValue(true);
+ Registry.get("debugger.compiling.evaluator.reflection.access.with.java8").setValue(false);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ try {
+ Registry.get("debugger.compiling.evaluator.magic.accessor").resetToDefault();
+ Registry.get("debugger.compiling.evaluator.reflection.access.with.java8").resetToDefault();
+ }
+ finally {
+ super.tearDown();
+ }
+ }
+
public void testSimpleGeneration() throws Exception {
doTest("int i = 0; int j = 0;", "Test test = new Test().invoke();int i = test.getI();int j = test.getJ();",