Skip to content

Commit 80847e4

Browse files
Replace deprecated Class#newInstance calls
1 parent 01b6f67 commit 80847e4

File tree

8 files changed

+124
-34
lines changed

8 files changed

+124
-34
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

+31-9
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).
@@ -117,17 +119,37 @@ public Object fromNative(Object nativeValue, FromNativeContext context) {
117119
long value = nativeValue == null
118120
? 0 : ((Number) nativeValue).longValue();
119121
try {
120-
IntegerType number = getClass().newInstance();
122+
IntegerType number = getClass().getDeclaredConstructor().newInstance();
121123
number.setValue(value);
122124
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());
125+
} catch (IllegalAccessException e) {
126+
String msg = "Can't create an instance of " + getClass()
127+
+ ", requires a public no-arg constructor: " + e;
128+
throw new IllegalArgumentException(msg, e);
129+
} catch (IllegalArgumentException e) {
130+
String msg = "Can't create an instance of " + getClass()
131+
+ ", requires a public no-arg constructor: " + e;
132+
throw new IllegalArgumentException(msg, e);
133+
} catch (InstantiationException e) {
134+
String msg = "Can't create an instance of " + getClass()
135+
+ ", requires a public no-arg constructor: " + e;
136+
throw new IllegalArgumentException(msg, e);
137+
} catch (NoSuchMethodException e) {
138+
String msg = "Can't create an instance of " + getClass()
139+
+ ", requires a public no-arg constructor: " + e;
140+
throw new IllegalArgumentException(msg, e);
141+
} catch (SecurityException e) {
142+
String msg = "Can't create an instance of " + getClass()
143+
+ ", requires a public no-arg constructor: " + e;
144+
throw new IllegalArgumentException(msg, e);
145+
} catch (InvocationTargetException e) {
146+
if(e.getCause() instanceof RuntimeException) {
147+
throw (RuntimeException) e.getCause();
148+
} else {
149+
String msg = "Can't create an instance of " + getClass()
150+
+ ", requires a public no-arg constructor: " + e;
151+
throw new IllegalArgumentException(msg, e);
152+
}
131153
}
132154
}
133155

src/com/sun/jna/NativeMappedConverter.java

+30-7
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

@@ -62,15 +63,37 @@ public NativeMapped defaultValue() {
6263
}
6364

6465
try {
65-
return (NativeMapped)type.newInstance();
66+
return (NativeMapped)type
67+
.getDeclaredConstructor()
68+
.newInstance();
69+
} catch (IllegalAccessException e) {
70+
String msg = "Can't create an instance of " + type
71+
+ ", requires a public no-arg constructor: " + e;
72+
throw new IllegalArgumentException(msg, e);
73+
} catch (IllegalArgumentException e) {
74+
String msg = "Can't create an instance of " + type
75+
+ ", requires a public no-arg constructor: " + e;
76+
throw new IllegalArgumentException(msg, e);
6677
} catch (InstantiationException e) {
6778
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);
79+
+ ", requires a public no-arg constructor: " + e;
80+
throw new IllegalArgumentException(msg, e);
81+
} catch (NoSuchMethodException e) {
82+
String msg = "Can't create an instance of " + type
83+
+ ", requires a public no-arg constructor: " + e;
84+
throw new IllegalArgumentException(msg, e);
85+
} catch (SecurityException e) {
86+
String msg = "Can't create an instance of " + type
87+
+ ", requires a public no-arg constructor: " + e;
88+
throw new IllegalArgumentException(msg, e);
89+
} catch (InvocationTargetException e) {
90+
if(e.getCause() instanceof RuntimeException) {
91+
throw (RuntimeException) e.getCause();
92+
} else {
93+
String msg = "Can't create an instance of " + type
94+
+ ", requires a public no-arg constructor: " + e;
95+
throw new IllegalArgumentException(msg, e);
96+
}
7497
}
7598
}
7699
@Override

src/com/sun/jna/PointerType.java

+31-7
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)
@@ -79,15 +81,37 @@ public Object fromNative(Object nativeValue, FromNativeContext context) {
7981
return null;
8082
}
8183
try {
82-
PointerType pt = getClass().newInstance();
84+
PointerType pt = getClass().getDeclaredConstructor().newInstance();
8385
pt.pointer = (Pointer)nativeValue;
8486
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());
87+
} catch (IllegalAccessException e) {
88+
String msg = "Can't create an instance of " + getClass()
89+
+ ", requires a public no-arg constructor: " + e;
90+
throw new IllegalArgumentException(msg, e);
91+
} catch (IllegalArgumentException e) {
92+
String msg = "Can't create an instance of " + getClass()
93+
+ ", requires a public no-arg constructor: " + e;
94+
throw new IllegalArgumentException(msg, e);
95+
} catch (InstantiationException e) {
96+
String msg = "Can't create an instance of " + getClass()
97+
+ ", requires a public no-arg constructor: " + e;
98+
throw new IllegalArgumentException(msg, e);
99+
} catch (NoSuchMethodException e) {
100+
String msg = "Can't create an instance of " + getClass()
101+
+ ", requires a public no-arg constructor: " + e;
102+
throw new IllegalArgumentException(msg, e);
103+
} catch (SecurityException e) {
104+
String msg = "Can't create an instance of " + getClass()
105+
+ ", requires a public no-arg constructor: " + e;
106+
throw new IllegalArgumentException(msg, e);
107+
} catch (InvocationTargetException e) {
108+
if(e.getCause() instanceof RuntimeException) {
109+
throw (RuntimeException) e.getCause();
110+
} else {
111+
String msg = "Can't create an instance of " + getClass()
112+
+ ", requires a public no-arg constructor: " + e;
113+
throw new IllegalArgumentException(msg, e);
114+
}
91115
}
92116
}
93117

src/com/sun/jna/Structure.java

+28-8
Original file line numberDiff line numberDiff line change
@@ -1871,20 +1871,40 @@ public static <T extends Structure> T newInstance(Class<T> type, Pointer init) t
18711871
*/
18721872
public static <T extends Structure> T newInstance(Class<T> type) throws IllegalArgumentException {
18731873
try {
1874-
T s = type.newInstance();
1874+
Constructor<T> constructor = type.getDeclaredConstructor();
1875+
T s = constructor.newInstance();
18751876
if (s instanceof ByValue) {
18761877
s.allocateMemory();
18771878
}
18781879
return s;
1879-
}
1880-
catch(InstantiationException e) {
1881-
String msg = "Can't instantiate " + type;
1880+
} catch (IllegalAccessException e) {
1881+
String msg = "Can't create an instance of " + type
1882+
+ ", requires a public no-arg constructor: " + e;
18821883
throw new IllegalArgumentException(msg, e);
1883-
}
1884-
catch(IllegalAccessException e) {
1885-
String msg = "Instantiation of " + type
1886-
+ " not allowed, is it public?";
1884+
} catch (IllegalArgumentException e) {
1885+
String msg = "Can't create an instance of " + type
1886+
+ ", requires a public no-arg constructor: " + e;
18871887
throw new IllegalArgumentException(msg, e);
1888+
} catch (InstantiationException e) {
1889+
String msg = "Can't create an instance of " + type
1890+
+ ", requires a public no-arg constructor: " + e;
1891+
throw new IllegalArgumentException(msg, e);
1892+
} catch (NoSuchMethodException e) {
1893+
String msg = "Can't create an instance of " + type
1894+
+ ", requires a public no-arg constructor: " + e;
1895+
throw new IllegalArgumentException(msg, e);
1896+
} catch (SecurityException e) {
1897+
String msg = "Can't create an instance of " + type
1898+
+ ", requires a public no-arg constructor: " + e;
1899+
throw new IllegalArgumentException(msg, e);
1900+
} catch (InvocationTargetException e) {
1901+
if(e.getCause() instanceof RuntimeException) {
1902+
throw (RuntimeException) e.getCause();
1903+
} else {
1904+
String msg = "Can't create an instance of " + type
1905+
+ ", requires a public no-arg constructor: " + e;
1906+
throw new IllegalArgumentException(msg, e);
1907+
}
18881908
}
18891909
}
18901910

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)