Skip to content

Instantly share code, notes, and snippets.

@pnuz3n
Last active July 6, 2024 03:56
Show Gist options
  • Save pnuz3n/753cafc4208032b77db9 to your computer and use it in GitHub Desktop.
Save pnuz3n/753cafc4208032b77db9 to your computer and use it in GitHub Desktop.
Simple try-out for DCI programming style (basically mixins) using Java 8, virtual default methods and dynamic proxy.
// Idea about simple DCI (data, context, interaction) using Java 8.
// Approach will force mixins to Java using virtual default methods
// and dynamic proxy. To keep it short there is no context object
// like DCI dictates.
//
// Run using
// javac MixinsExample.java; java MixinsExample
//
// Panu Wetterstrand <panu.wetterstrand@iki.fi>
import java.lang.reflect.*;
import java.lang.invoke.*;
public class MixinsExample {
public static void main(String[] args) {
// Lets have some accounts with initial balances
Account a = new AccountImpl(100);
Account b = new AccountImpl(50);
// Assign roles
TargetAccount target = assignRole(TargetAccount.class, a);
SourceAccount source = assignRole(SourceAccount.class, b);
// Print situation before
System.out.println("Source: "+source.balance());
System.out.println("Target: "+target.balance());
// Make the transfer
source.transferTo(target, 25);
// Print the results
System.out.println("Source: "+source.balance());
System.out.println("Target: "+target.balance());
}
// Account is basic data object containint the balance.
// Reason for this comes later...
static public interface Account {
public double balance();
public void deposit(double amount);
public void withdraw(double amount);
}
// Then we have the implementation
public static class AccountImpl implements Account {
double balance = 0;
public AccountImpl(double balance){
this.balance = balance;
}
public double balance(){
return balance;
}
public void deposit(double amount){
balance += amount;
}
public void withdraw(double amount){
balance -= amount;
}
}
// We have a role that acts as a source account.
// When this extends the Account interface we can use
// this.withdraw...
static public interface SourceAccount extends Account {
default public void transferTo(TargetAccount target, double amount) {
this.withdraw(amount);
target.deposit(amount);
}
}
// Just to mark the role of target account.
static public interface TargetAccount extends Account {
}
// Creates proxy that makes it possible to "cast" data objects
// to act in some role. This is where the magic happens...
static <T> T assignRole(Class<T> roleClass, final Object o){
return (T) Proxy.newProxyInstance(
roleClass.getClassLoader(),
new Class<?>[] { roleClass },
(Object proxy, Method method, Object[] args) -> {
if (method.isDefault()) {
// See https://rmannibucau.wordpress.com/2014/03/27/java-8-default-interface-methods-and-jdk-dynamic-proxies/#comment-1332
final Class<?> declaringClass = method.getDeclaringClass();
final MethodHandles.Lookup lookup = MethodHandles.publicLookup()
.in(declaringClass);
// ensure allowed mode will not check visibility
final Field f = MethodHandles.Lookup.class.getDeclaredField("allowedModes");
final int modifiers = f.getModifiers();
if (Modifier.isFinal(modifiers)) { // should be done a single time
final Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(f, modifiers & ~Modifier.FINAL);
f.setAccessible(true);
f.set(lookup, MethodHandles.Lookup.PRIVATE);
}
return lookup
.unreflectSpecial(method, declaringClass)
.bindTo(proxy)
.invokeWithArguments(args);
}
return method.invoke(o,args);
}
);
}
}
@pnuz3n
Copy link
Author

pnuz3n commented Mar 6, 2020

Hi! This is very interesting. Is it based on the DCI by James Coplien?
I'm starting to study this, and still a bit puzzled.

I think Data Context Interaction predates James Coplien, but yes this is simple Data Context Interaction implementation idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment