blob: ea757dbeddeae2ddfba256dbb90b51c34ba4539f [file] [log] [blame]
The Android Open Source Projectf8057102009-03-15 16:47:16 -07001/*
2 * Copyright (C) 2009 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
17package signature.converter.doclet;
18
19import java.io.IOException;
20import java.io.PrintWriter;
21import java.io.StringWriter;
22import java.lang.reflect.Constructor;
23import java.util.Set;
24
25import signature.converter.Visibility;
26import signature.io.IApiLoader;
27import signature.model.IApi;
28
29import com.sun.javadoc.RootDoc;
30import com.sun.tools.javac.util.Context;
31import com.sun.tools.javac.util.ListBuffer;
32import com.sun.tools.javac.util.Options;
33import com.sun.tools.javadoc.JavadocTool;
34import com.sun.tools.javadoc.Messager;
35import com.sun.tools.javadoc.ModifierFilter;
36import com.sun.tools.javadoc.RootDocImpl;
37
38public class DocletFactory implements IApiLoader {
39
40 public IApi loadApi(String name, Visibility visibility,
41 Set<String> fileNames, Set<String> packageNames) throws
42 IOException {
43 for (String packageName : packageNames) {
44 if (packageName.length() == 0)
45 throw new IllegalArgumentException(
46 "default package not supported by DocletFactory");
47 }
48 StringBuffer buf = new StringBuffer();
49 for (String filename : fileNames) {
50 buf.append(filename);
51 buf.append(":");
52 }
53 String sourcepath = buf.substring(0, buf.length() - 1);
54 RootDoc root = getRootDoc(visibility, sourcepath, packageNames);
55 DocletToSigConverter converter = new DocletToSigConverter();
56 IApi api = converter.convertDocletRoot(name, root, visibility,
57 packageNames);
58 return api;
59 }
60
61 private static RootDoc getRootDoc(Visibility visibility, String sourcepath,
62 java.util.Set<String> packages) throws IOException {
63 long accessModifier = 0;
64 switch (visibility) {
65 case PRIVATE:
66 accessModifier |= com.sun.tools.javac.code.Flags.PRIVATE; // 0x2
67 case PACKAGE: // 0x80000000
68 accessModifier |= com.sun.tools.javadoc.ModifierFilter.PACKAGE;
69 case PROTECTED:
70 accessModifier |= com.sun.tools.javac.code.Flags.PROTECTED; // 0x4
71 case PUBLIC:
72 accessModifier |= com.sun.tools.javac.code.Flags.PUBLIC; // 0x1
73 }
74
75 ModifierFilter showAccess = new ModifierFilter(accessModifier);
76 boolean breakiterator = false;
77 boolean quiet = false;
78 boolean legacy = false;
79 boolean docClasses = false;
80
81 String docLocale = "";
82 String encoding = null;
83 ListBuffer<String> javaNames = new ListBuffer<String>();
84 for (String p : packages)
85 javaNames.append(p);
86
87 ListBuffer<String[]> options = new ListBuffer<String[]>();
88
89 options.append(new String[] {"-sourcepath", sourcepath});
90
91 ListBuffer<String> subPackages = new ListBuffer<String>();
92 ListBuffer<String> excludedPackages = new ListBuffer<String>();
93
94 Context context = new Context();
95 Options compOpts = Options.instance(context);
96 compOpts.put("-sourcepath", sourcepath);
97
98 Constructor<Messager> c;
99 try {
100 // c = Messager.class.getDeclaredConstructor(Context.class,
101 // String.class);
102 // c.setAccessible(true);
103 // c.newInstance(context, "SigTest");
104 c = Messager.class.getDeclaredConstructor(Context.class,
105 String.class, PrintWriter.class, PrintWriter.class,
106 PrintWriter.class);
107 c.setAccessible(true);
108 PrintWriter err = new PrintWriter(new StringWriter());
109 PrintWriter warn = new PrintWriter(new StringWriter());
110 PrintWriter notice = new PrintWriter(new StringWriter());
111 c.newInstance(context, "SigTest", err, warn, notice);
112 } catch (Exception e) {
113 throw new RuntimeException(e);
114 }
115
116 JavadocTool comp = JavadocTool.make0(context);
117 RootDocImpl root = comp.getRootDocImpl(docLocale, encoding, showAccess,
118 javaNames.toList(), options.toList(), breakiterator,
119 subPackages.toList(), excludedPackages.toList(), docClasses,
120 legacy, quiet);
121 return root;
122 }
123
124}