Skip to content

Commit b806893

Browse files
authored
Java 21 update (syntax) (#344)
1 parent 158d4a7 commit b806893

10 files changed

Lines changed: 34 additions & 41 deletions

File tree

jsignpdf/src/main/java/net/sf/jsignpdf/Constants.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
public class Constants {
5353

5454
static {
55-
try (InputStream is = Constants.class.getClassLoader().
55+
try (var is = Constants.class.getClassLoader().
5656
getResourceAsStream("logging.properties")) {
5757
LogManager.getLogManager().readConfiguration(is);
5858
} catch (IOException e) {
@@ -426,10 +426,10 @@ public class Constants {
426426
SUPPORTED_CRITICAL_EXTENSION_OIDS = Collections.unmodifiableSet(oidSet);
427427

428428
String version = "[UNKNOWN]";
429-
try (InputStream is = Constants.class
429+
try (var is = Constants.class
430430
.getResourceAsStream("/META-INF/maven/com.github.kwart.jsign/jsignpdf/pom.properties")) {
431431
if (is != null) {
432-
Properties props = new Properties();
432+
var props = new Properties();
433433
props.load(is);
434434
if (props.containsKey("version")) {
435435
version = props.getProperty("version");

jsignpdf/src/main/java/net/sf/jsignpdf/JTextAreaHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void publish(LogRecord record) {
7070
jTextArea.append(record.getLevel() + " " + record.getMessage() + NEW_LINE);
7171
Throwable thrown = record.getThrown();
7272
if (thrown != null) {
73-
try (StringWriter stringWriter = new StringWriter()) {
73+
try (var stringWriter = new StringWriter()) {
7474
thrown.printStackTrace(new PrintWriter(stringWriter, true));
7575
jTextArea.append(stringWriter.toString());
7676
} catch (IOException e) {

jsignpdf/src/main/java/net/sf/jsignpdf/SignerFileChooser.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ private boolean confirmOverwrite(File file) {
101101
public void setSelectedFile(File file) {
102102
super.setSelectedFile(file);
103103
// safety check
104-
if (getUI() instanceof BasicFileChooserUI) {
104+
if (getUI() instanceof BasicFileChooserUI tmpUi) {
105105
// grab the ui and set the filename
106-
BasicFileChooserUI tmpUi = (BasicFileChooserUI) getUI();
107106
tmpUi.setFileName(file == null ? "" : file.getName());
108107
}
109108
}

jsignpdf/src/main/java/net/sf/jsignpdf/SignerOptionsFromCmdLine.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ public void loadCmdLine() throws ParseException {
255255
* @return
256256
*/
257257
private int getInt(Object aVal, int aDefVal) {
258-
if (aVal instanceof Number) {
259-
return ((Number) aVal).intValue();
258+
if (aVal instanceof Number n) {
259+
return n.intValue();
260260
}
261261
return aDefVal;
262262
}
@@ -269,8 +269,8 @@ private int getInt(Object aVal, int aDefVal) {
269269
* @return
270270
*/
271271
private float getFloat(Object aVal, float aDefVal) {
272-
if (aVal instanceof Number) {
273-
return ((Number) aVal).floatValue();
272+
if (aVal instanceof Number n) {
273+
return n.floatValue();
274274
}
275275
return aDefVal;
276276
}

jsignpdf/src/main/java/net/sf/jsignpdf/crl/CRLInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ private void initCrls() {
115115
LOGGER.info(RES.get("console.readingCRLs"));
116116
final Set<String> urls = new HashSet<String>();
117117
for (Certificate cert : certChain) {
118-
if (cert instanceof X509Certificate) {
119-
urls.addAll(getCrlUrls((X509Certificate) cert));
118+
if (cert instanceof X509Certificate x509Cert) {
119+
urls.addAll(getCrlUrls(x509Cert));
120120
}
121121
}
122122
final Set<CRL> crlSet = new HashSet<CRL>();

jsignpdf/src/main/java/net/sf/jsignpdf/preview/Pdf2Image.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,12 @@ public Pdf2Image(BasicSignerOptions anOpts) {
8686
public BufferedImage getImageForPage(final int aPage) {
8787
BufferedImage tmpResult = null;
8888
for (String libname : Constants.PDF2IMAGE_LIBRARIES.split("\\s*,\\s*")) {
89-
if (Constants.PDF2IMAGE_JPEDAL.equals(libname)) {
90-
tmpResult = getImageUsingJPedal(aPage);
91-
} else if (Constants.PDF2IMAGE_PDFBOX.equals(libname)) {
92-
tmpResult = getImageUsingPdfBox(aPage);
93-
} else if (Constants.PDF2IMAGE_OPENPDF.equals(libname)) {
94-
tmpResult = getImageUsingOpenPdfRenderer(aPage);
95-
}
89+
tmpResult = switch (libname) {
90+
case Constants.PDF2IMAGE_JPEDAL -> getImageUsingJPedal(aPage);
91+
case Constants.PDF2IMAGE_PDFBOX -> getImageUsingPdfBox(aPage);
92+
case Constants.PDF2IMAGE_OPENPDF -> getImageUsingOpenPdfRenderer(aPage);
93+
default -> null;
94+
};
9695
if (tmpResult != null)
9796
break;
9897
}

jsignpdf/src/main/java/net/sf/jsignpdf/ssl/DynamicX509TrustManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ private void reloadTrustStore() throws KeyStoreException, NoSuchAlgorithmExcepti
128128
// acquire X509 trust manager from factory
129129
TrustManager tms[] = trustManagerFactory.getTrustManagers();
130130
for (int i = 0; i < tms.length; i++) {
131-
if (tms[i] instanceof X509TrustManager) {
132-
trustManager = (X509TrustManager) tms[i];
131+
if (tms[i] instanceof X509TrustManager x509Tm) {
132+
trustManager = x509Tm;
133133
return;
134134
}
135135
}

jsignpdf/src/main/java/net/sf/jsignpdf/utils/ConvertUtils.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ public static Integer toInteger(Object anObj) {
106106
if (anObj == null)
107107
return null;
108108
Integer tmpResult = null;
109-
if (anObj instanceof Integer) {
110-
tmpResult = (Integer) anObj;
111-
} else if (anObj instanceof Number) {
112-
tmpResult = new Integer(((Number) anObj).intValue());
109+
if (anObj instanceof Integer i) {
110+
tmpResult = i;
111+
} else if (anObj instanceof Number n) {
112+
tmpResult = new Integer(n.intValue());
113113
} else {
114114
try {
115115
tmpResult = new Integer(toString(anObj));
@@ -142,10 +142,10 @@ public static Float toFloat(Object anObj) {
142142
if (anObj == null)
143143
return null;
144144
Float tmpResult = null;
145-
if (anObj instanceof Float) {
146-
tmpResult = (Float) anObj;
147-
} else if (anObj instanceof Number) {
148-
tmpResult = new Float(((Number) anObj).floatValue());
145+
if (anObj instanceof Float f) {
146+
tmpResult = f;
147+
} else if (anObj instanceof Number n) {
148+
tmpResult = new Float(n.floatValue());
149149
} else {
150150
try {
151151
tmpResult = new Float(toString(anObj));
@@ -189,8 +189,8 @@ public static Boolean toBoolean(Object anObj) {
189189
Boolean tmpResult = null;
190190
if (anObj == null) {
191191
// nothing to do
192-
} else if (anObj instanceof Boolean) {
193-
tmpResult = (Boolean) anObj;
192+
} else if (anObj instanceof Boolean b) {
193+
tmpResult = b;
194194
} else {
195195
final String tmpStr = toString(anObj);
196196
if ("true".equalsIgnoreCase(tmpStr) || "yes".equalsIgnoreCase(tmpStr) || "on".equalsIgnoreCase(tmpStr)) {

jsignpdf/src/main/java/net/sf/jsignpdf/utils/KeyStoreUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ private static List<String> getAliasesList(final KeyStore aKs, final BasicSigner
146146
if (aKs.isKeyEntry(tmpAlias)) {
147147
final Certificate tmpCert = aKs.getCertificate(tmpAlias);
148148
boolean tmpAddAlias = true;
149-
if (tmpCert instanceof X509Certificate) {
150-
final X509Certificate tmpX509 = (X509Certificate) tmpCert;
149+
if (tmpCert instanceof X509Certificate tmpX509) {
151150
if (checkValidity) {
152151
try {
153152
tmpX509.checkValidity();

jsignpdf/src/main/java/net/sf/jsignpdf/utils/ResourceProvider.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,20 @@ public ResourceProvider(ResourceBundle bundle) {
7676
public void setLabelAndMnemonic(final JComponent aComponent, final String aKey) {
7777
final String tmpLabelText = get(aKey);
7878
final int tmpMnemIndex = getMnemonicIndex(aKey);
79-
if (aComponent instanceof JLabel) {
80-
final JLabel tmpLabel = (JLabel) aComponent;
79+
if (aComponent instanceof JLabel tmpLabel) {
8180
tmpLabel.setText(tmpLabelText);
8281
if (tmpMnemIndex > -1) {
8382
tmpLabel.setDisplayedMnemonic(tmpLabelText.toLowerCase().charAt(tmpMnemIndex));
8483
tmpLabel.setDisplayedMnemonicIndex(tmpMnemIndex);
8584
}
86-
} else if (aComponent instanceof AbstractButton) {
85+
} else if (aComponent instanceof AbstractButton tmpBtn) {
8786
// handles Buttons, Checkboxes and Radiobuttons
88-
final AbstractButton tmpBtn = (AbstractButton) aComponent;
8987
tmpBtn.setText(tmpLabelText);
9088
if (tmpMnemIndex > -1) {
9189
tmpBtn.setMnemonic(tmpLabelText.toLowerCase().charAt(tmpMnemIndex));
9290
}
93-
} else if (aComponent instanceof JPanel) {
94-
final JPanel panel = (JPanel) aComponent;
95-
if (panel.getBorder() instanceof TitledBorder) {
96-
final TitledBorder titledBorder = (TitledBorder) panel.getBorder();
91+
} else if (aComponent instanceof JPanel panel) {
92+
if (panel.getBorder() instanceof TitledBorder titledBorder) {
9793
titledBorder.setTitle(tmpLabelText);
9894
}
9995
} else {

0 commit comments

Comments
 (0)