|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 IBM Corporation and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + *******************************************************************************/ |
| 10 | +package io.openliberty.java.internal; |
| 11 | + |
| 12 | +import module java.base; |
| 13 | +import javax.enterprise.context.ApplicationScoped; |
| 14 | +import javax.ws.rs.GET; |
| 15 | +import javax.ws.rs.Path; |
| 16 | +import java.net.http.HttpClient; |
| 17 | +import java.lang.reflect.Field; |
| 18 | +import java.lang.reflect.Modifier; |
| 19 | +import java.io.StringWriter; |
| 20 | +import java.io.PrintWriter; |
| 21 | + |
| 22 | +@Path("/") |
| 23 | +@ApplicationScoped |
| 24 | +public class TestService { |
| 25 | + |
| 26 | + static class FirstName { |
| 27 | + private final String value; |
| 28 | + |
| 29 | + public FirstName(String value) { |
| 30 | + this.value = value; |
| 31 | + } |
| 32 | + |
| 33 | + public String getValue() { |
| 34 | + return value; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public String toString() { |
| 39 | + return value; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + static class Person { |
| 44 | + public final FirstName name = new FirstName("Original"); |
| 45 | + } |
| 46 | + |
| 47 | + private StringWriter sw = new StringWriter(); |
| 48 | + |
| 49 | + @GET |
| 50 | + public String test() { |
| 51 | + try { |
| 52 | + log(">>> ENTER"); |
| 53 | + doTest(); |
| 54 | + log("<<< EXIT SUCCESSFUL"); |
| 55 | + } catch (Exception e) { |
| 56 | + e.printStackTrace(System.out); |
| 57 | + e.printStackTrace(new PrintWriter(sw)); |
| 58 | + log("<<< EXIT FAILED"); |
| 59 | + } |
| 60 | + String result = sw.toString(); |
| 61 | + sw = new StringWriter(); |
| 62 | + return result; |
| 63 | + } |
| 64 | + |
| 65 | + private void doTest() throws Exception { |
| 66 | + log("Beginning Java 26 testing"); |
| 67 | + |
| 68 | + // Test Final Mean Final (JEP 500) |
| 69 | + testFinalMeanFinal(); |
| 70 | + |
| 71 | + // Test HTTP/3 Support (JEP 517) |
| 72 | + testHTTP3Support(); |
| 73 | + |
| 74 | + log("Leaving testing"); |
| 75 | + } |
| 76 | + |
| 77 | + // Prepare to Make Final Mean Final : JEP 500 -> https://openjdk.org/jeps/500 |
| 78 | + // Prepare to Make Final Mean Final : JEP 500 -> https://openjdk.org/jeps/500 |
| 79 | + private void testFinalMeanFinal() throws Exception { |
| 80 | + log("Beginning JEP 500 testing: Prepare to Make Final Mean Final"); |
| 81 | + log("Testing final field mutation protection"); |
| 82 | + |
| 83 | + Person p = new Person(); |
| 84 | + log("Before mutation attempt: " + p.name); |
| 85 | + |
| 86 | + boolean mutationBlocked = false; |
| 87 | + String mode = null; |
| 88 | + |
| 89 | + try { |
| 90 | + // Use Java 12+ approach to attempt final field mutation |
| 91 | + Field f = Person.class.getDeclaredField("name"); |
| 92 | + f.setAccessible(true); |
| 93 | + |
| 94 | + // Attempt to set the final field directly |
| 95 | + log("Attempting direct mutation of final field..."); |
| 96 | + f.set(p, new FirstName("Mutated")); |
| 97 | + |
| 98 | + // If we reach here, mutation succeeded (WARN mode) |
| 99 | + mode = "WARN"; |
| 100 | + log("Mutation succeeded - running in WARN mode"); |
| 101 | + log("Field value after mutation: " + p.name); |
| 102 | + log("Check server logs for JEP 500 warning messages"); |
| 103 | + |
| 104 | + } catch (IllegalAccessException e) { |
| 105 | + // Mutation was blocked (DENY mode) -Future Java 26 Behaviour |
| 106 | + mutationBlocked = true; |
| 107 | + mode = "DENY"; |
| 108 | + log("SUCCESS: IllegalAccessException caught - running in DENY mode"); |
| 109 | + log("Exception message: " + e.getMessage()); |
| 110 | + log("Final field mutation was blocked as expected"); |
| 111 | + } |
| 112 | + |
| 113 | + // Verify results based on mode |
| 114 | + if (mode.equals("DENY")) { |
| 115 | + if (!"Original".equals(p.name.getValue())) { |
| 116 | + throw new Exception("JEP 500 test FAILED: Final field was mutated despite exception. Value: " + p.name); |
| 117 | + } |
| 118 | + log("RESULT: Final field remained immutable (value: " + p.name + ")"); |
| 119 | + } else { |
| 120 | + // WARN mode - mutation may succeed but should log warnings - Current Java 26 Behaviour |
| 121 | + if ("Mutated".equals(p.name.getValue())) { |
| 122 | + log("RESULT: Mutation succeeded in WARN mode - field value changed to: " + p.name); |
| 123 | + } else { |
| 124 | + log("RESULT: Mutation attempted in WARN mode but value unchanged: " + p.name); |
| 125 | + } |
| 126 | + log("Note: Check console.log/messages.log for JEP 500 warnings"); |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + log("JEP 500 Test Summary:"); |
| 131 | + log("Mode detected: " + mode); |
| 132 | + log("Mutation blocked: " + mutationBlocked); |
| 133 | + log("Final field value: " + p.name); |
| 134 | + log("Leaving JEP 500 testing"); |
| 135 | + } |
| 136 | + |
| 137 | + // HTTP/3 for the HTTP Client API : JEP 517 -> https://openjdk.org/jeps/517 |
| 138 | + private void testHTTP3Support() { |
| 139 | + log("Testing HTTP/3 Support (JEP 517)"); |
| 140 | + |
| 141 | + try { |
| 142 | + // Create an HTTP client with HTTP/3 support |
| 143 | + HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_3) // HTTP/3 support |
| 144 | + .build(); |
| 145 | + |
| 146 | + log("HTTP Client created with HTTP/3 support"); |
| 147 | + // This demonstrates that the API is available |
| 148 | + log("HTTP/3 version: " + HttpClient.Version.HTTP_3); |
| 149 | + |
| 150 | + // Note: Actual HTTP/3 requests would require a server that supports HTTP/3 |
| 151 | + log("HTTP/3 is now available in the standard HttpClient API"); |
| 152 | + |
| 153 | + } catch (Exception e) { |
| 154 | + log("HTTP/3 test note: " + e.getMessage()); |
| 155 | + log("HTTP/3 support is available in Java 26 HttpClient API"); |
| 156 | + } |
| 157 | + log("Leaving JEP 517 testing"); |
| 158 | + } |
| 159 | + |
| 160 | + public void log(String msg) { |
| 161 | + System.out.println(msg); |
| 162 | + sw.append(msg); |
| 163 | + sw.append("<br/>"); |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments