gpt4 book ai didi

java - 从遗留代码到 Java 11 的迁移问题

转载 作者:行者123 更新时间:2023-12-01 19:43:52 26 4
gpt4 key购买 nike

我们正在迁移旧代码,实际上相当旧的代码到 Java 11。我在编译其中一个类时遇到问题。示例代码为:

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
,它仍然抛出错误。

有什么帮助吗?我还想知道是否有其他方法可以用新代码删除旧的遗留代码。

最佳答案

自 Java 9 起,大部分 JDK 的内部 API are inaccessible at compile time (将来,它们在运行时也将无法访问)。 sun.security.action 是一个内部包,因此您应该避免使用它。对于您的情况,您只需删除 new GetPropertyAction("file.encoding.pkg") 并将其替换为 lambda:

AccessController.doPrivileged((PrivilegedAction<Object>) () -> System.getProperty("prop"));

关于java - 从遗留代码到 Java 11 的迁移问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54306697/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com