Add java8-to-25 module: legacy Java 8 app run on JDK 8, 17 and 25, tool coverage, JVM flags, libraries, OpenRewrite and the fixed version
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6vDJ5fWY5uEZaJFNi
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.ankurm</groupId>
|
||||
<artifactId>legacy-app</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.ankurm.legacy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/** Runs every feature of the legacy app and keeps going after a failure. */
|
||||
public final class App {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("running on java.version " + System.getProperty("java.version"));
|
||||
step("jaxb", () -> OrderXml.toXml(new Order("A-1", 12)));
|
||||
step("jaxws", SoapCheck::publishAndReadWsdl);
|
||||
step("base64", () -> Tokens.encode("ankur".getBytes("UTF-8")));
|
||||
step("nashorn", () -> Rules.discount(12));
|
||||
step("version", () -> "major=" + JavaVersion.major());
|
||||
step("classloader", () -> "classpath found=" + PluginScanner.classpathEntries());
|
||||
step("reflection", () -> "ArrayList capacity=" + Peek.capacity(new ArrayList<>(32)));
|
||||
step("securitymanager", Guard::install);
|
||||
step("thread.stop", Watchdog::kill);
|
||||
System.out.println(Report.lines());
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private static void step(String name, Callable<Object> work) {
|
||||
try {
|
||||
System.out.println("[ok] " + name + ": " + work.call());
|
||||
} catch (Throwable t) {
|
||||
System.out.println("[FAIL] " + name + ": " + t);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.ankurm.legacy;
|
||||
|
||||
/** Blocks System.exit from plugin code with a SecurityManager. */
|
||||
public final class Guard {
|
||||
public static String install() {
|
||||
System.setSecurityManager(new SecurityManager() {
|
||||
@Override public void checkExit(int status) {
|
||||
throw new SecurityException("plugins may not exit the JVM");
|
||||
}
|
||||
@Override public void checkPermission(java.security.Permission p) { }
|
||||
});
|
||||
try {
|
||||
System.exit(3);
|
||||
return "exited";
|
||||
} catch (SecurityException e) {
|
||||
return "blocked: " + e.getMessage();
|
||||
} finally {
|
||||
System.setSecurityManager(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.ankurm.legacy;
|
||||
|
||||
/** The classic "which Java is this?" parser: assumes versions look like 1.x.y. */
|
||||
public final class JavaVersion {
|
||||
public static int major() {
|
||||
String v = System.getProperty("java.version");
|
||||
return Integer.parseInt(v.split("\\.")[1]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.ankurm.legacy;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
/** A typical Java 8 era JAXB bean. javax.xml.bind shipped inside the JDK until Java 10. */
|
||||
@XmlRootElement(name = "order")
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public class Order {
|
||||
public String id;
|
||||
public int quantity;
|
||||
|
||||
public Order() { }
|
||||
|
||||
public Order(String id, int quantity) {
|
||||
this.id = id;
|
||||
this.quantity = quantity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.ankurm.legacy;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.Marshaller;
|
||||
|
||||
public final class OrderXml {
|
||||
public static String toXml(Order order) throws Exception {
|
||||
Marshaller m = JAXBContext.newInstance(Order.class).createMarshaller();
|
||||
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
|
||||
StringWriter out = new StringWriter();
|
||||
m.marshal(order, out);
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ankurm.legacy;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/** Reads a private JDK field by reflection: the "illegal access" pattern. */
|
||||
public final class Peek {
|
||||
public static int capacity(ArrayList<?> list) throws Exception {
|
||||
Field f = ArrayList.class.getDeclaredField("elementData");
|
||||
f.setAccessible(true);
|
||||
return ((Object[]) f.get(list)).length;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.ankurm.legacy;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
/** Assumes the application class loader is a URLClassLoader. True up to Java 8 only. */
|
||||
public final class PluginScanner {
|
||||
public static int classpathEntries() {
|
||||
URL[] urls = ((URLClassLoader) ClassLoader.getSystemClassLoader()).getURLs();
|
||||
return urls.length > 0 ? 1 : 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.ankurm.legacy;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.text.DateFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/** Nothing here throws on any JDK. The text it prints is what changes. */
|
||||
public final class Report {
|
||||
public static String lines() {
|
||||
GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.US);
|
||||
c.set(2026, Calendar.MARCH, 5, 14, 30, 0);
|
||||
DateFormat us = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.US);
|
||||
us.setTimeZone(c.getTimeZone());
|
||||
DateFormat de = DateFormat.getDateInstance(DateFormat.FULL, Locale.GERMANY);
|
||||
de.setTimeZone(c.getTimeZone());
|
||||
NumberFormat inr = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));
|
||||
return "date(en_US) = " + escape(us.format(c.getTime())) + "\n"
|
||||
+ "date(de_DE) = " + escape(de.format(c.getTime())) + "\n"
|
||||
+ "money(en_IN) = " + escape(inr.format(1234567.5)) + "\n"
|
||||
+ "defaultCharset = " + Charset.defaultCharset() + "\n"
|
||||
+ "rupee as bytes = " + "₹".getBytes().length + " byte(s)";
|
||||
}
|
||||
|
||||
/** Print non-ASCII characters as \\uXXXX so the transcript shows invisible differences. */
|
||||
static String escape(String s) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
for (char ch : s.toCharArray()) {
|
||||
if (ch < 127) b.append(ch);
|
||||
else b.append(String.format("\\u%04X", (int) ch));
|
||||
}
|
||||
return b.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ankurm.legacy;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
|
||||
/** Business rules kept as JavaScript, run by Nashorn (removed in Java 15). */
|
||||
public final class Rules {
|
||||
public static Object discount(int quantity) throws Exception {
|
||||
ScriptEngine js = new ScriptEngineManager().getEngineByName("nashorn");
|
||||
js.put("quantity", quantity);
|
||||
return js.eval("quantity > 10 ? 'BULK' : 'RETAIL'");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.ankurm.legacy;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Scanner;
|
||||
import javax.xml.ws.Endpoint;
|
||||
|
||||
public final class SoapCheck {
|
||||
public static String publishAndReadWsdl() throws Exception {
|
||||
String address = "http://127.0.0.1:18099/status";
|
||||
Endpoint endpoint = Endpoint.publish(address, new StatusService());
|
||||
try (InputStream in = new URL(address + "?wsdl").openStream()) {
|
||||
String wsdl = new Scanner(in, "UTF-8").useDelimiter("\\A").next();
|
||||
return wsdl.contains("name=\"StatusServiceService\"") ? "WSDL served" : "unexpected WSDL";
|
||||
} finally {
|
||||
endpoint.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ankurm.legacy;
|
||||
|
||||
import javax.jws.WebMethod;
|
||||
import javax.jws.WebService;
|
||||
|
||||
/** A JAX-WS (SOAP) service published with the JDK's built-in HTTP server. */
|
||||
@WebService
|
||||
public class StatusService {
|
||||
@WebMethod
|
||||
public String status() {
|
||||
return "UP";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.ankurm.legacy;
|
||||
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
public final class Tokens {
|
||||
/** sun.misc.BASE64Encoder was never a public API; it left the JDK in Java 9. */
|
||||
public static String encode(byte[] raw) {
|
||||
return new BASE64Encoder().encode(raw);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.ankurm.legacy;
|
||||
|
||||
/** Kills a runaway worker with Thread.stop(), deprecated since Java 1.2. */
|
||||
public final class Watchdog {
|
||||
@SuppressWarnings("deprecation")
|
||||
public static String kill() throws InterruptedException {
|
||||
Thread worker = new Thread(() -> {
|
||||
while (true) {
|
||||
try { Thread.sleep(50); } catch (InterruptedException ignored) { }
|
||||
}
|
||||
});
|
||||
worker.setDaemon(true);
|
||||
worker.start();
|
||||
Thread.sleep(100);
|
||||
worker.stop();
|
||||
worker.join(1000);
|
||||
return worker.isAlive() ? "still alive" : "stopped";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user