• 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 com.sun.tools.javac.Main;
20 
21 import java.io.File;
22 import java.io.PrintWriter;
23 import java.util.HashSet;
24 import java.util.Set;
25 
26 public class JavacBuildStep extends SourceBuildStep {
27 
28     private final String destPath;
29     private final String classPath;
30     private final Set<String> sourceFiles = new HashSet<String>();
JavacBuildStep(String destPath, String classPath)31     public JavacBuildStep(String destPath, String classPath) {
32         super(new File(destPath));
33         this.destPath = destPath;
34         this.classPath = classPath;
35     }
36 
37     @Override
addSourceFile(String sourceFile)38     public void addSourceFile(String sourceFile)
39     {
40         sourceFiles.add(sourceFile);
41     }
42 
43     @Override
build()44     boolean build() {
45         if (super.build())
46         {
47             if (sourceFiles.isEmpty())
48             {
49                 return true;
50             }
51 
52             File destFile = new File(destPath);
53             if (!destFile.exists() && !destFile.mkdirs())
54             {
55                 System.err.println("failed to create destination dir");
56                 return false;
57             }
58             int args = 8;
59             String[] commandLine = new String[sourceFiles.size()+args];
60             commandLine[0] = "-classpath";
61             commandLine[1] = classPath;
62             commandLine[2] = "-d";
63             commandLine[3] = destPath;
64             commandLine[4] = "-source";
65             commandLine[5] = "1.7";
66             commandLine[6] = "-target";
67             commandLine[7] = "1.7";
68 
69             String[] files = new String[sourceFiles.size()];
70             sourceFiles.toArray(files);
71 
72             System.arraycopy(files, 0, commandLine, args, files.length);
73 
74             return Main.compile(commandLine, new PrintWriter(System.err)) == 0;
75         }
76         return false;
77     }
78 
79     @Override
equals(Object obj)80     public boolean equals(Object obj) {
81         if (super.equals(obj))
82         {
83             JavacBuildStep other = (JavacBuildStep) obj;
84             return destPath.equals(other.destPath)
85                 && classPath.equals(other.classPath)
86                 && sourceFiles.equals(other.sourceFiles);
87         }
88         return false;
89     }
90 
91     @Override
hashCode()92     public int hashCode() {
93         return destPath.hashCode() ^ classPath.hashCode() ^ sourceFiles.hashCode();
94     }
95 }
96