Skip to content

Use Policeman Tools - #1213

Closed
boldtrn wants to merge 5 commits into
graphhopper:masterfrom
boldtrn:try_policemanttools
Closed

boldtrn wants to merge 5 commits into
graphhopper:masterfrom
boldtrn:try_policemanttools

Conversation

@boldtrn

@boldtrn boldtrn commented Nov 7, 2017

Copy link
Copy Markdown
Member

As mentioned in #1210, it would be nice to use the policeman tools to avoid locale issues.

I just pasted the default configuration of the policeman tools. I changed it so it does only run on travis or by using mvn forbiddenapis:check. It finds a lot of issues for the core so far.

For example:

[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.routing.util.EncodingManager (EncodingManager.java:111)

if (!encoderList.equals(encoderList.toLowerCase()))

The fix that was proposed here, is to rewrite the above line to:

if (!encoderList.equals(encoderList.toLowerCase(Locale.ROOT)))

As mentioned before, you can pass a locale to String.toLowerCase(), so the naive approach would be to tell Java, that we are in the US or using the English language: String.toLowerCase(Locale.US) or String.toLowerCase(Locale.ENGLISH). This produces identical results but is still not consistent. What happens if the US government decides to lowercase/uppercase like in Turkey? -- OK, don’t use Locale.US (this is also too US-centric). Locale.ENGLISH is fine and very generic, but languages also change over the years (who knows?), but we want to have it language invariant! If you are using Java 6, there is a much better constant: Locale.ROOT -- You should use this constant for our lowercase example: String.toLowerCase(Locale.ROOT).

Should I go ahead and try to fix all the found issues by the policeman tools?

The full list of issues for the core is:

[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.routing.util.EncodingManager (EncodingManager.java:119)
[ERROR] Forbidden method invocation: java.lang.String#format(java.lang.String,java.lang.Object[]) [Uses default locale]
[ERROR]   in com.graphhopper.routing.util.EncodingManager (EncodingManager.java:190)
[ERROR] Forbidden method invocation: java.lang.String#format(java.lang.String,java.lang.Object[]) [Uses default locale]
[ERROR]   in com.graphhopper.routing.util.EncodingManager (EncodingManager.java:196)
[ERROR] Forbidden method invocation: java.lang.String#format(java.lang.String,java.lang.Object[]) [Uses default locale]
[ERROR]   in com.graphhopper.routing.util.EncodingManager (EncodingManager.java:202)
[ERROR] Forbidden method invocation: java.lang.String#format(java.lang.String,java.lang.Object[]) [Uses default locale]
[ERROR]   in com.graphhopper.routing.util.EncodingManager (EncodingManager.java:209)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.util.Constants (Constants.java:117)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.GraphHopper (GraphHopper.java:556)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.GraphHopper (GraphHopper.java:898)
[ERROR] Forbidden method invocation: java.text.DecimalFormat#<init>(java.lang.String) [Uses default locale]
[ERROR]   in com.graphhopper.util.MiniPerfTest (MiniPerfTest.java:95)
[ERROR] Forbidden method invocation: java.lang.String#toUpperCase() [Uses default locale]
[ERROR]   in com.graphhopper.routing.util.TraversalMode (TraversalMode.java:73)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.storage.StorableProperties (StorableProperties.java:93)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.storage.StorableProperties (StorableProperties.java:101)
[ERROR] Forbidden method invocation: java.text.DecimalFormat#<init>(java.lang.String) [Uses default locale]
[ERROR]   in com.graphhopper.util.InstructionList (InstructionList.java:181)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.routing.util.HintsMap (HintsMap.java:56)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.routing.util.HintsMap (HintsMap.java:66)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.storage.GHDirectory (GHDirectory.java:75)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.storage.GHDirectory (GHDirectory.java:93)
[ERROR] Forbidden method invocation: java.lang.String#format(java.lang.String,java.lang.Object[]) [Uses default locale]
[ERROR]   in com.graphhopper.util.TranslationMap (TranslationMap.java:147)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.util.TranslationMap$TranslationHashMap (TranslationMap.java:192)
[ERROR] Forbidden method invocation: java.lang.String#format(java.lang.String,java.lang.Object[]) [Uses default locale]
[ERROR]   in com.graphhopper.util.TranslationMap$TranslationHashMap (TranslationMap.java:196)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.util.TranslationMap$TranslationHashMap (TranslationMap.java:200)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.routing.lm.LMAlgoFactoryDecorator (LMAlgoFactoryDecorator.java:151)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.util.CmdArgs (CmdArgs.java:96)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.util.CmdArgs (CmdArgs.java:98)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.routing.weighting.AbstractWeighting (AbstractWeighting.java:94)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.routing.util.spatialrules.SpatialRuleLookupBuilder (SpatialRuleLookupBuilder.java:42)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.routing.ch.CHAlgoFactoryDecorator (CHAlgoFactoryDecorator.java:230)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.routing.util.DataFlagEncoder (DataFlagEncoder.java:470)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.routing.util.DataFlagEncoder (DataFlagEncoder.java:484)
[ERROR] Forbidden method invocation: java.lang.String#toUpperCase() [Uses default locale]
[ERROR]   in com.graphhopper.storage.DAType (DAType.java:75)
[ERROR] Forbidden class/interface use: com.sun.management.OperatingSystemMXBean [non-portable or internal runtime class]
[ERROR]   in com.graphhopper.util.Helper7 (Helper7.java:53)
[ERROR] Forbidden class/interface use: com.sun.management.OperatingSystemMXBean [non-portable or internal runtime class]
[ERROR]   in com.graphhopper.util.Helper7 (Helper7.java:54)
[ERROR] Forbidden method invocation: java.lang.String#toLowerCase() [Uses default locale]
[ERROR]   in com.graphhopper.routing.util.NameSimilarityEdgeFilter (NameSimilarityEdgeFilter.java:70)
[ERROR] Forbidden class/interface use: sun.misc.Unsafe [non-portable or internal runtime class]
[ERROR]   in com.graphhopper.storage.UnsafeDataAccess (UnsafeDataAccess.java, field declaration of 'UNSAFE')
[ERROR] Forbidden class/interface use: sun.misc.Unsafe [non-portable or internal runtime class]
[ERROR]   in com.graphhopper.storage.UnsafeDataAccess (UnsafeDataAccess.java:94)
[ERROR] Forbidden class/interface use: sun.misc.Unsafe [non-portable or internal runtime class]
[ERROR]   in com.graphhopper.storage.UnsafeDataAccess (UnsafeDataAccess.java:101)
[ERROR] Forbidden class/interface use: sun.misc.Unsafe [non-portable or internal runtime class]
[ERROR]   in com.graphhopper.storage.UnsafeDataAccess (UnsafeDataAccess.java:182)
[ERROR] Forbidden class/interface use: sun.misc.Unsafe [non-portable or internal runtime class]
[ERROR]   in com.graphhopper.storage.UnsafeDataAccess (UnsafeDataAccess.java:187)
[ERROR] Forbidden class/interface use: sun.misc.Unsafe [non-portable or internal runtime class]
[ERROR]   in com.graphhopper.storage.UnsafeDataAccess (UnsafeDataAccess.java:192)
[ERROR] Forbidden class/interface use: sun.misc.Unsafe [non-portable or internal runtime class]
[ERROR]   in com.graphhopper.storage.UnsafeDataAccess (UnsafeDataAccess.java:197)
[ERROR] Forbidden class/interface use: sun.misc.Unsafe [non-portable or internal runtime class]
[ERROR]   in com.graphhopper.storage.UnsafeDataAccess (UnsafeDataAccess.java:202)
[ERROR] Forbidden class/interface use: sun.misc.Unsafe [non-portable or internal runtime class]
[ERROR]   in com.graphhopper.storage.UnsafeDataAccess (UnsafeDataAccess.java:208)
[ERROR] Forbidden class/interface use: sun.misc.Unsafe [non-portable or internal runtime class]
[ERROR]   in com.graphhopper.storage.UnsafeDataAccess (UnsafeDataAccess.java:216)
[ERROR] Forbidden class/interface use: sun.misc.Unsafe [non-portable or internal runtime class]
[ERROR]   in com.graphhopper.storage.UnsafeDataAccess (UnsafeDataAccess.java:52)
[ERROR] Scanned 404 class file(s) for forbidden API invocations (in 0.70s), 44 error(s).

@karussell

karussell commented Nov 7, 2017 •

Copy link
Copy Markdown
Member

Cool & thanks!

Should I go ahead and try to fix all the found issues by the policeman tools?

Hmmh, as the example and several other issues are no user input strings, this would introduce a bit ugliness without a benefit, especially for string comparison. Is there a possibility to skip or ignore them?

@boldtrn

boldtrn commented Nov 14, 2017

Copy link
Copy Markdown
Member Author

Hmmh, as the example and several other issues are no user input strings, this would introduce a bit ugliness without a benefit, especially for string comparison. Is there a possibility to skip or ignore them?

This really depends. For example in this blog you can find a couple of samples that show String comparision for e.g., the turkish language.

System.out.println("BIKE".toLowerCase(new Locale("tr", "TR")));

This prints: bıke which is not equal to bike (compare the i).

This means if for example we read "BIKE" from somewhere, does not need to be user input, can be defined somewhere in our code, and would want to match it with .toLowerCase to bike, this will fail. Ok, this example might be far fetched, but I see a benefit of adding a locale.

In the end, we might not introduce this issue, but maybe someone that uses GraphHopper as a library and passes a String to a method.

@karussell

Copy link
Copy Markdown
Member

Thanks - this is interesting and convincing example, still the resulting code would be ugly ;)

Maybe we introduce toLowerCase(String) as static helper method so we can write toLowerCase("BIKE") with a static import, instead of "BIKE".toLowerCase(Locale.US)?

@karussell

Copy link
Copy Markdown
Member

Locale.ROOT obviously ;)

@boldtrn

boldtrn commented Nov 20, 2017

Copy link
Copy Markdown
Member Author

Ok, so I fixed most issues that the Policeman Tools reported, I left a couple of issues unaddressed and added the whole class to the ignore file. WDYT?

Comment thread pom.xml
<excludes>
<!-- Excluded because of "Helper7#getBeanMemInfo" -->
<exclude>
com/graphhopper/util/Helper7.class

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this class uses OperatingSystemMXBean here.

Comment thread pom.xml
com/graphhopper/util/Helper7.class
</exclude>
<exclude>
com/graphhopper/storage/UnsafeDataAccess.class

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Policeman Tools does not like usage of unsafe packages :)

Comment thread pom.xml
</exclude>
<!-- Has a couple of issues with the FileWriter -->
<exclude>
com/graphhopper/tools/Measurement.class

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be very important to fix the measurement anyway?

Comment thread pom.xml Outdated
</exclude>
<!-- orbidden class/interface use: java.io.FileReader [Uses default charset] -->
<exclude>
com/graphhopper/spatialrules/SpatialRuleLookupHelper.class

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two use the FileReader. From the Javadoc:

The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate.  To specify these values yourself, construct an InputStreamReader on a FileInputStream.

I would fix this, if we decide to use PolicemanTools :).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I fixed the FileReader issue as well.

@devemux86

Copy link
Copy Markdown
Contributor

How about Helper.createFormatter with Locale.UK?

@karussell karussell left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (feel free to squash & merge)

@karussell karussell added this to the 0.10 milestone Nov 20, 2017
@karussell

Copy link
Copy Markdown
Member

@devemux86 do you mean createFormatter should use Locale.ROOT instead of Locale.UK?

@devemux86

Copy link
Copy Markdown
Contributor

It uses UTC time zone, so appears to be special case?

@karussell

Copy link
Copy Markdown
Member

Not sure what you mean ... can you elaborate :) ? This wasn't touched in this PR and is very important to avoid a local e.g. DE setting being propagated to consuming clients located e.g. in US (GPX export etc)

@devemux86

Copy link
Copy Markdown
Contributor

Because it wasn't changed, I wondered the case.
It's from the start UK with UTC for a reason?

@karussell

Copy link
Copy Markdown
Member

Yes, GPX should not be filled with German formatting and should be UTC.

@boldtrn

boldtrn commented Nov 21, 2017

Copy link
Copy Markdown
Member Author

Yes, GPX should not be filled with German formatting and should be UTC.

Yes, and it is. Here is one sample line from a generated GPX, using GH and this branch.

<time>2017-11-21T00:50:33Z</time>

This is correctly formatted and also the correct UTC time.

boldtrn and others added 5 commits November 21, 2017 02:46
Fix one issue

Fixed most PolicemanTools issues

Android sample: Gradle 4, Android plugin 3, SDK 27

make size to use shape vs. edgeId configurable, improves graphhopper#1214

minor UI style fixes
…pper#1216

Introduce Instruction.getLength() that fixes the ViaPoint issue
@boldtrn
boldtrn force-pushed the try_policemanttools branch 2 times, most recently from 476f45a to e8c8b21 Compare November 21, 2017 02:54
@boldtrn

boldtrn commented Nov 21, 2017

Copy link
Copy Markdown
Member Author

Ok, apparently I merged the master into this PR some time ago and forgot when I squashed. So this PR looks now broken as well. Again I can try to rebuild it in a new PR. Not sure if I use a wrong command or if there is a way to revert the squash. I tried with git reset --hard HEAD@{2}, which resulted in the current state you can see right now, which I hope should still contain all changes I made.

@boldtrn boldtrn mentioned this pull request Nov 30, 2017
@boldtrn

boldtrn commented Nov 30, 2017

Copy link
Copy Markdown
Member Author

The new PR is located at #1236. I got it fixed with:

# Switch to the master branch and make sure you are up to date.
git checkout master
git fetch # this may be necessary (depending on your git config) to receive updates on origin/master
git pull

# Merge the feature branch into the master branch.
git merge feature_branch

# Reset the master branch to origin's state.
git reset origin/master

# Git now considers all changes as unstaged changes.
# We can add these changes as one commit.
# Adding . will also add untracked files.
git add --all
git commit

as described here.

@boldtrn boldtrn closed this Nov 30, 2017
@karussell

Copy link
Copy Markdown
Member

BTW: you should be able to push into the same PR (no need to create a new)

@boldtrn

boldtrn commented Dec 4, 2017

Copy link
Copy Markdown
Member Author

BTW: you should be able to push into the same PR (no need to create a new)

Yes I thought so, but I wasn't sure if this might make things worse, sorry about that.

@boldtrn
boldtrn deleted the try_policemanttools branch December 4, 2017 23:50
@karussell

Copy link
Copy Markdown
Member

No problem. Better a new branch or PR instead of a wrong forced push, because they are really dangerous if applied in the wrong place :)

@boldtrn

boldtrn commented Dec 7, 2017

Copy link
Copy Markdown
Member Author

No problem. Better a new branch or PR instead of a wrong forced push, because they are really dangerous if applied in the wrong place :)

These were exactly my thoughts as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants