From 7e40d13947ed7bfd8495f2edfb450fd401e3d33b Mon Sep 17 00:00:00 2001 From: games647 Date: Tue, 13 Mar 2018 16:05:27 +0100 Subject: [PATCH 1/3] Update lombok and remove antrun to fix compiling with JDK9 * Lombok: Older version resulted in NoSuchFieldEx: pid (fixed since 1.16.8) * JDK9 no longer has a tools.jar because of it's modular system, but ant is no longer required. It was introduced in 3e9a2377a861d53f385900d9532c32b90b0304fd for GroupManager. Now GM is no longer maintained and removed in * 5ca02e65963b41b261f297ea4dfa50f2a153f79b (calling the build.xml) * fa0bbde23651eff31845edd1fed918b8fe430e0e (removing the remaining module) --- .../src/com/earth2me/essentials/Settings.java | 2 +- pom.xml | 25 +------------------ 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Settings.java b/Essentials/src/com/earth2me/essentials/Settings.java index b85aab2aa..5f283f6b0 100644 --- a/Essentials/src/com/earth2me/essentials/Settings.java +++ b/Essentials/src/com/earth2me/essentials/Settings.java @@ -1418,4 +1418,4 @@ public class Settings implements net.ess3.api.ISettings { public boolean isDirectHatAllowed() { return config.getBoolean("allow-direct-hat", true); } -} \ No newline at end of file +} diff --git a/pom.xml b/pom.xml index e369c413c..4b2297075 100644 --- a/pom.xml +++ b/pom.xml @@ -65,7 +65,7 @@ org.projectlombok lombok - 1.12.2 + 1.16.20 provided @@ -83,29 +83,6 @@ 1.7 - - org.apache.maven.plugins - maven-antrun-plugin - 1.7 - - - validate - - run - - - - - - com.sun - tools - 1.5.0 - system - ${java.home}/../lib/tools.jar - - - false - org.apache.maven.plugins maven-dependency-plugin From d24fbc9f55976f0767a04d247e19de4f1a1479ca Mon Sep 17 00:00:00 2001 From: games647 Date: Tue, 13 Mar 2018 16:20:02 +0100 Subject: [PATCH 2/3] Remove final modifier to fix illegal reflective access warning Java 9 runtimes report warnings for reflective access on JRE classes (in this case Field.modifiers). Future versions of Java may deny the access completely. Since we access our own code here, we could just remove the final modifier. With it's current visibility (of private) it's unlikely that it will be modified from somewhere else except our Settings class. --- Essentials/src/com/earth2me/essentials/Settings.java | 4 ---- .../src/com/earth2me/essentials/utils/NumberUtil.java | 6 +++--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Settings.java b/Essentials/src/com/earth2me/essentials/Settings.java index 5f283f6b0..fbca35f22 100644 --- a/Essentials/src/com/earth2me/essentials/Settings.java +++ b/Essentials/src/com/earth2me/essentials/Settings.java @@ -1338,11 +1338,7 @@ public class Settings implements net.ess3.api.ISettings { try { Field field = NumberUtil.class.getDeclaredField("PRETTY_FORMAT"); field.setAccessible(true); - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(null, currencyFormat); - modifiersField.setAccessible(false); field.setAccessible(false); } catch (NoSuchFieldException | IllegalAccessException e) { ess.getLogger().severe("Failed to apply custom currency format: " + e.getMessage()); diff --git a/Essentials/src/com/earth2me/essentials/utils/NumberUtil.java b/Essentials/src/com/earth2me/essentials/utils/NumberUtil.java index c4459f34c..3d2a5cef5 100644 --- a/Essentials/src/com/earth2me/essentials/utils/NumberUtil.java +++ b/Essentials/src/com/earth2me/essentials/utils/NumberUtil.java @@ -13,12 +13,12 @@ import static com.earth2me.essentials.I18n.tl; public class NumberUtil { - static DecimalFormat twoDPlaces = new DecimalFormat("#,###.##"); - static DecimalFormat currencyFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US)); + private static DecimalFormat twoDPlaces = new DecimalFormat("#,###.##"); + private static DecimalFormat currencyFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US)); // This field is likely to be modified in com.earth2me.essentials.Settings when loading currency format. // This ensures that we can supply a constant formatting. - static final NumberFormat PRETTY_FORMAT = NumberFormat.getInstance(Locale.US); + private static NumberFormat PRETTY_FORMAT = NumberFormat.getInstance(Locale.US); static { twoDPlaces.setRoundingMode(RoundingMode.HALF_UP); From c7000846cd3100d79434e893da49f2d2bb326155 Mon Sep 17 00:00:00 2001 From: games647 Date: Wed, 21 Mar 2018 11:16:16 +0100 Subject: [PATCH 3/3] Replace Reflection with setter implementation --- .../src/com/earth2me/essentials/Settings.java | 18 ++---------------- .../earth2me/essentials/utils/NumberUtil.java | 7 ++++++- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Settings.java b/Essentials/src/com/earth2me/essentials/Settings.java index fbca35f22..1bdb39641 100644 --- a/Essentials/src/com/earth2me/essentials/Settings.java +++ b/Essentials/src/com/earth2me/essentials/Settings.java @@ -16,8 +16,6 @@ import org.bukkit.event.EventPriority; import org.bukkit.inventory.ItemStack; import java.io.File; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; @@ -1333,20 +1331,8 @@ public class Settings implements net.ess3.api.ISettings { DecimalFormat currencyFormat = new DecimalFormat(currencyFormatString, decimalFormatSymbols); currencyFormat.setRoundingMode(RoundingMode.FLOOR); - // Updates NumberUtil#PRETTY_FORMAT field so that all of Essentials - // can follow a single format. - try { - Field field = NumberUtil.class.getDeclaredField("PRETTY_FORMAT"); - field.setAccessible(true); - field.set(null, currencyFormat); - field.setAccessible(false); - } catch (NoSuchFieldException | IllegalAccessException e) { - ess.getLogger().severe("Failed to apply custom currency format: " + e.getMessage()); - if (isDebug()) { - e.printStackTrace(); - } - } - + // Updates NumberUtil#PRETTY_FORMAT field so that all of Essentials can follow a single format. + NumberUtil.internalSetPrettyFormat(currencyFormat); return currencyFormat; } diff --git a/Essentials/src/com/earth2me/essentials/utils/NumberUtil.java b/Essentials/src/com/earth2me/essentials/utils/NumberUtil.java index 3d2a5cef5..f554ed6b3 100644 --- a/Essentials/src/com/earth2me/essentials/utils/NumberUtil.java +++ b/Essentials/src/com/earth2me/essentials/utils/NumberUtil.java @@ -11,8 +11,8 @@ import java.util.Locale; import static com.earth2me.essentials.I18n.tl; - public class NumberUtil { + private static DecimalFormat twoDPlaces = new DecimalFormat("#,###.##"); private static DecimalFormat currencyFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US)); @@ -30,6 +30,11 @@ public class NumberUtil { PRETTY_FORMAT.setMaximumFractionDigits(2); } + // this method should only be called by Essentials + public static void internalSetPrettyFormat(NumberFormat prettyFormat) { + PRETTY_FORMAT = prettyFormat; + } + public static String shortCurrency(final BigDecimal value, final IEssentials ess) { return ess.getSettings().getCurrencySymbol() + formatAsCurrency(value); }