Skip to content

Commit 2a3d354

Browse files
committed
jdk17 / switch
1 parent 542fc37 commit 2a3d354

File tree

12 files changed

+126
-293
lines changed

12 files changed

+126
-293
lines changed

src/main/java/org/htmlunit/HttpWebConnection.java

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -485,43 +485,17 @@ public long getContentLength() {
485485
* @return a new HttpClient HTTP method based on the specified parameters
486486
*/
487487
private static HttpRequestBase buildHttpMethod(final HttpMethod submitMethod, final URI uri) {
488-
final HttpRequestBase method;
489-
switch (submitMethod) {
490-
case GET:
491-
method = new HttpGet(uri);
492-
break;
493-
494-
case POST:
495-
method = new HttpPost(uri);
496-
break;
497-
498-
case PUT:
499-
method = new HttpPut(uri);
500-
break;
501-
502-
case DELETE:
503-
method = new org.htmlunit.httpclient.HttpDelete(uri);
504-
break;
505-
506-
case OPTIONS:
507-
method = new org.htmlunit.httpclient.HttpOptions(uri);
508-
break;
509-
510-
case HEAD:
511-
method = new HttpHead(uri);
512-
break;
513-
514-
case TRACE:
515-
method = new HttpTrace(uri);
516-
break;
517-
518-
case PATCH:
519-
method = new HttpPatch(uri);
520-
break;
521-
522-
default:
523-
throw new IllegalStateException("Submit method not yet supported: " + submitMethod);
524-
}
488+
final HttpRequestBase method = switch (submitMethod) {
489+
case GET -> new HttpGet(uri);
490+
case POST -> new HttpPost(uri);
491+
case PUT -> new HttpPut(uri);
492+
case DELETE -> new org.htmlunit.httpclient.HttpDelete(uri);
493+
case OPTIONS -> new org.htmlunit.httpclient.HttpOptions(uri);
494+
case HEAD -> new HttpHead(uri);
495+
case TRACE -> new HttpTrace(uri);
496+
case PATCH -> new HttpPatch(uri);
497+
default -> throw new IllegalStateException("Submit method not yet supported: " + submitMethod);
498+
};
525499
return method;
526500
}
527501

src/main/java/org/htmlunit/StorageHolder.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,11 @@ public enum Type {
5252
* @return the store
5353
*/
5454
public Map<String, String> getStore(final Type storageType, final Page page) {
55-
switch (storageType) {
56-
case LOCAL_STORAGE:
57-
return getLocalStorage(page.getUrl());
58-
59-
case SESSION_STORAGE:
60-
return getSessionStorage(page.getEnclosingWindow());
61-
62-
default:
63-
return null;
64-
}
55+
return switch (storageType) {
56+
case LOCAL_STORAGE -> getLocalStorage(page.getUrl());
57+
case SESSION_STORAGE -> getSessionStorage(page.getEnclosingWindow());
58+
default -> null;
59+
};
6560
}
6661

6762
/**

src/main/java/org/htmlunit/TextPage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ public void save(final File file) throws IOException {
6060
final Path savePath = file.toPath();
6161
final String text = getContent();
6262
final Charset charset = getWebResponse().getContentCharset();
63-
Files.write(savePath, text.getBytes(charset));
63+
Files.writeString(savePath, text, charset);
6464
}
6565
}

src/main/java/org/htmlunit/WebClient.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,26 +1555,14 @@ private WebResponse makeWebResponseForJavaScriptUrl(final WebWindow webWindow, f
15551555
*/
15561556
public WebResponse loadWebResponse(final WebRequest webRequest) throws IOException {
15571557
final String protocol = webRequest.getUrl().getProtocol();
1558-
switch (protocol) {
1559-
case UrlUtils.ABOUT:
1560-
return makeWebResponseForAboutUrl(webRequest);
1561-
1562-
case "file":
1563-
return makeWebResponseForFileUrl(webRequest);
1564-
1565-
case "data":
1566-
return makeWebResponseForDataUrl(webRequest);
1567-
1568-
case "blob":
1569-
return makeWebResponseForBlobUrl(webRequest);
1570-
1571-
case "http":
1572-
case "https":
1573-
return loadWebResponseFromWebConnection(webRequest, ALLOWED_REDIRECTIONS_SAME_URL);
1574-
1575-
default:
1576-
throw new IOException("Unsupported protocol '" + protocol + "'");
1577-
}
1558+
return switch (protocol) {
1559+
case UrlUtils.ABOUT -> makeWebResponseForAboutUrl(webRequest);
1560+
case "file" -> makeWebResponseForFileUrl(webRequest);
1561+
case "data" -> makeWebResponseForDataUrl(webRequest);
1562+
case "blob" -> makeWebResponseForBlobUrl(webRequest);
1563+
case "http", "https" -> loadWebResponseFromWebConnection(webRequest, ALLOWED_REDIRECTIONS_SAME_URL);
1564+
default -> throw new IOException("Unsupported protocol '" + protocol + "'");
1565+
};
15781566
}
15791567

15801568
/**

src/main/java/org/htmlunit/html/DefaultElementFactory.java

Lines changed: 30 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -803,107 +803,39 @@ private static HtmlElement createInputElement(final String qualifiedName, final
803803
}
804804
}
805805

806-
final HtmlInput result;
807-
switch (type.toLowerCase(Locale.ROOT)) {
808-
case "":
809-
// This not an illegal value, as it defaults to "text"
810-
// cf http://www.w3.org/TR/REC-html40/interact/forms.html#adef-type-INPUT
811-
// and the common browsers seem to treat it as a "text" input so we will as well.
812-
case "text":
813-
result = new HtmlTextInput(qualifiedName, page, attributeMap);
814-
break;
815-
816-
case "submit":
817-
result = new HtmlSubmitInput(qualifiedName, page, attributeMap);
818-
break;
819-
820-
case "checkbox":
821-
result = new HtmlCheckBoxInput(qualifiedName, page, attributeMap);
822-
break;
823-
824-
case "radio":
825-
result = new HtmlRadioButtonInput(qualifiedName, page, attributeMap);
826-
break;
827-
828-
case "hidden":
829-
result = new HtmlHiddenInput(qualifiedName, page, attributeMap);
830-
break;
831-
832-
case "password":
833-
result = new HtmlPasswordInput(qualifiedName, page, attributeMap);
834-
break;
835-
836-
case "image":
837-
result = new HtmlImageInput(qualifiedName, page, attributeMap);
838-
break;
839-
840-
case "reset":
841-
result = new HtmlResetInput(qualifiedName, page, attributeMap);
842-
break;
843-
844-
case "button":
845-
result = new HtmlButtonInput(qualifiedName, page, attributeMap);
846-
break;
847-
848-
case "file":
849-
result = new HtmlFileInput(qualifiedName, page, attributeMap);
850-
break;
851-
852-
case "color":
853-
result = new HtmlColorInput(qualifiedName, page, attributeMap);
854-
break;
855-
856-
case "date":
857-
result = new HtmlDateInput(qualifiedName, page, attributeMap);
858-
break;
859-
860-
case "datetime-local":
861-
result = new HtmlDateTimeLocalInput(qualifiedName, page, attributeMap);
862-
break;
863-
864-
case "email":
865-
result = new HtmlEmailInput(qualifiedName, page, attributeMap);
866-
break;
867-
868-
case "month":
869-
result = new HtmlMonthInput(qualifiedName, page, attributeMap);
870-
break;
871-
872-
case "number":
873-
result = new HtmlNumberInput(qualifiedName, page, attributeMap);
874-
break;
875-
876-
case "range":
877-
result = new HtmlRangeInput(qualifiedName, page, attributeMap);
878-
break;
879-
880-
case "search":
881-
result = new HtmlSearchInput(qualifiedName, page, attributeMap);
882-
break;
883-
884-
case "tel":
885-
result = new HtmlTelInput(qualifiedName, page, attributeMap);
886-
break;
887-
888-
case "time":
889-
result = new HtmlTimeInput(qualifiedName, page, attributeMap);
890-
break;
891-
892-
case "url":
893-
result = new HtmlUrlInput(qualifiedName, page, attributeMap);
894-
break;
895-
896-
case "week":
897-
result = new HtmlWeekInput(qualifiedName, page, attributeMap);
898-
break;
899-
900-
default:
806+
final HtmlInput result = switch (type.toLowerCase(Locale.ROOT)) {
807+
// This not an illegal value, as it defaults to "text"
808+
// cf http://www.w3.org/TR/REC-html40/interact/forms.html#adef-type-INPUT
809+
// and the common browsers seem to treat it as a "text" input so we will as well.
810+
case "", "text" -> new HtmlTextInput(qualifiedName, page, attributeMap);
811+
case "submit" -> new HtmlSubmitInput(qualifiedName, page, attributeMap);
812+
case "checkbox" -> new HtmlCheckBoxInput(qualifiedName, page, attributeMap);
813+
case "radio" -> new HtmlRadioButtonInput(qualifiedName, page, attributeMap);
814+
case "hidden" -> new HtmlHiddenInput(qualifiedName, page, attributeMap);
815+
case "password" -> new HtmlPasswordInput(qualifiedName, page, attributeMap);
816+
case "image" -> new HtmlImageInput(qualifiedName, page, attributeMap);
817+
case "reset" -> new HtmlResetInput(qualifiedName, page, attributeMap);
818+
case "button" -> new HtmlButtonInput(qualifiedName, page, attributeMap);
819+
case "file" -> new HtmlFileInput(qualifiedName, page, attributeMap);
820+
case "color" -> new HtmlColorInput(qualifiedName, page, attributeMap);
821+
case "date" -> new HtmlDateInput(qualifiedName, page, attributeMap);
822+
case "datetime-local" -> new HtmlDateTimeLocalInput(qualifiedName, page, attributeMap);
823+
case "email" -> new HtmlEmailInput(qualifiedName, page, attributeMap);
824+
case "month" -> new HtmlMonthInput(qualifiedName, page, attributeMap);
825+
case "number" -> new HtmlNumberInput(qualifiedName, page, attributeMap);
826+
case "range" -> new HtmlRangeInput(qualifiedName, page, attributeMap);
827+
case "search" -> new HtmlSearchInput(qualifiedName, page, attributeMap);
828+
case "tel" -> new HtmlTelInput(qualifiedName, page, attributeMap);
829+
case "time" -> new HtmlTimeInput(qualifiedName, page, attributeMap);
830+
case "url" -> new HtmlUrlInput(qualifiedName, page, attributeMap);
831+
case "week" -> new HtmlWeekInput(qualifiedName, page, attributeMap);
832+
default -> {
901833
if (LOG.isInfoEnabled()) {
902834
LOG.info("Bad input type: \"" + type + "\", creating a text input");
903835
}
904-
result = new HtmlTextInput(qualifiedName, page, attributeMap);
905-
break;
906-
}
836+
yield new HtmlTextInput(qualifiedName, page, attributeMap);
837+
}
838+
};
907839
return result;
908840
}
909841
}

src/main/java/org/htmlunit/html/HtmlDomTreeWalker.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -247,34 +247,21 @@ private short acceptNode(final Node n) {
247247
* @return the whatToShow constant for the type of specified node
248248
*/
249249
public static int getFlagForNode(final Node node) {
250-
switch (node.getNodeType()) {
251-
case Node.ELEMENT_NODE:
252-
return NodeFilter.SHOW_ELEMENT;
253-
case Node.ATTRIBUTE_NODE:
254-
return NodeFilter.SHOW_ATTRIBUTE;
255-
case Node.TEXT_NODE:
256-
return NodeFilter.SHOW_TEXT;
257-
case Node.CDATA_SECTION_NODE:
258-
return NodeFilter.SHOW_CDATA_SECTION;
259-
case Node.ENTITY_REFERENCE_NODE:
260-
return NodeFilter.SHOW_ENTITY_REFERENCE;
261-
case Node.ENTITY_NODE:
262-
return NodeFilter.SHOW_ENTITY;
263-
case Node.PROCESSING_INSTRUCTION_NODE:
264-
return NodeFilter.SHOW_PROCESSING_INSTRUCTION;
265-
case Node.COMMENT_NODE:
266-
return NodeFilter.SHOW_COMMENT;
267-
case Node.DOCUMENT_NODE:
268-
return NodeFilter.SHOW_DOCUMENT;
269-
case Node.DOCUMENT_TYPE_NODE:
270-
return NodeFilter.SHOW_DOCUMENT_TYPE;
271-
case Node.DOCUMENT_FRAGMENT_NODE:
272-
return NodeFilter.SHOW_DOCUMENT_FRAGMENT;
273-
case Node.NOTATION_NODE:
274-
return NodeFilter.SHOW_NOTATION;
275-
default:
276-
return 0;
277-
}
250+
return switch (node.getNodeType()) {
251+
case Node.ELEMENT_NODE -> NodeFilter.SHOW_ELEMENT;
252+
case Node.ATTRIBUTE_NODE -> NodeFilter.SHOW_ATTRIBUTE;
253+
case Node.TEXT_NODE -> NodeFilter.SHOW_TEXT;
254+
case Node.CDATA_SECTION_NODE -> NodeFilter.SHOW_CDATA_SECTION;
255+
case Node.ENTITY_REFERENCE_NODE -> NodeFilter.SHOW_ENTITY_REFERENCE;
256+
case Node.ENTITY_NODE -> NodeFilter.SHOW_ENTITY;
257+
case Node.PROCESSING_INSTRUCTION_NODE -> NodeFilter.SHOW_PROCESSING_INSTRUCTION;
258+
case Node.COMMENT_NODE -> NodeFilter.SHOW_COMMENT;
259+
case Node.DOCUMENT_NODE -> NodeFilter.SHOW_DOCUMENT;
260+
case Node.DOCUMENT_TYPE_NODE -> NodeFilter.SHOW_DOCUMENT_TYPE;
261+
case Node.DOCUMENT_FRAGMENT_NODE -> NodeFilter.SHOW_DOCUMENT_FRAGMENT;
262+
case Node.NOTATION_NODE -> NodeFilter.SHOW_NOTATION;
263+
default -> 0;
264+
};
278265
}
279266

280267
/* Returns whether the node is skipped by the TreeWalker. */

src/main/java/org/htmlunit/javascript/HtmlUnitContextFactory.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -335,24 +335,16 @@ public final <T> T callSecured(final ContextAction<T> action, final HtmlPage pag
335335
*/
336336
@Override
337337
protected boolean hasFeature(final Context cx, final int featureIndex) {
338-
switch (featureIndex) {
339-
case Context.FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER:
340-
case Context.FEATURE_OLD_UNDEF_NULL_THIS:
341-
case Context.FEATURE_LITTLE_ENDIAN:
342-
case Context.FEATURE_LOCATION_INFORMATION_IN_ERROR:
343-
case Context.FEATURE_INTL_402:
344-
case Context.FEATURE_HTMLUNIT_FN_ARGUMENTS_IS_RO_VIEW:
345-
return true;
346-
case Context.FEATURE_E4X:
347-
case Context.FEATURE_NON_ECMA_GET_YEAR:
348-
return false;
349-
case Context.FEATURE_HTMLUNIT_MEMBERBOX_NAME:
350-
return browserVersion_.hasFeature(JS_PROPERTY_DESCRIPTOR_NAME);
351-
case Context.FEATURE_HTMLUNIT_ARRAY_SORT_COMPERATOR_ACCEPTS_BOOL:
352-
return browserVersion_.hasFeature(JS_ARRAY_SORT_ACCEPTS_INCONSISTENT_COMPERATOR);
353-
default:
354-
return super.hasFeature(cx, featureIndex);
355-
}
338+
return switch (featureIndex) {
339+
case Context.FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER, Context.FEATURE_OLD_UNDEF_NULL_THIS,
340+
Context.FEATURE_LITTLE_ENDIAN, Context.FEATURE_LOCATION_INFORMATION_IN_ERROR, Context.FEATURE_INTL_402,
341+
Context.FEATURE_HTMLUNIT_FN_ARGUMENTS_IS_RO_VIEW -> true;
342+
case Context.FEATURE_E4X, Context.FEATURE_NON_ECMA_GET_YEAR -> false;
343+
case Context.FEATURE_HTMLUNIT_MEMBERBOX_NAME -> browserVersion_.hasFeature(JS_PROPERTY_DESCRIPTOR_NAME);
344+
case Context.FEATURE_HTMLUNIT_ARRAY_SORT_COMPERATOR_ACCEPTS_BOOL ->
345+
browserVersion_.hasFeature(JS_ARRAY_SORT_ACCEPTS_INCONSISTENT_COMPERATOR);
346+
default -> super.hasFeature(cx, featureIndex);
347+
};
356348
}
357349

358350
private static final class HtmlUnitErrorReporter implements ErrorReporter, Serializable {

src/main/java/org/htmlunit/javascript/host/URLSearchParams.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,12 @@ protected boolean isDone(final Context cx, final Scriptable scope) {
124124
@Override
125125
protected Object nextValue(final Context cx, final Scriptable scope) {
126126
final NameValuePair e = iterator_.next();
127-
switch (type_) {
128-
case KEYS:
129-
return e.getName();
130-
case VALUES:
131-
return e.getValue();
132-
case BOTH:
133-
return cx.newArray(scope, new Object[] {e.getName(), e.getValue()});
134-
default:
135-
throw new AssertionError();
136-
}
127+
return switch (type_) {
128+
case KEYS -> e.getName();
129+
case VALUES -> e.getValue();
130+
case BOTH -> cx.newArray(scope, new Object[]{e.getName(), e.getValue()});
131+
default -> throw new AssertionError();
132+
};
137133
}
138134
}
139135

0 commit comments

Comments
 (0)