Skip to content

Commit

Permalink
codebase cleanup - remove redundant code constructs, use modern APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
  • Loading branch information
lukasj committed Apr 30, 2023
1 parent 4af2540 commit 6a5c36b
Show file tree
Hide file tree
Showing 23 changed files with 63 additions and 123 deletions.
4 changes: 2 additions & 2 deletions api/src/main/java/jakarta/mail/Flags.java
Expand Up @@ -177,7 +177,7 @@ public Flags() {
public Flags(Flags flags) {
this.system_flags = flags.system_flags;
if (flags.user_flags != null)
this.user_flags = (Hashtable) flags.user_flags.clone();
this.user_flags = (Hashtable<String, String>) flags.user_flags.clone();
}

/**
Expand Down Expand Up @@ -492,7 +492,7 @@ public Object clone() {
// ignore, can't happen
}
if (this.user_flags != null)
f.user_flags = (Hashtable) this.user_flags.clone();
f.user_flags = (Hashtable<String, String>) this.user_flags.clone();
return f;
}

Expand Down
6 changes: 3 additions & 3 deletions api/src/main/java/jakarta/mail/Folder.java
Expand Up @@ -1307,7 +1307,7 @@ public Message[] search(SearchTerm term, Message[] msgs)
}
}

return matchedMsgs.toArray(new Message[matchedMsgs.size()]);
return matchedMsgs.toArray(new Message[0]);
}

/*
Expand Down Expand Up @@ -1626,7 +1626,6 @@ protected void notifyMessageChangedListeners(int type, Message msg) {
/*
* Add the event and vector of listeners to the queue to be delivered.
*/
@SuppressWarnings("unchecked")
private void queueEvent(MailEvent event,
Vector<? extends EventListener> vector) {
/*
Expand All @@ -1637,7 +1636,8 @@ private void queueEvent(MailEvent event,
* of this event will not take effect until after the event is
* delivered.
*/
Vector<? extends EventListener> v = (Vector) vector.clone();
@SuppressWarnings("unchecked")
Vector<? extends EventListener> v = (Vector<? extends EventListener>) vector.clone();
q.enqueue(event, v);
}

Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/jakarta/mail/MailLogger.java
Expand Up @@ -226,7 +226,7 @@ public void log(Level level, String msg) {
*/
public void log(Level level, String msg, Object param1) {
if (debug) {
msg = MessageFormat.format(msg, new Object[]{param1});
msg = MessageFormat.format(msg, param1);
debugOut(msg);
}

Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/jakarta/mail/MessagingException.java
Expand Up @@ -132,7 +132,7 @@ public synchronized String toString() {
sb.append(mex.superToString());
n = mex.next;
} else {
sb.append(n.toString());
sb.append(n);
n = null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/jakarta/mail/Service.java
Expand Up @@ -628,7 +628,7 @@ protected void queueEvent(MailEvent event,
* delivered.
*/
@SuppressWarnings("unchecked")
Vector<? extends EventListener> v = (Vector) vector.clone();
Vector<? extends EventListener> v = (Vector<? extends EventListener>) vector.clone();
q.enqueue(event, v);
}

Expand Down
30 changes: 10 additions & 20 deletions api/src/main/java/jakarta/mail/Session.java
Expand Up @@ -510,7 +510,7 @@ public synchronized Provider[] getProviders() {
*/
public synchronized Provider getProvider(String protocol) throws NoSuchProviderException {

if (protocol == null || protocol.length() <= 0) {
if (protocol == null || protocol.length() == 0) {
throw new NoSuchProviderException("Invalid protocol: null");
}

Expand Down Expand Up @@ -538,7 +538,7 @@ public synchronized Provider getProvider(String protocol) throws NoSuchProviderE
throw new NoSuchProviderException("No provider for " + protocol);
} else {
if (logger.isLoggable(Level.FINE)) {
logger.fine("getProvider() returning " + _provider.toString());
logger.fine("getProvider() returning " + _provider);
}
return _provider;
}
Expand Down Expand Up @@ -1053,9 +1053,9 @@ public void load(InputStream is) throws IOException {
// dump the output of the tables for debugging
logger.config("Tables of loaded providers");
logger.config("Providers Listed By Class Name: " +
providersByClassName.toString());
providersByClassName);
logger.config("Providers Listed By Protocol: " +
providersByProtocol.toString());
providersByProtocol);
}
}

Expand Down Expand Up @@ -1102,7 +1102,7 @@ private void loadProvidersFromStream(InputStream is) throws IOException {

// check if a valid Provider; else, continue
if (type == null || protocol == null || className == null
|| protocol.length() <= 0 || className.length() <= 0) {
|| protocol.length() == 0 || className.length() == 0) {

logger.log(Level.CONFIG, "Bad provider entry: {0}",
currLine);
Expand Down Expand Up @@ -1189,12 +1189,9 @@ private void loadFile(String name, StreamLoader loader) {
logger.log(Level.CONFIG, "successfully loaded file: {0}", name);
} catch (FileNotFoundException fex) {
// ignore it
} catch (IOException e) {
} catch (IOException | SecurityException e) {
if (logger.isLoggable(Level.CONFIG))
logger.log(Level.CONFIG, "not loading file: " + name, e);
} catch (SecurityException sex) {
if (logger.isLoggable(Level.CONFIG))
logger.log(Level.CONFIG, "not loading file: " + name, sex);
} finally {
try {
if (clis != null)
Expand All @@ -1220,10 +1217,8 @@ private void loadResource(String name, Class<?> cl, StreamLoader loader, boolean
logger.log(Level.WARNING,
"expected resource not found: {0}", name);
}
} catch (IOException e) {
} catch (IOException | SecurityException e) {
logger.log(Level.CONFIG, "Exception loading resource", e);
} catch (SecurityException sex) {
logger.log(Level.CONFIG, "Exception loading resource", sex);
} finally {
try {
if (clis != null)
Expand Down Expand Up @@ -1267,12 +1262,9 @@ private void loadAllResources(String name, Class<?> cl, StreamLoader loader) {
}
} catch (FileNotFoundException fex) {
// ignore it
} catch (IOException ioex) {
} catch (IOException | SecurityException ioex) {
logger.log(Level.CONFIG, "Exception loading resource",
ioex);
} catch (SecurityException sex) {
logger.log(Level.CONFIG, "Exception loading resource",
sex);
} finally {
try {
if (clis != null)
Expand Down Expand Up @@ -1349,8 +1341,7 @@ public URL[] run() {
ret = new URL[v.size()];
v.toArray(ret);
}
} catch (IOException ioex) {
} catch (SecurityException ex) {
} catch (IOException | SecurityException ioex) {
}
return ret;
}
Expand All @@ -1369,8 +1360,7 @@ public URL[] run() {
ret = new URL[v.size()];
v.toArray(ret);
}
} catch (IOException ioex) {
} catch (SecurityException ex) {
} catch (IOException | SecurityException ioex) {
}
return ret;
}
Expand Down
13 changes: 5 additions & 8 deletions api/src/main/java/jakarta/mail/Transport.java
Expand Up @@ -20,6 +20,7 @@
import jakarta.mail.event.TransportListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -251,8 +252,7 @@ private static void send0(Message msg, Address[] addresses,
if ((transport = s.getTransport(protaddresses[0])) == null) {
// Could not find an appropriate Transport ..
// Mark these addresses invalid.
for (int j = 0; j < protaddresses.length; j++)
invalid.add(protaddresses[j]);
Collections.addAll(invalid, protaddresses);
continue;
}
try {
Expand All @@ -269,20 +269,17 @@ private static void send0(Message msg, Address[] addresses,
// retrieve invalid addresses
Address[] a = sex.getInvalidAddresses();
if (a != null)
for (int j = 0; j < a.length; j++)
invalid.add(a[j]);
Collections.addAll(invalid, a);

// retrieve validSent addresses
a = sex.getValidSentAddresses();
if (a != null)
for (int k = 0; k < a.length; k++)
validSent.add(a[k]);
Collections.addAll(validSent, a);

// retrieve validUnsent addresses
Address[] c = sex.getValidUnsentAddresses();
if (c != null)
for (int l = 0; l < c.length; l++)
validUnsent.add(c[l]);
Collections.addAll(validUnsent, c);
} catch (MessagingException mex) {
sendFailed = true;
// chain the exception we're catching to any previous ones
Expand Down
23 changes: 9 additions & 14 deletions api/src/main/java/jakarta/mail/URLName.java
Expand Up @@ -19,13 +19,14 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.BitSet;
import java.util.Locale;
import java.util.Objects;


/**
Expand Down Expand Up @@ -201,7 +202,7 @@ public String toString() {
// add port (if needed)
if (port != -1) {
tempURL.append(":");
tempURL.append(Integer.toString(port));
tempURL.append(port);
}
if (file != null)
tempURL.append("/");
Expand Down Expand Up @@ -424,8 +425,7 @@ public boolean equals(Object obj) {
URLName u2 = (URLName) obj;

// compare protocols
if (!(protocol == u2.protocol ||
(protocol != null && protocol.equals(u2.protocol))))
if (!(Objects.equals(protocol, u2.protocol)))
return false;

// compare hosts
Expand All @@ -445,8 +445,7 @@ public boolean equals(Object obj) {
// at this point, hosts match

// compare usernames
if (!(username == u2.username ||
(username != null && username.equals(u2.username))))
if (!(Objects.equals(username, u2.username)))
return false;

// Forget about password since it doesn't
Expand Down Expand Up @@ -566,7 +565,7 @@ static String encode(String s) {
return null;
// the common case is no encoding is needed
for (int i = 0; i < s.length(); i++) {
int c = (int) s.charAt(i);
int c = s.charAt(i);
if (c == ' ' || !dontNeedEncoding.get(c))
return _encode(s);
}
Expand All @@ -580,7 +579,7 @@ private static String _encode(String s) {
OutputStreamWriter writer = new OutputStreamWriter(buf);

for (int i = 0; i < s.length(); i++) {
int c = (int) s.charAt(i);
int c = s.charAt(i);
if (dontNeedEncoding.get(c)) {
if (c == ' ') {
c = '+';
Expand Down Expand Up @@ -680,12 +679,8 @@ static String decode(String s) {
}
// Undo conversion to external encoding
String result = sb.toString();
try {
byte[] inputBytes = result.getBytes("8859_1");
result = new String(inputBytes);
} catch (UnsupportedEncodingException e) {
// The system should always have 8859_1
}
byte[] inputBytes = result.getBytes(StandardCharsets.ISO_8859_1);
result = new String(inputBytes);
return result;
}

Expand Down
Expand Up @@ -332,7 +332,7 @@ else if (c == ')')
currentPos++; // re-position currentPos
char[] ch = new char[1];
ch[0] = c;
return new Token((int) c, new String(ch));
return new Token(c, new String(ch));
}

// Check for ATOM
Expand Down
3 changes: 1 addition & 2 deletions api/src/main/java/jakarta/mail/internet/InternetAddress.java
Expand Up @@ -605,8 +605,7 @@ private static int lengthOfLastSegment(String s, int used) {
public static InternetAddress getLocalAddress(Session session) {
try {
return _getLocalAddress(session);
} catch (SecurityException sex) { // ignore it
} catch (AddressException ex) { // ignore it
} catch (SecurityException | AddressException sex) { // ignore it
} catch (UnknownHostException ex) {
} // ignore it
return null;
Expand Down
26 changes: 2 additions & 24 deletions api/src/main/java/jakarta/mail/internet/MimeBodyPart.java
Expand Up @@ -34,14 +34,13 @@
import jakarta.mail.util.StreamProvider.EncoderTypes;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
Expand Down Expand Up @@ -948,28 +947,7 @@ public String getEncoding() {
* @since JavaMail 1.4
*/
public void saveFile(File file) throws IOException, MessagingException {
OutputStream out = null;
InputStream in = null;
try {
out = new BufferedOutputStream(new FileOutputStream(file));
in = this.getInputStream();
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
} finally {
// close streams, but don't mask original exception, if any
try {
if (in != null)
in.close();
} catch (IOException ex) {
}
try {
if (out != null)
out.close();
} catch (IOException ex) {
}
}
Files.copy(getInputStream(), file.toPath());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/jakarta/mail/internet/MimeMessage.java
Expand Up @@ -1826,7 +1826,7 @@ private Address[] eliminateDuplicates(List<Address> v, Address[] addrs) {
boolean found = false;
// search the list for this address
for (int j = 0; j < v.size(); j++) {
if (((InternetAddress) v.get(j)).equals(addrs[i])) {
if (v.get(j).equals(addrs[i])) {
// found it; count it and remove it from the input array
found = true;
gone++;
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/jakarta/mail/internet/MimeMultipart.java
Expand Up @@ -544,7 +544,7 @@ public synchronized void writeTo(OutputStream os)
} else {
for (int i = 0; i < parts.size(); i++) {
los.writeln(boundary); // put out boundary
((MimeBodyPart) parts.elementAt(i)).writeTo(os);
parts.elementAt(i).writeTo(os);
los.writeln(); // put out empty line
}
}
Expand Down
Expand Up @@ -136,7 +136,7 @@ public String getContentType() {
public String getName() {
try {
if (part instanceof MimeBodyPart)
return ((MimeBodyPart) part).getFileName();
return part.getFileName();
} catch (MessagingException mex) {
// ignore it
}
Expand Down
8 changes: 2 additions & 6 deletions api/src/main/java/jakarta/mail/internet/MimeUtil.java
Expand Up @@ -46,13 +46,9 @@ class MimeUtil {
if (clsHandler == null)
clsHandler = Class.forName(cth);
meth = clsHandler.getMethod("cleanContentType",
new Class<?>[]{MimePart.class, String.class});
MimePart.class, String.class);
}
} catch (ClassNotFoundException ex) {
// ignore it
} catch (NoSuchMethodException ex) {
// ignore it
} catch (RuntimeException ex) {
} catch (ClassNotFoundException | RuntimeException | NoSuchMethodException ex) {
// ignore it
} finally {
cleanContentType = meth;
Expand Down

0 comments on commit 6a5c36b

Please sign in to comment.