diff --git a/CHANGES.md b/CHANGES.md index 76770df764..b29cb34b2e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,7 @@ Bug Fixes --------- * [#1025](https://github.com/java-native-access/jna/issues/1025): Restore java 6 compatibility and introduce animal-sniffer to prevent regressions - [@matthiasblaesing](https://github.com/matthiasblaesing). * [#1027](https://github.com/java-native-access/jna/issues/1027): Fix Linux LibC.Sysinfo FieldOrder - [@dbwiddis](https://github.com/dbwiddis). +* [#1033](https://github.com/java-native-access/jna/pull/1033): Replace deprecated Class#newInstance calls Release 5.0.0 ============= diff --git a/contrib/platform/test/com/sun/jna/platform/win32/WinspoolTest.java b/contrib/platform/test/com/sun/jna/platform/win32/WinspoolTest.java index 54ec2009bf..05a9d04a45 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/WinspoolTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/WinspoolTest.java @@ -118,7 +118,7 @@ public void testCorrectDeclarationOfMembers() throws InstantiationException, Ill if(Structure.class.isAssignableFrom(klass)) { boolean writeWorked = false; try { - Structure struct = (Structure) klass.newInstance(); + Structure struct = Structure.newInstance((Class) klass); struct.write(); writeWorked = true; } catch (java.lang.Throwable ex) { diff --git a/src/com/sun/jna/IntegerType.java b/src/com/sun/jna/IntegerType.java index 4d83e2b6f7..2bcd4883d1 100644 --- a/src/com/sun/jna/IntegerType.java +++ b/src/com/sun/jna/IntegerType.java @@ -24,6 +24,8 @@ package com.sun.jna; +import java.lang.reflect.InvocationTargetException; + /** * Represents a native integer value, which may have a platform-specific size * (e.g. long on unix-based platforms). @@ -116,19 +118,9 @@ public Object fromNative(Object nativeValue, FromNativeContext context) { // be forgiving of null values read from memory long value = nativeValue == null ? 0 : ((Number) nativeValue).longValue(); - try { - IntegerType number = getClass().newInstance(); - number.setValue(value); - return number; - } - catch (InstantiationException e) { - throw new IllegalArgumentException("Can't instantiate " - + getClass()); - } - catch (IllegalAccessException e) { - throw new IllegalArgumentException("Not allowed to instantiate " - + getClass()); - } + IntegerType number = Klass.newInstance(getClass()); + number.setValue(value); + return number; } @Override diff --git a/src/com/sun/jna/Klass.java b/src/com/sun/jna/Klass.java new file mode 100644 index 0000000000..da16e6f8c6 --- /dev/null +++ b/src/com/sun/jna/Klass.java @@ -0,0 +1,79 @@ +/* Copyright (c) 2018 Matthias Bläsing + * + * The contents of this file is dual-licensed under 2 + * alternative Open Source/Free licenses: LGPL 2.1 or later and + * Apache License 2.0. (starting with JNA version 4.0.0). + * + * You can freely decide which license you want to apply to + * the project. + * + * You may obtain a copy of the LGPL License at: + * + * http://www.gnu.org/licenses/licenses.html + * + * A copy is also included in the downloadable source code package + * containing JNA, in file "LGPL2.1". + * + * You may obtain a copy of the Apache License at: + * + * http://www.apache.org/licenses/ + * + * A copy is also included in the downloadable source code package + * containing JNA, in file "AL2.0". + */ + +package com.sun.jna; + +import java.lang.reflect.InvocationTargetException; + +abstract class Klass { + + private Klass() { + } + + /** + * Create a new instance for the given {@code klass}. Runtime exceptions + * thrown from the constructor are rethrown, all other exceptions + * generated from the reflective call are wrapped into a + * {@link java.lang.IllegalArgumentException} and rethrown. + * + * @param klass desired class to instantiate + * @return the new instance + * @throws IllegalArgumentException if the instantiation fails + * @throws RuntimeException if the constructor for {@code klass} throws + * a runtime exception + */ + public static T newInstance(Class klass) { + try { + return klass.getDeclaredConstructor().newInstance(); + } catch (IllegalAccessException e) { + String msg = "Can't create an instance of " + klass + + ", requires a public no-arg constructor: " + e; + throw new IllegalArgumentException(msg, e); + } catch (IllegalArgumentException e) { + String msg = "Can't create an instance of " + klass + + ", requires a public no-arg constructor: " + e; + throw new IllegalArgumentException(msg, e); + } catch (InstantiationException e) { + String msg = "Can't create an instance of " + klass + + ", requires a public no-arg constructor: " + e; + throw new IllegalArgumentException(msg, e); + } catch (NoSuchMethodException e) { + String msg = "Can't create an instance of " + klass + + ", requires a public no-arg constructor: " + e; + throw new IllegalArgumentException(msg, e); + } catch (SecurityException e) { + String msg = "Can't create an instance of " + klass + + ", requires a public no-arg constructor: " + e; + throw new IllegalArgumentException(msg, e); + } catch (InvocationTargetException e) { + if (e.getCause() instanceof RuntimeException) { + throw (RuntimeException) e.getCause(); + } else { + String msg = "Can't create an instance of " + klass + + ", requires a public no-arg constructor: " + e; + throw new IllegalArgumentException(msg, e); + } + } + } +} diff --git a/src/com/sun/jna/NativeMappedConverter.java b/src/com/sun/jna/NativeMappedConverter.java index d91baa6973..2d7c0ad63f 100644 --- a/src/com/sun/jna/NativeMappedConverter.java +++ b/src/com/sun/jna/NativeMappedConverter.java @@ -25,6 +25,7 @@ import java.lang.ref.Reference; import java.lang.ref.SoftReference; +import java.lang.reflect.InvocationTargetException; import java.util.Map; import java.util.WeakHashMap; @@ -61,18 +62,9 @@ public NativeMapped defaultValue() { return (NativeMapped) type.getEnumConstants()[0]; } - try { - return (NativeMapped)type.newInstance(); - } catch (InstantiationException e) { - String msg = "Can't create an instance of " + type - + ", requires a no-arg constructor: " + e; - throw new IllegalArgumentException(msg); - } catch (IllegalAccessException e) { - String msg = "Not allowed to create an instance of " + type - + ", requires a public, no-arg constructor: " + e; - throw new IllegalArgumentException(msg); - } + return (NativeMapped) Klass.newInstance(type); } + @Override public Object fromNative(Object nativeValue, FromNativeContext context) { return instance.fromNative(nativeValue, context); diff --git a/src/com/sun/jna/PointerType.java b/src/com/sun/jna/PointerType.java index 2703f380dc..e10f400fa3 100644 --- a/src/com/sun/jna/PointerType.java +++ b/src/com/sun/jna/PointerType.java @@ -23,6 +23,8 @@ */ package com.sun.jna; +import java.lang.reflect.InvocationTargetException; + /** Type representing a type-safe native pointer. * Derived classes may override the {@link NativeMapped#fromNative} method, * which should instantiate a new object (or look up an existing one) @@ -78,17 +80,9 @@ public Object fromNative(Object nativeValue, FromNativeContext context) { if (nativeValue == null) { return null; } - try { - PointerType pt = getClass().newInstance(); - pt.pointer = (Pointer)nativeValue; - return pt; - } - catch (InstantiationException e) { - throw new IllegalArgumentException("Can't instantiate " + getClass()); - } - catch (IllegalAccessException e) { - throw new IllegalArgumentException("Not allowed to instantiate " + getClass()); - } + PointerType pt = Klass.newInstance(getClass()); + pt.pointer = (Pointer)nativeValue; + return pt; } /** The hash code for a PointerType is the same as that for diff --git a/src/com/sun/jna/Structure.java b/src/com/sun/jna/Structure.java index eee919f6fd..c18185443c 100644 --- a/src/com/sun/jna/Structure.java +++ b/src/com/sun/jna/Structure.java @@ -1864,28 +1864,18 @@ public static T newInstance(Class type, Pointer init) t return s; } - /** Create a new Structure instance of the given type + /** + * Create a new Structure instance of the given type * @param type desired Structure type * @return the new instance * @throws IllegalArgumentException if the instantiation fails */ public static T newInstance(Class type) throws IllegalArgumentException { - try { - T s = type.newInstance(); - if (s instanceof ByValue) { - s.allocateMemory(); - } - return s; - } - catch(InstantiationException e) { - String msg = "Can't instantiate " + type; - throw new IllegalArgumentException(msg, e); - } - catch(IllegalAccessException e) { - String msg = "Instantiation of " + type - + " not allowed, is it public?"; - throw new IllegalArgumentException(msg, e); + T s = Klass.newInstance(type); + if (s instanceof ByValue) { + s.allocateMemory(); } + return s; } /** Keep track of the largest aggregate field of the union to use for diff --git a/test/com/sun/jna/StructureTest.java b/test/com/sun/jna/StructureTest.java index 4bf6c0d5c5..d88953589c 100644 --- a/test/com/sun/jna/StructureTest.java +++ b/test/com/sun/jna/StructureTest.java @@ -390,7 +390,7 @@ private void testAlignStruct(String prefix,int index) { IntByReference offset = new IntByReference(); LongByReference value = new LongByReference(); Class cls = Class.forName(getClass().getName() + "$" + prefix + "TestStructure" + index); - Structure s = (Structure)cls.newInstance(); + Structure s = Structure.newInstance((Class) cls); int result = lib.testStructureAlignment(s, index, offset, value); assertEquals("Wrong native value at field " + result + "=0x" + Long.toHexString(value.getValue()) diff --git a/test/com/sun/jna/WebStartTest.java b/test/com/sun/jna/WebStartTest.java index 3eff3959c3..9f20be5350 100644 --- a/test/com/sun/jna/WebStartTest.java +++ b/test/com/sun/jna/WebStartTest.java @@ -417,7 +417,7 @@ private static void sendResults(Throwable t, int port) throws IOException { } private static Throwable runTestCaseTest(String testClass, String method, int port) throws Exception { - TestCase test = (TestCase)Class.forName(testClass).newInstance(); + TestCase test = (TestCase)Class.forName(testClass).getConstructor().newInstance(); test.setName(method); TestResult result = new TestResult(); test.run(result);