From 0aa0260983cbf9c1444665c7f0cdc2378f1c9e5f Mon Sep 17 00:00:00 2001 From: Natalia Ukhorskaya Date: Thu, 4 Dec 2014 11:57:30 +0300 Subject: [PATCH] Extract base class for CompilingEvaluator to reuse it in Kotlin --- .../ui/impl/watch/CompilingEvaluator.java | 86 ++------------ .../ui/impl/watch/CompilingEvaluatorImpl.java | 109 ++++++++++++++++++ .../ui/impl/watch/EvaluationDescriptor.java | 4 +- 3 files changed, 120 insertions(+), 79 deletions(-) create mode 100644 java/debugger/impl/src/com/intellij/debugger/ui/impl/watch/CompilingEvaluatorImpl.java diff --git a/java/debugger/impl/src/com/intellij/debugger/ui/impl/watch/CompilingEvaluator.java b/java/debugger/impl/src/com/intellij/debugger/ui/impl/watch/CompilingEvaluator.java index f35dafc1d666..93e1ff136ad1 100644 --- a/java/debugger/impl/src/com/intellij/debugger/ui/impl/watch/CompilingEvaluator.java +++ b/java/debugger/impl/src/com/intellij/debugger/ui/impl/watch/CompilingEvaluator.java @@ -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() { - @Override - public String compute() { - return myData.getGeneratedInnerClass().getContainingFile().getText(); - } - }); - } - private String getMainClassName() { - return ApplicationManager.getApplication().runReadAction(new Computable() { - @Override - public String compute() { - return FileUtil.getNameWithoutExtension(myData.getGeneratedInnerClass().getContainingFile().getName()); - } - }); - } - - private String getGenClassQName() { + protected String getGenClassQName() { return ApplicationManager.getApplication().runReadAction(new Computable() { @Override public String compute() { @@ -250,57 +226,13 @@ public class CompilingEvaluator implements ExpressionEvaluator { ///////////////// Compiler stuff @NotNull - private Collection 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 diagnostic = new DiagnosticCollector(); - Module module = ApplicationManager.getApplication().runReadAction(new Computable() { - @Override - public Module compute() { - return ModuleUtilCore.findModuleForPsiElement(myPsiContext); - } - }); - List options = new ArrayList(); - 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 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 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 { - private final Collection classes = new ArrayList(); + protected static class MemoryFileManager extends ForwardingJavaFileManager { + protected final Collection classes = new ArrayList(); MemoryFileManager(JavaCompiler compiler) { super(compiler.getStandardFileManager(null, null, null)); diff --git a/java/debugger/impl/src/com/intellij/debugger/ui/impl/watch/CompilingEvaluatorImpl.java b/java/debugger/impl/src/com/intellij/debugger/ui/impl/watch/CompilingEvaluatorImpl.java new file mode 100644 index 000000000000..e7e2b8d62e5b --- /dev/null +++ b/java/debugger/impl/src/com/intellij/debugger/ui/impl/watch/CompilingEvaluatorImpl.java @@ -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 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 diagnostic = new DiagnosticCollector(); + Module module = ApplicationManager.getApplication().runReadAction(new Computable() { + @Override + public Module compute() { + return ModuleUtilCore.findModuleForPsiElement(myPsiContext); + } + }); + List options = new ArrayList(); + 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 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() { + @Override + public String compute() { + return myData.getGeneratedInnerClass().getContainingFile().getText(); + } + }); + } + + protected String getMainClassName() { + return ApplicationManager.getApplication().runReadAction(new Computable() { + @Override + public String compute() { + return FileUtil.getNameWithoutExtension(myData.getGeneratedInnerClass().getContainingFile().getName()); + } + }); + } +} diff --git a/java/debugger/impl/src/com/intellij/debugger/ui/impl/watch/EvaluationDescriptor.java b/java/debugger/impl/src/com/intellij/debugger/ui/impl/watch/EvaluationDescriptor.java index 20708864c104..f26d81292e35 100644 --- a/java/debugger/impl/src/com/intellij/debugger/ui/impl/watch/EvaluationDescriptor.java +++ b/java/debugger/impl/src/com/intellij/debugger/ui/impl/watch/EvaluationDescriptor.java @@ -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) {