Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migration Issue to Java 11 from legacy code

We are migrating our old code, in fact pretty old to Java 11. I am facing issues while compiling one of the classes. The sample code is:

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];
    }
}

While compiling it using javac Converter.java, I am getting the error below:

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

I tried to export this using --add--export java.base/sun.security=All-UNNAMED, it's still throwing the error.

Any help? Also would like to know is there any other way to remove this old legacy code with new one.

like image 744
vibhas Avatar asked Apr 12 '26 14:04

vibhas


2 Answers

Since Java 9, most of the JDK's internal APIs are inaccessible at compile time (and in the future, they will be inaccessible at run time too). sun.security.action is an internal package so you should avoid using it. In your case, you can simply remove new GetPropertyAction("file.encoding.pkg") and replace it with a lambda:

AccessController.doPrivileged((PrivilegedAction<Object>) () -> System.getProperty("prop"));
like image 102
ZhekaKozlov Avatar answered Apr 15 '26 03:04

ZhekaKozlov


As pointed out, these are internal classes that should not be used directly. If refactoring is out of scope, you should use --add-export and this should work!

Your --add-export argument is wrong. You need to use the correct package to export (sun.security.action in your case) and the name of your module (ALL-UNNAMED if you don't use modules, all in capital letters !).

So this should be --add-exports java.base/sun.security.action=ALL-UNNAMED

From the documentation of javac :

--add-exports module/package=other-module(,other-module)* Specifies a package to be considered as exported from its defining module to additional modules or to all unnamed modules when the value of other-module is ALL-UNNAMED.

like image 29
loicmathieu Avatar answered Apr 15 '26 02:04

loicmathieu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!