Changeset 12732 in josm for trunk/src


Ignore:
Timestamp:
2017-09-05T02:24:38+02:00 (2 years ago)
Author:
Don-vip
Message:

see #9995 - sonar - squid:S3655 - Optional value should only be accessed after calling isPresent()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/HiDPISupport.java

    r12724 r12732  
    2424 *
    2525 * Gives access to the class <code>BaseMultiResolutionImage</code> via reflection,
    26  * in case it is on classpath. This is to be expected for Java 9, but not for Java 8
    27  * runtime.
     26 * in case it is on classpath. This is to be expected for Java 9, but not for Java 8 runtime.
    2827 *
    2928 * @since 12722
     
    6665    public static Image getMultiResolutionImage(List<Image> imgs) {
    6766        CheckParameterUtil.ensure(imgs, "imgs", "not empty", ls -> !ls.isEmpty());
    68         if (getBaseMultiResolutionImageConstructor().isPresent()) {
    69             Constructor<? extends Image> c = getBaseMultiResolutionImageConstructor().get();
     67        Optional<Constructor<? extends Image>> baseMrImageConstructor = getBaseMultiResolutionImageConstructor();
     68        if (baseMrImageConstructor.isPresent()) {
    7069            try {
    71                 return c.newInstance((Object) imgs.toArray(new Image[0]));
     70                return baseMrImageConstructor.get().newInstance((Object) imgs.toArray(new Image[0]));
    7271            } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
    7372                Logging.error("Unexpected error while instantiating object of class BaseMultiResolutionImage: " + ex);
     
    8079     * Wrapper for the method <code>java.awt.image.BaseMultiResolutionImage#getBaseImage()</code>.
    8180     * <p>
    82      * Will return the argument <code>img</code> unchanged, if it is not a multi-resolution
    83      * image.
     81     * Will return the argument <code>img</code> unchanged, if it is not a multi-resolution image.
    8482     * @param img the image
    8583     * @return if <code>img</code> is a <code>java.awt.image.BaseMultiResolutionImage</code>,
     
    8785     */
    8886    public static Image getBaseImage(Image img) {
    89         if (!getBaseMultiResolutionImageClass().isPresent() || !getResolutionVariantsMethod().isPresent()) {
     87        Optional<Class<? extends Image>> baseMrImageClass = getBaseMultiResolutionImageClass();
     88        Optional<Method> resVariantsMethod = getResolutionVariantsMethod();
     89        if (!baseMrImageClass.isPresent() || !resVariantsMethod.isPresent()) {
    9090            return img;
    9191        }
    92         if (getBaseMultiResolutionImageClass().get().isInstance(img)) {
     92        if (baseMrImageClass.get().isInstance(img)) {
    9393            try {
    9494                @SuppressWarnings("unchecked")
    95                 List<Image> imgVars = (List) getResolutionVariantsMethod().get().invoke(img);
     95                List<Image> imgVars = (List<Image>) resVariantsMethod.get().invoke(img);
    9696                if (!imgVars.isEmpty()) {
    9797                    return imgVars.get(0);
     
    107107     * Wrapper for the method <code>java.awt.image.MultiResolutionImage#getResolutionVariants()</code>.
    108108     * <p>
    109      * Will return the argument as a singleton list, in case it is not a multi-resolution
    110      * image.
     109     * Will return the argument as a singleton list, in case it is not a multi-resolution image.
    111110     * @param img the image
    112111     * @return if <code>img</code> is a <code>java.awt.image.BaseMultiResolutionImage</code>,
     
    115114     */
    116115    public static List<Image> getResolutionVariants(Image img) {
    117         if (!getBaseMultiResolutionImageClass().isPresent() || !getResolutionVariantsMethod().isPresent()) {
     116        Optional<Class<? extends Image>> baseMrImageClass = getBaseMultiResolutionImageClass();
     117        Optional<Method> resVariantsMethod = getResolutionVariantsMethod();
     118        if (!baseMrImageClass.isPresent() || !resVariantsMethod.isPresent()) {
    118119            return Collections.singletonList(img);
    119120        }
    120         if (getBaseMultiResolutionImageClass().get().isInstance(img)) {
     121        if (baseMrImageClass.get().isInstance(img)) {
    121122            try {
    122123                @SuppressWarnings("unchecked")
    123                 List<Image> imgVars = (List) getResolutionVariantsMethod().get().invoke(img);
     124                List<Image> imgVars = (List<Image>) resVariantsMethod.get().invoke(img);
    124125                if (!imgVars.isEmpty()) {
    125126                    return imgVars;
     
    205206                    try {
    206207                        @SuppressWarnings("unchecked")
    207                         Class<? extends Image> c = (Class) Class.forName("java.awt.image.BaseMultiResolutionImage");
     208                        Class<? extends Image> c = (Class<? extends Image>) Class.forName("java.awt.image.BaseMultiResolutionImage");
    208209                        baseMultiResolutionImageClass = Optional.ofNullable(c);
    209210                    } catch (ClassNotFoundException ex) {
Note: See TracChangeset for help on using the changeset viewer.