hnOsmium0001

JMeta usage example v2

May 6th, 2020
888
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Meta({
  2.     @Do(
  3.         // Possible values: Do.Type.{
  4.         //     REMOVE_STATEMENT, ADD_ENTITY, REMOVE_ENTITY, GENERATE_CLASS
  5.         // }
  6.         // Note that there is no task for manipulating subclasses because subclasses are not actually "sub"classes
  7.         type = Do.Type.APPEND_STATEMENT,
  8.         // These fields are (mostly) specific to Do.Type.APPEND
  9.         target = @SelectEntity("#method;<staticInit>"), // <staticInit> is a special way to refer to it created by JMeta
  10.         // All sequences contain entities
  11.         statements = @Statements(
  12.             source = @SelectEntity(
  13.                 value = "#field;/.*/;$type",
  14.                 target = Block.class,
  15.                 condition = "$type <: $block",
  16.                 arguments = {
  17.                     @Pair(name = "block", clazz = Block.class)
  18.                 }
  19.             ),
  20.             // Finalize refers to a @MetaStatement method in the current class. The method must recieve the results' type
  21.             // from previous processing steps (field -> T, method -> MetaMethod)
  22.             finalize = "addBlockStatement"
  23.         )
  24.     )
  25. })
  26. class CollectBlocksExample {
  27.     static {} // For @SelectEntity("<staticInit>"), technically not needed because java compiler auto-generates them
  28.  
  29.     public static final List<Block> BLOCKS = new ArrayList<>();
  30.  
  31.     // Excludes this method in the final output
  32.     // All usages of @MetaStatement require the method have `void` return type and
  33.     @MetaStatement
  34.     private static void addBlockStatement(Block block) {
  35.         BLOCKS.add(block);
  36.     }
  37. }
  38.  
  39. @Meta({
  40.     @Do(
  41.         type = Do.Type.APPEND_ENTITY,
  42.         source = @SelectEntity(value = "#field;/.*/;_", target = MixinClassExample_Source.class)
  43.     ),
  44.     @Do(
  45.         type = Do.Type.APPEND_ENTITY,
  46.         source = @SelectEntity(value = "#method;/.*/;_", target = MixinClassExample_Source.class)
  47.     )
  48. })
  49. class MixinClassExample {
  50.     public void setX(int x) { this.x = x; }
  51.     public void setY(int y) { this.y = y; }
  52. }
  53. class MixinClassExample_Source {
  54.     protected int x, y;
  55.     protected int absX, absY;
  56.  
  57.     public setParent(MixinClassExample_Source parent) {
  58.         this.absX = parent.absX + x;
  59.         this.absY = parent.absY + y;
  60.     }
  61.  
  62.     public getX() { return x; }
  63.     public getY() { return y; }
  64.     public getAbsX() { return absX; }
  65.     public getAbsY() { return absY; }
  66. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×