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

Use UncheckedIOException rather than IllegalStateException for consistency #142

Closed
agentgt opened this issue May 3, 2024 · 4 comments · Fixed by #145
Closed

Use UncheckedIOException rather than IllegalStateException for consistency #142

agentgt opened this issue May 3, 2024 · 4 comments · Fixed by #145
Assignees
Milestone

Comments

@agentgt
Copy link

agentgt commented May 3, 2024

In io.avaje.config.Config we have

public class Config {

  private static final Configuration data = CoreConfiguration.initialise();
...
}

Any static method called on Config will cause initialization of data. Luckily and I assume this is why @rbygrave wrote it that way is that every static method in Config happens to access data.

However another developer could easily make the mistake of adding a static method that does not need data. Perhaps a utility method used by the builder or whatever like concatPath or somethign.

Thus I recommend the canonical holder pattern of a private static inner class:

public class Config {

  private static class Holder {
    static final Configuration data = CoreConfiguration.initialise();
  }
...
}

All the static config methods would now do Holder.data for data.

@SentryMan
Copy link
Collaborator

I've no objections to this

@agentgt
Copy link
Author

agentgt commented May 3, 2024

Also this:

Should throw either UncheckedIOException or ignored (as in the file no longer exists).

EDIT that whole code block looks like duplication of stuff that Configuration.builder should do or know about:

  InputStream resource(String resourcePath, InitialLoader.Source source) {
    InputStream is = null;
    if (source == InitialLoader.Source.RESOURCE) {
      is = resourceStream(resourcePath);
      if (is != null) {
        loadedResources.add("resource:" + resourcePath);
      }
    } else {
      File file = new File(resourcePath);
      if (file.exists()) {
        try {
          is = new FileInputStream(file);
          loadedResources.add("file:" + resourcePath);
          loadedFiles.add(file);
        } catch (FileNotFoundException e) {
          throw new IllegalStateException(e);
        }
      }
    }
    return is;
  }

EDIT whoops I put this in the wrong place

@rbygrave
Copy link
Contributor

rbygrave commented May 6, 2024

Holder ... However another developer could easily make the mistake of adding a static method that does not need data.

I'd say that is extremely unlikely. The design is such that Config has only the one job and I don't see that design changing. We get no real benefit from adding a Holder class per se. I don't think we need to make such a change.

Should throw either UncheckedIOException or ignored (as in the file no longer exists).

Well I don't think it should be ignored, I don't see a strong argument that ignoring that FileNotFoundException is the right thing to do.

It is certainly an unexpected state that a resource file exists and then doesn't when it is read. Throwing UncheckedIOException seems ok but IllegalStateException also seems ok given it has detected an unexpected and illegal state. I'll have a closer look there.

rbygrave added a commit that referenced this issue May 6, 2024
This improves consistency with other use of UncheckedIOException
@rbygrave
Copy link
Contributor

rbygrave commented May 6, 2024

So yes lets change to use UncheckedIOException for better consistency.

I'll rename this issue to reflect this change. I don't see the desire for the Holder change but it you want to continue to push for that add some more comments here and lets see.

@rbygrave rbygrave changed the title Config should have static inner class for data aka private holder. Use UncheckedIOException rather than IllegalStateException for consistency May 6, 2024
@rbygrave rbygrave self-assigned this May 6, 2024
rbygrave added a commit that referenced this issue May 6, 2024
This improves consistency with other use of UncheckedIOException
rbygrave added a commit that referenced this issue May 6, 2024
… fallback (#146)

* #142 Use UncheckedIOException rather than IllegalStateException

This improves consistency with other use of UncheckedIOException

* #141 DefaultResourceLoader uses ClassLoader.getSystemResourceAsStream() as fallback

This should be no change when using "normal" Config/Configuration but allows users
of the ConfigurationBuilder to have this fallback to ClassLoader.getSystemResourceAsStream()
when the resource is not found via the usual getClass().getResourceAsStream()

* #141 Use inputStream as variable name
@rob-bygrave rob-bygrave added this to the 3.15 milestone May 8, 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 a pull request may close this issue.

4 participants