将java类转换为groovy:Groovy:数组构造函数调用没有表达式

u12*_*123 2 java syntax groovy

出于测试目的,我想尝试使用下面的 java 类。但我需要将它移植到groovy。

java类:

public class HttpsTrustManager implements X509TrustManager {
    private static TrustManager[] trustManagers;
    private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[]{};

    @Override
    public void checkClientTrusted(
            X509Certificate[] x509Certificates, String s)
            throws java.security.cert.CertificateException {

    }

    @Override
    public void checkServerTrusted(
            X509Certificate[] x509Certificates, String s)
            throws java.security.cert.CertificateException {

    }

    public boolean isClientTrusted(X509Certificate[] chain) {
        return true;
    }

    public boolean isServerTrusted(X509Certificate[] chain) {
        return true;
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return _AcceptedIssuers;
    }

    public static void allowAllSSL() {
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }

        });

        SSLContext context = null;
        if (trustManagers == null) {
            trustManagers = new TrustManager[]{new HttpsTrustManager()};
        }

        try {
            context = SSLContext.getInstance("TLS");
            context.init(null, trustManagers, new SecureRandom());
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            e.printStackTrace();
        }

        HttpsURLConnection.setDefaultSSLSocketFactory(context != null ? context.getSocketFactory() : null);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我将其移动到 .groovy 文件时,我在这一行收到以下错误:

private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[]{};
Run Code Online (Sandbox Code Playgroud)

错误:

Groovy:No expression for the array constructor call at line:
Run Code Online (Sandbox Code Playgroud)

还有这一行:

if (trustManagers == null) {
    trustManagers = new TrustManager[]{new HttpsTrustManager()}; 
}
Run Code Online (Sandbox Code Playgroud)

错误:

Groovy:No expression for the array constructor call at line:
Run Code Online (Sandbox Code Playgroud)

我这样移植的第一行:

  private static final X509Certificate[] _AcceptedIssuers = [ new X509Certificate(){} ] as X509Certificate[]
Run Code Online (Sandbox Code Playgroud)

但不能 100% 确定其正确性。但我在第二行仍然有错误:

    trustManagers = new TrustManager[]{new HttpsTrustManager()};
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

小智 7

我们不能在 Groovy 中使用 Java 数组初始化语法。

所以

private static final X509Certificate[] _AcceptedIssuers = [ new X509Certificate(){} ] as X509Certificate[]
Run Code Online (Sandbox Code Playgroud)

是正确的,你还需要改变

trustManagers = new TrustManager[]{new HttpsTrustManager()};
Run Code Online (Sandbox Code Playgroud)

到

trustManagers = [new HttpsTrustManager()] as TrustManager[];
Run Code Online (Sandbox Code Playgroud)