我们正在将旧的代码(实际上是很旧的代码)迁移到Java11。在编译其中一个类时遇到了问题。示例代码为:
package XXXX;
import java.lang.ref.*;
import sun.security.action.*;
import java.security.*;
import java.io.*;
class Converters
{
private static Object lock;
private static String converterPackageName;
private static String defaultEncoding;
public static final int BYTE_TO_CHAR = 0;
public static final int CHAR_TO_BYTE = 1;
private static final String[] converterPrefix;
private static SoftReference[] classCache;
static /* synthetic */ Class class$sun$io$Converters;
private static String getConverterPackageName() {
final String converterPackageName = Converters.converterPackageName;
if (converterPackageName != null) {
return converterPackageName;
}
String converterPackageName2 = AccessController.doPrivileged((PrivilegedAction<String>)new GetPropertyAction("file.encoding.pkg"));
if (converterPackageName2 != null) {
Converters.converterPackageName = converterPackageName2;
}
else {
converterPackageName2 = "sun.io";
}
return converterPackageName2;
}
private static Class getConverterClass(final int n, final String s) throws UnsupportedEncodingException {
String aliasName = null;
if (!s.equals("ISO8859_1")) {
if (s.equals("8859_1")) {
aliasName = "ISO8859_1";
}
else if (s.equals("ISO8859-1")) {
aliasName = "ISO8859_1";
}
else if (s.equals("646")) {
aliasName = "ASCII";
}
else {
aliasName = CharacterEncoding.aliasName(s);
}
}
if (aliasName == null) {
aliasName = s;
}
try {
return Class.forName(getConverterPackageName() + "." + Converters.converterPrefix[n] + aliasName);
}
catch (ClassNotFoundException ex) {
throw new UnsupportedEncodingException(aliasName);
}
}
private static Object newConverter(final String s, final Class clazz) throws UnsupportedEncodingException {
try {
return clazz.newInstance();
}
catch (InstantiationException ex) {
throw new UnsupportedEncodingException(s);
}
catch (IllegalAccessException ex2) {
throw new UnsupportedEncodingException(s);
}
}
static Object newConverter(final int n, final String s) throws UnsupportedEncodingException {
final Class converterClass;
synchronized (Converters.lock) {
converterClass = getConverterClass(n, s);
}
return newConverter(s, converterClass);
}
private static Class getDefaultConverterClass(final int n) {
boolean b = false;
final SoftReference softReference = Converters.classCache[n];
if (softReference != null) {
final Class clazz = softReference.get();
if (clazz != null) {
return clazz;
}
Converters.classCache[n] = null;
}
String defaultEncoding = Converters.defaultEncoding;
if (Converters.defaultEncoding != null) {
b = true;
}
else {
defaultEncoding = AccessController.doPrivileged((PrivilegedAction<String>)new GetPropertyAction("file.encoding"));
if (defaultEncoding != null) {
Converters.defaultEncoding = defaultEncoding;
b = true;
}
else {
defaultEncoding = "ISO8859_1";
}
}
Class clazz2;
try {
clazz2 = getConverterClass(n, defaultEncoding);
if (b) {
Converters.classCache[n] = new SoftReference(clazz2);
}
}
catch (UnsupportedEncodingException ex) {
try {
clazz2 = getConverterClass(n, "ISO8859_1");
}
catch (UnsupportedEncodingException ex2) {
throw new InternalError("Cannot find default " + Converters.converterPrefix[n] + " converter class");
}
}
return clazz2;
}
static Object newDefaultConverter(final int n) {
final Class defaultConverterClass;
synchronized (Converters.lock) {
defaultConverterClass = getDefaultConverterClass(n);
}
try {
return newConverter("", defaultConverterClass);
}
catch (UnsupportedEncodingException ex) {
throw new InternalError("Cannot instantiate default converter class " + defaultConverterClass.getName());
}
}
static /* synthetic */ Class class$(final String s) {
try {
return Class.forName(s);
}
catch (ClassNotFoundException ex) {
throw new NoClassDefFoundError(ex.getMessage());
}
}
static {
Converters.lock = ((Converters.class$sun$io$Converters == null) ? (Converters.class$sun$io$Converters = class$("sun.io.Converters")) : Converters.class$sun$io$Converters);
Converters.converterPackageName = null;
Converters.defaultEncoding = null;
converterPrefix = new String[] { "ByteToChar", "CharToByte" };
Converters.classCache = new SoftReference[2];
}
}
使用javac Converter.java
进行编译时,出现以下错误:
import sun.security.action.*;
^
(package sun.security.action is declared in module java.base, which does not export it to the unnamed module)
Converters.java:47: error: cannot find symbol
aliasName = CharacterEncoding.aliasName(s);
^
symbol: variable CharacterEncoding
location: class Converters
Converters.java:85: error: incompatible types: Object cannot be converted to Class
final Class clazz = softReference.get();
^
Note: Converters.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Converters.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors
我尝试使用--add--export
java.base/sun.security=All-UNNAMED
导出此文件,但仍会引发错误。
有帮助吗?还想知道是否还有其他方法可以用新代码删除此旧遗留代码。
答案 0 :(得分:1)
如前所述,这些是内部类,不应直接使用。如果重构超出范围,则应使用--add-export
,这应该可以工作!
您的--add-export
参数是错误的。您需要使用正确的软件包导出(在您的情况下为sun.security.action
)和模块的名称(如果不使用模块,则为ALL-UNNAMED,全部用大写字母!)
所以应该是--add-exports java.base/sun.security.action=ALL-UNNAMED
来自javac的文档:
-add-exports module / package = other-module(,other-module)*指定要被视为从其定义模块导出到的包 附加模块或所有未命名模块的值 其他模块是ALL-UNNAMED。
答案 1 :(得分:0)
自Java 9开始,大多数JDK的内部API are inaccessible at compile time(并且在将来,它们在运行时也将无法访问)。 SITE Counter KEY Full_name G H
LO ALA LO_ALA ALPHA 1000 B
LO BLA LO_BLA BALPA 2000 B
是一个内部软件包,因此应避免使用它。就您而言,您只需删除sun.security.action
并将其替换为lambda:
new GetPropertyAction("file.encoding.pkg")