-
Notifications
You must be signed in to change notification settings - Fork 0
A Quick Example
Tom Martin edited this page Jul 17, 2025
·
7 revisions
I think an example is the simplest way to explain the basics of DASM.
Much of the rest of the Wiki is very focused on exactly what each annotation does; it doesn't look at how to bring everything together.
In this example there is a single method (doSomeVector2Maths) which uses the class Vector2 to do some highly complex
and brittle maths which I would rather not manually duplicate for Vec2d.
public class Foo {
public void doSomeVector2Maths(Vector2 a, Vector2 b) { // lorem ipsum ish example
Vector2 c = new Vector2(a.x, b.y).add(1.0, 1.0);
c.subInplace(5.0, -123.0);
a.addInplace(c);
}
}Tells DASM to replace:
-
Vector2withVec2d -
void addInplace(Vector2)withvoid add(Vec2d) -
void subInplace(Vector2)withvoid sub(Vec2d) -
new Vector2(double, double)withVec2d of(double, double)
@RedirectSet
interface MainSet {
@TypeRedirect(from = @Ref(Vector2.class), to = @Ref(Vec2d.class))
abstract class Vector2_to_Vec2d {
@MethodRedirect("addInplace(Lexample/Vector2;)V")
native void add(Vec2d vec2d);
@MethodRedirect("subInplace(Lexample/Vector2;)V")
native void sub(Vec2d vec2d);
@ConstructorToFactoryRedirect("<init>(DD)V")
static native Vec2d of(double x, double y);
}
}@Dasm(MainSet.class)
public class Bar {
@TransformFromMethod(owner = @Ref(Foo.class), value = "doSomeVector2Maths(Lexample/Vector2;Lexample/Vector2;)V")
public native void doSomeVec2dMaths(Vec2d a, Vec2d b);
}After DASM has applied to Bar:
public class Bar {
public void doSomeVec2dMaths(Vec2d a, Vec2d b) {
Vec2d c = Vec2d.of(a.x, b.y)
c = c.add(1.0, 1.0); // [1] (see below)
c.sub(5.0, -123.0);
a.add(c);
}
}[1] Vector2's Vector2 add(Vector2) and Vec2d's Vec2d add(Vec2d) match after the type redirect, so no method redirect was required