langtools/src/jdk.jshell/share/classes/jdk/jshell/TaskFactory.java
changeset 33362 65ec6de1d6b4
child 34092 fd97b0f6abcc
equal deleted inserted replaced
33361:1c96344ecd49 33362:65ec6de1d6b4
       
     1 /*
       
     2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.jshell;
       
    27 
       
    28 import com.sun.source.tree.CompilationUnitTree;
       
    29 import com.sun.source.tree.Tree;
       
    30 import com.sun.source.util.Trees;
       
    31 import com.sun.tools.javac.api.JavacTaskImpl;
       
    32 import com.sun.tools.javac.api.JavacTool;
       
    33 import com.sun.tools.javac.util.Context;
       
    34 import java.util.ArrayList;
       
    35 import java.util.Arrays;
       
    36 import java.util.Iterator;
       
    37 import java.util.List;
       
    38 import javax.tools.Diagnostic;
       
    39 import javax.tools.DiagnosticCollector;
       
    40 import javax.tools.JavaCompiler;
       
    41 import javax.tools.JavaFileManager;
       
    42 import javax.tools.JavaFileObject;
       
    43 import javax.tools.ToolProvider;
       
    44 import static jdk.jshell.Util.*;
       
    45 import com.sun.source.tree.ImportTree;
       
    46 import com.sun.tools.javac.code.Types;
       
    47 import com.sun.tools.javac.util.JavacMessages;
       
    48 import jdk.jshell.MemoryFileManager.OutputMemoryJavaFileObject;
       
    49 import java.util.Collections;
       
    50 import java.util.Locale;
       
    51 import static javax.tools.StandardLocation.CLASS_OUTPUT;
       
    52 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN;
       
    53 import java.io.File;
       
    54 import java.util.Collection;
       
    55 import java.util.HashMap;
       
    56 import java.util.LinkedHashMap;
       
    57 import java.util.Map;
       
    58 import java.util.stream.Collectors;
       
    59 import java.util.stream.Stream;
       
    60 import javax.lang.model.util.Elements;
       
    61 import javax.tools.FileObject;
       
    62 import jdk.jshell.MemoryFileManager.SourceMemoryJavaFileObject;
       
    63 import jdk.jshell.ClassTracker.ClassInfo;
       
    64 
       
    65 /**
       
    66  * The primary interface to the compiler API.  Parsing, analysis, and
       
    67  * compilation to class files (in memory).
       
    68  * @author Robert Field
       
    69  */
       
    70 class TaskFactory {
       
    71 
       
    72     private final JavaCompiler compiler;
       
    73     private final MemoryFileManager fileManager;
       
    74     private final JShell state;
       
    75     private String classpath = System.getProperty("java.class.path");
       
    76 
       
    77     TaskFactory(JShell state) {
       
    78         this.state = state;
       
    79         this.compiler = ToolProvider.getSystemJavaCompiler();
       
    80         if (compiler == null) {
       
    81             throw new UnsupportedOperationException("Compiler not available, must be run with full JDK 9.");
       
    82         }
       
    83         if (!System.getProperty("java.specification.version").equals("1.9"))  {
       
    84             throw new UnsupportedOperationException("Wrong compiler, must be run with full JDK 9.");
       
    85         }
       
    86         this.fileManager = new MemoryFileManager(
       
    87                 compiler.getStandardFileManager(null, null, null), state);
       
    88     }
       
    89 
       
    90     void addToClasspath(String path) {
       
    91         classpath = classpath + File.pathSeparator + path;
       
    92         List<String> args = new ArrayList<>();
       
    93         args.add(classpath);
       
    94         fileManager().handleOption("-classpath", args.iterator());
       
    95     }
       
    96 
       
    97     MemoryFileManager fileManager() {
       
    98         return fileManager;
       
    99     }
       
   100 
       
   101     private interface SourceHandler<T> {
       
   102 
       
   103         JavaFileObject sourceToFileObject(MemoryFileManager fm, T t);
       
   104 
       
   105         Diag diag(Diagnostic<? extends JavaFileObject> d);
       
   106     }
       
   107 
       
   108     private class StringSourceHandler implements SourceHandler<String> {
       
   109 
       
   110         @Override
       
   111         public JavaFileObject sourceToFileObject(MemoryFileManager fm, String src) {
       
   112             return fm.createSourceFileObject(src, "$NeverUsedName$", src);
       
   113         }
       
   114 
       
   115         @Override
       
   116         public Diag diag(final Diagnostic<? extends JavaFileObject> d) {
       
   117             return new Diag() {
       
   118 
       
   119                 @Override
       
   120                 public boolean isError() {
       
   121                     return d.getKind() == Diagnostic.Kind.ERROR;
       
   122                 }
       
   123 
       
   124                 @Override
       
   125                 public long getPosition() {
       
   126                     return d.getPosition();
       
   127                 }
       
   128 
       
   129                 @Override
       
   130                 public long getStartPosition() {
       
   131                     return d.getStartPosition();
       
   132                 }
       
   133 
       
   134                 @Override
       
   135                 public long getEndPosition() {
       
   136                     return d.getEndPosition();
       
   137                 }
       
   138 
       
   139                 @Override
       
   140                 public String getCode() {
       
   141                     return d.getCode();
       
   142                 }
       
   143 
       
   144                 @Override
       
   145                 public String getMessage(Locale locale) {
       
   146                     return expunge(d.getMessage(locale));
       
   147                 }
       
   148 
       
   149                 @Override
       
   150                 Unit unitOrNull() {
       
   151                     return null;
       
   152                 }
       
   153             };
       
   154         }
       
   155     }
       
   156 
       
   157     private class WrapSourceHandler implements SourceHandler<OuterWrap> {
       
   158 
       
   159         final OuterWrap wrap;
       
   160 
       
   161         WrapSourceHandler(OuterWrap wrap) {
       
   162             this.wrap = wrap;
       
   163         }
       
   164 
       
   165         @Override
       
   166         public JavaFileObject sourceToFileObject(MemoryFileManager fm, OuterWrap w) {
       
   167             return fm.createSourceFileObject(w, w.classFullName(), w.wrapped());
       
   168         }
       
   169 
       
   170         @Override
       
   171         public Diag diag(Diagnostic<? extends JavaFileObject> d) {
       
   172             return wrap.wrapDiag(d);
       
   173         }
       
   174     }
       
   175 
       
   176     private class UnitSourceHandler implements SourceHandler<Unit> {
       
   177 
       
   178         @Override
       
   179         public JavaFileObject sourceToFileObject(MemoryFileManager fm, Unit u) {
       
   180             return fm.createSourceFileObject(u,
       
   181                     state.maps.classFullName(u.snippet()),
       
   182                     u.snippet().outerWrap().wrapped());
       
   183         }
       
   184 
       
   185         @Override
       
   186         public Diag diag(Diagnostic<? extends JavaFileObject> d) {
       
   187             SourceMemoryJavaFileObject smjfo = (SourceMemoryJavaFileObject) d.getSource();
       
   188             Unit u = (Unit) smjfo.getOrigin();
       
   189             return u.snippet().outerWrap().wrapDiag(d);
       
   190         }
       
   191     }
       
   192 
       
   193     /**
       
   194      * Parse a snippet of code (as a String) using the parser subclass.  Return
       
   195      * the parse tree (and errors).
       
   196      */
       
   197     class ParseTask extends BaseTask {
       
   198 
       
   199         private final CompilationUnitTree cut;
       
   200         private final List<? extends Tree> units;
       
   201 
       
   202         ParseTask(final String source) {
       
   203             super(Stream.of(source),
       
   204                     new StringSourceHandler(),
       
   205                     "-XDallowStringFolding=false", "-proc:none");
       
   206             ReplParserFactory.instance(getContext());
       
   207             Iterable<? extends CompilationUnitTree> asts = parse();
       
   208             Iterator<? extends CompilationUnitTree> it = asts.iterator();
       
   209             if (it.hasNext()) {
       
   210                 this.cut = it.next();
       
   211                 List<? extends ImportTree> imps = cut.getImports();
       
   212                 this.units = !imps.isEmpty() ? imps : cut.getTypeDecls();
       
   213             } else {
       
   214                 this.cut = null;
       
   215                 this.units = Collections.emptyList();
       
   216             }
       
   217         }
       
   218 
       
   219         private Iterable<? extends CompilationUnitTree> parse() {
       
   220             try {
       
   221                 return task.parse();
       
   222             } catch (Exception ex) {
       
   223                 throw new InternalError("Exception during parse - " + ex.getMessage(), ex);
       
   224             }
       
   225         }
       
   226 
       
   227         List<? extends Tree> units() {
       
   228             return units;
       
   229         }
       
   230 
       
   231         @Override
       
   232         CompilationUnitTree cuTree() {
       
   233             return cut;
       
   234         }
       
   235     }
       
   236 
       
   237     /**
       
   238      * Run the normal "analyze()" pass of the compiler over the wrapped snippet.
       
   239      */
       
   240     class AnalyzeTask extends BaseTask {
       
   241 
       
   242         private final CompilationUnitTree cut;
       
   243 
       
   244         AnalyzeTask(final OuterWrap wrap) {
       
   245             this(Stream.of(wrap),
       
   246                     new WrapSourceHandler(wrap),
       
   247                     "-XDshouldStopPolicy=FLOW", "-proc:none");
       
   248         }
       
   249 
       
   250         AnalyzeTask(final Collection<Unit> units) {
       
   251             this(units.stream(), new UnitSourceHandler(),
       
   252                     "-XDshouldStopPolicy=FLOW", "-Xlint:unchecked", "-proc:none");
       
   253         }
       
   254 
       
   255         <T>AnalyzeTask(final Stream<T> stream, SourceHandler<T> sourceHandler,
       
   256                 String... extraOptions) {
       
   257             super(stream, sourceHandler, extraOptions);
       
   258             Iterator<? extends CompilationUnitTree> cuts = analyze().iterator();
       
   259             if (cuts.hasNext()) {
       
   260                 this.cut = cuts.next();
       
   261                 //proc.debug("AnalyzeTask element=%s  cutp=%s  cut=%s\n", e, cutp, cut);
       
   262             } else {
       
   263                 this.cut = null;
       
   264                 //proc.debug("AnalyzeTask -- no elements -- %s\n", getDiagnostics());
       
   265             }
       
   266         }
       
   267 
       
   268         private Iterable<? extends CompilationUnitTree> analyze() {
       
   269             try {
       
   270                 Iterable<? extends CompilationUnitTree> cuts = task.parse();
       
   271                 task.analyze();
       
   272                 return cuts;
       
   273             } catch (Exception ex) {
       
   274                 throw new InternalError("Exception during analyze - " + ex.getMessage(), ex);
       
   275             }
       
   276         }
       
   277 
       
   278         @Override
       
   279         CompilationUnitTree cuTree() {
       
   280             return cut;
       
   281         }
       
   282 
       
   283         Elements getElements() {
       
   284             return task.getElements();
       
   285         }
       
   286 
       
   287         javax.lang.model.util.Types getTypes() {
       
   288             return task.getTypes();
       
   289         }
       
   290     }
       
   291 
       
   292     /**
       
   293      * Unit the wrapped snippet to class files.
       
   294      */
       
   295     class CompileTask extends BaseTask {
       
   296 
       
   297         private final Map<Unit, List<OutputMemoryJavaFileObject>> classObjs = new HashMap<>();
       
   298 
       
   299         CompileTask(Collection<Unit> units) {
       
   300             super(units.stream(), new UnitSourceHandler(),
       
   301                     "-Xlint:unchecked", "-proc:none");
       
   302         }
       
   303 
       
   304         boolean compile() {
       
   305             fileManager.registerClassFileCreationListener(this::listenForNewClassFile);
       
   306             boolean result = task.call();
       
   307             fileManager.registerClassFileCreationListener(null);
       
   308             return result;
       
   309         }
       
   310 
       
   311 
       
   312         List<ClassInfo> classInfoList(Unit u) {
       
   313             List<OutputMemoryJavaFileObject> l = classObjs.get(u);
       
   314             if (l == null) return Collections.emptyList();
       
   315             return l.stream()
       
   316                     .map(fo -> state.classTracker.classInfo(fo.getName(), fo.getBytes()))
       
   317                     .collect(Collectors.toList());
       
   318         }
       
   319 
       
   320         private void listenForNewClassFile(OutputMemoryJavaFileObject jfo, JavaFileManager.Location location,
       
   321                 String className, JavaFileObject.Kind kind, FileObject sibling) {
       
   322             //debug("listenForNewClassFile %s loc=%s kind=%s\n", className, location, kind);
       
   323             if (location == CLASS_OUTPUT) {
       
   324                 state.debug(DBG_GEN, "Compiler generating class %s\n", className);
       
   325                 Unit u = ((sibling instanceof SourceMemoryJavaFileObject)
       
   326                         && (((SourceMemoryJavaFileObject) sibling).getOrigin() instanceof Unit))
       
   327                         ? (Unit) ((SourceMemoryJavaFileObject) sibling).getOrigin()
       
   328                         : null;
       
   329                 classObjs.compute(u, (k, v) -> (v == null)? new ArrayList<>() : v)
       
   330                         .add(jfo);
       
   331             }
       
   332         }
       
   333 
       
   334         @Override
       
   335         CompilationUnitTree cuTree() {
       
   336             throw new UnsupportedOperationException("Not supported.");
       
   337         }
       
   338     }
       
   339 
       
   340     abstract class BaseTask {
       
   341 
       
   342         final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
       
   343         final JavacTaskImpl task;
       
   344         private DiagList diags = null;
       
   345         private final SourceHandler<?> sourceHandler;
       
   346         private final Context context = new Context();
       
   347         private Types types;
       
   348         private JavacMessages messages;
       
   349         private Trees trees;
       
   350 
       
   351         private <T>BaseTask(Stream<T> inputs,
       
   352                 //BiFunction<MemoryFileManager, T, JavaFileObject> sfoCreator,
       
   353                 SourceHandler<T> sh,
       
   354                 String... extraOptions) {
       
   355             this.sourceHandler = sh;
       
   356             List<String> options = Arrays.asList(extraOptions);
       
   357             Iterable<? extends JavaFileObject> compilationUnits = inputs
       
   358                             .map(in -> sh.sourceToFileObject(fileManager, in))
       
   359                             .collect(Collectors.toList());
       
   360             this.task = (JavacTaskImpl) ((JavacTool) compiler).getTask(null,
       
   361                     fileManager, diagnostics, options, null,
       
   362                     compilationUnits, context);
       
   363         }
       
   364 
       
   365         abstract CompilationUnitTree cuTree();
       
   366 
       
   367         Diag diag(Diagnostic<? extends JavaFileObject> diag) {
       
   368             return sourceHandler.diag(diag);
       
   369         }
       
   370 
       
   371         Context getContext() {
       
   372             return context;
       
   373         }
       
   374 
       
   375         Types types() {
       
   376             if (types == null) {
       
   377                 types = Types.instance(context);
       
   378             }
       
   379             return types;
       
   380         }
       
   381 
       
   382         JavacMessages messages() {
       
   383             if (messages == null) {
       
   384                 messages = JavacMessages.instance(context);
       
   385             }
       
   386             return messages;
       
   387         }
       
   388 
       
   389         Trees trees() {
       
   390             if (trees == null) {
       
   391                 trees = Trees.instance(task);
       
   392             }
       
   393             return trees;
       
   394         }
       
   395 
       
   396         // ------------------ diags functionality
       
   397 
       
   398         DiagList getDiagnostics() {
       
   399             if (diags == null) {
       
   400                 LinkedHashMap<String, Diag> diagMap = new LinkedHashMap<>();
       
   401                 for (Diagnostic<? extends JavaFileObject> in : diagnostics.getDiagnostics()) {
       
   402                     Diag d = diag(in);
       
   403                     String uniqueKey = d.getCode() + ":" + d.getPosition() + ":" + d.getMessage(null);
       
   404                     diagMap.put(uniqueKey, d);
       
   405                 }
       
   406                 diags = new DiagList(diagMap.values());
       
   407             }
       
   408             return diags;
       
   409         }
       
   410 
       
   411         boolean hasErrors() {
       
   412             return getDiagnostics().hasErrors();
       
   413         }
       
   414 
       
   415         String shortErrorMessage() {
       
   416             StringBuilder sb = new StringBuilder();
       
   417             for (Diag diag : getDiagnostics()) {
       
   418                 for (String line : diag.getMessage(null).split("\\r?\\n")) {
       
   419                     if (!line.trim().startsWith("location:")) {
       
   420                         sb.append(line);
       
   421                     }
       
   422                 }
       
   423             }
       
   424             return sb.toString();
       
   425         }
       
   426 
       
   427         void debugPrintDiagnostics(String src) {
       
   428             for (Diag diag : getDiagnostics()) {
       
   429                 state.debug(DBG_GEN, "ERROR --\n");
       
   430                 for (String line : diag.getMessage(null).split("\\r?\\n")) {
       
   431                     if (!line.trim().startsWith("location:")) {
       
   432                         state.debug(DBG_GEN, "%s\n", line);
       
   433                     }
       
   434                 }
       
   435                 int start = (int) diag.getStartPosition();
       
   436                 int end = (int) diag.getEndPosition();
       
   437                 if (src != null) {
       
   438                     String[] srcLines = src.split("\\r?\\n");
       
   439                     for (String line : srcLines) {
       
   440                         state.debug(DBG_GEN, "%s\n", line);
       
   441                     }
       
   442 
       
   443                     StringBuilder sb = new StringBuilder();
       
   444                     for (int i = 0; i < start; ++i) {
       
   445                         sb.append(' ');
       
   446                     }
       
   447                     sb.append('^');
       
   448                     if (end > start) {
       
   449                         for (int i = start + 1; i < end; ++i) {
       
   450                             sb.append('-');
       
   451                         }
       
   452                         sb.append('^');
       
   453                     }
       
   454                     state.debug(DBG_GEN, "%s\n", sb.toString());
       
   455                 }
       
   456                 state.debug(DBG_GEN, "printDiagnostics start-pos = %d ==> %d -- wrap = %s\n",
       
   457                         diag.getStartPosition(), start, this);
       
   458                 state.debug(DBG_GEN, "Code: %s\n", diag.getCode());
       
   459                 state.debug(DBG_GEN, "Pos: %d (%d - %d) -- %s\n", diag.getPosition(),
       
   460                         diag.getStartPosition(), diag.getEndPosition(), diag.getMessage(null));
       
   461             }
       
   462         }
       
   463     }
       
   464 
       
   465 }