Extract base class for CompilingEvaluator to reuse it in Kotlin

This commit is contained in:
Natalia Ukhorskaya
2014-12-04 11:58:53 +03:00
committed by Egor.Ushakov
parent 8302b920e1
commit 0aa0260983
3 changed files with 120 additions and 79 deletions
@@ -27,19 +27,12 @@ import com.intellij.debugger.engine.evaluation.expression.Modifier;
import com.intellij.debugger.impl.DebuggerUtilsEx;
import com.intellij.debugger.jdi.VirtualMachineProxyImpl;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.JdkVersionUtil;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.refactoring.extractMethodObject.ExtractLightMethodObjectHandler;
import com.intellij.util.PathsList;
import com.sun.jdi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.org.objectweb.asm.ClassReader;
@@ -55,9 +48,9 @@ import java.util.*;
/**
* @author egor
*/
public class CompilingEvaluator implements ExpressionEvaluator {
@NotNull private final PsiElement myPsiContext;
@NotNull private final ExtractLightMethodObjectHandler.ExtractedData myData;
public abstract class CompilingEvaluator implements ExpressionEvaluator {
@NotNull protected final PsiElement myPsiContext;
@NotNull protected final ExtractLightMethodObjectHandler.ExtractedData myData;
public CompilingEvaluator(@NotNull PsiElement context, @NotNull ExtractLightMethodObjectHandler.ExtractedData data) {
myPsiContext = context;
@@ -203,25 +196,8 @@ public class CompilingEvaluator implements ExpressionEvaluator {
//private static final String GEN_CLASS_FULL_NAME = GEN_CLASS_PACKAGE + '.' + GEN_CLASS_NAME;
//private static final String GEN_METHOD_NAME = "invoke";
private String getClassCode() {
return ApplicationManager.getApplication().runReadAction(new Computable<String>() {
@Override
public String compute() {
return myData.getGeneratedInnerClass().getContainingFile().getText();
}
});
}
private String getMainClassName() {
return ApplicationManager.getApplication().runReadAction(new Computable<String>() {
@Override
public String compute() {
return FileUtil.getNameWithoutExtension(myData.getGeneratedInnerClass().getContainingFile().getName());
}
});
}
private String getGenClassQName() {
protected String getGenClassQName() {
return ApplicationManager.getApplication().runReadAction(new Computable<String>() {
@Override
public String compute() {
@@ -250,57 +226,13 @@ public class CompilingEvaluator implements ExpressionEvaluator {
///////////////// Compiler stuff
@NotNull
private Collection<OutputFileObject> compile(String target) throws EvaluateException {
if (!SystemInfo.isJavaVersionAtLeast(target)) {
throw new EvaluateException("Unable to compile for target level " + target + ". Need to run IDEA on java version at least " + target + ", currently running on " + SystemInfo.JAVA_RUNTIME_VERSION);
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
MemoryFileManager manager = new MemoryFileManager(compiler);
DiagnosticCollector<JavaFileObject> diagnostic = new DiagnosticCollector<JavaFileObject>();
Module module = ApplicationManager.getApplication().runReadAction(new Computable<Module>() {
@Override
public Module compute() {
return ModuleUtilCore.findModuleForPsiElement(myPsiContext);
}
});
List<String> options = new ArrayList<String>();
if (module != null) {
options.add("-cp");
PathsList cp = ModuleRootManager.getInstance(module).orderEntries().compileOnly().recursively().exportedOnly().withoutSdk().getPathsList();
options.add(cp.getPathsString());
}
if (!StringUtil.isEmpty(target)) {
options.add("-source");
options.add(target);
options.add("-target");
options.add(target);
}
try {
if (!compiler.getTask(null,
manager,
diagnostic,
options,
null,
Collections.singletonList(new SourceFileObject(getMainClassName(), JavaFileObject.Kind.SOURCE, getClassCode()))
).call()) {
StringBuilder res = new StringBuilder("Compilation failed:\n");
for (Diagnostic<? extends JavaFileObject> d : diagnostic.getDiagnostics()) {
res.append(d);
}
throw new EvaluateException(res.toString());
}
}
catch (Exception e) {
throw new EvaluateException(e.getMessage());
}
return manager.classes;
}
protected abstract Collection<OutputFileObject> compile(String target) throws EvaluateException;
private static URI getUri(String name, JavaFileObject.Kind kind) {
return URI.create("memo:///" + name.replace('.', '/') + kind.extension);
}
private static class SourceFileObject extends SimpleJavaFileObject {
protected static class SourceFileObject extends SimpleJavaFileObject {
private final String myContent;
SourceFileObject(String name, Kind kind, String content) {
@@ -314,7 +246,7 @@ public class CompilingEvaluator implements ExpressionEvaluator {
}
}
private static class OutputFileObject extends SimpleJavaFileObject {
protected static class OutputFileObject extends SimpleJavaFileObject {
private final ByteArrayOutputStream myStream = new ByteArrayOutputStream();
private final String myOrigName;
@@ -333,8 +265,8 @@ public class CompilingEvaluator implements ExpressionEvaluator {
}
}
private static class MemoryFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> {
private final Collection<OutputFileObject> classes = new ArrayList<OutputFileObject>();
protected static class MemoryFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> {
protected final Collection<OutputFileObject> classes = new ArrayList<OutputFileObject>();
MemoryFileManager(JavaCompiler compiler) {
super(compiler.getStandardFileManager(null, null, null));
@@ -0,0 +1,109 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.debugger.ui.impl.watch;
import com.intellij.debugger.engine.evaluation.EvaluateException;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.refactoring.extractMethodObject.ExtractLightMethodObjectHandler;
import com.intellij.util.PathsList;
import org.jetbrains.annotations.NotNull;
import javax.tools.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class CompilingEvaluatorImpl extends CompilingEvaluator {
public CompilingEvaluatorImpl(@NotNull PsiElement context, @NotNull ExtractLightMethodObjectHandler.ExtractedData data) {
super(context, data);
}
@Override
@NotNull
protected Collection<OutputFileObject> compile(String target) throws EvaluateException {
if (!SystemInfo.isJavaVersionAtLeast(target)) {
throw new EvaluateException("Unable to compile for target level " + target + ". Need to run IDEA on java version at least " + target + ", currently running on " + SystemInfo.JAVA_RUNTIME_VERSION);
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
MemoryFileManager manager = new MemoryFileManager(compiler);
DiagnosticCollector<JavaFileObject> diagnostic = new DiagnosticCollector<JavaFileObject>();
Module module = ApplicationManager.getApplication().runReadAction(new Computable<Module>() {
@Override
public Module compute() {
return ModuleUtilCore.findModuleForPsiElement(myPsiContext);
}
});
List<String> options = new ArrayList<String>();
if (module != null) {
options.add("-cp");
PathsList cp = ModuleRootManager.getInstance(module).orderEntries().compileOnly().recursively().exportedOnly().withoutSdk().getPathsList();
options.add(cp.getPathsString());
}
if (!StringUtil.isEmpty(target)) {
options.add("-source");
options.add(target);
options.add("-target");
options.add(target);
}
try {
if (!compiler.getTask(null,
manager,
diagnostic,
options,
null,
Collections.singletonList(new SourceFileObject(getMainClassName(), JavaFileObject.Kind.SOURCE, getClassCode()))
).call()) {
StringBuilder res = new StringBuilder("Compilation failed:\n");
for (Diagnostic<? extends JavaFileObject> d : diagnostic.getDiagnostics()) {
res.append(d);
}
throw new EvaluateException(res.toString());
}
}
catch (Exception e) {
throw new EvaluateException(e.getMessage());
}
return manager.classes;
}
protected String getClassCode() {
return ApplicationManager.getApplication().runReadAction(new Computable<String>() {
@Override
public String compute() {
return myData.getGeneratedInnerClass().getContainingFile().getText();
}
});
}
protected String getMainClassName() {
return ApplicationManager.getApplication().runReadAction(new Computable<String>() {
@Override
public String compute() {
return FileUtil.getNameWithoutExtension(myData.getGeneratedInnerClass().getContainingFile().getName());
}
});
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -101,7 +101,7 @@ public abstract class EvaluationDescriptor extends ValueDescriptorImpl{
ExtractLightMethodObjectHandler.ExtractedData data = ExtractLightMethodObjectHandler.extractLightMethodObject(myProject,
psiFile, fragment, CompilingEvaluator.getGeneratedClassName());
if (data != null) {
return new CompilingEvaluator(psiContext, data);
return new CompilingEvaluatorImpl(psiContext, data);
}
}
catch (PrepareFailedException e) {