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

Java 8/Junit 5 Migration #45

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open

Java 8/Junit 5 Migration #45

wants to merge 27 commits into from

Conversation

gribnut
Copy link
Contributor

@gribnut gribnut commented Dec 27, 2021

Was having a fair number of warnings/issues compiling for Java 8/11 so thought I'd update some of the code for use with current supported JVMs. Mainly updates to migrate toward Java 8/11 code style.

  • Set type and checks for raw maps, lists, sets, classes, etc.
  • Migrate to Junit 5
  • Some additional unit tests for RadiusAttribute subclasses (additional unit tests still needed)
  • Minor code changes/cleanup for Java 8+

I only use library with RADIUS client so server code not tested. Feel free to merge changes or toss as see fit. Happy to make additional changes if needed.

gribnut added 22 commits May 26, 2020 15:34
* commit 'b780c224d033f1a714937673a10cc2c751264fef':
  fix: handle closed socket (ctran#39)
  Bump junit from 4.12 to 4.13.1 (ctran#37)
  Create codeql-analysis.yml
  Avoid using up file handles if endpoint too many (ctran#35)
* 'master' of https://github.com/ctran/TinyRadius:
  Bump ipaddress from 5.3.1 to 5.3.3 (ctran#38)
  Bumped commons-logging to 1.2 (ctran#42)
  Run CI action on PR changes (ctran#44)
@ctran
Copy link
Owner

ctran commented Jan 4, 2022

Appreciate the effort. I'll need some time to review all the changes.

@@ -234,7 +234,7 @@ public static RadiusAttribute createRadiusAttribute(Dictionary dictionary, int v
AttributeType at = dictionary.getAttributeTypeByCode(vendorId, attributeType);
if (at != null && at.getAttributeClass() != null) {
try {
attribute = (RadiusAttribute) at.getAttributeClass().newInstance();
attribute = at.getAttributeClass().newInstance();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClassNewInstance: Class.newInstance() bypasses exception checking; prefer getDeclaredConstructor().newInstance() (details)
(at-me in a reply with help or ignore)

LinkedList result = new LinkedList();
for (Iterator i = subAttributes.iterator(); i.hasNext();) {
RadiusAttribute a = (RadiusAttribute) i.next();
LinkedList<RadiusAttribute> result = new LinkedList<>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JdkObsolete: It is very rare for LinkedList to out-perform ArrayList or ArrayDeque. Avoid it unless you're willing to invest a lot of time into benchmarking. Caveat: LinkedList supports null elements, but ArrayDeque does not. (details)
(at-me in a reply with help or ignore)

LinkedList result = new LinkedList();
for (Iterator i = attributes.iterator(); i.hasNext();) {
RadiusAttribute a = (RadiusAttribute) i.next();
LinkedList<RadiusAttribute> result = new LinkedList<>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JdkObsolete: It is very rare for LinkedList to out-perform ArrayList or ArrayDeque. Avoid it unless you're willing to invest a lot of time into benchmarking. Caveat: LinkedList supports null elements, but ArrayDeque does not. (details)
(at-me in a reply with help or ignore)

for (Iterator i = attributes.iterator(); i.hasNext();) {
RadiusAttribute a = (RadiusAttribute) i.next();
public List<VendorSpecificAttribute> getVendorAttributes(int vendorId) {
LinkedList<VendorSpecificAttribute> result = new LinkedList<>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JdkObsolete: It is very rare for LinkedList to out-perform ArrayList or ArrayDeque. Avoid it unless you're willing to invest a lot of time into benchmarking. Caveat: LinkedList supports null elements, but ArrayDeque does not. (details)
(at-me in a reply with help or ignore)

@@ -22,9 +21,10 @@
public class TestServer {

/**
* @throws IOException
* @throws Exception
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MissingSummary: A summary line is required on public/protected Javadocs. (details)
(at-me in a reply with help or ignore)

@@ -192,12 +192,11 @@ private static void includeDictionaryFile(WritableDictionary dictionary, StringT
* string|octets|integer|date|ipaddr|ipv6addr|ipv6prefix
* @return RadiusAttribute class or descendant
*/
private static Class getAttributeTypeClass(int attributeType, String typeStr) {
Class type = RadiusAttribute.class;
private static Class<? extends RadiusAttribute> getAttributeTypeClass(int attributeType, String typeStr) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnusedVariable: The parameter 'attributeType' is never read. (details)
(at-me in a reply with help or ignore)

@@ -368,6 +366,7 @@ public static RadiusPacket communicate(RadiusEndpoint remoteServer, RadiusPacket
*
* @return local socket
* @throws SocketException
* Socket Exception
*/
protected DatagramSocket getSocket() throws SocketException {
if (serverSocket == null || serverSocket.isClosed()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THREAD_SAFETY_VIOLATION: Read/Write race. Non-private method RadiusClient.getSocket() reads without synchronization from this.serverSocket. Potentially races with write in method RadiusClient.authenticate(...).
Reporting because another access to the same memory occurs on a background thread, although this access may not.
(at-me in a reply with help or ignore)

*/
public void setSocketTimeout(int socketTimeout) throws SocketException {
if (socketTimeout < 1)
throw new IllegalArgumentException("socket tiemout must be positive");
throw new IllegalArgumentException("socket timeout must be positive");
this.socketTimeout = socketTimeout;
if (serverSocket != null)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THREAD_SAFETY_VIOLATION: Read/Write race. Non-private method RadiusClient.setSocketTimeout(...) reads without synchronization from this.serverSocket. Potentially races with write in method RadiusClient.authenticate(...).
Reporting because another access to the same memory occurs on a background thread, although this access may not.
(at-me in a reply with help or ignore)

@@ -312,8 +315,7 @@ public RadiusPacket communicate(RadiusPacket request, int port) throws IOExcepti
DatagramPacket packetIn = new DatagramPacket(new byte[RadiusPacket.MAX_PACKET_LENGTH], RadiusPacket.MAX_PACKET_LENGTH);
DatagramPacket packetOut = makeDatagramPacket(request, port);

DatagramSocket socket = getSocket();
try {
try (DatagramSocket socket = getSocket()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THREAD_SAFETY_VIOLATION: Read/Write race. Non-private method RadiusClient.communicate(...) indirectly reads with synchronization from this.socketTimeout. Potentially races with unsynchronized write in method RadiusClient.setSocketTimeout(...).
Reporting because another access to the same memory occurs on a background thread, although this access may not.
(at-me in a reply with help or ignore)

*/
protected DatagramSocket getSocket() throws SocketException {
protected synchronized DatagramSocket getSocket() throws SocketException {
if (serverSocket == null || serverSocket.isClosed()) {
serverSocket = new DatagramSocket();
serverSocket.setSoTimeout(getSocketTimeout());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THREAD_SAFETY_VIOLATION: Read/Write race. Non-private method RadiusClient.getSocket() indirectly reads with synchronization from this.socketTimeout. Potentially races with unsynchronized write in method RadiusClient.setSocketTimeout(...).
Reporting because this access may occur on a background thread.
(at-me in a reply with help or ignore)

*/
public void setSocketTimeout(int socketTimeout) throws SocketException {
if (socketTimeout < 1)
throw new IllegalArgumentException("socket tiemout must be positive");
throw new IllegalArgumentException("socket timeout must be positive");
this.socketTimeout = socketTimeout;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THREAD_SAFETY_VIOLATION: Unprotected write. Non-private method RadiusClient.setSocketTimeout(...) writes to field this.socketTimeout outside of synchronization.
Reporting because this access may occur on a background thread.
(at-me in a reply with help or ignore)

@gribnut
Copy link
Contributor Author

gribnut commented Jan 16, 2022

Merged recent CI changes and fixed sonatype lift warnings (the ones marked new anyway).

@ctran ctran force-pushed the master branch 3 times, most recently from 67019ad to 5526820 Compare February 1, 2022 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants