简体   繁体   English

不应在数组实例(SonarLint)上调用“ hashCode”和“ toString”

[英]“hashCode” and “toString” should not be called on array instances (SonarLint)

I am passing my code through SonarLint and I came across this linter violation: "hashCode" and "toString" should not be called on array instances . 我正在通过SonarLint传递我的代码,并且遇到了这种短绒冲突: "hashCode" and "toString" should not be called on array instances

This is my code: 这是我的代码:

byte[] lblobPic;
lblobPic = r.get(PEOPLE.PPIC);
if (lblobPic != null) {
    String argStr = lblobPic.toString();
    peopleDto.setUrlPic(argStr);
}

SonarLint gives the following code snippet as a hint to improve my code: SonarLint提供以下代码段作为改进我的代码的提示:

public static void main( String[] args )  {
    String argStr = Arrays.toString(args);
    int argHash = Arrays.hashCode(args);    
}

How should I change my code to satisfy the linter and why? 我应该如何更改代码以满足要求,为什么?

Actual Answer 实际答案

SonarLint is suggesting to you, that instead of calling toString() on the array instance, you should rather use the Arrays utility's method. SonarLint建议您,而不是在数组实例上调用toString() ,而应使用Arrays实用程序的方法。

It suggests you to change your code to something along the lines: 它建议您将代码更改为以下内容:

byte[] lblobPic;
lblobPic = r.get(ALUNO.PFOTO);
if (lblobPic != null) {
    String argStr = Arrays.toString(lblobPic);
    peopleDto.setUrlPic(argStr);
}

Reason behind Answer 答案背后的原因

a) Readability for Humans a)对人类的可读性

Consider the following code snippet: 考虑以下代码片段:

String[] strings = { "foo", "bar", "bla", "boo" };

System.out.println(strings.toString());
// prints: [Ljava.lang.String;@7852e922 

System.out.println(Arrays.toString(strings));
// prints: [foo, bar, bla, boo]

The Linter rule assumes, that a developer actually wants a readable output of an array (considering its elements) and suggests you to use Arrays.toString() method which does this (as outlined in the documentation ). Linter规则假设开发人员实际上想要数组的可读输出(考虑其元素),并建议您使用Arrays.toString()方法执行此操作(如文档所述 )。

Similarly, Arrays.hashCode() considers the elements of the given array in hashing (as outlined in the docoumentation ). 类似地, Arrays.hashCode()在哈希中考虑给定数组的元素(如docoumentation所述 )。

b) Determinism b)确定性
(per @andi-turner's suggestion) (根据@ andi-turner的建议)

The Arrays utility's methods, take only the elements into account when constructing a string / calculating a hash. Arrays实用程序的方法在构造字符串/计算哈希时仅考虑元素。 You will always end up with the same string/hash, when using an input array consisting of the same strings (or values of another type) in the same sequence. 当使用由相同序列(或其他类型的值)组成的输入数组时,您将总是以相同的字符串/哈希结束。 yourArray.toHashcode() or yourArray.toString() does not give you that. yourArray.toHashcode()yourArray.toString()不会为您提供。

String argStr = lblobPic.toString();

should better be 最好是

String argStr = Arrays.toString(lblobPic);

as the original Object.toString would give a cryptic hex address. 因为原始的Object.toString将给出一个神秘的十六进制地址。

However what you want to achieve, storing bytes as String is a no-go in java, as java uses Unicode for String and char (two bytes, UTF-16), always with a conversion (of an assumed text encoding of those bytes). 但是,要实现的目标是将字节存储为String在Java中是不行的,因为Java将Unicode用于String和char(两个字节,UTF-16),并且始终进行转换(假定这些字节的文本编码) 。

Sometimes such bytes are Base64 encoded: 有时,此类字节是Base64编码的:

byte[] lblobPic = r.get(ALUNO.PFOTO);
if (lblobPic != null) {
    String argStr = Base64.getUrlEncoder().encode(lblobPic);
    peopleDto.setUrlPic(argStr);
}

Better still to provide a byte[] field in the DTO. 最好在DTO中提供byte[]字段。


Should the further processing be a problem; 如果进一步处理有问题; there exist embedding of images. 存在图像嵌入。

How (normal) Base64 can be used for HTML with an embedded image: 如何(正常)将Base64用于带有嵌入式图像的HTML:

    String argStr = Base64.getEncoder().encode(lblobPic);
    String html = "<img src="data:image/jpeg;base64," + argStr + "\" alt=\"\">";

(JPEG assumed here.) (此处假定为JPEG。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM