提交 390a2aad 编写于 作者: J jjg

8003257: refactor javadoc tool option handling

Reviewed-by: darcy
上级 b62d577d
...@@ -211,8 +211,8 @@ public class DocEnv { ...@@ -211,8 +211,8 @@ public class DocEnv {
public void setLocale(String localeName) { public void setLocale(String localeName) {
// create locale specifics // create locale specifics
doclocale = new DocLocale(this, localeName, breakiterator); doclocale = new DocLocale(this, localeName, breakiterator);
// reset Messager if locale has changed. // update Messager if locale has changed.
messager.reset(); messager.setLocale(doclocale.locale);
} }
/** Check whether this member should be documented. */ /** Check whether this member should be documented. */
......
...@@ -49,7 +49,7 @@ class DocLocale { ...@@ -49,7 +49,7 @@ class DocLocale {
final String localeName; final String localeName;
/** /**
* The locale to be used. If user doesen't provide this, * The locale to be used. If user doesn't provide this,
* then set it to default locale value. * then set it to default locale value.
*/ */
final Locale locale; final Locale locale;
...@@ -98,7 +98,7 @@ class DocLocale { ...@@ -98,7 +98,7 @@ class DocLocale {
if (locale == null) { if (locale == null) {
docenv.exit(); docenv.exit();
} else { } else {
Locale.setDefault(locale); Locale.setDefault(locale); // NOTE: updating global state
} }
collator = Collator.getInstance(locale); collator = Collator.getInstance(locale);
sentenceBreaker = BreakIterator.getSentenceInstance(locale); sentenceBreaker = BreakIterator.getSentenceInstance(locale);
......
...@@ -95,7 +95,7 @@ public class DocletInvoker { ...@@ -95,7 +95,7 @@ public class DocletInvoker {
try { try {
dc = appClassLoader.loadClass(docletClassName); dc = appClassLoader.loadClass(docletClassName);
} catch (ClassNotFoundException exc) { } catch (ClassNotFoundException exc) {
messager.error(null, "main.doclet_class_not_found", docletClassName); messager.error(Messager.NOPOS, "main.doclet_class_not_found", docletClassName);
messager.exit(); messager.exit();
} }
docletClass = dc; docletClass = dc;
...@@ -168,7 +168,7 @@ public class DocletInvoker { ...@@ -168,7 +168,7 @@ public class DocletInvoker {
if (retVal instanceof Boolean) { if (retVal instanceof Boolean) {
return ((Boolean)retVal).booleanValue(); return ((Boolean)retVal).booleanValue();
} else { } else {
messager.error(null, "main.must_return_boolean", messager.error(Messager.NOPOS, "main.must_return_boolean",
docletClassName, methodName); docletClassName, methodName);
return false; return false;
} }
...@@ -192,7 +192,7 @@ public class DocletInvoker { ...@@ -192,7 +192,7 @@ public class DocletInvoker {
if (retVal instanceof Integer) { if (retVal instanceof Integer) {
return ((Integer)retVal).intValue(); return ((Integer)retVal).intValue();
} else { } else {
messager.error(null, "main.must_return_int", messager.error(Messager.NOPOS, "main.must_return_int",
docletClassName, methodName); docletClassName, methodName);
return -1; return -1;
} }
...@@ -217,7 +217,7 @@ public class DocletInvoker { ...@@ -217,7 +217,7 @@ public class DocletInvoker {
if (retVal instanceof Boolean) { if (retVal instanceof Boolean) {
return ((Boolean)retVal).booleanValue(); return ((Boolean)retVal).booleanValue();
} else { } else {
messager.error(null, "main.must_return_boolean", messager.error(Messager.NOPOS, "main.must_return_boolean",
docletClassName, methodName); docletClassName, methodName);
return false; return false;
} }
...@@ -241,7 +241,7 @@ public class DocletInvoker { ...@@ -241,7 +241,7 @@ public class DocletInvoker {
if (retVal instanceof LanguageVersion) { if (retVal instanceof LanguageVersion) {
return (LanguageVersion)retVal; return (LanguageVersion)retVal;
} else { } else {
messager.error(null, "main.must_return_languageversion", messager.error(Messager.NOPOS, "main.must_return_languageversion",
docletClassName, methodName); docletClassName, methodName);
return JAVA_1_1; return JAVA_1_1;
} }
...@@ -261,19 +261,19 @@ public class DocletInvoker { ...@@ -261,19 +261,19 @@ public class DocletInvoker {
meth = docletClass.getMethod(methodName, paramTypes); meth = docletClass.getMethod(methodName, paramTypes);
} catch (NoSuchMethodException exc) { } catch (NoSuchMethodException exc) {
if (returnValueIfNonExistent == null) { if (returnValueIfNonExistent == null) {
messager.error(null, "main.doclet_method_not_found", messager.error(Messager.NOPOS, "main.doclet_method_not_found",
docletClassName, methodName); docletClassName, methodName);
throw new DocletInvokeException(); throw new DocletInvokeException();
} else { } else {
return returnValueIfNonExistent; return returnValueIfNonExistent;
} }
} catch (SecurityException exc) { } catch (SecurityException exc) {
messager.error(null, "main.doclet_method_not_accessible", messager.error(Messager.NOPOS, "main.doclet_method_not_accessible",
docletClassName, methodName); docletClassName, methodName);
throw new DocletInvokeException(); throw new DocletInvokeException();
} }
if (!Modifier.isStatic(meth.getModifiers())) { if (!Modifier.isStatic(meth.getModifiers())) {
messager.error(null, "main.doclet_method_must_be_static", messager.error(Messager.NOPOS, "main.doclet_method_must_be_static",
docletClassName, methodName); docletClassName, methodName);
throw new DocletInvokeException(); throw new DocletInvokeException();
} }
...@@ -283,23 +283,23 @@ public class DocletInvoker { ...@@ -283,23 +283,23 @@ public class DocletInvoker {
Thread.currentThread().setContextClassLoader(appClassLoader); Thread.currentThread().setContextClassLoader(appClassLoader);
return meth.invoke(null , params); return meth.invoke(null , params);
} catch (IllegalArgumentException exc) { } catch (IllegalArgumentException exc) {
messager.error(null, "main.internal_error_exception_thrown", messager.error(Messager.NOPOS, "main.internal_error_exception_thrown",
docletClassName, methodName, exc.toString()); docletClassName, methodName, exc.toString());
throw new DocletInvokeException(); throw new DocletInvokeException();
} catch (IllegalAccessException exc) { } catch (IllegalAccessException exc) {
messager.error(null, "main.doclet_method_not_accessible", messager.error(Messager.NOPOS, "main.doclet_method_not_accessible",
docletClassName, methodName); docletClassName, methodName);
throw new DocletInvokeException(); throw new DocletInvokeException();
} catch (NullPointerException exc) { } catch (NullPointerException exc) {
messager.error(null, "main.internal_error_exception_thrown", messager.error(Messager.NOPOS, "main.internal_error_exception_thrown",
docletClassName, methodName, exc.toString()); docletClassName, methodName, exc.toString());
throw new DocletInvokeException(); throw new DocletInvokeException();
} catch (InvocationTargetException exc) { } catch (InvocationTargetException exc) {
Throwable err = exc.getTargetException(); Throwable err = exc.getTargetException();
if (err instanceof java.lang.OutOfMemoryError) { if (err instanceof java.lang.OutOfMemoryError) {
messager.error(null, "main.out.of.memory"); messager.error(Messager.NOPOS, "main.out.of.memory");
} else { } else {
messager.error(null, "main.exception_thrown", messager.error(Messager.NOPOS, "main.exception_thrown",
docletClassName, methodName, exc.toString()); docletClassName, methodName, exc.toString());
exc.getTargetException().printStackTrace(); exc.getTargetException().printStackTrace();
} }
......
...@@ -38,7 +38,6 @@ import javax.tools.StandardJavaFileManager; ...@@ -38,7 +38,6 @@ import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation; import javax.tools.StandardLocation;
import com.sun.tools.javac.code.Symbol.CompletionFailure; import com.sun.tools.javac.code.Symbol.CompletionFailure;
import com.sun.tools.javac.comp.Annotate;
import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCClassDecl; import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
...@@ -65,11 +64,9 @@ import com.sun.tools.javac.util.Position; ...@@ -65,11 +64,9 @@ import com.sun.tools.javac.util.Position;
public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler { public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler {
DocEnv docenv; DocEnv docenv;
final Context context;
final Messager messager; final Messager messager;
final JavadocClassReader reader; final JavadocClassReader javadocReader;
final JavadocEnter enter; final JavadocEnter javadocEnter;
final Annotate annotate;
/** /**
* Construct a new JavaCompiler processor, using appropriately * Construct a new JavaCompiler processor, using appropriately
...@@ -77,11 +74,9 @@ public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler { ...@@ -77,11 +74,9 @@ public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler {
*/ */
protected JavadocTool(Context context) { protected JavadocTool(Context context) {
super(context); super(context);
this.context = context;
messager = Messager.instance0(context); messager = Messager.instance0(context);
reader = JavadocClassReader.instance0(context); javadocReader = JavadocClassReader.instance0(context);
enter = JavadocEnter.instance0(context); javadocEnter = JavadocEnter.instance0(context);
annotate = Annotate.instance(context);
} }
/** /**
...@@ -138,7 +133,7 @@ public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler { ...@@ -138,7 +133,7 @@ public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler {
docenv.setEncoding(encoding); docenv.setEncoding(encoding);
docenv.docClasses = docClasses; docenv.docClasses = docClasses;
docenv.legacyDoclet = legacyDoclet; docenv.legacyDoclet = legacyDoclet;
reader.sourceCompleter = docClasses ? null : this; javadocReader.sourceCompleter = docClasses ? null : this;
ListBuffer<String> names = new ListBuffer<String>(); ListBuffer<String> names = new ListBuffer<String>();
ListBuffer<JCCompilationUnit> classTrees = new ListBuffer<JCCompilationUnit>(); ListBuffer<JCCompilationUnit> classTrees = new ListBuffer<JCCompilationUnit>();
...@@ -179,7 +174,7 @@ public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler { ...@@ -179,7 +174,7 @@ public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler {
// Enter symbols for all files // Enter symbols for all files
docenv.notice("main.Building_tree"); docenv.notice("main.Building_tree");
enter.main(classTrees.toList().appendList(packTrees.toList())); javadocEnter.main(classTrees.toList().appendList(packTrees.toList()));
} }
} catch (Abort ex) {} } catch (Abort ex) {}
...@@ -240,7 +235,7 @@ public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler { ...@@ -240,7 +235,7 @@ public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler {
} }
if (!hasFiles) { if (!hasFiles) {
messager.warning(null, "main.no_source_files_for_package", messager.warning(Messager.NOPOS, "main.no_source_files_for_package",
name.replace(File.separatorChar, '.')); name.replace(File.separatorChar, '.'));
} }
} }
......
...@@ -25,13 +25,15 @@ ...@@ -25,13 +25,15 @@
package com.sun.tools.javadoc; package com.sun.tools.javadoc;
import java.io.PrintWriter; // Access to 'javac' output streams import java.io.PrintWriter;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.MissingResourceException; import java.util.Locale;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import com.sun.javadoc.*; import com.sun.javadoc.*;
import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic;
import com.sun.tools.javac.util.JavacMessages;
import com.sun.tools.javac.util.Log; import com.sun.tools.javac.util.Log;
/** /**
...@@ -51,6 +53,7 @@ import com.sun.tools.javac.util.Log; ...@@ -51,6 +53,7 @@ import com.sun.tools.javac.util.Log;
* @author Neal Gafter (rewrite) * @author Neal Gafter (rewrite)
*/ */
public class Messager extends Log implements DocErrorReporter { public class Messager extends Log implements DocErrorReporter {
public static final SourcePosition NOPOS = null;
/** Get the current messager, which is also the compiler log. */ /** Get the current messager, which is also the compiler log. */
public static Messager instance0(Context context) { public static Messager instance0(Context context) {
...@@ -91,7 +94,9 @@ public class Messager extends Log implements DocErrorReporter { ...@@ -91,7 +94,9 @@ public class Messager extends Log implements DocErrorReporter {
final String programName; final String programName;
private ResourceBundle messageRB = null; private Locale locale;
private final JavacMessages messages;
private final JCDiagnostic.Factory javadocDiags;
/** The default writer for diagnostics /** The default writer for diagnostics
*/ */
...@@ -121,6 +126,9 @@ public class Messager extends Log implements DocErrorReporter { ...@@ -121,6 +126,9 @@ public class Messager extends Log implements DocErrorReporter {
PrintWriter warnWriter, PrintWriter warnWriter,
PrintWriter noticeWriter) { PrintWriter noticeWriter) {
super(context, errWriter, warnWriter, noticeWriter); super(context, errWriter, warnWriter, noticeWriter);
messages = JavacMessages.instance(context);
messages.add("com.sun.tools.javadoc.resources.javadoc");
javadocDiags = new JCDiagnostic.Factory(messages, "javadoc");
this.programName = programName; this.programName = programName;
} }
...@@ -134,94 +142,18 @@ public class Messager extends Log implements DocErrorReporter { ...@@ -134,94 +142,18 @@ public class Messager extends Log implements DocErrorReporter {
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
} }
/** public void setLocale(Locale locale) {
* Reset resource bundle, eg. locale has changed. this.locale = locale;
*/
public void reset() {
messageRB = null;
}
/**
* Get string from ResourceBundle, initialize ResourceBundle
* if needed.
*/
private String getString(String key) {
if (messageRB == null) {
try {
messageRB = ResourceBundle.getBundle(
"com.sun.tools.javadoc.resources.javadoc");
} catch (MissingResourceException e) {
throw new Error("Fatal: Resource for javadoc is missing");
}
}
return messageRB.getString(key);
}
/**
* get and format message string from resource
*
* @param key selects message from resource
*/
String getText(String key) {
return getText(key, (String)null);
}
/**
* get and format message string from resource
*
* @param key selects message from resource
* @param a1 first argument
*/
String getText(String key, String a1) {
return getText(key, a1, null);
}
/**
* get and format message string from resource
*
* @param key selects message from resource
* @param a1 first argument
* @param a2 second argument
*/
String getText(String key, String a1, String a2) {
return getText(key, a1, a2, null);
}
/**
* get and format message string from resource
*
* @param key selects message from resource
* @param a1 first argument
* @param a2 second argument
* @param a3 third argument
*/
String getText(String key, String a1, String a2, String a3) {
return getText(key, a1, a2, a3, null);
} }
/** /**
* get and format message string from resource * get and format message string from resource
* *
* @param key selects message from resource * @param key selects message from resource
* @param a1 first argument * @param args arguments for the message
* @param a2 second argument
* @param a3 third argument
* @param a4 fourth argument
*/ */
String getText(String key, String a1, String a2, String a3, String getText(String key, Object... args) {
String a4) { return messages.getLocalizedString(locale, key, args);
try {
String message = getString(key);
String[] args = new String[4];
args[0] = a1;
args[1] = a2;
args[2] = a3;
args[3] = a4;
return MessageFormat.format(message, (Object[])args);
} catch (MissingResourceException e) {
return "********** Resource for javadoc is broken. There is no " +
key + " key in resource.";
}
} }
/** /**
...@@ -307,138 +239,26 @@ public class Messager extends Log implements DocErrorReporter { ...@@ -307,138 +239,26 @@ public class Messager extends Log implements DocErrorReporter {
* *
* @param key selects message from resource * @param key selects message from resource
*/ */
public void error(SourcePosition pos, String key) { public void error(SourcePosition pos, String key, Object... args) {
printError(pos, getText(key)); printError(pos, getText(key, args));
}
/**
* Print error message, increment error count.
*
* @param key selects message from resource
* @param a1 first argument
*/
public void error(SourcePosition pos, String key, String a1) {
printError(pos, getText(key, a1));
}
/**
* Print error message, increment error count.
*
* @param key selects message from resource
* @param a1 first argument
* @param a2 second argument
*/
public void error(SourcePosition pos, String key, String a1, String a2) {
printError(pos, getText(key, a1, a2));
}
/**
* Print error message, increment error count.
*
* @param key selects message from resource
* @param a1 first argument
* @param a2 second argument
* @param a3 third argument
*/
public void error(SourcePosition pos, String key, String a1, String a2, String a3) {
printError(pos, getText(key, a1, a2, a3));
}
/**
* Print warning message, increment warning count.
*
* @param key selects message from resource
*/
public void warning(SourcePosition pos, String key) {
printWarning(pos, getText(key));
}
/**
* Print warning message, increment warning count.
*
* @param key selects message from resource
* @param a1 first argument
*/
public void warning(SourcePosition pos, String key, String a1) {
printWarning(pos, getText(key, a1));
}
/**
* Print warning message, increment warning count.
*
* @param key selects message from resource
* @param a1 first argument
* @param a2 second argument
*/
public void warning(SourcePosition pos, String key, String a1, String a2) {
printWarning(pos, getText(key, a1, a2));
}
/**
* Print warning message, increment warning count.
*
* @param key selects message from resource
* @param a1 first argument
* @param a2 second argument
* @param a3 third argument
*/
public void warning(SourcePosition pos, String key, String a1, String a2, String a3) {
printWarning(pos, getText(key, a1, a2, a3));
} }
/** /**
* Print warning message, increment warning count. * Print warning message, increment warning count.
* *
* @param key selects message from resource * @param key selects message from resource
* @param a1 first argument
* @param a2 second argument
* @param a3 third argument
*/
public void warning(SourcePosition pos, String key, String a1, String a2, String a3,
String a4) {
printWarning(pos, getText(key, a1, a2, a3, a4));
}
/**
* Print a message.
*
* @param key selects message from resource
*/ */
public void notice(String key) { public void warning(SourcePosition pos, String key, Object... args) {
printNotice(getText(key)); printWarning(pos, getText(key, args));
} }
/** /**
* Print a message. * Print a message.
* *
* @param key selects message from resource * @param key selects message from resource
* @param a1 first argument
*/ */
public void notice(String key, String a1) { public void notice(String key, Object... args) {
printNotice(getText(key, a1)); printNotice(getText(key, args));
}
/**
* Print a message.
*
* @param key selects message from resource
* @param a1 first argument
* @param a2 second argument
*/
public void notice(String key, String a1, String a2) {
printNotice(getText(key, a1, a2));
}
/**
* Print a message.
*
* @param key selects message from resource
* @param a1 first argument
* @param a2 second argument
* @param a3 third argument
*/
public void notice(String key, String a1, String a2, String a3) {
printNotice(getText(key, a1, a2, a3));
} }
/** /**
...@@ -475,5 +295,4 @@ public class Messager extends Log implements DocErrorReporter { ...@@ -475,5 +295,4 @@ public class Messager extends Log implements DocErrorReporter {
public void exit() { public void exit() {
throw new ExitJavadoc(); throw new ExitJavadoc();
} }
} }
...@@ -29,7 +29,6 @@ import java.io.File; ...@@ -29,7 +29,6 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.StringTokenizer;
import com.sun.javadoc.*; import com.sun.javadoc.*;
import com.sun.tools.javac.main.CommandLine; import com.sun.tools.javac.main.CommandLine;
...@@ -53,7 +52,7 @@ import static com.sun.tools.javac.code.Flags.*; ...@@ -53,7 +52,7 @@ import static com.sun.tools.javac.code.Flags.*;
* @author Robert Field * @author Robert Field
* @author Neal Gafter (rewrite) * @author Neal Gafter (rewrite)
*/ */
class Start { public class Start extends ToolOption.Helper {
/** Context for this invocation. */ /** Context for this invocation. */
private final Context context; private final Context context;
...@@ -65,25 +64,12 @@ class Start { ...@@ -65,25 +64,12 @@ class Start {
private static final String standardDocletClassName = private static final String standardDocletClassName =
"com.sun.tools.doclets.standard.Standard"; "com.sun.tools.doclets.standard.Standard";
private ListBuffer<String[]> options = new ListBuffer<String[]>();
private ModifierFilter showAccess = null;
private long defaultFilter = PUBLIC | PROTECTED; private long defaultFilter = PUBLIC | PROTECTED;
private final Messager messager; private final Messager messager;
String docLocale = "";
boolean breakiterator = false;
boolean quiet = false;
String encoding = null;
private DocletInvoker docletInvoker; private DocletInvoker docletInvoker;
/* Treat warnings as errors. */
private boolean rejectWarnings = false;
Start(String programName, Start(String programName,
PrintWriter errWriter, PrintWriter errWriter,
PrintWriter warnWriter, PrintWriter warnWriter,
...@@ -132,23 +118,58 @@ class Start { ...@@ -132,23 +118,58 @@ class Start {
this(javadocName); this(javadocName);
} }
public Start(Context context) {
context.getClass(); // null check
this.context = context;
defaultDocletClassName = standardDocletClassName;
docletParentClassLoader = null;
Log log = context.get(Log.logKey);
if (log instanceof Messager)
messager = (Messager) log;
else {
PrintWriter out = context.get(Log.outKey);
messager = (out == null) ? new Messager(context, javadocName)
: new Messager(context, javadocName, out, out, out);
}
}
/**
* Usage
*/
@Override
void usage() {
usage(true);
}
/** /**
* Usage * Usage
*/ */
private void usage() { private void usage(boolean exit) {
// RFE: it would be better to replace the following with code to
// write a header, then help for each option, then a footer.
messager.notice("main.usage"); messager.notice("main.usage");
// let doclet print usage information (does nothing on error) // let doclet print usage information (does nothing on error)
if (docletInvoker != null) { if (docletInvoker != null) {
docletInvoker.optionLength("-help"); docletInvoker.optionLength("-help");
} }
if (exit) exit();
}
@Override
void Xusage() {
Xusage(true);
} }
/** /**
* Usage * Usage
*/ */
private void Xusage() { private void Xusage(boolean exit) {
messager.notice("main.Xusage"); messager.notice("main.Xusage");
if (exit) exit();
} }
/** /**
...@@ -167,18 +188,18 @@ class Start { ...@@ -167,18 +188,18 @@ class Start {
try { try {
failed = !parseAndExecute(argv); failed = !parseAndExecute(argv);
} catch(Messager.ExitJavadoc exc) { } catch (Messager.ExitJavadoc exc) {
// ignore, we just exit this way // ignore, we just exit this way
} catch (OutOfMemoryError ee) { } catch (OutOfMemoryError ee) {
messager.error(null, "main.out.of.memory"); messager.error(Messager.NOPOS, "main.out.of.memory");
failed = true; failed = true;
} catch (Error ee) { } catch (Error ee) {
ee.printStackTrace(System.err); ee.printStackTrace(System.err);
messager.error(null, "main.fatal.error"); messager.error(Messager.NOPOS, "main.fatal.error");
failed = true; failed = true;
} catch (Exception ee) { } catch (Exception ee) {
ee.printStackTrace(System.err); ee.printStackTrace(System.err);
messager.error(null, "main.fatal.exception"); messager.error(Messager.NOPOS, "main.fatal.exception");
failed = true; failed = true;
} finally { } finally {
messager.exitNotice(); messager.exitNotice();
...@@ -189,15 +210,6 @@ class Start { ...@@ -189,15 +210,6 @@ class Start {
return failed ? 1 : 0; return failed ? 1 : 0;
} }
private void addToList(ListBuffer<String> list, String str){
StringTokenizer st = new StringTokenizer(str, ":");
String current;
while(st.hasMoreTokens()){
current = st.nextToken();
list.append(current);
}
}
/** /**
* Main program - internal * Main program - internal
*/ */
...@@ -210,7 +222,7 @@ class Start { ...@@ -210,7 +222,7 @@ class Start {
try { try {
argv = CommandLine.parse(argv); argv = CommandLine.parse(argv);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
messager.error(null, "main.cant.read", e.getMessage()); messager.error(Messager.NOPOS, "main.cant.read", e.getMessage());
exit(); exit();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(System.err); e.printStackTrace(System.err);
...@@ -218,116 +230,29 @@ class Start { ...@@ -218,116 +230,29 @@ class Start {
} }
setDocletInvoker(argv); setDocletInvoker(argv);
ListBuffer<String> subPackages = new ListBuffer<String>();
ListBuffer<String> excludedPackages = new ListBuffer<String>();
Options compOpts = Options.instance(context); compOpts = Options.instance(context);
boolean docClasses = false;
// Parse arguments // Parse arguments
for (int i = 0 ; i < argv.length ; i++) { for (int i = 0 ; i < argv.length ; i++) {
String arg = argv[i]; String arg = argv[i];
if (arg.equals("-subpackages")) {
oneArg(argv, i++); ToolOption o = ToolOption.get(arg);
addToList(subPackages, argv[i]); if (o != null) {
} else if (arg.equals("-exclude")){ // hack: this restriction should be removed
oneArg(argv, i++); if (o == ToolOption.LOCALE && i > 0)
addToList(excludedPackages, argv[i]);
} else if (arg.equals("-verbose")) {
setOption(arg);
compOpts.put("-verbose", "");
} else if (arg.equals("-encoding")) {
oneArg(argv, i++);
encoding = argv[i];
compOpts.put("-encoding", argv[i]);
} else if (arg.equals("-breakiterator")) {
breakiterator = true;
setOption("-breakiterator");
} else if (arg.equals("-quiet")) {
quiet = true;
setOption("-quiet");
} else if (arg.equals("-help")) {
usage();
exit();
} else if (arg.equals("-Xclasses")) {
setOption(arg);
docClasses = true;
} else if (arg.equals("-Xwerror")) {
setOption(arg);
rejectWarnings = true;
} else if (arg.equals("-private")) {
setOption(arg);
setFilter(ModifierFilter.ALL_ACCESS);
} else if (arg.equals("-package")) {
setOption(arg);
setFilter(PUBLIC | PROTECTED |
ModifierFilter.PACKAGE );
} else if (arg.equals("-protected")) {
setOption(arg);
setFilter(PUBLIC | PROTECTED );
} else if (arg.equals("-public")) {
setOption(arg);
setFilter(PUBLIC);
} else if (arg.equals("-source")) {
oneArg(argv, i++);
if (compOpts.get("-source") != null) {
usageError("main.option.already.seen", arg);
}
compOpts.put("-source", argv[i]);
} else if (arg.equals("-prompt")) {
compOpts.put("-prompt", "-prompt");
messager.promptOnError = true;
} else if (arg.equals("-sourcepath")) {
oneArg(argv, i++);
if (compOpts.get("-sourcepath") != null) {
usageError("main.option.already.seen", arg);
}
compOpts.put("-sourcepath", argv[i]);
} else if (arg.equals("-classpath")) {
oneArg(argv, i++);
if (compOpts.get("-classpath") != null) {
usageError("main.option.already.seen", arg);
}
compOpts.put("-classpath", argv[i]);
} else if (arg.equals("-sysclasspath")) {
oneArg(argv, i++);
if (compOpts.get("-bootclasspath") != null) {
usageError("main.option.already.seen", arg);
}
compOpts.put("-bootclasspath", argv[i]);
} else if (arg.equals("-bootclasspath")) {
oneArg(argv, i++);
if (compOpts.get("-bootclasspath") != null) {
usageError("main.option.already.seen", arg);
}
compOpts.put("-bootclasspath", argv[i]);
} else if (arg.equals("-extdirs")) {
oneArg(argv, i++);
if (compOpts.get("-extdirs") != null) {
usageError("main.option.already.seen", arg);
}
compOpts.put("-extdirs", argv[i]);
} else if (arg.equals("-overview")) {
oneArg(argv, i++);
} else if (arg.equals("-doclet")) {
i++; // handled in setDocletInvoker
} else if (arg.equals("-docletpath")) {
i++; // handled in setDocletInvoker
} else if (arg.equals("-locale")) {
if (i != 0)
usageError("main.locale_first"); usageError("main.locale_first");
if (o.hasArg) {
oneArg(argv, i++); oneArg(argv, i++);
docLocale = argv[i]; o.process(this, argv[i]);
} else if (arg.equals("-Xmaxerrs") || arg.equals("-Xmaxwarns")) { } else {
oneArg(argv, i++); setOption(arg);
if (compOpts.get(arg) != null) { o.process(this);
usageError("main.option.already.seen", arg);
} }
compOpts.put(arg, argv[i]);
} else if (arg.equals("-X")) {
Xusage();
exit();
} else if (arg.startsWith("-XD")) { } else if (arg.startsWith("-XD")) {
// hidden javac options
String s = arg.substring("-XD".length()); String s = arg.substring("-XD".length());
int eq = s.indexOf('='); int eq = s.indexOf('=');
String key = (eq < 0) ? s : s.substring(0, eq); String key = (eq < 0) ? s : s.substring(0, eq);
...@@ -336,7 +261,7 @@ class Start { ...@@ -336,7 +261,7 @@ class Start {
} }
// call doclet for its options // call doclet for its options
// other arg starts with - is invalid // other arg starts with - is invalid
else if ( arg.startsWith("-") ) { else if (arg.startsWith("-")) {
int optionLength; int optionLength;
optionLength = docletInvoker.optionLength(arg); optionLength = docletInvoker.optionLength(arg);
if (optionLength < 0) { if (optionLength < 0) {
...@@ -380,12 +305,18 @@ class Start { ...@@ -380,12 +305,18 @@ class Start {
LanguageVersion languageVersion = docletInvoker.languageVersion(); LanguageVersion languageVersion = docletInvoker.languageVersion();
RootDocImpl root = comp.getRootDocImpl( RootDocImpl root = comp.getRootDocImpl(
docLocale, encoding, showAccess, docLocale,
javaNames.toList(), options.toList(), breakiterator, encoding,
subPackages.toList(), excludedPackages.toList(), showAccess,
javaNames.toList(),
options.toList(),
breakiterator,
subPackages.toList(),
excludedPackages.toList(),
docClasses, docClasses,
// legacy? // legacy?
languageVersion == null || languageVersion == LanguageVersion.JAVA_1_1, quiet); languageVersion == null || languageVersion == LanguageVersion.JAVA_1_1,
quiet);
// release resources // release resources
comp = null; comp = null;
...@@ -437,15 +368,6 @@ class Start { ...@@ -437,15 +368,6 @@ class Start {
docletParentClassLoader); docletParentClassLoader);
} }
private void setFilter(long filterBits) {
if (showAccess != null) {
messager.error(null, "main.incompatible.access.flags");
usage();
exit();
}
showAccess = new ModifierFilter(filterBits);
}
/** /**
* Set one arg option. * Set one arg option.
* Error and exit if one argument is not provided. * Error and exit if one argument is not provided.
...@@ -458,22 +380,10 @@ class Start { ...@@ -458,22 +380,10 @@ class Start {
} }
} }
private void usageError(String key) { @Override
messager.error(null, key); void usageError(String key, Object... args) {
usage(); messager.error(Messager.NOPOS, key, args);
exit(); usage(true);
}
private void usageError(String key, String a1) {
messager.error(null, key, a1);
usage();
exit();
}
private void usageError(String key, String a1, String a2) {
messager.error(null, key, a1, a2);
usage();
exit();
} }
/** /**
...@@ -502,7 +412,6 @@ class Start { ...@@ -502,7 +412,6 @@ class Start {
for (List<String> i = arguments; i.nonEmpty(); i=i.tail) { for (List<String> i = arguments; i.nonEmpty(); i=i.tail) {
args[k++] = i.head; args[k++] = i.head;
} }
options = options.append(args); options.append(args);
} }
} }
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.javadoc;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Options;
import java.util.StringTokenizer;
/**
* javadoc tool options.
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*/
public enum ToolOption {
// ----- options for underlying compiler -----
BOOTCLASSPATH("-bootclasspath", true) {
@Override
public void process(Helper helper, String arg) {
helper.setCompilerOpt(opt, arg);
}
},
CLASSPATH("-classpath", true) {
@Override
public void process(Helper helper, String arg) {
helper.setCompilerOpt(opt, arg);
}
},
EXTDIRS("-extdirs", true) {
@Override
public void process(Helper helper, String arg) {
helper.setCompilerOpt(opt, arg);
}
},
SOURCEPATH("-sourcepath", true) {
@Override
public void process(Helper helper, String arg) {
helper.setCompilerOpt(opt, arg);
}
},
SYSCLASSPATH("-sysclasspath", true) {
@Override
public void process(Helper helper, String arg) {
helper.setCompilerOpt("-bootclasspath", arg);
}
},
ENCODING("-encoding", true) {
@Override
public void process(Helper helper, String arg) {
helper.encoding = arg;
helper.setCompilerOpt(opt, arg);
}
},
SOURCE("-source", true) {
@Override
public void process(Helper helper, String arg) {
helper.setCompilerOpt(opt, arg);
}
},
XMAXERRS("-Xmaxerrs", true) {
@Override
public void process(Helper helper, String arg) {
helper.setCompilerOpt(opt, arg);
}
},
XMAXWARNS("-Xmaxwarns", true) {
@Override
public void process(Helper helper, String arg) {
helper.setCompilerOpt(opt, arg);
}
},
// ----- doclet options -----
DOCLET("-doclet", true), // handled in setDocletInvoker
DOCLETPATH("-docletpath", true), // handled in setDocletInvoker
// ----- selection options -----
SUBPACKAGES("-subpackages", true) {
@Override
public void process(Helper helper, String arg) {
helper.addToList(helper.subPackages, arg);
}
},
EXCLUDE("-exclude", true) {
@Override
public void process(Helper helper, String arg) {
helper.addToList(helper.excludedPackages, arg);
}
},
// ----- filtering options -----
PACKAGE("-package") {
@Override
public void process(Helper helper) {
helper.setFilter(
Flags.PUBLIC | Flags.PROTECTED | ModifierFilter.PACKAGE);
}
},
PRIVATE("-private") {
@Override
public void process(Helper helper) {
helper.setFilter(ModifierFilter.ALL_ACCESS);
}
},
PROTECTED("-protected") {
@Override
public void process(Helper helper) {
helper.setFilter(Flags.PUBLIC | Flags.PROTECTED);
}
},
PUBLIC("-public") {
@Override
public void process(Helper helper) {
helper.setFilter(Flags.PUBLIC);
}
},
// ----- output control options -----
PROMPT("-prompt") {
@Override
public void process(Helper helper) {
helper.compOpts.put("-prompt", "-prompt");
helper.promptOnError = true;
}
},
QUIET("-quiet") {
@Override
public void process(Helper helper) {
helper.quiet = true;
}
},
VERBOSE("-verbose") {
@Override
public void process(Helper helper) {
helper.compOpts.put("-verbose", "");
}
},
XWERROR("-Xwerror") {
@Override
public void process(Helper helper) {
helper.rejectWarnings = true;
}
},
// ----- other options -----
BREAKITERATOR("-breakiterator") {
@Override
public void process(Helper helper) {
helper.breakiterator = true;
}
},
LOCALE("-locale", true) {
@Override
public void process(Helper helper, String arg) {
helper.docLocale = arg;
}
},
OVERVIEW("-overview", true),
XCLASSES("-Xclasses") {
@Override
public void process(Helper helper) {
helper.docClasses = true;
}
},
// ----- help options -----
HELP("-help") {
@Override
public void process(Helper helper) {
helper.usage();
}
},
X("-X") {
@Override
public void process(Helper helper) {
helper.Xusage();
}
};
public final String opt;
public final boolean hasArg;
ToolOption(String opt) {
this(opt, false);
}
ToolOption(String opt, boolean hasArg) {
this.opt = opt;
this.hasArg = hasArg;
}
void process(Helper helper, String arg) { }
void process(Helper helper) { }
static ToolOption get(String name) {
for (ToolOption o: values()) {
if (name.equals(o.opt))
return o;
}
return null;
}
static abstract class Helper {
/** List of decoded options. */
final ListBuffer<String[]> options = new ListBuffer<String[]>();
/** Selected packages, from -subpackages. */
final ListBuffer<String> subPackages = new ListBuffer<String>();
/** Excluded packages, from -exclude. */
final ListBuffer<String> excludedPackages = new ListBuffer<String>();
/** javac options, set by various options. */
Options compOpts; // = Options.instance(context)
/* Encoding for javac, and files written? set by -encoding. */
String encoding = null;
/** Set by -breakiterator. */
boolean breakiterator = false;
/** Set by -quiet. */
boolean quiet = false;
/** Set by -Xclasses. */
boolean docClasses = false;
/** Set by -Xwerror. */
boolean rejectWarnings = false;
/** Set by -prompt. */
boolean promptOnError;
/** Set by -locale. */
String docLocale = "";
/** Set by -public, private, -protected, -package. */
ModifierFilter showAccess = null;
abstract void usage();
abstract void Xusage();
abstract void usageError(String msg, Object... args);
protected void addToList(ListBuffer<String> list, String str){
StringTokenizer st = new StringTokenizer(str, ":");
String current;
while(st.hasMoreTokens()){
current = st.nextToken();
list.append(current);
}
}
protected void setFilter(long filterBits) {
if (showAccess != null) {
usageError("main.incompatible.access.flags");
}
showAccess = new ModifierFilter(filterBits);
}
private void setCompilerOpt(String opt, String arg) {
if (compOpts.get(opt) != null) {
usageError("main.option.already.seen", opt);
}
compOpts.put(opt, arg);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册