Skip to content

Commit

Permalink
Use JDK7 and JDK8 Code Features in Platform mappings (#1565)
Browse files Browse the repository at this point in the history
* Use Diamond Operator except for anonymous inner classes

* Use try-with-resources

* Use Multi-catch

* Use constants for system properties
  • Loading branch information
dbwiddis authored Nov 21, 2023
1 parent 9e5243f commit d8e55dc
Show file tree
Hide file tree
Showing 55 changed files with 164 additions and 197 deletions.
2 changes: 1 addition & 1 deletion contrib/platform/src/com/sun/jna/platform/EnumUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static <E extends Enum<E>> E fromInteger(int idx, Class<E> clazz)
public static <T extends FlagEnum> Set<T> setFromInteger(int flags, Class<T> clazz)
{
T[] vals = clazz.getEnumConstants();
Set<T> result = new HashSet<T>();
Set<T> result = new HashSet<>();

for (T val : vals)
{
Expand Down
8 changes: 4 additions & 4 deletions contrib/platform/src/com/sun/jna/platform/FileMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public String toString() {
}
}

private final Map<File, Integer> watched = new HashMap<File, Integer>();
private List<FileListener> listeners = new ArrayList<FileListener>();
private final Map<File, Integer> watched = new HashMap<>();
private List<FileListener> listeners = new ArrayList<>();

protected abstract void watch(File file, int mask, boolean recursive) throws IOException ;
protected abstract void unwatch(File file);
Expand Down Expand Up @@ -107,13 +107,13 @@ protected void notify(FileEvent e) {
}

public synchronized void addFileListener(FileListener listener) {
List<FileListener> list = new ArrayList<FileListener>(listeners);
List<FileListener> list = new ArrayList<>(listeners);
list.add(listener);
listeners = list;
}

public synchronized void removeFileListener(FileListener x) {
List<FileListener> list = new ArrayList<FileListener>(listeners);
List<FileListener> list = new ArrayList<>(listeners);
list.remove(x);
listeners = list;
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/platform/src/com/sun/jna/platform/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void moveToTrash(File... files) throws IOException {
if (!trash.exists()) {
throw new IOException("No trash location found (define fileutils.trash to be the path to the trash)");
}
List<File> failed = new ArrayList<File>();
List<File> failed = new ArrayList<>();
for (int i=0;i < files.length;i++) {
File src = files[i];
File target = new File(trash, src.getName());
Expand Down
10 changes: 5 additions & 5 deletions contrib/platform/src/com/sun/jna/platform/RasterRangesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ public static boolean outputOccupiedRanges(Raster raster, RangesOutput out) {
* @return true if the output succeeded, false otherwise
*/
public static boolean outputOccupiedRangesOfBinaryPixels(byte[] binaryBits, int w, int h, RangesOutput out) {
Set<Rectangle> rects = new HashSet<Rectangle>();
Set<Rectangle> rects = new HashSet<>();
Set<Rectangle> prevLine = Collections.<Rectangle>emptySet();
int scanlineBytes = binaryBits.length / h;
for (int row = 0; row < h; row++) {
Set<Rectangle> curLine = new TreeSet<Rectangle>(COMPARATOR);
Set<Rectangle> curLine = new TreeSet<>(COMPARATOR);
int rowOffsetBytes = row * scanlineBytes;
int startCol = -1;
// Look at each batch of 8 columns in this row
Expand Down Expand Up @@ -201,10 +201,10 @@ public static boolean outputOccupiedRangesOfBinaryPixels(byte[] binaryBits, int
* @return true if the output succeeded, false otherwise
*/
public static boolean outputOccupiedRanges(int[] pixels, int w, int h, int occupationMask, RangesOutput out) {
Set<Rectangle> rects = new HashSet<Rectangle>();
Set<Rectangle> rects = new HashSet<>();
Set<Rectangle> prevLine = Collections.<Rectangle>emptySet();
for (int row = 0; row < h; row++) {
Set<Rectangle> curLine = new TreeSet<Rectangle>(COMPARATOR);
Set<Rectangle> curLine = new TreeSet<>(COMPARATOR);
int idxOffset = row * w;
int startCol = -1;

Expand Down Expand Up @@ -241,7 +241,7 @@ public static boolean outputOccupiedRanges(int[] pixels, int w, int h, int occup
}

private static Set<Rectangle> mergeRects(Set<Rectangle> prev, Set<Rectangle> current) {
Set<Rectangle> unmerged = new HashSet<Rectangle>(prev);
Set<Rectangle> unmerged = new HashSet<>(prev);
if (!prev.isEmpty() && !current.isEmpty()) {
Rectangle[] pr = prev.toArray(new Rectangle[0]);
Rectangle[] cr = current.toArray(new Rectangle[0]);
Expand Down
10 changes: 5 additions & 5 deletions contrib/platform/src/com/sun/jna/platform/WindowUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1043,9 +1043,9 @@ private void setMask(final Component w, final Area area) {
int mode = pi.getWindingRule() == PathIterator.WIND_NON_ZERO
? WinGDI.WINDING: WinGDI.ALTERNATE;
float[] coords = new float[6];
List<POINT> points = new ArrayList<POINT>();
List<POINT> points = new ArrayList<>();
int size = 0;
List<Integer> sizes = new ArrayList<Integer>();
List<Integer> sizes = new ArrayList<>();
while (!pi.isDone()) {
int type = pi.currentSegment(coords);
if (type == PathIterator.SEG_MOVETO) {
Expand Down Expand Up @@ -1239,7 +1239,7 @@ public Dimension getIconSize(final HICON hIcon) {

@Override
public List<DesktopWindow> getAllWindows(final boolean onlyVisibleWindows) {
final List<DesktopWindow> result = new LinkedList<DesktopWindow>();
final List<DesktopWindow> result = new LinkedList<>();

final WNDENUMPROC lpEnumFunc = new WNDENUMPROC() {
@Override
Expand Down Expand Up @@ -1576,7 +1576,7 @@ private static Pixmap createBitmap(final Display dpy,
}
x11.XSetForeground(dpy, gc, new NativeLong(0));
x11.XFillRectangle(dpy, pm, gc, 0, 0, width, height);
final List<Rectangle> rlist = new ArrayList<Rectangle>();
final List<Rectangle> rlist = new ArrayList<>();
try {
RasterRangesUtils.outputOccupiedRanges(raster, new RasterRangesUtils.RangesOutput() {
@Override
Expand Down Expand Up @@ -1686,7 +1686,7 @@ private synchronized long[] getAlphaVisualIDs() {
IntByReference pcount = new IntByReference();
info = x11.XGetVisualInfo(dpy, mask, template, pcount);
if (info != null) {
List<X11.VisualID> list = new ArrayList<X11.VisualID>();
List<X11.VisualID> list = new ArrayList<>();
XVisualInfo[] infos =
(XVisualInfo[])info.toArray(pcount.getValue());
for (int i = 0; i < infos.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static void delete(String path, String name) throws IOException {
}

private static List<String> decodeStringList(ByteBuffer buffer) {
List<String> list = new ArrayList<String>();
List<String> list = new ArrayList<>();

while (buffer.hasRemaining()) {
int length = buffer.get() & 0xFF;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public void drop(DropTargetDropEvent e) {
* @return whether any of the given flavors are supported
*/
protected boolean isSupported(DataFlavor[] flavors) {
Set<DataFlavor> set = new HashSet<DataFlavor>(Arrays.asList(flavors));
Set<DataFlavor> set = new HashSet<>(Arrays.asList(flavors));
set.retainAll(acceptedFlavors);
return !set.isEmpty();
}
Expand Down
8 changes: 4 additions & 4 deletions contrib/platform/src/com/sun/jna/platform/linux/LibC.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Sysinfo extends Structure {
*/
@Override
protected List<Field> getFieldList() {
List<Field> fields = new ArrayList<Field>(super.getFieldList());
List<Field> fields = new ArrayList<>(super.getFieldList());
if (PADDING_SIZE == 0) {
Iterator<Field> fieldIterator = fields.iterator();
while (fieldIterator.hasNext()) {
Expand All @@ -88,7 +88,7 @@ protected List<Field> getFieldList() {

@Override
protected List<String> getFieldOrder() {
List<String> fieldOrder = new ArrayList<String>(super.getFieldOrder());
List<String> fieldOrder = new ArrayList<>(super.getFieldOrder());
if (PADDING_SIZE == 0) {
fieldOrder.remove("_f");
}
Expand Down Expand Up @@ -122,7 +122,7 @@ class Statvfs extends Structure {
*/
@Override
protected List<Field> getFieldList() {
List<Field> fields = new ArrayList<Field>(super.getFieldList());
List<Field> fields = new ArrayList<>(super.getFieldList());
if (NativeLong.SIZE > 4) {
Iterator<Field> fieldIterator = fields.iterator();
while (fieldIterator.hasNext()) {
Expand All @@ -137,7 +137,7 @@ protected List<Field> getFieldList() {

@Override
protected List<String> getFieldOrder() {
List<String> fieldOrder = new ArrayList<String>(super.getFieldOrder());
List<String> fieldOrder = new ArrayList<>(super.getFieldOrder());
if (NativeLong.SIZE > 4) {
fieldOrder.remove("_f_unused");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ public static void fRemoveXAttr(int fd, String name) throws IOException {
private static Collection<String> splitBufferToStrings(byte[] valueMem, String encoding)
throws IOException {
final Charset charset = Charset.forName(encoding);
final Set<String> attributesList = new LinkedHashSet<String>(1);
final Set<String> attributesList = new LinkedHashSet<>(1);
int offset = 0;
for(int i = 0; i < valueMem.length; i++) {
// each entry is terminated by a single \0 byte
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class FSRef extends Structure {

@Override
public void moveToTrash(File... files) throws IOException {
List<String> failed = new ArrayList<String>();
List<String> failed = new ArrayList<>();
for (File src: files) {
FileManager.FSRef fsref = new FileManager.FSRef();
int status = FileManager.INSTANCE.FSPathMakeRefWithOptions(src.getAbsolutePath(),
Expand Down
4 changes: 2 additions & 2 deletions contrib/platform/src/com/sun/jna/platform/mac/XAttrUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static List<String> listXAttr(String path) {
return null;

if (bufferLength == 0)
return new ArrayList<String>(0);
return new ArrayList<>(0);

Memory valueBuffer = new Memory(bufferLength);
long valueLength = XAttr.INSTANCE.listxattr(path, valueBuffer, bufferLength, 0);
Expand Down Expand Up @@ -97,7 +97,7 @@ protected static String decodeString(ByteBuffer bb) {
}

protected static List<String> decodeStringSequence(ByteBuffer bb) {
List<String> names = new ArrayList<String>();
List<String> names = new ArrayList<>();

bb.mark(); // first key starts from here
while (bb.hasRemaining()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static Map<String, Object> getOptions() {
int RTLD_MEMBER = 0x40000; // allows "lib.a(obj.o)" syntax
int RTLD_GLOBAL = 0x10000;
int RTLD_LAZY = 0x4;
Map<String, Object> options = new HashMap<String, Object>();
Map<String, Object> options = new HashMap<>();
options.put(Library.OPTION_OPEN_FLAGS, RTLD_MEMBER | RTLD_GLOBAL | RTLD_LAZY);
return Collections.unmodifiableMap(options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public static Account[] getTokenGroups(HANDLE hToken) {
tokenInformationLength.getValue(), tokenInformationLength)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
ArrayList<Account> userGroups = new ArrayList<Account>();
ArrayList<Account> userGroups = new ArrayList<>();
// make array of names
for (SID_AND_ATTRIBUTES sidAndAttribute : groups.getGroups()) {
Account group;
Expand Down Expand Up @@ -975,7 +975,7 @@ public static String[] registryGetStringArray(HKEY hKey, String value) {
* @return An array of strings corresponding to the strings in the buffer.
*/
static String[] regMultiSzBufferToStringArray(Memory data) {
ArrayList<String> result = new ArrayList<String>();
ArrayList<String> result = new ArrayList<>();
int offset = 0;
while (offset < data.size()) {
String s;
Expand Down Expand Up @@ -1995,7 +1995,7 @@ public static String[] registryGetKeys(HKEY hKey) {
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
ArrayList<String> keys = new ArrayList<String>(lpcSubKeys.getValue());
ArrayList<String> keys = new ArrayList<>(lpcSubKeys.getValue());
char[] name = new char[lpcMaxSubKeyLen.getValue() + 1];
for (int i = 0; i < lpcSubKeys.getValue(); i++) {
IntByReference lpcchValueName = new IntByReference(
Expand Down Expand Up @@ -2129,7 +2129,7 @@ public static TreeMap<String, Object> registryGetValues(HKEY hKey) {
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
TreeMap<String, Object> keyValues = new TreeMap<String, Object>();
TreeMap<String, Object> keyValues = new TreeMap<>();
char[] name = new char[lpcMaxValueNameLen.getValue() + 1];
// Allocate enough memory to hold largest value and two
// terminating WCHARs -- the memory is zeroed so after
Expand Down Expand Up @@ -2200,7 +2200,7 @@ public static TreeMap<String, Object> registryGetValues(HKEY hKey) {
break;
}
case WinNT.REG_MULTI_SZ: {
ArrayList<String> result = new ArrayList<String>();
ArrayList<String> result = new ArrayList<>();
int offset = 0;
while (offset < byteData.size()) {
String s;
Expand Down Expand Up @@ -2529,7 +2529,7 @@ public EventLogRecord(Pointer pevlr) {
}
// strings
if (_record.NumStrings.intValue() > 0) {
ArrayList<String> strings = new ArrayList<String>();
ArrayList<String> strings = new ArrayList<>();
int count = _record.NumStrings.intValue();
long offset = _record.StringOffset.intValue();
while (count > 0) {
Expand Down Expand Up @@ -2700,8 +2700,8 @@ public static ACE_HEADER[] getFileSecurity(String fileName,
ACE_HEADER[] aceStructures = dacl.getACEs();

if (compact) {
List<ACE_HEADER> result = new ArrayList<ACE_HEADER>();
Map<String, ACCESS_ACEStructure> aceMap = new HashMap<String, ACCESS_ACEStructure>();
List<ACE_HEADER> result = new ArrayList<>();
Map<String, ACCESS_ACEStructure> aceMap = new HashMap<>();
for (ACE_HEADER aceStructure : aceStructures) {
if (aceStructure instanceof ACCESS_ACEStructure) {
ACCESS_ACEStructure accessACEStructure = (ACCESS_ACEStructure) aceStructure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public static ArrayList<COMInfo> getAllCOMInfoOnSystem() {
HKEYByReference phkResult = new HKEYByReference();
HKEYByReference phkResult2 = new HKEYByReference();
String subKey;
ArrayList<COMInfo> comInfos = new ArrayList<COMUtils.COMInfo>();
ArrayList<COMInfo> comInfos = new ArrayList<>();

try {
// open root key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,12 @@ private static <T extends Enum<T>> IEnumWbemClassObject selectProperties(IWbemSe
*/
private static <T extends Enum<T>> WmiResult<T> enumerateProperties(IEnumWbemClassObject enumerator,
Class<T> propertyEnum, int timeout) throws TimeoutException {
WmiResult<T> values = INSTANCE.new WmiResult<T>(propertyEnum);
WmiResult<T> values = INSTANCE.new WmiResult<>(propertyEnum);
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
Pointer[] pclsObj = new Pointer[1];
IntByReference uReturn = new IntByReference(0);
Map<T, WString> wstrMap = new HashMap<T, WString>();
Map<T, WString> wstrMap = new HashMap<>();
HRESULT hres = null;
for (T property : propertyEnum.getEnumConstants()) {
wstrMap.put(property, new WString(property.name()));
Expand Down Expand Up @@ -381,11 +381,11 @@ public class WmiResult<T extends Enum<T>> {
* The enum associated with this map
*/
public WmiResult(Class<T> propertyEnum) {
propertyMap = new EnumMap<T, List<Object>>(propertyEnum);
vtTypeMap = new EnumMap<T, Integer>(propertyEnum);
cimTypeMap = new EnumMap<T, Integer>(propertyEnum);
propertyMap = new EnumMap<>(propertyEnum);
vtTypeMap = new EnumMap<>(propertyEnum);
cimTypeMap = new EnumMap<>(propertyEnum);
for (T prop : propertyEnum.getEnumConstants()) {
propertyMap.put(prop, new ArrayList<Object>());
propertyMap.put(prop, new ArrayList<>());
vtTypeMap.put(prop, Variant.VT_NULL);
cimTypeMap.put(prop, Wbemcli.CIM_EMPTY);
}
Expand Down Expand Up @@ -486,7 +486,7 @@ public static boolean hasNamespace(String namespace) {
ns = namespace.substring(5);
}
// Test
WmiQuery<NamespaceProperty> namespaceQuery = new WmiQuery<NamespaceProperty>("ROOT", "__NAMESPACE", NamespaceProperty.class);
WmiQuery<NamespaceProperty> namespaceQuery = new WmiQuery<>("ROOT", "__NAMESPACE", NamespaceProperty.class);
WmiResult<NamespaceProperty> namespaces = namespaceQuery.execute();
for (int i = 0; i < namespaces.getResultCount(); i++) {
if (ns.equalsIgnoreCase((String) namespaces.getValue(NamespaceProperty.NAME, i))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import com.sun.jna.platform.win32.COM.COMUtils;

public class ComThread {
private static ThreadLocal<Boolean> isCOMThread = new ThreadLocal<Boolean>();
private static ThreadLocal<Boolean> isCOMThread = new ThreadLocal<>();

ExecutorService executor;
Runnable firstTask;
Expand Down Expand Up @@ -116,9 +116,7 @@ public void run() {

executor.shutdown();

} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
executor.shutdownNow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ public IRunningObjectTable getRunningObjectTable() {
private <T> T runInComThread(Callable<T> callable) {
try {
return comThread.execute(callable);
} catch (TimeoutException ex) {
throw new RuntimeException(ex);
} catch (InterruptedException ex) {
} catch (TimeoutException | InterruptedException ex) {
throw new RuntimeException(ex);
} catch (ExecutionException ex) {
Throwable cause = ex.getCause();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ IDispatchCallback createDispatchCallback(Class<?> comEventCallbackInterface, ICo
// environment and can't be used anymore. registeredObjects is used
// to dispose interfaces even if garbadge collection has not yet collected
// the proxy objects.
private final List<WeakReference<ProxyObject>> registeredObjects = new LinkedList<WeakReference<ProxyObject>>();
private final List<WeakReference<ProxyObject>> registeredObjects = new LinkedList<>();

public void register(ProxyObject proxyObject) {
synchronized (this.registeredObjects) {
this.registeredObjects.add(new WeakReference<ProxyObject>(proxyObject));
this.registeredObjects.add(new WeakReference<>(proxyObject));
}
}

Expand All @@ -203,7 +203,7 @@ public void unregister(ProxyObject proxyObject) {

public void disposeAll() {
synchronized (this.registeredObjects) {
List<WeakReference<ProxyObject>> s = new ArrayList<WeakReference<ProxyObject>>(this.registeredObjects);
List<WeakReference<ProxyObject>> s = new ArrayList<>(this.registeredObjects);
for (WeakReference<ProxyObject> weakRef : s) {
ProxyObject po = weakRef.get();
if (po != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Iterable<IDispatch> enumRunning() {
public <T> List<T> getActiveObjectsByInterface(Class<T> comInterface) {
assert COMUtils.comIsInitialized() : "COM not initialized";

List<T> result = new ArrayList<T>();
List<T> result = new ArrayList<>();

for (IDispatch obj : this.enumRunning()) {
try {
Expand Down
Loading

0 comments on commit d8e55dc

Please sign in to comment.