COMMUNITY

How can i create meta for all private method

target platform is c#.

private function will be public function (in .cs)

I want add @:private to all private function.

thank you very mush.

You can add @:build() meta data to the class to run some macro code that modifies the class. In your build macro you can iterate the fields and do something roughly like if (field.access.indexOf(APublic) == -1) field.meta.push({name: ':private'})

Here’s an example of using build macros from the haxe docs

I doesn’t work.

macro static public function correctAccessLevel():Array<Field> {
    if(haxe.macro.Context.getLocalClass() != null) {
      var m:Map<String, Type> = haxe.macro.Context.getLocalVars();
      for(k in m.keys()) {
        trace(k);
      }
      var cc:haxe.macro.ClassType = haxe.macro.Context.getLocalClass().get();
      var classFields:Array<Field> = haxe.macro.Context.getBuildFields();
      for(i in 0...classFields.length) {
        var classField:Field = classFields[i];
        var isPrivate:Bool = classField.access.indexOf(APrivate) != -1;
        if(isPrivate) {
          classField.meta.push({name:':private', pos:classField.pos});
          // all tested.
          //classField.meta.push({name:'@:private', pos:classField.pos});
          //classField.meta.push({name:'@:private', pos:null});
          //classField.meta.push({name:':private', pos:null});
        }
        trace('>>', classField.name, isPrivate);
        trace(haxe.macro.Context.getPosInfos(classField.pos));
      }
    }
    return $a{null};
  }

print:

macro/CompilerMacro.hx:75: >>,Update,true
macro/CompilerMacro.hx:76: {min: 431, max: 583, file: src/HaxeGameProject.hx}

cs output

public virtual void Update() {
		if (( this._initializer.get_context().gameSystem != null )) {
			this._initializer.get_context().gameSystem.get_tickManager().update();
		}
		
	}

ok.

in Context.onGenerate function

1.get all field
var ct:haxe.macro.Type.ClassType = t.get();
var fields:Array<ClassField> = ct.fields.get();
2.use add function
fields[i].meta.add(':private', [] ,fields[i].pos);

@xhfzcmt that’s nearly it but you need to use @:build metadata on the class, not Context.onGenerate

See how it’s done in the example on this page

:slightly_smiling_face:ok

thank you :laughing: