Skip to content

Commit

Permalink
Replace deprecated Class#newInstance calls
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasblaesing committed Nov 1, 2018
1 parent 01b6f67 commit 616ec37
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=============
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Structure>) klass);
struct.write();
writeWorked = true;
} catch (java.lang.Throwable ex) {
Expand Down
18 changes: 5 additions & 13 deletions src/com/sun/jna/IntegerType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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. <code>long</code> on unix-based platforms).
Expand Down Expand Up @@ -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
Expand Down
79 changes: 79 additions & 0 deletions src/com/sun/jna/Klass.java
Original file line number Diff line number Diff line change
@@ -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> T newInstance(Class<T> 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);
}
}
}
}
14 changes: 3 additions & 11 deletions src/com/sun/jna/NativeMappedConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
16 changes: 5 additions & 11 deletions src/com/sun/jna/PointerType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 <code>PointerType</code> is the same as that for
Expand Down
22 changes: 6 additions & 16 deletions src/com/sun/jna/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -1864,28 +1864,18 @@ public static <T extends Structure> T newInstance(Class<T> 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 extends Structure> T newInstance(Class<T> 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
Expand Down
2 changes: 1 addition & 1 deletion test/com/sun/jna/StructureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Structure>) cls);
int result = lib.testStructureAlignment(s, index, offset, value);
assertEquals("Wrong native value at field " + result
+ "=0x" + Long.toHexString(value.getValue())
Expand Down
2 changes: 1 addition & 1 deletion test/com/sun/jna/WebStartTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 616ec37

Please sign in to comment.