Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JDK7 and JDK8 Code Features #1564

Merged
merged 5 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 12 additions & 15 deletions src/com/sun/jna/CallbackReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ public class CallbackReference extends WeakReference<Callback> implements Closea

// Access to callbackMap, directCallbackMap, pointerCallbackMap is protected
// by synchonizing on pointerCallbackMap
static final Map<Callback, CallbackReference> callbackMap = new WeakHashMap<Callback, CallbackReference>();
static final Map<Callback, CallbackReference> directCallbackMap = new WeakHashMap<Callback, CallbackReference>();
static final Map<Callback, CallbackReference> callbackMap = new WeakHashMap<>();
static final Map<Callback, CallbackReference> directCallbackMap = new WeakHashMap<>();
//callbacks with different signatures sharing the same pointer
static final Map<Pointer, Reference<Callback>[]> pointerCallbackMap = new WeakHashMap<Pointer, Reference<Callback>[]>();
static final Map<Pointer, Reference<Callback>[]> pointerCallbackMap = new WeakHashMap<>();
// Track memory allocations associated with this closure (usually String args)
static final Map<Object, Object> allocations =
Collections.synchronizedMap(new WeakHashMap<Object, Object>());
Collections.synchronizedMap(new WeakHashMap<>());
// Global map of allocated closures to facilitate centralized cleanup
private static final Map<Long, Reference<CallbackReference>> allocatedMemory =
new ConcurrentHashMap<Long, Reference<CallbackReference>>();
new ConcurrentHashMap<>();
private static final Method PROXY_CALLBACK_METHOD;

static {
Expand All @@ -87,7 +87,7 @@ public class CallbackReference extends WeakReference<Callback> implements Closea
}
}

private static final Map<Callback, CallbackThreadInitializer> initializers = new WeakHashMap<Callback, CallbackThreadInitializer>();
private static final Map<Callback, CallbackThreadInitializer> initializers = new WeakHashMap<>();
/**
* @param cb The {@link Callback} instance
* @param initializer The {@link CallbackThreadInitializer} - if {@code null} then the
Expand Down Expand Up @@ -211,14 +211,14 @@ private static Reference<Callback>[] addCallbackToArray(Callback cb,Reference<Ca
}
}
}
newArray[nidx] = new WeakReference<Callback>(cb);
newArray[nidx] = new WeakReference<>(cb);
return newArray;
}

private static Callback createCallback(Class<?> type, Pointer p) {
int ctype = AltCallingConvention.class.isAssignableFrom(type)
? Function.ALT_CONVENTION : Function.C_CONVENTION;
Map<String, Object> foptions = new HashMap<String, Object>(Native.getLibraryOptions(type));
Map<String, Object> foptions = new HashMap<>(Native.getLibraryOptions(type));
foptions.put(Function.OPTION_INVOKING_METHOD, getCallbackMethod(type));
NativeFunctionHandler h = new NativeFunctionHandler(p, ctype, foptions);
return (Callback)Proxy.newProxyInstance(type.getClassLoader(), new Class[] { type }, h);
Expand Down Expand Up @@ -330,7 +330,7 @@ private CallbackReference(Callback callback, int callingConvention, boolean dire
}
cbstruct = peer != 0 ? new Pointer(peer) : null;
if(peer != 0) {
allocatedMemory.put(peer, new WeakReference<CallbackReference>(this));
allocatedMemory.put(peer, new WeakReference<>(this));
cleanable = Cleaner.getCleaner().register(this, new CallbackReferenceDisposer(cbstruct));
}
}
Expand Down Expand Up @@ -401,7 +401,7 @@ private static Method getCallbackMethod(Class<?> cls) {
// Look at only public methods defined by the Callback class
Method[] pubMethods = cls.getDeclaredMethods();
Method[] classMethods = cls.getMethods();
Set<Method> pmethods = new HashSet<Method>(Arrays.asList(pubMethods));
Set<Method> pmethods = new HashSet<>(Arrays.asList(pubMethods));
pmethods.retainAll(Arrays.asList(classMethods));

// Remove Object methods disallowed as callback method names
Expand Down Expand Up @@ -456,7 +456,7 @@ protected void dispose() {
/** Dispose of all memory allocated for callbacks. */
static void disposeAll() {
// use a copy since dispose() modifes the map
Collection<Reference<CallbackReference>> refs = new LinkedList<Reference<CallbackReference>>(allocatedMemory.values());
Collection<Reference<CallbackReference>> refs = new LinkedList<>(allocatedMemory.values());
for (Reference<CallbackReference> r : refs) {
CallbackReference ref = r.get();
if(ref != null) {
Expand Down Expand Up @@ -584,10 +584,7 @@ private Object invokeCallback(Object[] args) {
try {
result = convertResult(callbackMethod.invoke(cb, callbackArgs));
}
catch (IllegalArgumentException e) {
Native.getCallbackExceptionHandler().uncaughtException(cb, e);
}
catch (IllegalAccessException e) {
catch (IllegalArgumentException | IllegalAccessException e) {
Native.getCallbackExceptionHandler().uncaughtException(cb, e);
}
catch (InvocationTargetException e) {
Expand Down
4 changes: 2 additions & 2 deletions src/com/sun/jna/DefaultTypeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public Entry(Class<?> type, Object converter) {
}
}

private List<Entry> toNativeConverters = new ArrayList<Entry>();
private List<Entry> fromNativeConverters = new ArrayList<Entry>();
private List<Entry> toNativeConverters = new ArrayList<>();
private List<Entry> fromNativeConverters = new ArrayList<>();

private Class<?> getAltClass(Class<?> cls) {
if (cls == Boolean.class) {
Expand Down
12 changes: 6 additions & 6 deletions src/com/sun/jna/ELFAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private void parseEabiAapcsVfp(ByteBuffer headerData, RandomAccessFile raf) thro
}

static class ELFSectionHeaders {
private final List<ELFSectionHeaderEntry> entries = new ArrayList<ELFSectionHeaderEntry>();
private final List<ELFSectionHeaderEntry> entries = new ArrayList<>();

public ELFSectionHeaders(boolean _64bit, boolean bigEndian, ByteBuffer headerData, RandomAccessFile raf) throws IOException {
long shoff;
Expand Down Expand Up @@ -401,9 +401,9 @@ public boolean equals(Object obj) {
return true;
}

private static final List<ArmAeabiAttributesTag> tags = new LinkedList<ArmAeabiAttributesTag>();
private static final Map<Integer, ArmAeabiAttributesTag> valueMap = new HashMap<Integer, ArmAeabiAttributesTag>();
private static final Map<String, ArmAeabiAttributesTag> nameMap = new HashMap<String, ArmAeabiAttributesTag>();
private static final List<ArmAeabiAttributesTag> tags = new LinkedList<>();
private static final Map<Integer, ArmAeabiAttributesTag> valueMap = new HashMap<>();
private static final Map<String, ArmAeabiAttributesTag> nameMap = new HashMap<>();

// Enumerated from ARM IHI 0045E, 2.5 Attributes summary and history
public static final ArmAeabiAttributesTag File = addTag(1, "File", ParameterType.UINT32);
Expand Down Expand Up @@ -520,7 +520,7 @@ private static Map<Integer, Map<ArmAeabiAttributesTag, Object>> parseArmAttribut
}

private static Map<Integer, Map<ArmAeabiAttributesTag, Object>> parseAEABI(ByteBuffer buffer) {
Map<Integer, Map<ArmAeabiAttributesTag, Object>> data = new HashMap<Integer, Map<ArmAeabiAttributesTag, Object>>();
Map<Integer, Map<ArmAeabiAttributesTag, Object>> data = new HashMap<>();
while (buffer.position() < buffer.limit()) {
int pos = buffer.position();
int subsectionTag = readULEB128(buffer).intValue();
Expand All @@ -534,7 +534,7 @@ private static Map<Integer, Map<ArmAeabiAttributesTag, Object>> parseAEABI(ByteB
}

private static Map<ArmAeabiAttributesTag, Object> parseFileAttribute(ByteBuffer bb) {
Map<ArmAeabiAttributesTag, Object> result = new HashMap<ArmAeabiAttributesTag, Object>();
Map<ArmAeabiAttributesTag, Object> result = new HashMap<>();
while (bb.position() < bb.limit()) {
int tagValue = readULEB128(bb).intValue();
ArmAeabiAttributesTag tag = ArmAeabiAttributesTag.getByValue(tagValue);
Expand Down
18 changes: 1 addition & 17 deletions src/com/sun/jna/Klass.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,7 @@ private Klass() {
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) {
} catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException e) {
String msg = "Can't create an instance of " + klass
+ ", requires a public no-arg constructor: " + e;
throw new IllegalArgumentException(msg, e);
Expand Down
6 changes: 3 additions & 3 deletions src/com/sun/jna/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private static final class FunctionInfo {
// Library invocation options
private final Map<String, Object> options;
private final InvocationMapper invocationMapper;
private final Map<Method, FunctionInfo> functions = new WeakHashMap<Method, FunctionInfo>();
private final Map<Method, FunctionInfo> functions = new WeakHashMap<>();
public Handler(String libname, Class<?> interfaceClass, Map<String, ?> options) {

if (libname != null && "".equals(libname.trim())) {
Expand All @@ -184,7 +184,7 @@ public Handler(String libname, Class<?> interfaceClass, Map<String, ?> options)
}

this.interfaceClass = interfaceClass;
this.options = new HashMap<String, Object>(options);
this.options = new HashMap<>(options);
int callingConvention = AltCallingConvention.class.isAssignableFrom(interfaceClass)
? Function.ALT_CONVENTION
: Function.C_CONVENTION;
Expand Down Expand Up @@ -247,7 +247,7 @@ public Object invoke(Object proxy, Method method, Object[] inArgs)
// Find the function to invoke
function = nativeLibrary.getFunction(method.getName(), method);
parameterTypes = method.getParameterTypes();
options = new HashMap<String, Object>(this.options);
options = new HashMap<>(this.options);
options.put(Function.OPTION_INVOKING_METHOD, method);
}
f = new FunctionInfo(handler, function, parameterTypes, isVarArgs, options);
Expand Down
6 changes: 3 additions & 3 deletions src/com/sun/jna/Memory.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
public class Memory extends Pointer implements Closeable {
/** Keep track of all allocated memory so we can dispose of it before unloading. */
private static final Map<Long, Reference<Memory>> allocatedMemory =
new ConcurrentHashMap<Long, Reference<Memory>>();
new ConcurrentHashMap<>();

private static final WeakMemoryHolder buffers = new WeakMemoryHolder();

Expand All @@ -68,7 +68,7 @@ public static void purge() {
/** Dispose of all allocated memory. */
public static void disposeAll() {
// use a copy since dispose() modifies the map
Collection<Reference<Memory>> refs = new ArrayList<Reference<Memory>>(allocatedMemory.values());
Collection<Reference<Memory>> refs = new ArrayList<>(allocatedMemory.values());
for (Reference<Memory> r : refs) {
Memory m = r.get();
if(m != null) {
Expand Down Expand Up @@ -118,7 +118,7 @@ public Memory(long size) {
if (peer == 0)
throw new OutOfMemoryError("Cannot allocate " + size + " bytes");

allocatedMemory.put(peer, new WeakReference<Memory>(this));
allocatedMemory.put(peer, new WeakReference<>(this));
cleanable = Cleaner.getCleaner().register(this, new MemoryDisposer(peer));
}

Expand Down
24 changes: 10 additions & 14 deletions src/com/sun/jna/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ static boolean isCompatibleVersion(String expectedVersion, String nativeVersion)
loadNativeDispatchLibrary();

if (! isCompatibleVersion(VERSION_NATIVE, getNativeVersion())) {
String LS = System.getProperty("line.separator");
String LS = System.lineSeparator();
throw new Error(LS + LS
+ "There is an incompatible JNA native library installed on this system" + LS
+ "Expected: " + VERSION_NATIVE + LS
Expand Down Expand Up @@ -397,11 +397,7 @@ private static Charset getCharset(String encoding) {
try {
charset = Charset.forName(encoding);
}
catch(IllegalCharsetNameException e) {
LOG.log(Level.WARNING, "JNA Warning: Encoding ''{0}'' is unsupported ({1})",
new Object[]{encoding, e.getMessage()});
}
catch(UnsupportedCharsetException e) {
catch(IllegalCharsetNameException | UnsupportedCharsetException e) {
LOG.log(Level.WARNING, "JNA Warning: Encoding ''{0}'' is unsupported ({1})",
new Object[]{encoding, e.getMessage()});
}
Expand Down Expand Up @@ -517,7 +513,7 @@ public static List<String> toStringList(char[] buf) {
* @return A {@link List} of all the strings in the buffer
*/
public static List<String> toStringList(char[] buf, int offset, int len) {
List<String> list = new ArrayList<String>();
List<String> list = new ArrayList<>();
int lastPos = offset;
int maxPos = offset + len;
for (int curPos = offset; curPos < maxPos; curPos++) {
Expand Down Expand Up @@ -691,7 +687,7 @@ private static void loadLibraryInstance(Class<?> cls) {
&& Modifier.isStatic(field.getModifiers())) {
// Ensure the field gets initialized by reading it
field.setAccessible(true); // interface might be private
libraries.put(cls, new WeakReference<Object>(field.get(null)));
libraries.put(cls, new WeakReference<>(field.get(null)));
break;
}
}
Expand Down Expand Up @@ -785,7 +781,7 @@ public static Map<String, Object> getLibraryOptions(Class<?> type) {
throw new IllegalArgumentException("OPTIONS must be a public field of type java.util.Map (" + e + "): " + mappingClass);
}
// Make a clone of the original options
libraryOptions = new HashMap<String, Object>(libraryOptions);
libraryOptions = new HashMap<>(libraryOptions);
if (!libraryOptions.containsKey(Library.OPTION_TYPE_MAPPER)) {
libraryOptions.put(Library.OPTION_TYPE_MAPPER, lookupField(mappingClass, "TYPE_MAPPER", TypeMapper.class));
}
Expand Down Expand Up @@ -1552,8 +1548,8 @@ public static void setCallbackThreadInitializer(Callback cb, CallbackThreadIniti
CallbackReference.setCallbackThreadInitializer(cb, initializer);
}

private static final Map<Class<?>, long[]> registeredClasses = new WeakHashMap<Class<?>, long[]>();
private static final Map<Class<?>, NativeLibrary> registeredLibraries = new WeakHashMap<Class<?>, NativeLibrary>();
private static final Map<Class<?>, long[]> registeredClasses = new WeakHashMap<>();
private static final Map<Class<?>, NativeLibrary> registeredLibraries = new WeakHashMap<>();

private static void unregisterAll() {
synchronized(registeredClasses) {
Expand Down Expand Up @@ -1786,7 +1782,7 @@ public static void register(Class<?> cls, String libName) {
// method name, library name, call conv
public static void register(Class<?> cls, NativeLibrary lib) {
Method[] methods = cls.getDeclaredMethods();
List<Method> mlist = new ArrayList<Method>();
List<Method> mlist = new ArrayList<>();
Map<String, ?> options = lib.getOptions();
TypeMapper mapper = (TypeMapper) options.get(Library.OPTION_TYPE_MAPPER);
boolean allowObjects = Boolean.TRUE.equals(options.get(Library.OPTION_ALLOW_OBJECTS));
Expand Down Expand Up @@ -1926,11 +1922,11 @@ public static void register(Class<?> cls, NativeLibrary lib) {
* looking them up later.
*/
private static Map<String, Object> cacheOptions(Class<?> cls, Map<String, ?> options, Object proxy) {
Map<String, Object> libOptions = new HashMap<String, Object>(options);
Map<String, Object> libOptions = new HashMap<>(options);
libOptions.put(_OPTION_ENCLOSING_LIBRARY, cls);
typeOptions.put(cls, libOptions);
if (proxy != null) {
libraries.put(cls, new WeakReference<Object>(proxy));
libraries.put(cls, new WeakReference<>(proxy));
}

// If it's a direct mapping, AND implements a Library interface,
Expand Down