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

[java] Recognize @SuppressWarnings("fallthrough") for MissingBreakInSwitch #1899

Closed
jsotuyod opened this issue Jul 2, 2019 · 2 comments
Closed
Labels
an:enhancement An improvement on existing features / rules
Projects
Milestone

Comments

@jsotuyod
Copy link
Member

jsotuyod commented Jul 2, 2019

package networking;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

@SuppressWarnings("PMD.ShortClassName")
public final class Time {
  private static final String HOSTNAME = "time.nist.gov";

  private Time() {
    throw new IllegalStateException("Private constructor");
  }

  @SuppressWarnings("fallthrough")
  public static void main(String[] args) {
    try {
      Date d;
      switch (args.length) {
        case 0:
          d = Time.getDateFromNetwork();
          System.out.println("It is " + d);
          System.exit(0);

        case 1:
          d = Time.getDateFromNetwork(args[0], 37);
          System.out.println("It is " + d);
          System.exit(0);

        case 2:

        default:
          d = Time.getDateFromNetwork(args[0], Integer.parseInt(args[1]));
          System.out.println("It is " + d);
          System.exit(0);
      }
    } catch (IOException e) {
      System.err.println(e.getMessage());
    }
  }

  public static Date getDateFromNetwork() throws IOException {
    return getDateFromNetwork(HOSTNAME, 37);
  }

  public static Date getDateFromNetwork(String host, int port)
      throws IOException {
    // The time protocol sets the epoch at 1900,
    // the Java Date class at 1970. This number
    // converts between them.
    // long differenceBetweenEpochs = 2208988800L;
    TimeZone gmt = TimeZone.getTimeZone("GMT");
    Calendar epoch1900 = Calendar.getInstance(gmt);
    epoch1900.set(1900, 01, 01, 00, 00, 00);
    long epoch1900ms = epoch1900.getTime().getTime();
    Calendar epoch1970 = Calendar.getInstance(gmt);
    epoch1970.set(1970, 01, 01, 00, 00, 00);
    long epoch1970ms = epoch1970.getTime().getTime();
    long differenceInMS = epoch1970ms - epoch1900ms;
    long differenceBetweenEpochs = differenceInMS / 1000;
    Socket socket = new Socket(host, port);
    socket.setSoTimeout(15_000);
    InputStream raw = socket.getInputStream();

    long secondsSince1900 = 0;
    for (int i = 0; i < 4; i++) {
      secondsSince1900 = (secondsSince1900 << 8) | raw.read();
    }
    long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs;
    long msSince1970 = secondsSince1970 * 1000;
    return new Date(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?

Originally posted by @Fernal73 in #659 (comment)

@jsotuyod jsotuyod added the an:enhancement An improvement on existing features / rules label Jul 2, 2019
@linusjf
Copy link

linusjf commented Jul 2, 2019 via email

@oowekyala oowekyala added this to the 7.0.0 milestone Nov 11, 2020
@oowekyala oowekyala added this to Done in PMD 7 Apr 3, 2021
@adangel adangel changed the title [java] Recognize @SuppressWanings("fallthrough") for MissingBreakInSwitch [java] Recognize @SuppressWarnings("fallthrough") for MissingBreakInSwitch Nov 21, 2022
@adangel adangel mentioned this issue Jan 23, 2023
55 tasks
@adangel
Copy link
Member

adangel commented Apr 22, 2023

This has been fixed with PMD 7.0.0-rc1.

@adangel adangel closed this as completed Apr 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
an:enhancement An improvement on existing features / rules
Projects
No open projects
PMD 7
  
Done
Development

No branches or pull requests

4 participants