Changeset 18811 in josm


Ignore:
Timestamp:
2023-08-17T15:03:10+02:00 (10 months ago)
Author:
taylor.smock
Message:

Fix #23124: Azul Java 17.0.4.1 will cause a NumberFormatException

Java can have multiple dots instead of just two. Specifically, the Java version
specifier is $MAJOR.$MINOR.$SECURITY.$PATCH.

Previously, we assumed that there would only ever be two dots in the version
specifier, and got the first and last dot. Now we get the first and second dot.

In addition, this fixes some lint issues.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/DefaultNameFormatter.java

    r18808 r18811  
    172172     *
    173173     * @param name the name without the nodes count
    174      * @param primitive the primitive
     174     * @param way the way
    175175     * @since 18808
    176176     */
    177     protected void decorateNameWithNodes(StringBuilder name, IWay way) {
     177    protected void decorateNameWithNodes(StringBuilder name, IWay<?> way) {
    178178        char mark;
    179179        // If current language is left-to-right (almost all languages)
     
    559559     * Decorates the name of primitive with its id, if the preference
    560560     * <code>osm-primitives.showid</code> is set.
    561      *
    562      * The id is append to the {@link StringBuilder} passed in <code>name</code>.
     561     * <p>
     562     * The id is appended to the {@link StringBuilder} passed in <code>name</code>.
    563563     *
    564564     * @param name  the name without the id
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r18801 r18811  
    7676
    7777    /** Pattern matching white spaces */
    78     public static final Pattern WHITE_SPACES_PATTERN = Pattern.compile("\\s+");
     78    public static final Pattern WHITE_SPACES_PATTERN = Pattern.compile("\\s+", Pattern.UNICODE_CHARACTER_CLASS);
    7979
    8080    private static final long MILLIS_OF_SECOND = TimeUnit.SECONDS.toMillis(1);
     
    105105
    106106    private static Method mapOfEntriesMethod() {
    107         try {
    108             return Map.class.getMethod("ofEntries", Map.Entry[].class);
    109         } catch (NoSuchMethodException e) {
    110             return null;
    111         }
     107        if (getJavaVersion() >= 9) {
     108            try {
     109                return Map.class.getMethod("ofEntries", Map.Entry[].class);
     110            } catch (NoSuchMethodException noSuchMethodException) {
     111                Logging.trace(noSuchMethodException);
     112            }
     113        }
     114        return null;
    112115    }
    113116
     
    197200     *  empty string
    198201     * @return null if values is null. The joined string otherwise.
    199      * @deprecated use {@link String#join} or {@link Collectors#joining}
     202     * @deprecated since 15718, use {@link String#join} or {@link Collectors#joining}
    200203     */
    201204    @Deprecated
     
    521524
    522525        char[] hexChars = new char[len * 2];
    523         for (int i = 0, j = 0; i < len; i++) {
    524             final int v = bytes[i];
     526        int j = 0;
     527        for (final int v : bytes) {
    525528            hexChars[j++] = HEX_ARRAY[(v & 0xf0) >> 4];
    526529            hexChars[j++] = HEX_ARRAY[v & 0xf];
     
    682685            try {
    683686                // Java 9: use Map.ofEntries(...)
    684                 return (Map<K, V>) mapOfEntries.invoke(null, new Object[]{map.entrySet().toArray(new Map.Entry[0])});
    685             } catch (Exception ignore) {
    686                 Logging.trace(ignore);
     687                return (Map<K, V>) mapOfEntries.invoke(null, (Object) map.entrySet().toArray(new Map.Entry[0]));
     688            } catch (ReflectiveOperationException toLog) {
     689                Logging.trace(toLog);
    687690            }
    688691        }
     
    15201523            return new byte[0];
    15211524        }
    1522         try { // NOPMD
    1523             ByteArrayOutputStream bout = new ByteArrayOutputStream(stream.available());
     1525        try (ByteArrayOutputStream bout = new ByteArrayOutputStream(stream.available())) {
    15241526            byte[] buffer = new byte[8192];
    15251527            boolean finished = false;
     
    15431545     * Returns the initial capacity to pass to the HashMap / HashSet constructor
    15441546     * when it is initialized with a known number of entries.
    1545      *
     1547     * <p>
    15461548     * When a HashMap is filled with entries, the underlying array is copied over
    15471549     * to a larger one multiple times. To avoid this process when the number of
     
    15601562     * Returns the initial capacity to pass to the HashMap / HashSet constructor
    15611563     * when it is initialized with a known number of entries.
    1562      *
     1564     * <p>
    15631565     * When a HashMap is filled with entries, the underlying array is copied over
    15641566     * to a larger one multiple times. To avoid this process when the number of
     
    15661568     * given to the HashMap constructor. This method returns a suitable value
    15671569     * that avoids rehashing but doesn't waste memory.
    1568      *
     1570     * <p>
    15691571     * Assumes default load factor (0.75).
    15701572     * @param nEntries the number of entries expected
     
    16401642     */
    16411643    public static double clamp(double val, double min, double max) {
     1644        // Switch to Math.clamp when we move to Java 21
    16421645        if (min > max) {
    16431646            throw new IllegalArgumentException(MessageFormat.format("Parameter min ({0}) cannot be greater than max ({1})", min, max));
     
    16741677    /**
    16751678     * Convert angle from radians to degrees.
    1676      *
     1679     * <p>
    16771680     * Replacement for {@link Math#toDegrees(double)} to match the Java 9
    16781681     * version of that method. (Can be removed when JOSM support for Java 8 ends.)
     
    16891692    /**
    16901693     * Convert angle from degrees to radians.
    1691      *
     1694     * <p>
    16921695     * Replacement for {@link Math#toRadians(double)} to match the Java 9
    16931696     * version of that method. (Can be removed when JOSM support for Java 8 ends.)
     
    17081711     */
    17091712    public static int getJavaVersion() {
    1710         String version = getSystemProperty("java.version");
     1713        // Switch to Runtime.version() once we move past Java 8
     1714        String version = Objects.requireNonNull(getSystemProperty("java.version"));
    17111715        if (version.startsWith("1.")) {
    17121716            version = version.substring(2);
     
    17291733     */
    17301734    public static int getJavaUpdate() {
    1731         String version = getSystemProperty("java.version");
     1735        // Switch to Runtime.version() once we move past Java 8
     1736        String version = Objects.requireNonNull(getSystemProperty("java.version"));
    17321737        if (version.startsWith("1.")) {
    17331738            version = version.substring(2);
     
    17381743        // 9
    17391744        // 9.0.1
     1745        // 17.0.4.1+1-LTS
     1746        // $MAJOR.$MINOR.$SECURITY.$PATCH
    17401747        int undePos = version.indexOf('_');
    17411748        int dashPos = version.indexOf('-');
     
    17451752        }
    17461753        int firstDotPos = version.indexOf('.');
    1747         int lastDotPos = version.lastIndexOf('.');
    1748         if (firstDotPos == lastDotPos) {
     1754        int secondDotPos = version.indexOf('.', firstDotPos + 1);
     1755        if (firstDotPos == secondDotPos) {
    17491756            return 0;
    17501757        }
    17511758        return firstDotPos > -1 ? Integer.parseInt(version.substring(firstDotPos + 1,
    1752                 lastDotPos > -1 ? lastDotPos : version.length())) : 0;
     1759                secondDotPos > -1 ? secondDotPos : version.length())) : 0;
    17531760    }
    17541761
     
    17591766     */
    17601767    public static int getJavaBuild() {
    1761         String version = getSystemProperty("java.runtime.version");
     1768        // Switch to Runtime.version() once we move past Java 8
     1769        String version = Objects.requireNonNull(getSystemProperty("java.runtime.version"));
    17621770        int bPos = version.indexOf('b');
    17631771        int pPos = version.indexOf('+');
     
    20412049    public static String stripHtml(String rawString) {
    20422050        // remove HTML tags
    2043         rawString = rawString.replaceAll("<.*?>", " ");
     2051        rawString = rawString.replaceAll("<[^>]+>", " ");
    20442052        // consolidate multiple spaces between a word to a single space
    2045         rawString = rawString.replaceAll("\\b\\s{2,}\\b", " ");
     2053        rawString = rawString.replaceAll("(?U)\\b\\s{2,}\\b", " ");
    20462054        // remove extra whitespaces
    20472055        return rawString.trim();
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r18553 r18811  
    312312            System.setProperty("java.version", "9.1.2");
    313313            assertEquals(1, Utils.getJavaUpdate());
     314
     315            System.setProperty("java.version", "17.0.4.1+1-LTS");
     316            assertEquals(0, Utils.getJavaUpdate());
    314317        } finally {
    315318            System.setProperty("java.version", javaVersion);
Note: See TracChangeset for help on using the changeset viewer.