1 /*
   2  * Copyright (c) 2014, 2016, 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.List;
  37 import javax.tools.Diagnostic;
  38 import javax.tools.DiagnosticCollector;
  39 import javax.tools.JavaCompiler;
  40 import javax.tools.JavaFileManager;
  41 import javax.tools.JavaFileObject;
  42 import javax.tools.ToolProvider;
  43 import static jdk.jshell.Util.*;
  44 import com.sun.source.tree.ImportTree;
  45 import com.sun.tools.javac.code.Types;
  46 import com.sun.tools.javac.util.JavacMessages;
  47 import jdk.jshell.MemoryFileManager.OutputMemoryJavaFileObject;
  48 import java.util.Collections;
  49 import java.util.Locale;
  50 import static javax.tools.StandardLocation.CLASS_OUTPUT;
  51 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN;
  52 import java.io.File;
  53 import java.util.Collection;
  54 import java.util.HashMap;
  55 import java.util.LinkedHashMap;
  56 import java.util.Map;
  57 import java.util.stream.Collectors;
  58 import static java.util.stream.Collectors.toList;
  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 import java.lang.Runtime.Version;
  65 
  66 /**
  67  * The primary interface to the compiler API.  Parsing, analysis, and
  68  * compilation to class files (in memory).
  69  * @author Robert Field
  70  */
  71 class TaskFactory {
  72 
  73     private final JavaCompiler compiler;
  74     private final MemoryFileManager fileManager;
  75     private final JShell state;
  76     private String classpath = System.getProperty("java.class.path");
  77     private final static Version INITIAL_SUPPORTED_VER = Version.parse("9");
  78 
  79     TaskFactory(JShell state) {
  80         this.state = state;
  81         this.compiler = ToolProvider.getSystemJavaCompiler();
  82         if (compiler == null) {
  83             throw new UnsupportedOperationException("Compiler not available, must be run with full JDK 9.");
  84         }
  85         Version current = Version.parse(System.getProperty("java.specification.version"));
  86         if (INITIAL_SUPPORTED_VER.compareToIgnoreOpt(current) > 0)  {
  87             throw new UnsupportedOperationException("Wrong compiler, must be run with full JDK 9.");
  88         }
  89         this.fileManager = new MemoryFileManager(
  90                 compiler.getStandardFileManager(null, null, null), state);
  91     }
  92 
  93     void addToClasspath(String path) {
  94         classpath = classpath + File.pathSeparator + path;
  95         List<String> args = new ArrayList<>();
  96         args.add(classpath);
  97         fileManager().handleOption("-classpath", args.iterator());
  98     }
  99 
 100     MemoryFileManager fileManager() {
 101         return fileManager;
 102     }
 103 
 104     private interface SourceHandler<T> {
 105 
 106         JavaFileObject sourceToFileObject(MemoryFileManager fm, T t);
 107 
 108         Diag diag(Diagnostic<? extends JavaFileObject> d);
 109     }
 110 
 111     private class StringSourceHandler implements SourceHandler<String> {
 112 
 113         @Override
 114         public JavaFileObject sourceToFileObject(MemoryFileManager fm, String src) {
 115             return fm.createSourceFileObject(src, "$NeverUsedName$", src);
 116         }
 117 
 118         @Override
 119         public Diag diag(final Diagnostic<? extends JavaFileObject> d) {
 120             return new Diag() {
 121 
 122                 @Override
 123                 public boolean isError() {
 124                     return d.getKind() == Diagnostic.Kind.ERROR;
 125                 }
 126 
 127                 @Override
 128                 public long getPosition() {
 129                     return d.getPosition();
 130                 }
 131 
 132                 @Override
 133                 public long getStartPosition() {
 134                     return d.getStartPosition();
 135                 }
 136 
 137                 @Override
 138                 public long getEndPosition() {
 139                     return d.getEndPosition();
 140                 }
 141 
 142                 @Override
 143                 public String getCode() {
 144                     return d.getCode();
 145                 }
 146 
 147                 @Override
 148                 public String getMessage(Locale locale) {
 149                     return expunge(d.getMessage(locale));
 150                 }
 151             };
 152         }
 153     }
 154 
 155     private class WrapSourceHandler implements SourceHandler<OuterWrap> {
 156 
 157         @Override
 158         public JavaFileObject sourceToFileObject(MemoryFileManager fm, OuterWrap w) {
 159             return fm.createSourceFileObject(w, w.classFullName(), w.wrapped());
 160         }
 161 
 162         @Override
 163         public Diag diag(Diagnostic<? extends JavaFileObject> d) {
 164             SourceMemoryJavaFileObject smjfo = (SourceMemoryJavaFileObject) d.getSource();
 165             OuterWrap w = (OuterWrap) smjfo.getOrigin();
 166             return w.wrapDiag(d);
 167         }
 168     }
 169 
 170     /**
 171      * Parse a snippet of code (as a String) using the parser subclass.  Return
 172      * the parse tree (and errors).
 173      */
 174     class ParseTask extends BaseTask {
 175 
 176         private final Iterable<? extends CompilationUnitTree> cuts;
 177         private final List<? extends Tree> units;
 178 
 179         ParseTask(final String source) {
 180             super(Stream.of(source),
 181                     new StringSourceHandler(),
 182                     "-XDallowStringFolding=false", "-proc:none");
 183             ReplParserFactory.instance(getContext());
 184             cuts = parse();
 185             units = Util.stream(cuts)
 186                     .flatMap(cut -> {
 187                         List<? extends ImportTree> imps = cut.getImports();
 188                         return (!imps.isEmpty() ? imps : cut.getTypeDecls()).stream();
 189                     })
 190                     .collect(toList());
 191         }
 192 
 193         private Iterable<? extends CompilationUnitTree> parse() {
 194             try {
 195                 return task.parse();
 196             } catch (Exception ex) {
 197                 throw new InternalError("Exception during parse - " + ex.getMessage(), ex);
 198             }
 199         }
 200 
 201         List<? extends Tree> units() {
 202             return units;
 203         }
 204 
 205         @Override
 206         Iterable<? extends CompilationUnitTree> cuTrees() {
 207             return cuts;
 208         }
 209     }
 210 
 211     /**
 212      * Run the normal "analyze()" pass of the compiler over the wrapped snippet.
 213      */
 214     class AnalyzeTask extends BaseTask {
 215 
 216         private final Iterable<? extends CompilationUnitTree> cuts;
 217 
 218         AnalyzeTask(final OuterWrap wrap, String... extraArgs) {
 219             this(Collections.singletonList(wrap), extraArgs);
 220         }
 221 
 222         AnalyzeTask(final Collection<OuterWrap> wraps, String... extraArgs) {
 223             this(wraps.stream(),
 224                     new WrapSourceHandler(),
 225                     Util.join(new String[] {
 226                         "-XDshouldStopPolicy=FLOW", "-Xlint:unchecked",
 227                         "-XaddExports:jdk.jshell/jdk.internal.jshell.remote=ALL-UNNAMED",
 228                         "-proc:none"
 229                     }, extraArgs));
 230         }
 231 
 232         private <T>AnalyzeTask(final Stream<T> stream, SourceHandler<T> sourceHandler,
 233                 String... extraOptions) {
 234             super(stream, sourceHandler, extraOptions);
 235             cuts = analyze();
 236         }
 237 
 238         private Iterable<? extends CompilationUnitTree> analyze() {
 239             try {
 240                 Iterable<? extends CompilationUnitTree> cuts = task.parse();
 241                 task.analyze();
 242                 return cuts;
 243             } catch (Exception ex) {
 244                 throw new InternalError("Exception during analyze - " + ex.getMessage(), ex);
 245             }
 246         }
 247 
 248         @Override
 249         Iterable<? extends CompilationUnitTree> cuTrees() {
 250             return cuts;
 251         }
 252 
 253         Elements getElements() {
 254             return task.getElements();
 255         }
 256 
 257         javax.lang.model.util.Types getTypes() {
 258             return task.getTypes();
 259         }
 260     }
 261 
 262     /**
 263      * Unit the wrapped snippet to class files.
 264      */
 265     class CompileTask extends BaseTask {
 266 
 267         private final Map<OuterWrap, List<OutputMemoryJavaFileObject>> classObjs = new HashMap<>();
 268 
 269         CompileTask(final Collection<OuterWrap> wraps) {
 270             super(wraps.stream(), new WrapSourceHandler(),
 271                     "-Xlint:unchecked", "-XaddExports:jdk.jshell/jdk.internal.jshell.remote=ALL-UNNAMED", "-proc:none", "-parameters");
 272         }
 273 
 274         boolean compile() {
 275             fileManager.registerClassFileCreationListener(this::listenForNewClassFile);
 276             boolean result = task.call();
 277             fileManager.registerClassFileCreationListener(null);
 278             return result;
 279         }
 280 
 281 
 282         List<ClassInfo> classInfoList(OuterWrap w) {
 283             List<OutputMemoryJavaFileObject> l = classObjs.get(w);
 284             if (l == null) return Collections.emptyList();
 285             return l.stream()
 286                     .map(fo -> state.classTracker.classInfo(fo.getName(), fo.getBytes()))
 287                     .collect(Collectors.toList());
 288         }
 289 
 290         private void listenForNewClassFile(OutputMemoryJavaFileObject jfo, JavaFileManager.Location location,
 291                 String className, JavaFileObject.Kind kind, FileObject sibling) {
 292             //debug("listenForNewClassFile %s loc=%s kind=%s\n", className, location, kind);
 293             if (location == CLASS_OUTPUT) {
 294                 state.debug(DBG_GEN, "Compiler generating class %s\n", className);
 295                 OuterWrap w = ((sibling instanceof SourceMemoryJavaFileObject)
 296                         && (((SourceMemoryJavaFileObject) sibling).getOrigin() instanceof OuterWrap))
 297                         ? (OuterWrap) ((SourceMemoryJavaFileObject) sibling).getOrigin()
 298                         : null;
 299                 classObjs.compute(w, (k, v) -> (v == null)? new ArrayList<>() : v)
 300                         .add(jfo);
 301             }
 302         }
 303 
 304         @Override
 305         Iterable<? extends CompilationUnitTree> cuTrees() {
 306             throw new UnsupportedOperationException("Not supported.");
 307         }
 308     }
 309 
 310     abstract class BaseTask {
 311 
 312         final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
 313         final JavacTaskImpl task;
 314         private DiagList diags = null;
 315         private final SourceHandler<?> sourceHandler;
 316         private final Context context = new Context();
 317         private Types types;
 318         private JavacMessages messages;
 319         private Trees trees;
 320 
 321         private <T>BaseTask(Stream<T> inputs,
 322                 //BiFunction<MemoryFileManager, T, JavaFileObject> sfoCreator,
 323                 SourceHandler<T> sh,
 324                 String... extraOptions) {
 325             this.sourceHandler = sh;
 326             List<String> options = Arrays.asList(extraOptions);
 327             Iterable<? extends JavaFileObject> compilationUnits = inputs
 328                             .map(in -> sh.sourceToFileObject(fileManager, in))
 329                             .collect(Collectors.toList());
 330             this.task = (JavacTaskImpl) ((JavacTool) compiler).getTask(null,
 331                     fileManager, diagnostics, options, null,
 332                     compilationUnits, context);
 333         }
 334 
 335         abstract Iterable<? extends CompilationUnitTree> cuTrees();
 336 
 337         CompilationUnitTree firstCuTree() {
 338             return cuTrees().iterator().next();
 339         }
 340 
 341         Diag diag(Diagnostic<? extends JavaFileObject> diag) {
 342             return sourceHandler.diag(diag);
 343         }
 344 
 345         Context getContext() {
 346             return context;
 347         }
 348 
 349         Types types() {
 350             if (types == null) {
 351                 types = Types.instance(context);
 352             }
 353             return types;
 354         }
 355 
 356         JavacMessages messages() {
 357             if (messages == null) {
 358                 messages = JavacMessages.instance(context);
 359             }
 360             return messages;
 361         }
 362 
 363         Trees trees() {
 364             if (trees == null) {
 365                 trees = Trees.instance(task);
 366             }
 367             return trees;
 368         }
 369 
 370         // ------------------ diags functionality
 371 
 372         DiagList getDiagnostics() {
 373             if (diags == null) {
 374                 LinkedHashMap<String, Diag> diagMap = new LinkedHashMap<>();
 375                 for (Diagnostic<? extends JavaFileObject> in : diagnostics.getDiagnostics()) {
 376                     Diag d = diag(in);
 377                     String uniqueKey = d.getCode() + ":" + d.getPosition() + ":" + d.getMessage(PARSED_LOCALE);
 378                     diagMap.put(uniqueKey, d);
 379                 }
 380                 diags = new DiagList(diagMap.values());
 381             }
 382             return diags;
 383         }
 384 
 385         boolean hasErrors() {
 386             return getDiagnostics().hasErrors();
 387         }
 388 
 389         String shortErrorMessage() {
 390             StringBuilder sb = new StringBuilder();
 391             for (Diag diag : getDiagnostics()) {
 392                 for (String line : diag.getMessage(PARSED_LOCALE).split("\\r?\\n")) {
 393                     if (!line.trim().startsWith("location:")) {
 394                         sb.append(line);
 395                     }
 396                 }
 397             }
 398             return sb.toString();
 399         }
 400 
 401         void debugPrintDiagnostics(String src) {
 402             for (Diag diag : getDiagnostics()) {
 403                 state.debug(DBG_GEN, "ERROR --\n");
 404                 for (String line : diag.getMessage(PARSED_LOCALE).split("\\r?\\n")) {
 405                     if (!line.trim().startsWith("location:")) {
 406                         state.debug(DBG_GEN, "%s\n", line);
 407                     }
 408                 }
 409                 int start = (int) diag.getStartPosition();
 410                 int end = (int) diag.getEndPosition();
 411                 if (src != null) {
 412                     String[] srcLines = src.split("\\r?\\n");
 413                     for (String line : srcLines) {
 414                         state.debug(DBG_GEN, "%s\n", line);
 415                     }
 416 
 417                     StringBuilder sb = new StringBuilder();
 418                     for (int i = 0; i < start; ++i) {
 419                         sb.append(' ');
 420                     }
 421                     sb.append('^');
 422                     if (end > start) {
 423                         for (int i = start + 1; i < end; ++i) {
 424                             sb.append('-');
 425                         }
 426                         sb.append('^');
 427                     }
 428                     state.debug(DBG_GEN, "%s\n", sb.toString());
 429                 }
 430                 state.debug(DBG_GEN, "printDiagnostics start-pos = %d ==> %d -- wrap = %s\n",
 431                         diag.getStartPosition(), start, this);
 432                 state.debug(DBG_GEN, "Code: %s\n", diag.getCode());
 433                 state.debug(DBG_GEN, "Pos: %d (%d - %d) -- %s\n", diag.getPosition(),
 434                         diag.getStartPosition(), diag.getEndPosition(), diag.getMessage(null));
 435             }
 436         }
 437     }
 438 
 439 }