Coder Social home page Coder Social logo

Comments (4)

amaembo avatar amaembo commented on May 18, 2024

Surely FindBugs does not track whether method is pure (does not depend on external state and does not change it) or not. How we can know where two subsequent calls to getFileName() always return the same thing? Note that Path is actually an interface, so it can be implemented in various ways including the possible way when the results are always different. This is quite sofisticated analysis. We could implement it for simple getters, but it's complex and still would be fragile. Even JDK implementation of Path.getFileName() is not a simple getter: every time you call it it extracts the part of internally stored path to the newly-created String (using lastIndexOf, substring, etc.), then creates a new Path object, so if (p.getFileName() == null) actually creates at least 3 intermediate useless objects. The proper way to make this check is to use the local variable:

Path fileName = p.getFileName();
if(fileName == null) return;
String s = fileName.toString().

On the other hand probably we should not complain about getFileName() at all. It was added by @iloveeclipse in this commit. The method indeed may return null, but in many cases the programmer can be sure than null cannot be returned. It's somewhat similar to Map.get: while it may return null in many cases it's known in advance that the returned value cannot be null. Andrey, what do you think? Should we warn about unchecked use of Path.getFileName()? Do you have any positive experience with this bug report?

from findbugs.

iloveeclipse avatar iloveeclipse commented on May 18, 2024

Tagir, if you think it is more abusing, we should remove this method. Since this is a new API, I guess this is not used often yet. This was a personal hate to the File API in Java which is often inconsistent.

from findbugs.

djeikyb avatar djeikyb commented on May 18, 2024

Oh, I see. I simplified my original encounter down to the simple null check, but my original case was more like:

if (path.getFileCount() > 0) {
    String fn = path.getFileName();
}

which satisfies the conditions in the javadoc of Path::getFileName. I'm not sure it's reasonable to expect findbugs to know that skipping the null check of file name is okay when the file count is greater than 0.

from findbugs.

iloveeclipse avatar iloveeclipse commented on May 18, 2024

This project continues development in a new home: https://github.com/spotbugs/spotbugs/

Please do not open new issues here anymore!

from findbugs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.