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;
  }
}
6
  • stackoverflow.com/questions/2186931/… here is a example. Commented Dec 3, 2019 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. Commented Dec 3, 2019 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. Commented Dec 3, 2019 at 23:04
  • It's unclear why you think you need to use annotations for this. Commented Dec 3, 2019 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. Commented Dec 3, 2019 at 23:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.