From add1c7ef1ecfa745c2516fecd6d6dadfd6f40342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=C5=99=C3=ADk?= Date: Wed, 10 Feb 2021 18:36:39 +0100 Subject: [PATCH] cls.newInstance() causing warnings replaced with cls.getDeclaredConstructor().newInstance() --- .../flash/helpers/SWFDecompilerPlugin.java | 18 +++++++++++++----- .../src/com/jpexs/helpers/ReflectionTools.java | 13 ++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/helpers/SWFDecompilerPlugin.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/helpers/SWFDecompilerPlugin.java index 9da106b60..3902df1fe 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/helpers/SWFDecompilerPlugin.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/helpers/SWFDecompilerPlugin.java @@ -12,7 +12,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.flash.helpers; import com.jpexs.decompiler.flash.SWF; @@ -28,6 +29,7 @@ import com.jpexs.helpers.plugin.CharSequenceJavaFileObject; import com.jpexs.helpers.plugin.ClassFileManager; import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; @@ -111,12 +113,12 @@ public class SWFDecompilerPlugin { String pluginName = Path.getFileNameWithoutExtension(pluginFile); Class cls = cl.loadClass(pluginName); if (SWFDecompilerListener.class.isAssignableFrom(cls)) { - SWFDecompilerListener listener = (SWFDecompilerListener) cls.newInstance(); + SWFDecompilerListener listener = (SWFDecompilerListener) cls.getDeclaredConstructor().newInstance(); listeners.add(listener); } System.out.println("Plugin loaded: " + pluginName); - } catch (MalformedURLException | ClassNotFoundException | InstantiationException | IllegalAccessException ex) { + } catch (MalformedURLException | ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException ex) { logger.log(Level.SEVERE, null, ex); } } @@ -159,9 +161,15 @@ public class SWFDecompilerPlugin { // Creating an instance of our compiled class and try { - listeners.add((SWFDecompilerListener) fileManager.getClassLoader(null).loadClass(fullName).newInstance()); + listeners.add((SWFDecompilerListener) fileManager.getClassLoader(null).loadClass(fullName).getDeclaredConstructor().newInstance()); System.out.println("Plugin loaded: " + fullName); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) { + } catch (ClassNotFoundException + | InstantiationException + | IllegalAccessException + | NoSuchMethodException + | SecurityException + | IllegalArgumentException + | InvocationTargetException ex) { logger.log(Level.SEVERE, null, ex); } } diff --git a/libsrc/ffdec_lib/src/com/jpexs/helpers/ReflectionTools.java b/libsrc/ffdec_lib/src/com/jpexs/helpers/ReflectionTools.java index 5484d523b..62a4f99fc 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/helpers/ReflectionTools.java +++ b/libsrc/ffdec_lib/src/com/jpexs/helpers/ReflectionTools.java @@ -12,7 +12,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.helpers; import com.jpexs.decompiler.flash.types.FieldChangeObserver; @@ -20,6 +21,7 @@ import com.jpexs.decompiler.flash.types.annotations.Internal; import com.jpexs.decompiler.flash.types.annotations.SWFField; import java.lang.reflect.Array; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.util.ArrayList; @@ -207,7 +209,7 @@ public class ReflectionTools { } - public static Object newInstanceOf(Class cls) throws InstantiationException, IllegalAccessException { + public static Object newInstanceOf(Class cls) throws InstantiationException, IllegalAccessException { if (cls == Integer.class || cls == int.class) { return 0; } else if (cls == Float.class || cls == float.class) { @@ -223,7 +225,12 @@ public class ReflectionTools { if (Modifier.isAbstract(cls.getModifiers())) { return null; } - return cls.newInstance(); + try { + return cls.getDeclaredConstructor().newInstance(); + } catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException ex) { + Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex); + } + return null; } @SuppressWarnings("unchecked")