1 /*
   2  * Copyright (c) 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import javax.tools.*;
  25 import java.io.ByteArrayOutputStream;
  26 import java.io.IOException;
  27 import java.io.OutputStream;
  28 import java.net.URI;
  29 import java.util.Arrays;
  30 import java.util.List;
  31 import java.util.Map;
  32 import java.util.stream.Collectors;
  33 
  34 class Compiler {
  35     final private Map<String,String> input;
  36     private List<String> options;
  37 
  38     Compiler(Map<String,String> input) {
  39         this.input = input;
  40     }
  41 
  42     Compiler setRelease(int release) {
  43         // Setting the -release option does not work for some reason
  44         // so do it the old fashioned way
  45         // options = Arrays.asList("-release", String.valueOf(release));
  46         String target = String.valueOf(release);
  47         options = Arrays.asList("-source", target, "-target", target);
  48         return this;
  49     }
  50 
  51     Map<String,byte[]> compile() {
  52         List<SourceFileObject> cunits = createCompilationUnits();
  53         Map<String,ClassFileObject> cfos = createClassFileObjects();
  54         JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
  55         JavaFileManager jfm = new CustomFileManager(jc.getStandardFileManager(null, null, null), cfos);
  56         jc.getTask(null, jfm, null, options, null, cunits).call();
  57         return createOutput(cfos);
  58     }
  59 
  60     private List<SourceFileObject> createCompilationUnits() {
  61         return input.entrySet().stream()
  62                 .map(e -> new SourceFileObject(e.getKey(), e.getValue())).collect(Collectors.toList());
  63     }
  64 
  65     private Map<String,ClassFileObject> createClassFileObjects() {
  66         return input.keySet().stream()
  67                 .collect(Collectors.toMap(k -> k, k -> new ClassFileObject(k)));
  68     }
  69 
  70     private Map<String,byte[]> createOutput(Map<String,ClassFileObject> cfos) {
  71         return cfos.keySet().stream().collect(Collectors.toMap(k -> k, k -> cfos.get(k).getBytes()));
  72     }
  73 
  74     private static class SourceFileObject extends SimpleJavaFileObject {
  75         private final String source;
  76 
  77         SourceFileObject(String name, String source) {
  78             super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
  79             this.source = source;
  80         }
  81 
  82         @Override
  83         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
  84             return source;
  85         }
  86     }
  87 
  88     private static class ClassFileObject extends SimpleJavaFileObject {
  89         private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  90 
  91         ClassFileObject(String className) {
  92             super(URI.create(className), Kind.CLASS);
  93         }
  94 
  95         @Override
  96         public OutputStream openOutputStream() throws IOException {
  97             return baos;
  98         }
  99 
 100         public byte[] getBytes() {
 101             return baos.toByteArray();
 102         }
 103     }
 104 
 105     private static class CustomFileManager extends ForwardingJavaFileManager<JavaFileManager> {
 106         private final Map<String,ClassFileObject> cfos;
 107 
 108         CustomFileManager(JavaFileManager jfm, Map<String,ClassFileObject> cfos) {
 109             super(jfm);
 110             this.cfos = cfos;
 111         }
 112 
 113         @Override
 114         public JavaFileObject getJavaFileForOutput(JavaFileManager.Location loc, String name,
 115                                                    JavaFileObject.Kind kind, FileObject sibling) throws IOException {
 116             ClassFileObject cfo = cfos.get(name);
 117             return cfo;
 118         }
 119     }
 120 }