8000 Use JDK7 and JDK8 Code Features (#1564) · java-native-access/jna@9e5243f · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e5243f

Browse files
authored
Use JDK7 and JDK8 Code Features (#1564)
* Use Diamond Operator except for anonymous inner classes * Use try-with-resources * Use Multi-catch * Use constants for system properties * Change Charset displayName to Name to use canonical encoding Signed-off-by: Daniel Widdis <widdis@gmail.com> --------- Signed-off-by: Daniel Widdis <widdis@gmail.com>
1 parent 0e1f5d4 commit 9e5243f

30 files changed

+123
-166
lines changed

src/com/sun/jna/CallbackReference.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ public class CallbackReference extends WeakReference<Callback> implements Closea
5353

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

6868
static {
@@ -87,7 +87,7 @@ public class CallbackReference extends WeakReference<Callback> implements Closea
8787
}
8888
}
8989

90-
private static final Map<Callback, CallbackThreadInitializer> initializers = new WeakHashMap<Callback, CallbackThreadInitializer>();
90+
private static final Map<Callback, CallbackThreadInitializer> initializers = new WeakHashMap<>();
9191
/**
9292
* @param cb The {@link Callback} instance
9393
* @param initializer The {@link CallbackThreadInitializer} - if {@code null} then the
@@ -211,14 +211,14 @@ private static Reference<Callback>[] addCallbackToArray(Callback cb,Reference<Ca
211211
}
212212
}
213213
}
214-
newArray[nidx] = new WeakReference<Callback>(cb);
214+
newArray[nidx] = new WeakReference<>(cb);
215215
return newArray;
216216
}
217217

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

407407
// Remove Object methods disallowed as callback method names
@@ -456,7 +456,7 @@ protected void dispose() {
456456
/** Dispose of all memory allocated for callbacks. */
457457
static void disposeAll() {
458458
// use a copy since dispose() modifes the map
459-
Collection<Reference<CallbackReference>> refs = new LinkedList<Reference<CallbackReference>>(allocatedMemory.values());
459+
Collection<Reference<CallbackReference>> refs = new LinkedList<>(allocatedMemory.values());
460460
for (Reference<CallbackReference> r : refs) {
461461
CallbackReference ref = r.get();
462462
if(ref != null) {
@@ -584,10 +584,7 @@ private Object invokeCallback(Object[] args) {
584584
try {
585585
result = convertResult(callbackMethod.invoke(cb, callbackArgs));
586586
}
587-
catch (IllegalArgumentException e) {
588-
Native.getCallbackExceptionHandler().uncaughtException(cb, e);
589-
}
590-
catch (IllegalAccessException e) {
587+
catch (IllegalArgumentException | IllegalAccessException e) {
591588
Native.getCallbackExceptionHandler().uncaughtException(cb, e);
592589
}
593590
catch (InvocationTargetException e) {

src/com/sun/jna/DefaultTypeMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public Entry(Class<?> type, Object converter) {
5252
}
5353
}
5454

55-
private List<Entry> toNativeConverters = new ArrayList<Entry>();
56-
private List<Entry> fromNativeConverters = new ArrayList<Entry>();
55+
private List<Entry> toNativeConverters = new ArrayList<>();
56+
private List<Entry> fromNativeConverters = new ArrayList<>();
5757

5858
private Class<?> getAltClass(Class<?> cls) {
5959
if (cls == Boolean.class) {

src/com/sun/jna/ELFAnalyser.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ private void parseEabiAapcsVfp(ByteBuffer headerData, RandomAccessFile raf) thro
230230
}
231231

232232
static class ELFSectionHeaders {
233-
private final List<ELFSectionHeaderEntry> entries = new ArrayList<ELFSectionHeaderEntry>();
233+
private final List<ELFSectionHeaderEntry> entries = new ArrayList<>();
234234

235235
public ELFSectionHeaders(boolean _64bit, boolean bigEndian, ByteBuffer headerData, RandomAccessFile raf) throws IOException {
236236
long shoff;
@@ -401,9 +401,9 @@ public boolean equals(Object obj) {
401401
return true;
402402
}
403403

404-
private static final List<ArmAeabiAttributesTag> tags = new LinkedList<ArmAeabiAttributesTag>();
405-
private static final Map<Integer, ArmAeabiAttributesTag> valueMap = new HashMap<Integer, ArmAeabiAttributesTag>();
406-
private static final Map<String, ArmAeabiAttributesTag> nameMap = new HashMap<String, ArmAeabiAttributesTag>();
404+
private static final List<ArmAeabiAttributesTag> tags = new LinkedList<>();
405+
private static final Map<Integer, ArmAeabiAttributesTag> valueMap = new HashMap<>();
406+
private static final Map<String, ArmAeabiAttributesTag> nameMap = new HashMap<>();
407407

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

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

536536
private static Map<ArmAeabiAttributesTag, Object> parseFileAttribute(ByteBuffer bb) {
537-
Map<ArmAeabiAttributesTag, Object> result = new HashMap<ArmAeabiAttributesTag, Object>();
537+
Map<ArmAeabiAttributesTag, Object> result = new HashMap<>();
538538
while (bb.position() < bb.limit()) {
539539
int tagValue = readULEB128(bb).intValue();
540540
ArmAeabiAttributesTag tag = ArmAeabiAttributesTag.getByValue(tagValue);

src/com/sun/jna/Klass.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,7 @@ private Klass() {
4646
public static <T> T newInstance(Class<T> klass) {
4747
try {
4848
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 (N F43B oSuchMethodException 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) {
49+
} catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException e) {
6650
String msg = "Can't create an instance of " + klass
6751
+ ", requires a public no-arg constructor: " + e;
6852
throw new IllegalArgumentException(msg, e);

src/com/sun/jna/Library.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private static final class FunctionInfo {
172172
// Library invocation options
173173
private final Map<String, Object> options;
174174
private final InvocationMapper invocationMapper;
175-
private final Map<Method, FunctionInfo> functions = new WeakHashMap<Method, FunctionInfo>();
175+
private final Map<Method, FunctionInfo> functions = new WeakHashMap<>();
176176
public Handler(String libname, Class<?> interfaceClass, Map<String, ?> options) {
177177

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

186186
this.interfaceClass = interfaceClass;
187-
this.options = new HashMap<String, Object>(options);
187+
this.options = new HashMap<>(options);
188188
int callingConvention = AltCallingConvention.class.isAssignableFrom(interfaceClass)
189189
? Function.ALT_CONVENTION
190190
: Function.C_CONVENTION;
@@ -247,7 +247,7 @@ public Object invoke(Object proxy, Method method, Object[] inArgs)
247247
// Find the function to invoke
248248
function = nativeLibrary.getFunction(method.getName(), method);
249249
parameterTypes = method.getParameterTypes();
250-
options = new HashMap<String, Object>(this.options);
250+
options = new HashMap<>(this.options);
251251
options.put(Function.OPTION_INVOKING_METHOD, method);
252252
}
253253
f = new FunctionInfo(handler, function, parameterTypes, isVarArgs, options);

src/com/sun/jna/Memory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
public class Memory extends Pointer implements Closeable {
5555
/** Keep track of all allocated memory so we can dispose of it before unloading. */
5656
private static final Map<Long, Reference<Memory>> allocatedMemory =
57-
new ConcurrentHashMap<Long, Reference<Memory>>();
57+
new ConcurrentHashMap<>();
5858

5959
private static final WeakMemoryHolder buffers = new WeakMemoryHolder();
6060

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

121-
allocatedMemory.put(peer, new WeakReference<Memory>(this));
121+
allocatedMemory.put(peer, new WeakReference<>(this));
122122
cleanable = Cleaner.getCleaner().register(this, new MemoryDisposer(peer));
123123
}
124124

src/com/sun/jna/Native.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ static boolean isCompatibleVersion(String expectedVersion, String nativeVersion)
221221
loadNativeDispatchLibrary();
222222

223223
if (! isCompatibleVersion(VERSION_NATIVE, getNativeVersion())) {
224-
String LS = System.getProperty("line.separator");
224+
String LS = System.lineSeparator();
225225
throw new Error(LS + LS
226226
+ "There is an incompatible JNA native library installed on this system" + LS
227227
+ "Expected: " + VERSION_NATIVE + LS
@@ -397,11 +397,7 @@ private static Charset getCharset(String encoding) {
397397
try {
398398
charset = Charset.forName(encoding);
399399
}
400-
catch(IllegalCharsetNameException e) {
401-
LOG.log(Level.WARNING, "JNA Warning: Encoding ''{0}'' is unsupported ({1})",
402-
new Object[]{encoding, e.getMessage()});
403-
}
404-
catch(UnsupportedCharsetException e) {
400+
catch(IllegalCharsetNameException | UnsupportedCharsetException e) {
405401
LOG.log(Level.WARNING, "JNA Warning: Encoding ''{0}'' is unsupported ({1})",
406402
new Object[]{encoding, e.getMessage()});
407403
}
@@ -517,7 +513,7 @@ public static List<String> toStringList(char[] buf) {
517513
* @return A {@link List} of all the strings in the buffer
518514
*/
519515
public static List<String> toStringList(char[] buf, int offset, int len) {
520-
List<String> list = new ArrayList<String>();
516+
List<String> list = new ArrayList<>();
521517
int lastPos = offset;
522518
int maxPos = offset + len;
523519
for (int curPos = offset; curPos < maxPos; curPos++) {
@@ -691,7 +687,7 @@ private static void loadLibraryInstance(Class<?> cls) {
691687
&& Modifier.isStatic(field.getModifiers())) {
692688
// Ensure the field gets initialized by reading it
693689
field.setAccessible(true); // interface might be private
694-
libraries.put(cls, new WeakReference<Object>(field.get(null)));
690+
libraries.put(cls, new WeakReference<>(field.get(null)));
695691
break;
696692
}
697693
}
@@ -785,7 +781,7 @@ public static Map<String, Object> getLibraryOptions(Class<?> type) {
785781
throw new IllegalArgumentException("OPTIONS must be a public field of type java.util.Map (" + e + "): " + mappingClass);
786782
}
787783
// Make a clone of the original options
788-
libraryOptions = new HashMap<String, Object>(libraryOptions);
784+
libraryOptions = new HashMap<>(libraryOptions);
789785
if (!libraryOptions.containsKey(Library.OPTION_TYPE_MAPPER)) {
790786
libraryOptions.put(Library.OPTION_TYPE_MAPPER, lookupField(mappingClass, "TYPE_MAPPER", TypeMapper.class));
791787
}
@@ -1552,8 +1548,8 @@ public static void setCallbackThreadInitializer(Callback cb, CallbackThreadIniti
15521548
CallbackReference.setCallbackThreadInitializer(cb, initializer);
15531549
}
15541550

1555-
private static final Map<Class<?>, long[]> registeredClasses = new WeakHashMap<Class<?>, long[]>();
1556-
private static final Map<Class<?>, NativeLibrary> registeredLibraries = new WeakHashMap<Class<?>, NativeLibrary>();
1551+
private static final Map<Class<?>, long[]> registeredClasses = new WeakHashMap<>();
1552+
private static final Map<Class<?>, NativeLibrary> registeredLibraries = new WeakHashMap<>();
15571553

15581554
private static void unregisterAll() {
15591555
synchronized(registeredClasses) {
@@ -1786,7 +1782,7 @@ public static void register(Class<?> cls, String libName) {
17861782
// method name, library name, call conv
17871783
public static void register(Class<?> cls, NativeLibrary lib) {
17881784
Method[] methods = cls.getDeclaredMethods();
1789-
List<Method> mlist = new ArrayList<Method>();
1785+
List<Method> mlist = new ArrayList<>();
17901786
Map<String, ?> options = lib.getOptions();
17911787
TypeMapper mapper = (TypeMapper) options.get(Library.OPTION_TYPE_MAPPER);
17921788
boolean allowObjects = Boolean.TRUE.equals(options.get(Library.OPTION_ALLOW_OBJECTS));
@@ -1926,11 +1922,11 @@ public static void register(Class<?> cls, NativeLibrary lib) {
19261922
* looking them up later.
19271923
*/
19281924
private static Map<String, Object> cacheOptions(Class<?> cls, Map<String, ?> options, Object proxy) {
1929-
Map<String, Object> libOptions = new HashMap<String, Object>(options);
1925+
Map<String, Object> libOptions = new HashMap<>(options);
19301926
libOptions.put(_OPTION_ENCLOSING_LIBRARY, cls);
19311927
typeOptions.put(cls, libOptions);
19321928
if (proxy != null) {
1933-
libraries.put(cls, new WeakReference<Object>(proxy));
1929+
libraries.put(cls, new WeakReference<>(proxy));
19341930
}
19351931

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

0 commit comments

Comments
 (0)
0