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