Skip to content

integrated di framework #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion annotations/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
dependencies {
api 'io.avaje:avaje-inject:9.12'
annotationProcessor 'io.avaje:avaje-inject-generator:9.12'

implementation 'org.javassist:javassist:3.30.2-GA'

testImplementation project(":core")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@

import com.javadiscord.jdi.core.annotations.EventListener;

import io.avaje.inject.BeanScope;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ListenerLoader {
private static final Logger LOGGER = LogManager.getLogger(ListenerLoader.class);

private final BeanScope beanScope = BeanScope.builder().build();
private final EventListenerValidator eventListenerValidator = new EventListenerValidator();
private final List<Object> eventListeners;

Expand All @@ -29,8 +33,8 @@ public void loadListeners() {
try {
Class<?> clazz = Class.forName(ClassFileUtil.getClassName(classFile));
if (clazz.isAnnotationPresent(EventListener.class)) {
registerListener(clazz);
if (validateListener(clazz)) {
registerListener(clazz);
} else {
LOGGER.error("{} failed validation", clazz.getName());
}
Expand All @@ -43,7 +47,7 @@ public void loadListeners() {

private void registerListener(Class<?> clazz) {
try {
eventListeners.add(getZeroArgConstructor(clazz).newInstance());
eventListeners.add(beanScope.get(clazz));
LOGGER.info("Registered listener {}", clazz.getName());
} catch (Exception e) {
LOGGER.error("Failed to create {} instance", clazz.getName(), e);
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ group = 'com.javadiscord'
version = project.version

subprojects {
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'com.diffplug.spotless'

spotless {
Expand Down
2 changes: 2 additions & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
allprojects {
dependencies {
annotationProcessor 'io.avaje:avaje-inject-generator:9.12'

implementation project(":core")
implementation project(":annotations")
implementation project(":models")
Expand Down
2 changes: 1 addition & 1 deletion example/echo-bot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ shadowJar {
archiveBaseName.set('example-bot')
archiveClassifier.set('')
archiveVersion.set('')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@
import com.javadiscord.jdi.core.api.builders.CreateMessageBuilder;
import com.javadiscord.jdi.core.models.message.Message;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;

@EventListener
@Singleton
public class EchoHandler {
private final String prefix;

@Inject
public EchoHandler(String prefix) {
this.prefix = prefix;
}

@MessageCreate
public void echoMessages(Message message, Guild guild) {
if (!message.author().bot()) {
guild.channel().createMessage(
new CreateMessageBuilder(message.channelId()).content(message.content())
new CreateMessageBuilder(message.channelId()).content(prefix + message.content())
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

import com.javadiscord.jdi.core.Discord;

import io.avaje.inject.Bean;
import io.avaje.inject.Factory;

@Factory
public class Main {
public static void main(String[] args) {
Discord discord = new Discord(System.getenv("BOT_TOKEN"));
Discord discord = new Discord("");
discord.start();
}

@Bean
public static String prefix() {
return "Message: ";
}
}
Loading