6873845: refine access to symbol file

Reviewed-by: darcy
This commit is contained in:
Jonathan Gibbons 2009-08-21 14:58:21 -07:00
parent bde1a2faf8
commit 0ec5040633
7 changed files with 179 additions and 33 deletions

View File

@ -193,10 +193,20 @@ public class Lint
/**
* Warn about unchecked operations on raw types.
*/
RAW("rawtypes");
RAW("rawtypes"),
/**
* Warn about Sun proprietary API that may be removed in a future release.
*/
SUNAPI("sunapi", true);
LintCategory(String option) {
this(option, false);
}
LintCategory(String option, boolean hidden) {
this.option = option;
this.hidden = hidden;
map.put(option, this);
}
@ -205,6 +215,7 @@ public class Lint
}
public final String option;
public final boolean hidden;
};
/**

View File

@ -119,6 +119,7 @@ public class Attr extends JCTree.Visitor {
options.get("-relax") != null);
useBeforeDeclarationWarning = options.get("useBeforeDeclarationWarning") != null;
allowInvokedynamic = options.get("invokedynamic") != null;
enableSunApiLintControl = options.get("enableSunApiLintControl") != null;
}
/** Switch: relax some constraints for retrofit mode.
@ -160,6 +161,12 @@ public class Attr extends JCTree.Visitor {
*/
boolean useBeforeDeclarationWarning;
/**
* Switch: allow lint infrastructure to control Sun proprietary
* API warnings.
*/
boolean enableSunApiLintControl;
/** Check kind and type of given tree against protokind and prototype.
* If check succeeds, store type in tree and return it.
* If check fails, store errType in tree and return it.
@ -2215,8 +2222,12 @@ public class Attr extends JCTree.Visitor {
sym.outermostClass() != env.info.scope.owner.outermostClass())
chk.warnDeprecated(tree.pos(), sym);
if ((sym.flags() & PROPRIETARY) != 0)
log.strictWarning(tree.pos(), "sun.proprietary", sym);
if ((sym.flags() & PROPRIETARY) != 0) {
if (enableSunApiLintControl)
chk.warnSunApi(tree.pos(), "sun.proprietary", sym);
else
log.strictWarning(tree.pos(), "sun.proprietary", sym);
}
// Test (3): if symbol is a variable, check that its type and
// kind are compatible with the prototype and protokind.

View File

@ -104,12 +104,15 @@ public class Check {
boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION);
boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED);
boolean verboseSunApi = lint.isEnabled(LintCategory.SUNAPI);
boolean enforceMandatoryWarnings = source.enforceMandatoryWarnings();
deprecationHandler = new MandatoryWarningHandler(log, verboseDeprecated,
enforceMandatoryWarnings, "deprecated");
uncheckedHandler = new MandatoryWarningHandler(log, verboseUnchecked,
enforceMandatoryWarnings, "unchecked");
sunApiHandler = new MandatoryWarningHandler(log, verboseSunApi,
enforceMandatoryWarnings, "sunapi");
}
/** Switch: generics enabled?
@ -137,6 +140,9 @@ public class Check {
*/
private MandatoryWarningHandler uncheckedHandler;
/** A handler for messages about using Sun proprietary API.
*/
private MandatoryWarningHandler sunApiHandler;
/* *************************************************************************
* Errors and Warnings
@ -166,12 +172,22 @@ public class Check {
uncheckedHandler.report(pos, msg, args);
}
/** Warn about using Sun proprietary API.
* @param pos Position to be used for error reporting.
* @param msg A string describing the problem.
*/
public void warnSunApi(DiagnosticPosition pos, String msg, Object... args) {
if (!lint.isSuppressed(LintCategory.SUNAPI))
sunApiHandler.report(pos, msg, args);
}
/**
* Report any deferred diagnostics.
*/
public void reportDeferredDiagnostics() {
deprecationHandler.reportDeferredDiagnostic();
uncheckedHandler.reportDeferredDiagnostic();
sunApiHandler.reportDeferredDiagnostic();
}

View File

@ -25,11 +25,11 @@
package com.sun.tools.javac.main;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Options;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
/**
* TODO: describe com.sun.tools.javac.main.JavacOption
@ -106,9 +106,10 @@ public interface JavacOption {
*/
ChoiceKind choiceKind;
/** The choices for this option, if any.
/** The choices for this option, if any, and whether or not the choices
* are hidden
*/
Collection<String> choices;
Map<String,Boolean> choices;
Option(OptionName name, String argsNameKey, String descrKey) {
this.name = name;
@ -123,10 +124,18 @@ public interface JavacOption {
}
Option(OptionName name, String descrKey, ChoiceKind choiceKind, String... choices) {
this(name, descrKey, choiceKind, Arrays.asList(choices));
this(name, descrKey, choiceKind, createChoices(choices));
}
Option(OptionName name, String descrKey, ChoiceKind choiceKind, Collection<String> choices) {
private static Map<String,Boolean> createChoices(String... choices) {
Map<String,Boolean> map = new LinkedHashMap<String,Boolean>();
for (String c: choices)
map.put(c, true);
return map;
}
Option(OptionName name, String descrKey, ChoiceKind choiceKind,
Map<String,Boolean> choices) {
this(name, null, descrKey);
if (choiceKind == null || choices == null)
throw new NullPointerException();
@ -153,10 +162,10 @@ public interface JavacOption {
if (choices != null) {
String arg = option.substring(name.optionName.length());
if (choiceKind == ChoiceKind.ONEOF)
return choices.contains(arg);
return choices.keySet().contains(arg);
else {
for (String a: arg.split(",+")) {
if (!choices.contains(a))
if (!choices.keySet().contains(a))
return false;
}
}
@ -181,10 +190,12 @@ public interface JavacOption {
if (argsNameKey == null) {
if (choices != null) {
String sep = "{";
for (String c: choices) {
sb.append(sep);
sb.append(c);
sep = ",";
for (Map.Entry<String,Boolean> e: choices.entrySet()) {
if (!e.getValue()) {
sb.append(sep);
sb.append(e.getKey());
sep = ",";
}
}
sb.append("}");
}
@ -209,8 +220,8 @@ public interface JavacOption {
if (choices != null) {
if (choiceKind == ChoiceKind.ONEOF) {
// some clients like to see just one of option+choice set
for (String c: choices)
options.remove(option + c);
for (String s: choices.keySet())
options.remove(option + s);
String opt = option + arg;
options.put(opt, opt);
// some clients like to see option (without trailing ":")
@ -256,7 +267,7 @@ public interface JavacOption {
XOption(OptionName name, String descrKey, ChoiceKind kind, String... choices) {
super(name, descrKey, kind, choices);
}
XOption(OptionName name, String descrKey, ChoiceKind kind, Collection<String> choices) {
XOption(OptionName name, String descrKey, ChoiceKind kind, Map<String,Boolean> choices) {
super(name, descrKey, kind, choices);
}
@Override

View File

@ -38,9 +38,9 @@ import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import javax.lang.model.SourceVersion;
@ -598,14 +598,14 @@ public class RecognizedOptions {
};
}
private static Collection<String> getXLintChoices() {
Collection<String> choices = new LinkedHashSet<String>();
choices.add("all");
private static Map<String,Boolean> getXLintChoices() {
Map<String,Boolean> choices = new LinkedHashMap<String,Boolean>();
choices.put("all", false);
for (Lint.LintCategory c : Lint.LintCategory.values())
choices.add(c.option);
choices.put(c.option, c.hidden);
for (Lint.LintCategory c : Lint.LintCategory.values())
choices.add("-" + c.option);
choices.add("none");
choices.put("-" + c.option, c.hidden);
choices.put("none", false);
return choices;
}

View File

@ -564,12 +564,6 @@ compiler.note.deprecated.filename.additional=\
compiler.note.deprecated.plural.additional=\
Some input files additionally use or override a deprecated API.
# Notes related to annotation processing
# Print a client-generated note; assumed to be localized, no translation required
compiler.note.proc.messager=\
{0}
compiler.note.unchecked.filename=\
{0} uses unchecked or unsafe operations.
compiler.note.unchecked.plural=\
@ -584,6 +578,25 @@ compiler.note.unchecked.filename.additional=\
compiler.note.unchecked.plural.additional=\
Some input files additionally use unchecked or unsafe operations.
compiler.note.sunapi.filename=\
{0} uses Sun proprietary API that may be removed in a future release.
compiler.note.sunapi.plural=\
Some input files use Sun proprietary API that may be removed in a future release.
# The following string may appear after one of the above sunapi messages.
compiler.note.sunapi.recompile=\
Recompile with -Xlint:sunapi for details.
compiler.note.sunapi.filename.additional=\
{0} uses additional Sun proprietary API that may be removed in a future release.
compiler.note.sunapi.plural.additional=\
Some input files additionally use Sun proprietary API that may be removed in a future release.
# Notes related to annotation processing
# Print a client-generated note; assumed to be localized, no translation required
compiler.note.proc.messager=\
{0}
#####
compiler.misc.count.error=\

View File

@ -0,0 +1,84 @@
import java.io.*;
import java.util.*;
import sun.misc.*;
/*
* @test /nodynamiccopyright/
* @bug 6873845
* @summary refine access to symbol file
*/
public class T6873845 {
public static void main(String... args) throws Exception {
new T6873845().run();
}
public void run() throws Exception {
String out = compile(Arrays.asList("-XDrawDiagnostics", "-X"));
if (out.contains("sunapi"))
throw new Exception("unexpected output for -X");
String warn1 = "T6873845.java:72:9: compiler.warn.sun.proprietary: sun.misc.Unsafe" + newline;
String warn2 = "T6873845.java:77:9: compiler.warn.sun.proprietary: sun.misc.Unsafe" + newline;
String note1 = "- compiler.note.sunapi.filename: T6873845.java" + newline;
String note2 = "- compiler.note.sunapi.recompile" + newline;
test(opts(),
warn1 + warn2 + "2 warnings" + newline);
test(opts("-XDenableSunApiLintControl"),
note1 + note2);
test(opts("-XDenableSunApiLintControl", "-XDsuppressNotes"),
"");
test(opts("-XDenableSunApiLintControl", "-Xlint:sunapi"),
warn1 + "1 warning" + newline);
test(opts("-XDenableSunApiLintControl", "-Xlint:all"),
warn1 + "1 warning" + newline);
test(opts("-XDenableSunApiLintControl", "-Xlint:all,-sunapi"),
note1 + note2);
}
List<String> opts(String... opts) {
return Arrays.asList(opts);
}
void test(List<String> opts, String expect) throws Exception {
List<String> args = new ArrayList<String>();
args.addAll(opts);
args.add("-d");
args.add(testClasses.getPath());
args.add(new File(testSrc, "T6873845.java").getPath());
compile(args); // to verify resource strings exist
args.add(0, "-XDrawDiagnostics");
String out = compile(args);
if (!out.equals(expect))
throw new Exception("unexpected output from compiler");
}
String compile(List<String> args) throws Exception{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
System.out.println("compile: " + args);
int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
pw.close();
String out = sw.toString();
System.out.println(out);
if (rc != 0)
throw new Exception("compilation failed unexpectedly");
return out;
}
void m1() {
Unsafe.getUnsafe();
}
@SuppressWarnings("sunapi")
void m2() {
Unsafe.getUnsafe();
}
private File testSrc = new File(System.getProperty("test.src", "."));
private File testClasses = new File(System.getProperty("test.classes", "."));
private String newline = System.getProperty("line.separator");
}