You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
packagenetworking;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.Socket;
importjava.util.Calendar;
importjava.util.Date;
importjava.util.TimeZone;
@SuppressWarnings("PMD.ShortClassName")
publicfinalclassTime {
privatestaticfinalStringHOSTNAME = "time.nist.gov";
privateTime() {
thrownewIllegalStateException("Private constructor");
}
@SuppressWarnings("fallthrough")
publicstaticvoidmain(String[] args) {
try {
Dated;
switch (args.length) {
case0:
d = Time.getDateFromNetwork();
System.out.println("It is " + d);
System.exit(0);
case1:
d = Time.getDateFromNetwork(args[0], 37);
System.out.println("It is " + d);
System.exit(0);
case2:
default:
d = Time.getDateFromNetwork(args[0], Integer.parseInt(args[1]));
System.out.println("It is " + d);
System.exit(0);
}
} catch (IOExceptione) {
System.err.println(e.getMessage());
}
}
publicstaticDategetDateFromNetwork() throwsIOException {
returngetDateFromNetwork(HOSTNAME, 37);
}
publicstaticDategetDateFromNetwork(Stringhost, intport)
throwsIOException {
// The time protocol sets the epoch at 1900,// the Java Date class at 1970. This number// converts between them.// long differenceBetweenEpochs = 2208988800L;TimeZonegmt = TimeZone.getTimeZone("GMT");
Calendarepoch1900 = Calendar.getInstance(gmt);
epoch1900.set(1900, 01, 01, 00, 00, 00);
longepoch1900ms = epoch1900.getTime().getTime();
Calendarepoch1970 = Calendar.getInstance(gmt);
epoch1970.set(1970, 01, 01, 00, 00, 00);
longepoch1970ms = epoch1970.getTime().getTime();
longdifferenceInMS = epoch1970ms - epoch1900ms;
longdifferenceBetweenEpochs = differenceInMS / 1000;
Socketsocket = newSocket(host, port);
socket.setSoTimeout(15_000);
InputStreamraw = socket.getInputStream();
longsecondsSince1900 = 0;
for (inti = 0; i < 4; i++) {
secondsSince1900 = (secondsSince1900 << 8) | raw.read();
}
longsecondsSince1970 = secondsSince1900 - differenceBetweenEpochs;
longmsSince1970 = secondsSince1970 * 1000;
returnnewDate(msSince1970);
}
}
The empty case statement breaks the above rule giving a false positive. Obviously, there's not a fix in for this as yet. Do you have a workaround or do we suppress the rule until a fix is in?
Why can't PMD recognise the SuppressWarnings for fall through or comments beside the case statements stating the same?
Thank you very much.
You might also wish to check out this example source:
```
package pmdtest;
import java.util.concurrent.ThreadLocalRandom;
public final class ThrowSwitch {
private ThrowSwitch() {
throw new IllegalStateException("Private constructor");
}
@SuppressWarnings("fallthrough")
public static void main(String... args) {
int errCode = getSimulatedErrorCode();
switch (errCode) {
case 1:
case 2:
case 3:
case 4:
case 5:
throw new AssertionError("Error code: " + errCode);
default:
System.exit(0);
}
}
public static int getSimulatedErrorCode() {
return ThreadLocalRandom.current().nextInt(0, 6);
}
}
```
It triggers the same rule about no break statement found.
adangel
changed the title
[java] Recognize @SuppressWanings("fallthrough") for MissingBreakInSwitch
[java] Recognize @SuppressWarnings("fallthrough") for MissingBreakInSwitch
Nov 21, 2022
The empty case statement breaks the above rule giving a false positive. Obviously, there's not a fix in for this as yet. Do you have a workaround or do we suppress the rule until a fix is in?
Why can't PMD recognise the SuppressWarnings for fall through or comments beside the case statements stating the same?
Originally posted by @Fernal73 in #659 (comment)
The text was updated successfully, but these errors were encountered: