Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc IntelliJ warnings and fixes #884

Closed
wants to merge 20 commits into from

Conversation

Juiceman
Copy link
Contributor

Assortment of fixes suggested by IntelliJ when analyzing the source code.

Code compiles and my node seems to be working.

Integer.valueOf(value.substring(3,4),16).intValue();
Integer.valueOf(value.substring(1, 2), 16);
Integer.valueOf(value.substring(2, 3), 16);
Integer.valueOf(value.substring(3, 4), 16);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here the indentation seems to have broken. Please keep it consistent with the surrounding indentation, even if that may seem strange. Consistency wins against beauty here, because we cannot just re-indent all files without breaking all pull-requests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed in [b32d928]

Thank you

Integer.valueOf(value.substring(5,7),16).intValue();
Integer.valueOf(value.substring(1, 3), 16);
Integer.valueOf(value.substring(3, 5), 16);
Integer.valueOf(value.substring(5, 7), 16);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed in [b32d928]

Thank you

buffer.add(Byte.valueOf((byte) ((frameHeader & 0xFF00) >>> 8)));
buffer.add(Byte.valueOf((byte) (frameHeader & 0x00FF)));
buffer.add((byte) ((frameHeader & 0xFF00) >>> 8));
buffer.add((byte) (frameHeader & 0x00FF));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it guaranteed that casting to byte will work in all cases here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The casting already happens inside the call to valueOf, removing the manual boxing here doesn’t change anything about that.

@@ -90,10 +90,10 @@ public void readFilter(
packet = new FlacFrame(payload);
} else {
firstHalfOfSyncHeaderFound = false;
buffer.add(Byte.valueOf((byte) 0xFF));
buffer.add((byte) 0xFF);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the cast safe?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing, the casting isn’t changed, only the boxing is removed.

}
}
buffer.add(Byte.valueOf((byte) (data & 0xFF)));
buffer.add((byte) (data & 0xFF));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the cast safe?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also safe. 🙂

}
if(concludingPartialSegment != 0) {
segmentSizes.add(Byte.valueOf(intToUnsignedByte(concludingPartialSegment)));
segmentSizes.add(intToUnsignedByte(concludingPartialSegment));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these two are also implicit casts. Are they safe?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s no implicit casting happening here, intToUnsignedByte already returns a byte, that would be an identity conversion, followed by a boxing conversion (JLS § 5.1.1, § 5.1.7).

Logger.error(this, "This should really not happen!", new Exception("FIXME"));
handler.send(new ProtocolErrorMessage(ProtocolErrorMessage.INTERNAL_ERROR, false, "This should really not happen! See logs for details.", identifier, false));
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks much nicer! (but needs consistent indentation)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally hate switch statements and would much rather see this replaced with a different construct that actually hides all this ugliness (e.g. by a map or a strategy pattern or something) but that’s very much an issue for another day, this is fine. 🙂

@@ -499,7 +499,7 @@ public void handleMethodGET(URI uri, final HTTPRequest request, ToadletContext c
//i now points to the proper location (equal, insertion point, or end-of-list)
//maybe better called "reverseGroup"?
List<PeerNodeStatus> peerGroup;
if (i<max && locations.get(i).doubleValue()==location) {
if (i<max && locations.get(i) ==location) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is == safe without the doubleValue()? This may cause type coercion to float.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, doubles are not coerced to floats; in an equality comparison binary numeric promotion is performed, and as one of the operands is a double, the other one is promoted to double as well; as they are both already doubles, nothing bad at all will happen here. (JLS § 15.21.1, § 5.6.2.)

return ((DarknetPeerNodeStatus) firstNode).getTheirVisibility().compareTo(((DarknetPeerNodeStatus) secondNode).getTheirVisibility());
default:
return super.customCompare(firstNode, secondNode, sortBy);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mind the indentation.

@@ -1103,8 +1103,7 @@ private static String getForceValue(FreenetURI key, long time) {
throw new Error(e);
}

String f = HexUtil.bytesToHex(SHA256.digest(bos.toByteArray()));
return f;
return HexUtil.bytesToHex(SHA256.digest(bos.toByteArray()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

default:
isSet = false;
break;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation again (IntelliJ can have problems there)

@@ -322,7 +322,7 @@
window.__gwt_module_id = 0;
</script></head>
<body>
<font face='arial' size='-1'>This html file is for Development Mode support.</font>
<span style="font-family: arial; font-size: smaller; ">This html file is for Development Mode support.</span>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yikes, we still had those old styles in there! Good catch!

} catch (IllegalArgumentException e) {
//Render the default page if unable to parse password prompt type.
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

case "delete":
SecurityLevelsToadlet.sendCantDeleteMasterKeysFileInner(helper, core.node.getMasterPasswordFile().getPath(), newThreatLevel.name());
return true;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs consistent indentation again

@@ -87,8 +87,7 @@ public Inet4AddressMatcher(String cidrHostname) {
*/
public static int convertToBytes(String address) {
StringTokenizer addressTokens = new StringTokenizer(address, ".");
int bytes = Integer.parseInt(addressTokens.nextToken()) << 24 | Integer.parseInt(addressTokens.nextToken()) << 16 | Integer.parseInt(addressTokens.nextToken()) << 8 | Integer.parseInt(addressTokens.nextToken());
return bytes;
return Integer.parseInt(addressTokens.nextToken()) << 24 | Integer.parseInt(addressTokens.nextToken()) << 16 | Integer.parseInt(addressTokens.nextToken()) << 8 | Integer.parseInt(addressTokens.nextToken());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

@@ -1439,8 +1439,7 @@ public static Message createFNPVisibility(short visibility) {
}};

public static Message createFNPVoid() {
Message msg = new Message(FNPVoid);
return msg;
return new Message(FNPVoid);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

throw new MalformedURLException("Cannot write USKs as binary keys");
default:
throw new MalformedURLException("Cannot write key of type " + keyType + " - do not know how");
}
if(!keyType.equals("KSK")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is nice! (but indentation)

return Scope.Special;
default:
throw new IllegalArgumentException("Unknown scope abbreviation: " + abbreviation);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

return Type.Special;
default:
throw new IllegalArgumentException("Unknwon type abbreviation: " + abbreviation);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

@@ -302,8 +302,7 @@ protected void stop() {

public int getAnnouncementThreshold() {
// First, do we actually need to announce?
int target = Math.min(MIN_OPENNET_CONNECTED_PEERS, om.getNumberOfConnectedPeersToAimIncludingDarknet() / 2);
return target;
return Math.min(MIN_OPENNET_CONNECTED_PEERS, om.getNumberOfConnectedPeersToAimIncludingDarknet() / 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

1000L * 1000L * 1000L, 1L << 30,
1000L * 1000L * 1000L * 1000L, 1L << 40,
1000L * 1000L * 1000L * 1000L * 1000, 1L << 50,
1000L * 1000L * 1000L * 1000L * 1000L * 1000L, 1L << 60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unable to find an issue with the indentation of the source file.

default:
throw new NumberFormatException(
"unknown time/date delta type: " + deltaTypeString);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the string switch already available in Java 8?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, with constant expressions for the case statements. Had to check myself because I also remember something about it being loudly introduced at a much later version but that’s probably for non-constant expressions in the case statements. 🙂

@@ -219,7 +219,7 @@ public InsertExceptionMode getFirstCodeInsert() {
}

public synchronized int getFirstCode() {
return ((Integer) map.keySet().toArray()[0]).intValue();
return (Integer) map.keySet().toArray()[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would replace this with

return map.keySet().iterator().next();

and avoid the casting altogether.

buffer.add(Byte.valueOf((byte) ((frameHeader & 0xFF00) >>> 8)));
buffer.add(Byte.valueOf((byte) (frameHeader & 0x00FF)));
buffer.add((byte) ((frameHeader & 0xFF00) >>> 8));
buffer.add((byte) (frameHeader & 0x00FF));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The casting already happens inside the call to valueOf, removing the manual boxing here doesn’t change anything about that.

@@ -90,10 +90,10 @@ public void readFilter(
packet = new FlacFrame(payload);
} else {
firstHalfOfSyncHeaderFound = false;
buffer.add(Byte.valueOf((byte) 0xFF));
buffer.add((byte) 0xFF);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing, the casting isn’t changed, only the boxing is removed.

}
}
buffer.add(Byte.valueOf((byte) (data & 0xFF)));
buffer.add((byte) (data & 0xFF));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also safe. 🙂

}
if(concludingPartialSegment != 0) {
segmentSizes.add(Byte.valueOf(intToUnsignedByte(concludingPartialSegment)));
segmentSizes.add(intToUnsignedByte(concludingPartialSegment));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s no implicit casting happening here, intToUnsignedByte already returns a byte, that would be an identity conversion, followed by a boxing conversion (JLS § 5.1.1, § 5.1.7).

default:
throw new NumberFormatException(
"unknown time/date delta type: " + deltaTypeString);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, with constant expressions for the case statements. Had to check myself because I also remember something about it being loudly introduced at a much later version but that’s probably for non-constant expressions in the case statements. 🙂

spaces back into tabs to match existing style
@Juiceman
Copy link
Contributor Author

Juiceman commented Feb 9, 2024

Closing. Will redo sometime in the future as smaller patches that can be merged or changed without holding up the whole PR.

@Juiceman Juiceman closed this Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants