1 /* 2 * Copyright (C) 2011 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 package com.android.cts.javascanner; 17 18 import java.io.BufferedReader; 19 import java.io.File; 20 import java.io.FileFilter; 21 import java.io.IOException; 22 import java.io.InputStreamReader; 23 import java.util.ArrayList; 24 import java.util.List; 25 26 class DocletRunner { 27 28 private final List<File> mSourceDirs; 29 private final File mDocletPath; 30 DocletRunner(List<File> sourceDirs, File docletPath)31 DocletRunner(List<File> sourceDirs, File docletPath) { 32 mSourceDirs = sourceDirs; 33 mDocletPath = docletPath; 34 } 35 runJavaDoc()36 int runJavaDoc() throws IOException, InterruptedException { 37 List<String> args = new ArrayList<String>(); 38 args.add("javadoc"); 39 args.add("-doclet"); 40 args.add("com.android.cts.javascannerdoclet.CtsJavaScannerDoclet"); 41 args.add("-docletpath"); 42 args.add(mDocletPath.toString()); 43 args.add("-sourcepath"); 44 args.add(getSourcePath(mSourceDirs)); 45 args.add("-classpath"); 46 args.add(getClassPath()); 47 for (File sourceDir : mSourceDirs) { 48 args.addAll(getSourceFiles(sourceDir)); 49 } 50 51 52 // NOTE: We redirect the error stream to make sure the child process 53 // isn't blocked due to a full pipe. (The javadoc tool writes source errors 54 // to stderr.) 55 Process process = new ProcessBuilder(args).redirectErrorStream(true).start(); 56 BufferedReader reader = new BufferedReader( 57 new InputStreamReader(process.getInputStream())); 58 try { 59 String line = null; 60 while ((line = reader.readLine()) != null) { 61 System.out.println(line); 62 } 63 } finally { 64 if (reader != null) { 65 reader.close(); 66 } 67 } 68 69 return process.waitFor(); 70 } 71 getSourcePath(List<File> sourceDirs)72 private String getSourcePath(List<File> sourceDirs) { 73 List<String> sourcePath = new ArrayList<String>(); 74 sourcePath.add("./frameworks/base/core/java"); 75 sourcePath.add("./frameworks/base/test-runner/src"); 76 sourcePath.add("./external/junit/src"); 77 sourcePath.add("./development/tools/hosttestlib/src"); 78 sourcePath.add("./libcore/dalvik/src/main/java"); 79 sourcePath.add("./cts/tests/src"); 80 sourcePath.add("./cts/libs/commonutil/src"); 81 sourcePath.add("./cts/libs/deviceutil/src"); 82 for (File sourceDir : sourceDirs) { 83 sourcePath.add(sourceDir.toString()); 84 } 85 return join(sourcePath, ":"); 86 } 87 getClassPath()88 private String getClassPath() { 89 List<String> classPath = new ArrayList<String>(); 90 classPath.add("./prebuilts/misc/common/tradefed/tradefed-prebuilt.jar"); 91 classPath.add("./prebuilts/misc/common/ub-uiautomator/ub-uiautomator.jar"); 92 classPath.add("./prebuilts/misc/common/ub-janktesthelper/ub-janktesthelper.jar"); 93 return join(classPath, ":"); 94 } 95 getSourceFiles(File sourceDir)96 private List<String> getSourceFiles(File sourceDir) { 97 List<String> sourceFiles = new ArrayList<String>(); 98 99 File[] files = sourceDir.listFiles(new FileFilter() { 100 @Override 101 public boolean accept(File pathname) { 102 return pathname.isDirectory() || pathname.toString().endsWith(".java"); 103 } 104 }); 105 106 for (File file : files) { 107 if (file.isDirectory()) { 108 sourceFiles.addAll(getSourceFiles(file)); 109 } else { 110 sourceFiles.add(file.toString()); 111 } 112 } 113 114 return sourceFiles; 115 } 116 join(List<String> options, String delimiter)117 private String join(List<String> options, String delimiter) { 118 StringBuilder builder = new StringBuilder(); 119 int numOptions = options.size(); 120 for (int i = 0; i < numOptions; i++) { 121 builder.append(options.get(i)); 122 if (i + 1 < numOptions) { 123 builder.append(delimiter); 124 } 125 } 126 return builder.toString(); 127 } 128 } 129