🏷 Migration
5 patterns
Topic: Migration
All Java patterns related to Migration — java.evolved
I/O
Deprecated URL constructors to URI
Old
URL endpoint =
new URL("https://example.com/api?q=java");
Modern
URI endpointUri =
URI.create("https://example.com/api?q=java");
URL endpoint = endpointUri.toURL();
hover to see modern →
JDK 20+
learn more →
I/O
Finalizers to deterministic resource cleanup
Old
@Override
protected void finalize() throws Throwable {
nativeHandle.release();
}
Modern
final class NativeResource implements AutoCloseable {
private static final Cleaner CLEANER = Cleaner.create();
private static final class State implements Runnable {
private final long handle;
State(long handle) {
this.handle = handle;
}
@Override
public void run() {
release(handle);
}
}
private final Cleaner.Cleanable cleanable;
NativeResource(long handle) {
cleanable = CLEANER.register(this, new State(handle));
}
@Override
public void close() {
cleanable.clean();
}
}
hover to see modern →
JDK 9+
learn more →
Security
SecurityManager checks to explicit authorization
Old
SecurityManager manager = System.getSecurityManager();
if (manager != null) {
manager.checkRead(path.toString());
}
return Files.readString(path);
Modern
// Untrusted users must not be able to modify this tree
Path root = allowedRoot.toRealPath();
Path resolved = root.resolve(requested)
.normalize()
.toRealPath();
if (!resolved.startsWith(root)) {
throw new SecurityException(
"Path is outside the allowed root");
}
return Files.readString(resolved);
hover to see modern →
JDK 24+
learn more →
Tooling
Class.newInstance() to constructor reflection
Old
Plugin plugin = pluginClass.newInstance();
Modern
Plugin plugin = pluginClass
.getDeclaredConstructor()
.newInstance();
hover to see modern →
JDK 9+
learn more →
Enterprise
Spring Boot MVC Configuration
Old
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(
ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
}
Modern
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(
ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
}
hover to see modern →
JDK 17+
learn more →