Checker Resources   as of Julia version 2.6.0 (built on 6 Sep 2018)

belongs to group Basic

Identify incorrect uses of the resources of the application


This checker identifies problems with the use of the external resources of the application. These are resource files, scripts or XML configuration files.

Action: Verify if the problem is real and the resource is actually used in an incorrect way. Avoid hardcoded file names: move them to a configuration file. Avoid OS-dependent resources and resource names.

Examples


Consider the following program:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class Resources {

  public static void main(String[] args) throws FileNotFoundException {
    new File("hello.txt");
    new FileInputStream("C:\\myfile.txt");
    new FileOutputStream("/home/spoto/myfile.bmp");
    if (args.length > 0)
      new File(args[0]);
  }
}

This checker issues the following warnings:

Resources.java:9: [Resources: HardcodedFileNameWarning] a hardcoded file name is used here. Move it to a configuration file
Resources.java:10: [Resources: HardcodedFileNameWarning] a OS-dependent hardcoded file name is used here. Move it to a configuration file and make it OS-independent
Resources.java:11: [Resources: HardcodedFileNameWarning] a OS-dependent hardcoded file name is used here. Move it to a configuration file and make it OS-independent

since hardcoded file names are used for files or streams. Moreover, two of those file names are also OS-dependent, which means that the program will not work properly across distinct operating systems.

In this example, the programmer should move the file names into configuration files, so that they can be modified and swapped for distinct operating systems. He might also think about using File.separatorChar to split path components in a OS-independent way.