Skip to content

Commit 616ec37

Browse files
Replace deprecated Class#newInstance calls
1 parent 01b6f67 commit 616ec37

File tree

9 files changed

+102
-54
lines changed

9 files changed

+102
-54
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Bug Fixes
1414
---------
1515
* [#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).
1616
* [#1027](https://github.com/java-native-access/jna/issues/1027): Fix Linux LibC.Sysinfo FieldOrder - [@dbwiddis](https://github.com/dbwiddis).
17+
* [#1033](https://github.com/java-native-access/jna/pull/1033): Replace deprecated Class#newInstance calls
1718

1819
Release 5.0.0
1920
=============

contrib/platform/test/com/sun/jna/platform/win32/WinspoolTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void testCorrectDeclarationOfMembers() throws InstantiationException, Ill
118118
if(Structure.class.isAssignableFrom(klass)) {
119119
boolean writeWorked = false;
120120
try {
121-
Structure struct = (Structure) klass.newInstance();
121+
Structure struct = Structure.newInstance((Class<? extends Structure>) klass);
122122
struct.write();
123123
writeWorked = true;
124124
} catch (java.lang.Throwable ex) {

src/com/sun/jna/IntegerType.java

+5-13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
package com.sun.jna;
2626

27+
import java.lang.reflect.InvocationTargetException;
28+
2729
/**
2830
* Represents a native integer value, which may have a platform-specific size
2931
* (e.g. <code>long</code> on unix-based platforms).
@@ -116,19 +118,9 @@ public Object fromNative(Object nativeValue, FromNativeContext context) {
116118
// be forgiving of null values read from memory
117119
long value = nativeValue == null
118120
? 0 : ((Number) nativeValue).longValue();
119-
try {
120-
IntegerType number = getClass().newInstance();
121-
number.setValue(value);
122-
return number;
123-
}
124-
catch (InstantiationException e) {
125-
throw new IllegalArgumentException("Can't instantiate "
126-
+ getClass());
127-
}
128-
catch (IllegalAccessException e) {
129-
throw new IllegalArgumentException("Not allowed to instantiate "
130-
+ getClass());
131-
}
121+
IntegerType number = Klass.newInstance(getClass());
122+
number.setValue(value);
123+
return number;
132124
}
133125

134126
@Override

src/com/sun/jna/Klass.java

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Copyright (c) 2018 Matthias Bläsing
2+
*
3+
* The contents of this file is dual-licensed under 2
4+
* alternative Open Source/Free licenses: LGPL 2.1 or later and
5+
* Apache License 2.0. (starting with JNA version 4.0.0).
6+
*
7+
* You can freely decide which license you want to apply to
8+
* the project.
9+
*
10+
* You may obtain a copy of the LGPL License at:
11+
*
12+
* http://www.gnu.org/licenses/licenses.html
13+
*
14+
* A copy is also included in the downloadable source code package
15+
* containing JNA, in file "LGPL2.1".
16+
*
17+
* You may obtain a copy of the Apache License at:
18+
*
19+
* http://www.apache.org/licenses/
20+
*
21+
* A copy is also included in the downloadable source code package
22+
* containing JNA, in file "AL2.0".
23+
*/
24+
25+
package com.sun.jna;
26+
27+
import java.lang.reflect.InvocationTargetException;
28+
29+
abstract class Klass {
30+
31+
private Klass() {
32+
}
33+
34+
/**
35+
* Create a new instance for the given {@code klass}. Runtime exceptions
36+
* thrown from the constructor are rethrown, all other exceptions
37+
* generated from the reflective call are wrapped into a
38+
* {@link java.lang.IllegalArgumentException} and rethrown.
39+
*
40+
* @param klass desired class to instantiate
41+
* @return the new instance
42+
* @throws IllegalArgumentException if the instantiation fails
43+
* @throws RuntimeException if the constructor for {@code klass} throws
44+
* a runtime exception
45+
*/
46+
public static <T> T newInstance(Class<T> klass) {
47+
try {
48+
return klass.getDeclaredConstructor().newInstance();
49+
} catch (IllegalAccessException e) {
50+
String msg = "Can't create an instance of " + klass
51+
+ ", requires a public no-arg constructor: " + e;
52+
throw new IllegalArgumentException(msg, e);
53+
} catch (IllegalArgumentException e) {
54+
String msg = "Can't create an instance of " + klass
55+
+ ", requires a public no-arg constructor: " + e;
56+
throw new IllegalArgumentException(msg, e);
57+
} catch (InstantiationException e) {
58+
String msg = "Can't create an instance of " + klass
59+
+ ", requires a public no-arg constructor: " + e;
60+
throw new IllegalArgumentException(msg, e);
61+
} catch (NoSuchMethodException e) {
62+
String msg = "Can't create an instance of " + klass
63+
+ ", requires a public no-arg constructor: " + e;
64+
throw new IllegalArgumentException(msg, e);
65+
} catch (SecurityException e) {
66+
String msg = "Can't create an instance of " + klass
67+
+ ", requires a public no-arg constructor: " + e;
68+
throw new IllegalArgumentException(msg, e);
69+
} catch (InvocationTargetException e) {
70+
if (e.getCause() instanceof RuntimeException) {
71+
throw (RuntimeException) e.getCause();
72+
} else {
73+
String msg = "Can't create an instance of " + klass
74+
+ ", requires a public no-arg constructor: " + e;
75+
throw new IllegalArgumentException(msg, e);
76+
}
77+
}
78+
}
79+
}

src/com/sun/jna/NativeMappedConverter.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.lang.ref.Reference;
2727
import java.lang.ref.SoftReference;
28+
import java.lang.reflect.InvocationTargetException;
2829
import java.util.Map;
2930
import java.util.WeakHashMap;
3031

@@ -61,18 +62,9 @@ public NativeMapped defaultValue() {
6162
return (NativeMapped) type.getEnumConstants()[0];
6263
}
6364

64-
try {
65-
return (NativeMapped)type.newInstance();
66-
} catch (InstantiationException e) {
67-
String msg = "Can't create an instance of " + type
68-
+ ", requires a no-arg constructor: " + e;
69-
throw new IllegalArgumentException(msg);
70-
} catch (IllegalAccessException e) {
71-
String msg = "Not allowed to create an instance of " + type
72-
+ ", requires a public, no-arg constructor: " + e;
73-
throw new IllegalArgumentException(msg);
74-
}
65+
return (NativeMapped) Klass.newInstance(type);
7566
}
67+
7668
@Override
7769
public Object fromNative(Object nativeValue, FromNativeContext context) {
7870
return instance.fromNative(nativeValue, context);

src/com/sun/jna/PointerType.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
package com.sun.jna;
2525

26+
import java.lang.reflect.InvocationTargetException;
27+
2628
/** Type representing a type-safe native pointer.
2729
* Derived classes may override the {@link NativeMapped#fromNative} method,
2830
* which should instantiate a new object (or look up an existing one)
@@ -78,17 +80,9 @@ public Object fromNative(Object nativeValue, FromNativeContext context) {
7880
if (nativeValue == null) {
7981
return null;
8082
}
81-
try {
82-
PointerType pt = getClass().newInstance();
83-
pt.pointer = (Pointer)nativeValue;
84-
return pt;
85-
}
86-
catch (InstantiationException e) {
87-
throw new IllegalArgumentException("Can't instantiate " + getClass());
88-
}
89-
catch (IllegalAccessException e) {
90-
throw new IllegalArgumentException("Not allowed to instantiate " + getClass());
91-
}
83+
PointerType pt = Klass.newInstance(getClass());
84+
pt.pointer = (Pointer)nativeValue;
85+
return pt;
9286
}
9387

9488
/** The hash code for a <code>PointerType</code> is the same as that for

src/com/sun/jna/Structure.java

+6-16
Original file line numberDiff line numberDiff line change
@@ -1864,28 +1864,18 @@ public static <T extends Structure> T newInstance(Class<T> type, Pointer init) t
18641864
return s;
18651865
}
18661866

1867-
/** Create a new Structure instance of the given type
1867+
/**
1868+
* Create a new Structure instance of the given type
18681869
* @param type desired Structure type
18691870
* @return the new instance
18701871
* @throws IllegalArgumentException if the instantiation fails
18711872
*/
18721873
public static <T extends Structure> T newInstance(Class<T> type) throws IllegalArgumentException {
1873-
try {
1874-
T s = type.newInstance();
1875-
if (s instanceof ByValue) {
1876-
s.allocateMemory();
1877-
}
1878-
return s;
1879-
}
1880-
catch(InstantiationException e) {
1881-
String msg = "Can't instantiate " + type;
1882-
throw new IllegalArgumentException(msg, e);
1883-
}
1884-
catch(IllegalAccessException e) {
1885-
String msg = "Instantiation of " + type
1886-
+ " not allowed, is it public?";
1887-
throw new IllegalArgumentException(msg, e);
1874+
T s = Klass.newInstance(type);
1875+
if (s instanceof ByValue) {
1876+
s.allocateMemory();
18881877
}
1878+
return s;
18891879
}
18901880

18911881
/** Keep track of the largest aggregate field of the union to use for

test/com/sun/jna/StructureTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ private void testAlignStruct(String prefix,int index) {
390390
IntByReference offset = new IntByReference();
391391
LongByReference value = new LongByReference();
392392
Class<?> cls = Class.forName(getClass().getName() + "$" + prefix + "TestStructure" + index);
393-
Structure s = (Structure)cls.newInstance();
393+
Structure s = Structure.newInstance((Class<? extends Structure>) cls);
394394
int result = lib.testStructureAlignment(s, index, offset, value);
395395
assertEquals("Wrong native value at field " + result
396396
+ "=0x" + Long.toHexString(value.getValue())

test/com/sun/jna/WebStartTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ private static void sendResults(Throwable t, int port) throws IOException {
417417
}
418418

419419
private static Throwable runTestCaseTest(String testClass, String method, int port) throws Exception {
420-
TestCase test = (TestCase)Class.forName(testClass).newInstance();
420+
TestCase test = (TestCase)Class.forName(testClass).getConstructor().newInstance();
421421
test.setName(method);
422422
TestResult result = new TestResult();
423423
test.run(result);

0 commit comments

Comments
 (0)