#annotation confusion

1 messages · Page 1 of 1 (latest)

wraith lark
#

@prime current urgent !!
if in a class :

@Using(Handler.class,Data.class)
public class View extends Widget{}

suppose Using annotation will expand it to

public class View extends Widget{
    Handler handler;
    Data data; // here is the added attribute by "Using" annotation
    View(Handler handler, Data data) { 
        this.handler=handler;
        this.data=v;
    }
}

------ My Question ------
when i am doing my logic in View class here

@Using(Handler.class,Data.class)
public class View extends Widget{
// example
  private void foo(){
    // using data attributes generated by "Using" annotation
    System.Out.println(data.name);
  }
}

can i use the attributes handler and data that the annotation generates ?

south raptorBOT
#

<@&987246399047479336> please have a look, thanks.

fallen jungle
#

why is this urgent

#

and that sounds like a lombok annotation

#

so how are you even doing such a thing?

wraith lark
#

what do you mean lombok ?

fallen jungle
#

like - in normal java there is no way for this to work

#

thats not what annotations do

#

and its beyond what annotation processors can do

wraith lark
#

ok then i will just add the attributes manualy with DI

#

thanks

fallen jungle
#

i mean

#

so wait

#

you can do things kinda like that

#

with a few restrictions

#

but it really depends why you are thinking of it

#

because its a non-trivial amount of work to build the annotation procesor to do that

wraith lark
#

after thinking, i want to do a MVC structure with DI

#

and Services.

fallen jungle
wraith lark
#

desktop app with custom rendering with lwjgl

#

i want to structure that app with MVC and have shared Services (basicly a singleton)
i want to be able to inject data types as needed in the constructor
including services

fallen jungle
#

will all the things you inject be made at the start of the program?

#

if so you can use a normal DI library

wraith lark
#

like ?

fallen jungle
#
<dependency>
    <groupId>dev.mccue</groupId>
    <artifactId>feather</artifactId>
    <version>2024.09.09.1</version>
</dependency>
wraith lark
#

gradle 🙂

fallen jungle
#
implementation 'dev.mccue:feather:2024.09.09.1'
#

this is one of many di libraries; i didn't write it but i did fork it

#

i'll make a basic example:

wraith lark
#

what is differance between dev.mccue:feather and org.codejargon.feather

fallen jungle
#
import dev.mccue.feather.DependencyInjector;
import dev.mccue.feather.Provides;
import jakarta.inject.Inject;

class SomeService {

}

class DIModule {
    private final SomeService service;

    DIModule(SomeService service) {
        this.service = service;
    }

    @Provides
    public SomeService service() {
        return service;
    }

}

class ToMake {
    @Inject
    public ToMake(SomeService service) {
        System.out.println(service);
    }
}

public class Main {
    public static void main(String[] args) {
        var di = DependencyInjector.builder()
                .module(new DIModule(
                        new SomeService()
                ))
                .build();

        System.out.println(di.instance(ToMake.class));
    }
}
fallen jungle
#

dev.mccue:feather is a fork i made with a few bug fixes and the class package names changed

wraith lark
#

ah ok

#

what about Dagger 2 and Dirk ?

fallen jungle
#

they'd work too

#

dagger does compile time code generation, so you'd need to set that up

wraith lark
fallen jungle
#

yeah any of those would work

wraith lark
#

ok, my question is :
for my needs where, Data classes will inject a new instance in every constructor (when needed )
but Service will always inject the same instance, like a singleton

#

is feather the easiest filling my needs?

fallen jungle
#

do you mean you will have some singletons and some things that are generated fresh?

#

im curious what the things you'd be generating fresh are

wraith lark
#
  • Services are singletons : every time in a constructor it is requested i will inject the same instance for all of them
  • Data classes are like models : every time in a constructor it is requested i will generate a fresh instance
#

basicly MVC with shared services but no database.

fallen jungle
#

well

tawny apex
#

Hmm, why would you create new entities? If you're not using a DB wouldn't you keep your entities in memory using some kind of map?

fallen jungle
#

if this is enough for you

#
class SomeService {

}

class Other {
    String x;
}

class DIModule {
    private final SomeService service;

    DIModule(SomeService service) {
        this.service = service;
    }

    @Provides
    public Other other() {
        return new Other();
    }

    @Provides
    @Singleton
    public SomeService service() {
        return service;
    }
}
wraith lark
#

i am not using a db

fallen jungle
#

then it will be enough

#

if it is not, then it will not

#

and we need to drill more in to what exactly these model classes are to understand which one to pick

wraith lark
#

i have a confusion before this...