Skip to content

Instantly share code, notes, and snippets.

@edefazio
Created August 7, 2019 15:08
Show Gist options
  • Save edefazio/3f4f1c2bbde4db33d7698416e091f317 to your computer and use it in GitHub Desktop.
Save edefazio/3f4f1c2bbde4db33d7698416e091f317 to your computer and use it in GitHub Desktop.
Using jdraft to dynamically build and create / use objects
import org.jdraft.*;
import org.jdraft.adhoc.*;
import org.jdraft.macro.*;
import java.net.UUID;
public class BuildAndUseTest extends TestCase {
public void testBuildAndUseModel(){
_class _c = _class.of("demo.Point2D", new @_dto Object(){
@_final int x, y;
public UUID uuid;
});
System.out.println(_c);
_proxy _p1 = _proxy.of(_c, 2, 100);
_proxy _p2 = _p1.of(2, 100);
assertEquals( _p1, _p2);
assertEquals( _p1.instance, _p2.instance); //the underlying objects are ==
assertEquals( _p1.hashCode(), _p2.hashCode()); //hashcodes are equal
//change p2
_p2.set("uuid", UUID.randomUUID());
assertFalse( _p1.equals( _p2)); //objects are not equal
assertFalse( _p1.hashCode() == _p2.hashCode()); //hashcodes are not equal
}
}
@edefazio
Copy link
Author

edefazio commented Aug 7, 2019

The code built from :

 _class _c = _class.of("demo.Point2D", new @_dto Object(){
            @_final int x, y;
            public UUID uuid;
        });

is:

package demo;

import java.util.UUID;

public class Point2D {

    final int x, y;

    public UUID uuid;

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public UUID getUuid() {
        return uuid;
    }

    public Point2D setUuid(UUID uuid) {
        this.uuid = uuid;
        return this;
    }

    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        if (this == o) {
            return true;
        }
        if (getClass() != o.getClass()) {
            return false;
        }
        Point2D test = (Point2D) o;
        boolean eq = true;
        eq = eq && this.x == test.x;
        eq = eq && this.y == test.y;
        eq = eq && java.util.Objects.equals(this.uuid, test.uuid);
        return eq;
    }

    public int hashCode() {
        int hash = 67;
        int prime = 191;
        hash = hash * prime + x;
        hash = hash * prime + y;
        hash = hash * prime + java.util.Objects.hashCode(uuid);
        return hash;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Point2D").append("{");
        sb.append(System.lineSeparator());
        sb.append(" x: ").append(x).append(System.lineSeparator());
        sb.append(" y: ").append(y).append(System.lineSeparator());
        sb.append(" uuid: ").append(uuid).append(System.lineSeparator());
        sb.append("}");
        return sb.toString();
    }

    public Point2D(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

NOTES:

  1. the name of the class "demo.Point2D" infers the package as "demo"
  2. the field "uuid" of type UUID is automatically imported "import java.util.UUID;" because it's on the public API
  3. the @_dto annotation is a reference to a "macro" which will automatically create:
  • get() methods for all non static fields (x, y, uuid) (see @_get)
  • set() methods for all non-final fields (uuid) (see @_setFluent)
  • equals() method to test for equality (see @_equals)
  • hashCode() method based on the fields (x,y,uuid) (see @_hashCode)
  • toString() method based on the fields (x,y,uuid) (see @_toString)
    -Point2D() constructor accepting and setting all final non-initialized fields (x, y) ({@see @_autoConstructor})
  1. the @_final annotation makes the fields (x,y) final

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