• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package util.build;
18 
19 import java.io.File;
20 import java.lang.Iterable;
21 import java.util.stream.Collectors;
22 import java.util.Arrays;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 import javax.tools.JavaCompiler;
27 import javax.tools.JavaFileObject;
28 import javax.tools.StandardJavaFileManager;
29 import javax.tools.StandardLocation;
30 import javax.tools.ToolProvider;
31 
32 public class JavacBuildStep extends SourceBuildStep {
33 
34     private final String destPath;
35     private final String classPath;
36     private final Set<String> sourceFiles = new HashSet<String>();
JavacBuildStep(String destPath, String classPath)37     public JavacBuildStep(String destPath, String classPath) {
38         super(new File(destPath));
39         this.destPath = destPath;
40         this.classPath = classPath;
41     }
42 
43     @Override
addSourceFile(String sourceFile)44     public void addSourceFile(String sourceFile)
45     {
46         sourceFiles.add(sourceFile);
47     }
48 
49     @Override
build()50     boolean build() {
51         if (super.build())
52         {
53             if (sourceFiles.isEmpty())
54             {
55                 return true;
56             }
57 
58             File destFile = new File(destPath);
59             if (!destFile.exists() && !destFile.mkdirs())
60             {
61                 System.err.println("failed to create destination dir");
62                 return false;
63             }
64 
65             Iterable<File> classPathFiles = Arrays.asList(classPath.split(":"))
66                     .stream()
67                     .map(File::new)
68                     .collect(Collectors.toList());
69 
70             JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
71             try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(
72                     null,     // diagnosticListener: we don't care about the details.
73                     null,     // locale: use default locale.
74                     null)) {  // charset: use platform default.
75                 fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(
76                         new File(destPath)));
77                 fileManager.setLocation(StandardLocation.CLASS_PATH, classPathFiles);
78 
79                 Iterable<? extends JavaFileObject> compilationUnits =
80                         fileManager.getJavaFileObjectsFromStrings(sourceFiles);
81 
82                 List<String> options = Arrays.asList("-source", "1.7", "-target", "1.7");
83 
84                 return compiler.getTask(
85                         null,  // out: write errors to System.err.
86                         fileManager,
87                         null,  // diagnosticListener: we don't care about the details.
88                         options,
89                         null,  // classes: classes for annotation processing = none.
90                         compilationUnits).call();
91             } catch (Exception e) {
92                 e.printStackTrace();
93                 return false;
94             }
95         }
96         return false;
97     }
98 
99     @Override
equals(Object obj)100     public boolean equals(Object obj) {
101         if (super.equals(obj))
102         {
103             JavacBuildStep other = (JavacBuildStep) obj;
104             return destPath.equals(other.destPath)
105                 && classPath.equals(other.classPath)
106                 && sourceFiles.equals(other.sourceFiles);
107         }
108         return false;
109     }
110 
111     @Override
hashCode()112     public int hashCode() {
113         return destPath.hashCode() ^ classPath.hashCode() ^ sourceFiles.hashCode();
114     }
115 }
116