Skip to content

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.

The Setup

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.

The source class:

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);
    }
}

The redirect set

Tells DASM to replace:

  • Vector2 with Vec2d
  • void addInplace(Vector2) with void add(Vec2d)
  • void subInplace(Vector2) with void sub(Vec2d)
  • new Vector2(double, double) with Vec2d 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);
    }
}

The transform:

@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);
}

The Results

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

Back to the homepage

Clone this wiki locally