0

I am currently working on an application and I would like to have annotated functions that can be used as callbacks.

History
When I used to do Swift development, I remember to use some functions as arguments, you needed to run @objc.
I would like to do something similar and use @Function.

Current Code

Function.java

    package com.iprogrammer.annotations;

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Target;

    @Target(value={ElementType.METHOD})
    public @interface Function{

    }

App.java

package dev.davidkeen.annotations;

import java.lang.annotation.Annotation;

public final class App {
    private App() {
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
}


class Main{
    public Main(){}

    public void run(){
        someFunc(someFunc2);
    }

    void someFunc(Function function){
        function();
    }

    @Function void someFuncTwo(){
        System.out.println("Hello");
    }
}

Is it posible to parse someFuncTwo as an argument to someFunc?

Edit

Reason why I want to use an annotation is becase of the following usage

class AdminController{
  private AdminModdle _moddle;
  private JFrame _view;

  public AdminController{
     _view.buttonOne.setAction(actionOne);
     _view.buttonTwo.setAction(actionTwo);
  }

  @Function void actionOne(){
    //Do stuff
  }

  @Function void actionTwo(){
    //Do Stuff
  }

  public JFrame getView(){
    return _view;
  }
}
iProgram
  • 6,057
  • 9
  • 39
  • 80
  • https://stackoverflow.com/questions/2186931/java-pass-method-as-parameter here is a example. – Linitha Dec 03 '19 at 22:37
  • @Linitha but that's using a interface for it though instead of annotation? What I want to do is be able to parse in a function as an argument, rather than a function. Basicly I want to have `@Function void func(){//Do Stuff}` and do `doSomthing(func)`. Otherwise I have to create a new class for each function. I'm trying to create the controller out of mvc so @Function would work better. – iProgram Dec 03 '19 at 22:48
  • @gurioso Please see my edit which explains why I want this usage. As you see, that usage looks much nicer than lambdas or classes. Basically I want sugger syntax. – iProgram Dec 03 '19 at 23:04
  • It's unclear why you think you need to use annotations for this. – Sotirios Delimanolis Dec 03 '19 at 23:27
  • @SotiriosDelimanolis I’ve said it’s for sugar syntax and just want to learn how to do it. The “duplicate question” is not duplicate. – iProgram Dec 03 '19 at 23:29
  • Improved the list to show you how to retrieve the annotated methods and invoke them. Throw that into an annotation listener and you’re golden. – Sotirios Delimanolis Dec 03 '19 at 23:33

0 Answers0